mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
WebScript Runtime response/url model extended to support output of client-side webscript url generation function.
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5902 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -206,11 +206,12 @@ public abstract class AbstractWebScript implements WebScript
|
||||
* Create a model for script usage
|
||||
*
|
||||
* @param req web script request
|
||||
* @param res web script response
|
||||
* @param customModel custom model entries
|
||||
*
|
||||
* @return script model
|
||||
*/
|
||||
final protected Map<String, Object> createScriptModel(WebScriptRequest req, Map<String, Object> customModel)
|
||||
final protected Map<String, Object> createScriptModel(WebScriptRequest req, WebScriptResponse res, Map<String, Object> customModel)
|
||||
{
|
||||
// create script model
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
@@ -235,7 +236,7 @@ public abstract class AbstractWebScript implements WebScript
|
||||
// add web script context
|
||||
model.put("args", createArgModel(req));
|
||||
model.put("guest", req.isGuest());
|
||||
model.put("url", new URLModel(req));
|
||||
model.put("url", new URLModel(req, res));
|
||||
model.put("server", new ServerModel(descriptorService.getServerDescriptor()));
|
||||
|
||||
// add custom model
|
||||
@@ -282,7 +283,7 @@ public abstract class AbstractWebScript implements WebScript
|
||||
// add web script context
|
||||
model.put("args", createArgModel(req));
|
||||
model.put("guest", req.isGuest());
|
||||
model.put("url", new URLModel(req));
|
||||
model.put("url", new URLModel(req, res));
|
||||
model.put("server", new ServerModel(descriptorService.getServerDescriptor()));
|
||||
|
||||
// add template support
|
||||
|
@@ -109,7 +109,7 @@ public class DeclarativeWebScript extends AbstractWebScript
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Executing script " + executeScript);
|
||||
|
||||
Map<String, Object> scriptModel = createScriptModel(req, model);
|
||||
Map<String, Object> scriptModel = createScriptModel(req, res, model);
|
||||
// add return model allowing script to add items to template model
|
||||
Map<String, Object> returnModel = new ScriptableHashMap<String, Object>();
|
||||
scriptModel.put("model", returnModel);
|
||||
|
@@ -33,15 +33,18 @@ package org.alfresco.web.scripts;
|
||||
public class URLModel
|
||||
{
|
||||
private WebScriptRequest req;
|
||||
private WebScriptResponse res;
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
URLModel(WebScriptRequest req)
|
||||
URLModel(WebScriptRequest req, WebScriptResponse res)
|
||||
{
|
||||
this.req = req;
|
||||
this.res = res;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,5 +156,16 @@ public class URLModel
|
||||
{
|
||||
return getExtension();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the client url encoding function script
|
||||
*
|
||||
* @param name name of the function object to return
|
||||
*
|
||||
* @return script to encode urls on the client
|
||||
*/
|
||||
public String getClientUrlFunction(String name)
|
||||
{
|
||||
return res.getEncodeScriptUrlFunction(name);
|
||||
}
|
||||
}
|
||||
|
@@ -166,5 +166,4 @@ public interface WebScriptRequest
|
||||
* @return true => force return of 200, otherwise return status explicitly set
|
||||
*/
|
||||
public boolean forceSuccessStatus();
|
||||
|
||||
}
|
||||
|
@@ -34,7 +34,6 @@ import org.alfresco.web.scripts.WebScriptDescription.FormatStyle;
|
||||
*/
|
||||
public abstract class WebScriptRequestImpl implements WebScriptRequest
|
||||
{
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptRequest#getExtensionPath()
|
||||
*/
|
||||
@@ -154,5 +153,4 @@ public abstract class WebScriptRequestImpl implements WebScriptRequest
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -82,11 +82,19 @@ public interface WebScriptResponse
|
||||
/**
|
||||
* Encode a script URL
|
||||
*
|
||||
* Note: Some Web Script Runtime environments (e.g. portal) require urls to be re-written.
|
||||
* Note: Some Web Script Runtime environments (e.g. JSR-168, JSF) require urls to be re-written.
|
||||
*
|
||||
* @param url url to encode
|
||||
* @return encoded url
|
||||
* @param url to encode
|
||||
* @return encoded url
|
||||
*/
|
||||
public String encodeScriptUrl(String url);
|
||||
|
||||
/**
|
||||
* Return a client side javascript function to build urls to this service
|
||||
*
|
||||
* @param name Generated function name
|
||||
*
|
||||
* @return javascript function definition
|
||||
*/
|
||||
public String getEncodeScriptUrlFunction(String name);
|
||||
}
|
||||
|
@@ -204,7 +204,7 @@ public abstract class WebScriptRuntime
|
||||
WebScriptResponse res = createResponse();
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("status", status);
|
||||
model.put("url", new URLModel(req));
|
||||
model.put("url", new URLModel(req, res));
|
||||
|
||||
// locate status template
|
||||
// NOTE: search order...
|
||||
|
@@ -30,6 +30,8 @@ import java.io.Writer;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.web.ui.common.Utils;
|
||||
|
||||
/**
|
||||
* HTTP Servlet Web Script Response
|
||||
*
|
||||
@@ -113,4 +115,13 @@ public class WebScriptServletResponse implements WebScriptResponse
|
||||
return url;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getEncodeScriptUrlFunction(java.lang.String)
|
||||
*/
|
||||
public String getEncodeScriptUrlFunction(String name)
|
||||
{
|
||||
return Utils.encodeJavascript(ENCODE_FUNCTION.replace("$name$", name));
|
||||
}
|
||||
|
||||
private static final String ENCODE_FUNCTION = "{ $name$: function(url) { return url; } }";
|
||||
}
|
||||
|
@@ -87,7 +87,6 @@ public abstract class WebScriptURLRequest extends WebScriptRequestImpl
|
||||
return urlParts;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
@@ -199,6 +198,4 @@ public abstract class WebScriptURLRequest extends WebScriptRequestImpl
|
||||
{
|
||||
return queryArgs.get(name);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -38,7 +38,6 @@ import org.alfresco.web.scripts.WebScriptURLRequest;
|
||||
*/
|
||||
public class WebScriptJSFRequest extends WebScriptURLRequest
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -85,5 +84,4 @@ public class WebScriptJSFRequest extends WebScriptURLRequest
|
||||
// NOTE: unknown in the JSF environment
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -105,11 +105,12 @@ public class WebScriptJSFResponse implements WebScriptResponse
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/**
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#reset()
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,11 +129,12 @@ public class WebScriptJSFResponse implements WebScriptResponse
|
||||
return fc.getResponseWriter();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/**
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setStatus(int)
|
||||
*/
|
||||
public void setStatus(int status)
|
||||
{
|
||||
// makes no sense in the JSF env
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,5 +144,32 @@ public class WebScriptJSFResponse implements WebScriptResponse
|
||||
{
|
||||
// Alfresco JSF framework only supports the default of text-html
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getEncodeScriptUrlFunction(java.lang.String)
|
||||
*/
|
||||
public String getEncodeScriptUrlFunction(String name)
|
||||
{
|
||||
UIForm form = Utils.getParentForm(fc, component);
|
||||
if (form == null)
|
||||
{
|
||||
throw new IllegalStateException("Must nest components inside UIForm to generate form submit!");
|
||||
}
|
||||
String fieldId = component.getClientId(fc);
|
||||
String formClientId = form.getClientId(fc);
|
||||
HtmlFormRendererBase.addHiddenCommandParameter(fc, form, fieldId);
|
||||
|
||||
String func = ENCODE_FUNCTION.replace("$name$", name);
|
||||
func = func.replace("$formClientId$", formClientId);
|
||||
func = func.replace("$fieldId$", fieldId);
|
||||
return Utils.encodeJavascript(func);
|
||||
}
|
||||
|
||||
private static final String ENCODE_FUNCTION =
|
||||
"{ $name$: function(url) {" +
|
||||
" var out = '';" +
|
||||
" out += \"#\\\" onclick=\\\"document.forms['$formClientId$']['$fieldId$'].value='\";" +
|
||||
" out += escape(url);" +
|
||||
" out += \"';document.forms['$formClientId$'].submit();return false;\";" +
|
||||
" return out; } }";
|
||||
}
|
||||
|
@@ -33,6 +33,7 @@ import javax.portlet.RenderResponse;
|
||||
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptResponse;
|
||||
import org.alfresco.web.ui.common.Utils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -127,4 +128,34 @@ public class WebScriptPortletResponse implements WebScriptResponse
|
||||
return portletUrl.toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getEncodeScriptUrlFunction(java.lang.String)
|
||||
*/
|
||||
public String getEncodeScriptUrlFunction(String name)
|
||||
{
|
||||
PortletURL portletUrl = res.createActionURL();
|
||||
|
||||
String func = ENCODE_FUNCTION.replace("$name$", name);
|
||||
func = func.replace("$actionUrl$", portletUrl.toString());
|
||||
return Utils.encodeJavascript(func);
|
||||
}
|
||||
|
||||
private static final String ENCODE_FUNCTION =
|
||||
"{ $name$: function(url) {" +
|
||||
" var out = \"$actionUrl$\";" +
|
||||
" var argsIndex = url.indexOf(\"?\");" +
|
||||
" if (argsIndex == -1)" +
|
||||
" {" +
|
||||
" out += \"&scriptUrl=\" + escape(url);" +
|
||||
" }" +
|
||||
" else" +
|
||||
" {" +
|
||||
" out += \"&scriptUrl=\" + escape(url.substring(0, argsIndex));" +
|
||||
" var args = url.substring(argsIndex + 1).split(\"&\");" +
|
||||
" for (var i=0; i<args.length; i++)" +
|
||||
" {" +
|
||||
" out += \"arg.\" + args[i];" +
|
||||
" }" +
|
||||
" }" +
|
||||
" return out; } }";
|
||||
}
|
||||
|
Reference in New Issue
Block a user