KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > ftl > OfbizUrlTransform


1 /*
2  * $Id: OfbizUrlTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.ftl;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import freemarker.core.Environment;
35 import freemarker.ext.beans.BeanModel;
36 import freemarker.template.SimpleScalar;
37 import freemarker.template.TemplateModelException;
38 import freemarker.template.TemplateScalarModel;
39 import freemarker.template.TemplateTransformModel;
40
41 import org.ofbiz.webapp.control.RequestHandler;
42
43 /**
44  * OfbizUrlTransform - Freemarker Transform for URLs (links)
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 2.1
49  */

50 public class OfbizUrlTransform implements TemplateTransformModel {
51     
52     public boolean checkArg(Map JavaDoc args, String JavaDoc key, boolean defaultValue) {
53         if (!args.containsKey(key)) {
54             return defaultValue;
55         } else {
56             Object JavaDoc o = args.get(key);
57             if (o instanceof SimpleScalar) {
58                 SimpleScalar s = (SimpleScalar) o;
59                 return "true".equalsIgnoreCase(s.getAsString());
60             }
61             return defaultValue;
62         }
63     }
64     
65     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
66         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
67         final boolean fullPath = checkArg(args, "fullPath", false);
68         final boolean secure = checkArg(args, "secure", false);
69         final boolean encode = checkArg(args, "encode", true);
70         
71         return new Writer JavaDoc(out) {
72             public void write(char cbuf[], int off, int len) {
73                 buf.append(cbuf, off, len);
74             }
75
76             public void flush() throws IOException JavaDoc {
77                 out.flush();
78             }
79
80             public void close() throws IOException JavaDoc {
81                 try {
82                     Environment env = Environment.getCurrentEnvironment();
83                     BeanModel req = (BeanModel) env.getVariable("request");
84                     BeanModel res = (BeanModel) env.getVariable("response");
85                     Object JavaDoc prefix = env.getVariable("urlPrefix");
86                     if (req != null) {
87                         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req.getWrappedObject();
88                         ServletContext JavaDoc ctx = (ServletContext JavaDoc) request.getAttribute("servletContext");
89                         HttpServletResponse JavaDoc response = null;
90                         if (res != null) {
91                             response = (HttpServletResponse JavaDoc) res.getWrappedObject();
92                         }
93                                             
94                         // make the link
95
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
96                         out.write(rh.makeLink(request, response, buf.toString(), fullPath, secure, encode));
97                     } else if (prefix != null) {
98                         if (prefix instanceof TemplateScalarModel) {
99                             TemplateScalarModel s = (TemplateScalarModel) prefix;
100                             String JavaDoc prefixString = s.getAsString();
101                             String JavaDoc bufString = buf.toString();
102                             boolean prefixSlash = prefixString.endsWith("/");
103                             boolean bufSlash = bufString.startsWith("/");
104                             if (prefixSlash && bufSlash) {
105                                 bufString = bufString.substring(1);
106                             } else if (!prefixSlash && !bufSlash) {
107                                 bufString = "/" + bufString;
108                             }
109                             out.write(prefixString + bufString);
110                         }
111                     } else {
112                         out.write(buf.toString());
113                     }
114                 } catch (TemplateModelException e) {
115                     throw new IOException JavaDoc(e.getMessage());
116                 }
117             }
118         };
119     }
120 }
121
Popular Tags