001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.webapp.view;
019:
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.OutputStream;
025: import java.io.OutputStreamWriter;
026: import java.io.Reader;
027: import java.io.Writer;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpSession;
033:
034: import com.anthonyeden.lib.config.Configuration;
035: import com.anthonyeden.lib.config.ConfigurationException;
036: import com.anthonyeden.lib.config.XMLConfiguration;
037:
038: import org.ofbiz.base.util.Debug;
039: import org.ofbiz.entity.GenericValue;
040: import org.ofbiz.security.Security;
041:
042: import org.jpublish.JPublishContext;
043: import org.jpublish.Page;
044: import org.jpublish.SiteContext;
045: import org.jpublish.Template;
046: import org.jpublish.page.PageInstance;
047: import org.jpublish.view.ViewRenderException;
048: import org.jpublish.view.ViewRenderer;
049:
050: /**
051: * Generic JPublish View Renderer - This is in testing; for use in wrapping other renderers
052: */
053: public class GenericViewRenderer implements ViewRenderer {
054:
055: public static final String module = GenericViewRenderer.class
056: .getName();
057: public static final String DEFAULT_RENDERER = "freemarker";
058: public Map renderers = null;
059:
060: protected SiteContext siteContext = null;
061:
062: /**
063: * @see org.jpublish.view.ViewRenderer#setSiteContext(org.jpublish.SiteContext)
064: */
065: public void setSiteContext(SiteContext siteContext) {
066: this .siteContext = siteContext;
067: this .renderers = new HashMap();
068: try {
069: loadCustom();
070: } catch (Exception e) {
071: Debug.logError(e, "Problems loading custom settings",
072: module);
073: throw new RuntimeException(e.toString());
074: }
075: }
076:
077: /* (non-Javadoc)
078: * @see org.jpublish.view.ViewRenderer#init()
079: */
080: public void init() throws Exception {
081: }
082:
083: /**
084: * @see org.jpublish.view.ViewRenderer#render(org.jpublish.JPublishContext, java.io.Reader, java.io.Writer)
085: */
086: public void render(JPublishContext context, String path, Reader in,
087: Writer out) throws IOException, ViewRenderException {
088: HttpServletRequest request = context.getRequest();
089: HttpSession session = context.getSession();
090: Security security = (Security) request.getAttribute("security");
091: GenericValue userLogin = (GenericValue) session
092: .getAttribute("userLogin");
093:
094: Page parent = (Page) context.get("page");
095: Page page = getPage(path);
096:
097: // decorate the content w/ edit images if we have permission
098: if (userLogin != null
099: && security.hasEntityPermission("CONTENTMGR",
100: "_UPDATE", userLogin)) {
101: out.write("<a href='/content/control/editContent?filePath="
102: + path + "'>*</a>");
103: }
104:
105: /* this loops -- not good
106: // if this page has a template, lets render the template
107: if (page != null && parent != null && page.getPath() != parent.getPath()) {
108: Debug.logInfo("Parent: " + parent.getPath(), module);
109: Debug.logInfo("Page: " + page.getPath(), module);
110: Debug.logInfo("Template: " + page.getFullTemplateName(), module);
111: if (!page.getTemplateName().equals("basic")) {
112: renderTemplate(cloneContext(context), page, out);
113: return;
114: }
115: }
116: */
117:
118: // get the view renderer for this page
119: if (Debug.verboseOn())
120: Debug.logVerbose("Getting renderer for: " + path, module);
121: String rendererName = DEFAULT_RENDERER;
122: if (page != null) {
123: rendererName = page.getProperty("page-renderer");
124: if (rendererName == null)
125: rendererName = DEFAULT_RENDERER;
126: }
127:
128: ViewRenderer renderer = (ViewRenderer) renderers
129: .get(rendererName);
130: if (renderer == null)
131: renderer = (ViewRenderer) renderers.get(DEFAULT_RENDERER);
132:
133: // call the renderer to render the rest of the page.
134: Debug.logVerbose("Calling render", module);
135: renderer.render(context, path, in, out);
136: }
137:
138: private void renderTemplate(JPublishContext context, Page page,
139: Writer out) throws IOException, ViewRenderException {
140: context.disableCheckReservedNames(this );
141: context.put("page", page);
142: if (siteContext.isProtectReservedNames()) {
143: context.enableCheckReservedNames(this );
144: }
145: try {
146: Debug.logInfo("Merging template", module);
147: Template template = siteContext.getTemplateManager()
148: .getTemplate(page.getFullTemplateName());
149: template.merge(context, page, out);
150: } catch (Exception e) {
151: throw new ViewRenderException(e);
152: }
153: }
154:
155: private JPublishContext cloneContext(JPublishContext context) {
156: JPublishContext newContext = new JPublishContext(this );
157: context.disableCheckReservedNames(this );
158: Object keys[] = context.getKeys();
159: for (int i = 0; i < keys.length; i++)
160: newContext.put((String) keys[i], context
161: .get((String) keys[i]));
162: if (siteContext.isProtectReservedNames()) {
163: context.enableCheckReservedNames(this );
164: }
165: return newContext;
166: }
167:
168: /**
169: * @see org.jpublish.view.ViewRenderer#render(org.jpublish.JPublishContext, java.io.InputStream, java.io.OutputStream)
170: */
171: public void render(JPublishContext context, String path,
172: InputStream in, OutputStream out) throws IOException,
173: ViewRenderException {
174: render(context, path, new InputStreamReader(in),
175: new OutputStreamWriter(out));
176: }
177:
178: /**
179: * @see org.jpublish.view.ViewRenderer#loadConfiguration(com.anthonyeden.lib.config.Configuration)
180: */
181: public void loadConfiguration(Configuration config)
182: throws ConfigurationException {
183: }
184:
185: private Page getPage(String path) {
186: Page page = null;
187: try {
188: PageInstance pi = siteContext.getPageManager().getPage(
189: path.substring(path.lastIndexOf(":") + 1));
190: if (pi != null)
191: page = new Page(pi);
192: } catch (Exception e) {
193: }
194: return page;
195: }
196:
197: private void loadCustom() throws Exception {
198: ClassLoader cl = Thread.currentThread().getContextClassLoader();
199: InputStream in = new FileInputStream(siteContext
200: .getConfigurationFile());
201: Configuration configuration = new XMLConfiguration(in);
202:
203: Iterator renderElements = configuration.getChildren(
204: "page-renderer").iterator();
205: while (renderElements.hasNext()) {
206: Configuration viewRendererConfiguration = (Configuration) renderElements
207: .next();
208: String renderName = viewRendererConfiguration
209: .getAttribute("name");
210: String className = viewRendererConfiguration
211: .getAttribute("classname");
212: ViewRenderer renderer = (ViewRenderer) cl.loadClass(
213: className).newInstance();
214: renderer.setSiteContext(siteContext);
215: renderer.loadConfiguration(viewRendererConfiguration);
216: renderer.init();
217: Debug.logInfo("Added renderer [" + renderName + "] - ["
218: + className + "]", module);
219: renderers.put(renderName, renderer);
220: }
221: }
222: }
|