001: /*
002: * $Id: OfbizUrlTransform.java,v 1.2 2003/09/14 05:36:47 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.ftl;
026:
027: import java.io.IOException;
028: import java.io.Writer;
029: import java.util.Map;
030:
031: import javax.servlet.ServletContext;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.ofbiz.content.webapp.control.RequestHandler;
036:
037: import freemarker.ext.beans.BeanModel;
038: import freemarker.template.Environment;
039: import freemarker.template.SimpleScalar;
040: import freemarker.template.TemplateModelException;
041: import freemarker.template.TemplateScalarModel;
042: import freemarker.template.TemplateTransformModel;
043:
044: /**
045: * OfbizUrlTransform - Freemarker Transform for URLs (links)
046: *
047: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
048: * @version $Revision: 1.2 $
049: * @since 2.1
050: */
051: public class OfbizUrlTransform implements TemplateTransformModel {
052:
053: public boolean checkArg(Map args, String key) {
054: if (!args.containsKey(key)) {
055: return false;
056: } else {
057: Object o = args.get(key);
058: if (o instanceof SimpleScalar) {
059: SimpleScalar s = (SimpleScalar) o;
060: return "true".equalsIgnoreCase(s.getAsString());
061: }
062: return false;
063: }
064: }
065:
066: public Writer getWriter(final Writer out, Map args) {
067: final StringBuffer buf = new StringBuffer();
068: final boolean fullPath = checkArg(args, "fullPath");
069: final boolean secure = checkArg(args, "secure");
070: final boolean encode = checkArg(args, "encode");
071:
072: return new Writer(out) {
073: public void write(char cbuf[], int off, int len) {
074: buf.append(cbuf, off, len);
075: }
076:
077: public void flush() throws IOException {
078: out.flush();
079: }
080:
081: public void close() throws IOException {
082: try {
083: Environment env = Environment
084: .getCurrentEnvironment();
085: BeanModel req = (BeanModel) env
086: .getVariable("request");
087: BeanModel res = (BeanModel) env
088: .getVariable("response");
089: Object prefix = env.getVariable("urlPrefix");
090: if (req != null) {
091: HttpServletRequest request = (HttpServletRequest) req
092: .getWrappedObject();
093: ServletContext ctx = (ServletContext) request
094: .getAttribute("servletContext");
095: HttpServletResponse response = null;
096: if (res != null) {
097: response = (HttpServletResponse) res
098: .getWrappedObject();
099: }
100:
101: // make the link
102: RequestHandler rh = (RequestHandler) ctx
103: .getAttribute("_REQUEST_HANDLER_");
104: out.write(rh.makeLink(request, response, buf
105: .toString(), fullPath, secure, encode));
106: } else if (prefix != null) {
107: if (prefix instanceof TemplateScalarModel) {
108: TemplateScalarModel s = (TemplateScalarModel) prefix;
109: out.write(s + buf.toString());
110: }
111: } else {
112: out.write(buf.toString());
113: }
114: } catch (TemplateModelException e) {
115: throw new IOException(e.getMessage());
116: }
117: }
118: };
119: }
120: }
|