001: /**
002: * $Id: JSPSingleRenderingContainerProvider.java,v 1.7 2005/09/21 10:48:40 dg154973 Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.providers.containers.jsp.rendering.single;
014:
015: import com.sun.portal.log.common.PortalLogger;
016: import com.sun.portal.providers.containers.jsp.single.JSPSingleContainerProvider;
017: import com.sun.portal.wireless.providers.rendering.RenderingUtil;
018: import com.sun.portal.wireless.providers.containers.rendering.RenderingContainerProvider;
019: import com.sun.portal.wireless.providers.containers.template.WirelessContainerProviderAdapter;
020: import com.sun.portal.providers.context.ProviderContext;
021: import com.sun.portal.providers.context.ContainerProviderContext;
022: import com.sun.portal.providers.ProviderException;
023: import com.sun.portal.providers.context.ProviderContextException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import java.io.File;
028: import java.util.ResourceBundle;
029: import java.util.MissingResourceException;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: /**
034: * The <code>JSPSingleRenderingContainerProvider</code> class extends from
035: * the <code>JSPSingleContainerProvider</code> and overrides the
036: * <code>getContent, getEdit</code> and <code>getMostSpecificPath</code>
037: * methods of <code>JSPSingleContainerProvider</code>. This is a public class
038: * and can be extended to add more functionality to the provider if needed.
039: * While creating container channels using
040: * <code>JSPSingleRenderingContainerProvider</code>, the channel developer has
041: * to write a contentPage.jsp and editPage.jsp which would
042: * get the content from AML JSPs it has.<p>
043: * All JSPs created for the channel should be valid AML documents.
044: */
045:
046: public class JSPSingleRenderingContainerProvider extends
047: JSPSingleContainerProvider implements
048: RenderingContainerProvider {
049:
050: public final static String ERROR_CHANNEL = "errorChannel";
051: private ResourceBundle bundle = null;
052:
053: private static Logger logger = PortalLogger
054: .getLogger(JSPSingleRenderingContainerProvider.class);
055:
056: public void init(String name, HttpServletRequest req)
057: throws ProviderException {
058: super .init(name, req);
059: //Get the provider resource bundle
060: if (bundle == null) {
061: bundle = getResourceBundle();
062: }
063: }
064:
065: public StringBuffer getContent(HttpServletRequest req,
066: HttpServletResponse res) throws ProviderException {
067: StringBuffer sb = null;
068: try {
069: sb = super .getContent(req, res);
070: sb = renderContent(req, sb);
071: } catch (Throwable t) {
072: logger.log(Level.INFO, "", t);
073: return handleException(req, res);
074: }
075: return sb;
076: }
077:
078: public StringBuffer getEdit(HttpServletRequest req,
079: HttpServletResponse res) throws ProviderException {
080: // Set shouldRender and isTopLevel as false for children
081: req.setAttribute(getName() + ".shouldRender", Boolean.FALSE);
082: req.setAttribute(getName() + ".isTopLevel", Boolean.FALSE);
083: StringBuffer sb = null;
084: try {
085: sb = super .getEdit(req, res);
086: sb = renderContent(req, sb);
087: } catch (Throwable t) {
088: logger.log(Level.INFO, "", t);
089: return handleException(req, res);
090: }
091: return sb;
092: }
093:
094: /**
095: * Exception handling method for the Rendering Container. This method
096: * reads the error page and passes it through the rendering engine
097: * and returns the resulting stringbuffer to the caller.
098: *
099: * @return StringBuffer holding the channel content that is markup
100: * specific or AML.
101: *
102: * @param req An HttpServletRequest that contains information related
103: * to this request for content.
104: * @param res An HttpServletResponse that allows the provider to
105: * influence the overall response for desktop page
106: **/
107:
108: protected StringBuffer handleException(HttpServletRequest req,
109: HttpServletResponse res) {
110:
111: StringBuffer sb = null;
112: ContainerProviderContext cpc = (ContainerProviderContext) getProviderContext();
113: String defErrMsg = getClass().getName()
114: + ".handleException(): Error while reading error page."
115: + " Defaulting to base error page";
116: String defErrPg = null;
117: try {
118: defErrPg = bundle.getString("defaultError");
119: } catch (MissingResourceException mre) {
120: defErrPg = "";
121: }
122: if (defErrPg == null) {
123: defErrPg = "";
124: }
125: try {
126: String errorCh = getStringProperty(ERROR_CHANNEL);
127: if ((errorCh != null) && (errorCh.length() != 0)) {
128: sb = cpc.getContent(req, res, null, errorCh);
129: } else {
130: logger.info("PSMA_CSPWPCJRS0002");
131: // Default error page
132: sb = new StringBuffer(defErrPg);
133: sb = RenderingUtil.doRender(req, cpc, getName(), sb,
134: true, false);
135: }
136: } catch (Throwable t) {
137: logger.log(Level.INFO, "PSMA_CSPWPCJRS0002", t);
138: // Default error page
139: sb = new StringBuffer(defErrPg);
140: try {
141: sb = RenderingUtil.doRender(req, cpc, getName(), sb,
142: true, false);
143: } catch (Throwable t1) {
144: logger.log(Level.INFO, "PSMA_CSPWPCJRS0003", t);
145: // Rendering is failing. Cannot do much. Return original
146: // string buffer. Device will get a compile error.
147: return sb;
148: }
149: }
150:
151: // Content coming back is rendered content as getContent which doesnt
152: // set the parent is called. Thus error channel renders itself.
153: return sb;
154: }
155:
156: protected StringBuffer renderContent(HttpServletRequest req,
157: StringBuffer sb) throws ProviderException {
158: ContainerProviderContext cpc = (ContainerProviderContext) getProviderContext();
159: return RenderingUtil.renderContent(req, cpc, getName(), sb);
160: }
161:
162: protected StringBuffer renderEditContent(HttpServletRequest req,
163: StringBuffer sb) throws ProviderException {
164: ContainerProviderContext cpc = (ContainerProviderContext) getProviderContext();
165: // This is a top level container.. so shouldRender and isTopLevel
166: // should be true.
167: req.setAttribute(getName() + ".shouldRender", Boolean.TRUE);
168: req.setAttribute(getName() + ".isTopLevel", Boolean.TRUE);
169: return RenderingUtil.renderEditContent(req, cpc, getName(), sb);
170: }
171:
172: /**
173: * Gets the most specific template path for the given channel name,
174: * the given template name and the given filepath. The file returned may
175: * not exist at all. If non-null, the key returned by this method will map
176: * to the most specific template file. The file may or may not exist. It
177: * internally calls ProviderContext.getTemplateMostSpecificPath to get
178: * the template path.
179: *
180: * @param pc - ProviderContext of the calling provider
181: * @param name - Channel name
182: * @param file - Template name
183: *
184: * @return Path key to access the template
185: */
186: protected File getMostSpecificJSPPath(ProviderContext pc,
187: String name, String file) throws ProviderException {
188: File jspPath = null;
189: try {
190: jspPath = RenderingUtil.getTemplateMostSpecificPath(
191: (ContainerProviderContext) pc, name, file);
192: } catch (ProviderContextException e) {
193: throw new ProviderException(e.getMessage());
194: }
195:
196: return jspPath;
197: }
198:
199: /**
200: * Gets the template path for the given channel name, the given template
201: * name and the given filepath. If non-null, the key returned by this
202: * method will map to a valid template file. It internally calls
203: * ProviderContext.getTemplateMostSpecificPath to get the template path.
204: *
205: * @param pc - ProviderContext of the calling provider
206: * @param name - Channel name
207: * @param file - Template name
208: *
209: * @return Path key to access the template
210: */
211:
212: public File getExistingJSPPath(ProviderContext pc, String name,
213: String file) throws ProviderException {
214: File jspPath = null;
215: try {
216: jspPath = RenderingUtil.getTemplatePath(
217: (ContainerProviderContext) pc, name, file);
218: } catch (ProviderContextException e) {
219: throw new ProviderException(e.getMessage());
220: }
221:
222: return jspPath;
223: }
224:
225: }
|