Web Scripts - small refactor to template client url function - it's now a template method.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6018 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-06-19 14:35:02 +00:00
parent 5f5d83d00b
commit bcdf03bc80
5 changed files with 81 additions and 18 deletions

View File

@@ -246,7 +246,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, res));
model.put("url", new URLModel(req));
model.put("server", new ServerModel(descriptorService.getServerDescriptor()));
// add custom model
@@ -298,12 +298,13 @@ 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, res));
model.put("url", new URLModel(req));
model.put("server", new ServerModel(descriptorService.getServerDescriptor()));
// add template support
model.put("absurl", new AbsoluteUrlMethod(req.getServerPath()));
model.put("scripturl", new ScriptUrlMethod(req, res));
model.put("clienturlfunction", new ClientUrlFunctionMethod(res));
model.put("date", new Date());
model.put(TemplateService.KEY_IMAGE_RESOLVER, getWebScriptRegistry().getTemplateImageResolver());

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.web.scripts;
import java.util.List;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;
/**
* @author David Caruana
*
* Custom FreeMarker Template language method.
* <p>
* Render a client side javascript function to build urls to this service.
*
* <p>
* Usage: clientUrlFunction(String funcName)
*/
public final class ClientUrlFunctionMethod implements TemplateMethodModelEx
{
WebScriptResponse res;
/**
* Construct
*/
public ClientUrlFunctionMethod(WebScriptResponse res)
{
this.res = res;
}
/**
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
*/
public Object exec(List args) throws TemplateModelException
{
String result = "";
if (args.size() != 0)
{
Object arg0 = args.get(0);
if (arg0 instanceof TemplateScalarModel)
{
String arg = ((TemplateScalarModel)arg0).getAsString();
result = res.getEncodeScriptUrlFunction(arg);
}
}
return result;
}
}

View File

@@ -33,7 +33,6 @@ package org.alfresco.web.scripts;
public class URLModel
{
private WebScriptRequest req;
private WebScriptResponse res;
/**
* Construct
@@ -41,10 +40,9 @@ public class URLModel
* @param req
* @param res
*/
URLModel(WebScriptRequest req, WebScriptResponse res)
URLModel(WebScriptRequest req)
{
this.req = req;
this.res = res;
}
/**
@@ -157,15 +155,4 @@ 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);
}
}

View File

@@ -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, res));
model.put("url", new URLModel(req));
// locate status template
// NOTE: search order...