001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.base;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.HashMap;
025: import java.util.Locale;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: import javax.portlet.GenericPortlet;
030: import javax.portlet.PortletConfig;
031: import javax.portlet.PortletContext;
032: import javax.portlet.PortletException;
033: import javax.portlet.PortletMode;
034: import javax.portlet.PortletRequestDispatcher;
035: import javax.portlet.RenderRequest;
036: import javax.portlet.RenderResponse;
037: import javax.portlet.WindowState;
038:
039: import com.nabhinc.portlet.common.CommonUtil;
040: import com.nabhinc.util.StringUtil;
041:
042: /**
043: *
044: *
045: * @author Padmanabh Dabke
046: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
047: */
048: public class BasePortlet extends GenericPortlet {
049:
050: public static final String CONTENT_JSP_PARAM = "contentPage";
051: public static final String HELP_JSP_PARAM = "helpPage";
052: public static final String EDIT_JSP_PARAM = "editPage";
053: public static final String ADMIN_JSP_PARAM = "adminPage";
054: public static final String PREVIEW_JSP_PARAM = "previewPage";
055: public static final String CONFIG_JSP_PARAM = "configPage";
056: public static final String ROOT_DIR_PARAM = "rootDir";
057:
058: public static final PortletMode ADMIN_MODE = new PortletMode(
059: "admin");
060: public static final PortletMode PREVIEW_MODE = new PortletMode(
061: "preview");
062: public static final PortletMode CONFIG_MODE = new PortletMode(
063: "config");
064:
065: /**
066: * Default logger for the portlet
067: */
068: protected Logger bpLogger = null;
069:
070: protected PortletContext bpContext;
071:
072: private String bpEditJSP = null;
073:
074: private String bpContentJSP = null;
075:
076: private String bpHelpJSP = null;
077:
078: private String bpAdminJSP = null;
079:
080: private String bpConfigJSP = null;
081:
082: private String bpPreviewJSP = null;
083:
084: private String bpRootDir = null;
085:
086: private HashMap bpLocalizedPaths = new HashMap();
087:
088: public void init(PortletConfig config) throws PortletException {
089: super .init(config);
090: bpContext = config.getPortletContext();
091: bpContentJSP = config.getInitParameter(CONTENT_JSP_PARAM);
092: bpEditJSP = config.getInitParameter(EDIT_JSP_PARAM);
093: bpHelpJSP = config.getInitParameter(HELP_JSP_PARAM);
094: bpAdminJSP = config.getInitParameter(ADMIN_JSP_PARAM);
095: bpConfigJSP = config.getInitParameter(CONFIG_JSP_PARAM);
096: bpPreviewJSP = config.getInitParameter(PREVIEW_JSP_PARAM);
097: bpRootDir = config.getInitParameter(ROOT_DIR_PARAM);
098: bpLogger = Logger.getLogger(getPortletName());
099: }
100:
101: /**
102: * The default implementation of this method routes the render request
103: * to a set of helper methods depending on the current portlet mode the
104: * portlet is currently in.
105: * These methods are:
106: * <ul>
107: * <li><code>doView</code> for handling <code>view</code> requests
108: * <li><code>doEdit</code> for handling <code>edit</code> requests
109: * <li><code>doHelp</code> for handling <code>help</code> requests
110: * </ul>
111: * <P>
112: * If the window state of this portlet is <code>minimized</code>, this
113: * method does not invoke any of the portlet mode rendering methods.
114: * <p>
115: * For handling custom portlet modes the portlet should override this
116: * method.
117: *
118: * @param request
119: * the render request
120: * @param response
121: * the render response
122: *
123: * @exception PortletException
124: * if the portlet cannot fulfilling the request
125: * @exception UnavailableException
126: * if the portlet is unavailable to perform render at this time
127: * @exception PortletSecurityException
128: * if the portlet cannot fullfill this request because of security reasons
129: * @exception java.io.IOException
130: * if the streaming causes an I/O problem
131: *
132: * @see #doView(RenderRequest, RenderResponse)
133: * @see #doEdit(RenderRequest, RenderResponse)
134: * @see #doHelp(RenderRequest, RenderResponse)
135: */
136: protected void doDispatch(RenderRequest request,
137: RenderResponse response) throws PortletException,
138: java.io.IOException {
139: WindowState state = request.getWindowState();
140:
141: if (!state.equals(WindowState.MINIMIZED)) {
142: PortletMode mode = request.getPortletMode();
143: if (mode.equals(PortletMode.VIEW)) {
144: doView(request, response);
145: } else if (mode.equals(PortletMode.EDIT)) {
146: doEdit(request, response);
147: } else if (mode.equals(PortletMode.HELP)) {
148: doHelp(request, response);
149: } else if (mode.equals(ADMIN_MODE)) {
150: doAdmin(request, response);
151: } else if (mode.equals(PREVIEW_MODE)) {
152: doPreview(request, response);
153: } else if (mode.equals(CONFIG_MODE)) {
154: doConfig(request, response);
155: } else {
156: throw new PortletException("unknown portlet mode: "
157: + mode);
158: }
159: }
160:
161: }
162:
163: /**
164: * Displays portlet content configured via "contentPage" parameter.
165: * The default implementation throws an exception.
166: * @param request The portlet request.
167: * @param response The render response.
168: * @exception PortletException If the portlet cannot fulfilling the request.
169: * @exception IOException If the streaming causes an I/O problem
170: */
171: public void doView(RenderRequest request, RenderResponse response)
172: throws PortletException, IOException {
173:
174: String contentJSP = getContentJSP(request);
175:
176: if (contentJSP == null || contentJSP.trim().equals("")) {
177: throw new PortletException("Content page not set.");
178: } else {
179: response.setContentType(request.getResponseContentType());
180: PortletRequestDispatcher dispatcher = bpContext
181: .getRequestDispatcher(contentJSP);
182: dispatcher.include(request, response);
183: }
184:
185: }
186:
187: /**
188: * Helper method to serve up the <code>edit</code> mode.
189: * The default implementation throws an exception.
190: * @param request The portlet request
191: * @param response The render response
192: * @exception PortletException If the portlet cannot fulfilling the request
193: * @exception IOException If the streaming causes an I/O problem
194: */
195: public void doEdit(RenderRequest request, RenderResponse response)
196: throws PortletException, IOException {
197:
198: String editJSP = getEditJSP(request);
199:
200: if (editJSP == null || editJSP.trim().equals("")) {
201: throw new PortletException("Edit page not set.");
202: } else {
203: response.setContentType(request.getResponseContentType());
204: PortletRequestDispatcher dispatcher = bpContext
205: .getRequestDispatcher(editJSP);
206: dispatcher.include(request, response);
207: }
208: }
209:
210: /**
211: * Helper method to serve up the <code>help</code> mode.
212: * The default implementation throws an exception.
213: * @param request The portlet request
214: * @param response The render response
215: * @exception PortletException If the portlet cannot fulfilling the request
216: * @exception IOException If the streaming causes an I/O problem
217: */
218: public void doHelp(RenderRequest request, RenderResponse response)
219: throws PortletException, IOException {
220: String helpJSP = getHelpJSP(request);
221:
222: if (helpJSP == null || helpJSP.trim().equals("")) {
223: throw new PortletException("Help page not set.");
224: } else {
225: response.setContentType(request.getResponseContentType());
226: PortletRequestDispatcher dispatcher = bpContext
227: .getRequestDispatcher(helpJSP);
228: dispatcher.include(request, response);
229: }
230: }
231:
232: /**
233: * Displays portlet content configured via "adminPage" parameter.
234: * The default implementation throws an exception.
235: * @param request The portlet request.
236: * @param response The render response.
237: * @exception PortletException If the portlet cannot fulfilling the request.
238: * @exception IOException If the streaming causes an I/O problem
239: */
240: public void doAdmin(RenderRequest request, RenderResponse response)
241: throws PortletException, IOException {
242:
243: String contentJSP = getAdminJSP(request);
244:
245: if (contentJSP == null || contentJSP.trim().equals("")) {
246: throw new PortletException("Admin page not set.");
247: } else {
248: response.setContentType(request.getResponseContentType());
249: PortletRequestDispatcher dispatcher = bpContext
250: .getRequestDispatcher(contentJSP);
251: dispatcher.include(request, response);
252: }
253:
254: }
255:
256: /**
257: * Helper method to serve up the <code>preview</code> mode.
258: * The default implementation throws an exception.
259: * @param request The portlet request
260: * @param response The render response
261: * @exception PortletException If the portlet cannot fulfilling the request
262: * @exception IOException If the streaming causes an I/O problem
263: */
264: public void doPreview(RenderRequest request, RenderResponse response)
265: throws PortletException, IOException {
266:
267: String previewJSP = getPreviewJSP(request);
268:
269: if (previewJSP == null || previewJSP.trim().equals("")) {
270: throw new PortletException("Preview page not set.");
271: } else {
272: response.setContentType(request.getResponseContentType());
273: PortletRequestDispatcher dispatcher = bpContext
274: .getRequestDispatcher(previewJSP);
275: dispatcher.include(request, response);
276: }
277: }
278:
279: /**
280: * Helper method to serve up the <code>config</code> mode.
281: * The default implementation throws an exception.
282: * @param request The portlet request
283: * @param response The render response
284: * @exception PortletException If the portlet cannot fulfilling the request
285: * @exception IOException If the streaming causes an I/O problem
286: */
287: public void doConfig(RenderRequest request, RenderResponse response)
288: throws PortletException, IOException {
289:
290: String configJSP = getConfigJSP(request);
291:
292: if (configJSP == null || configJSP.trim().equals("")) {
293: throw new PortletException("Config page not set.");
294: } else {
295: response.setContentType(request.getResponseContentType());
296: PortletRequestDispatcher dispatcher = bpContext
297: .getRequestDispatcher(configJSP);
298: dispatcher.include(request, response);
299: }
300: }
301:
302: protected String getContentJSP(RenderRequest request)
303: throws PortletException {
304: return getLocalizedJSP(request.getLocale(), bpContentJSP);
305: }
306:
307: protected String getEditJSP(RenderRequest request)
308: throws PortletException {
309: return getLocalizedJSP(request.getLocale(), bpEditJSP);
310: }
311:
312: protected String getHelpJSP(RenderRequest request)
313: throws PortletException {
314: return getLocalizedJSP(request.getLocale(), bpHelpJSP);
315: }
316:
317: protected String getAdminJSP(RenderRequest request)
318: throws PortletException {
319: return getLocalizedJSP(request.getLocale(), bpAdminJSP);
320: }
321:
322: protected String getPreviewJSP(RenderRequest request)
323: throws PortletException {
324: return getLocalizedJSP(request.getLocale(), bpPreviewJSP);
325: }
326:
327: protected String getConfigJSP(RenderRequest request)
328: throws PortletException {
329: return getLocalizedJSP(request.getLocale(), bpConfigJSP);
330: }
331:
332: /**
333: * Log a debug message.
334: * @param msg Message to be logged
335: */
336: public void debug(String method, String msg) {
337: bpLogger.logp(Level.FINEST, getPortletName(), method, msg);
338: }
339:
340: /**
341: * Log an error message.
342: * @param msg Message to be logged
343: */
344: public void error(String method, String msg) {
345: bpLogger.logp(Level.SEVERE, getPortletName(), method, msg);
346: }
347:
348: /**
349: * Log informational message.
350: * @param msg Message to be logged
351: */
352: public void info(String method, String msg) {
353: bpLogger.logp(Level.INFO, getPortletName(), method, msg);
354: }
355:
356: /**
357: * Log a warning message.
358: * @param msg Message to be logged
359: */
360: public void warning(String method, String msg) {
361: bpLogger.logp(Level.WARNING, getPortletName(), method, msg);
362: }
363:
364: protected String getRealPath(PortletContext pContext, String relPath) {
365: if (bpRootDir == null) {
366: return pContext.getRealPath(relPath);
367: } else {
368: String realPath = null;
369: if (relPath.startsWith("/")) {
370: realPath = bpRootDir + relPath;
371: } else {
372: realPath = bpRootDir + File.separator + relPath;
373: }
374: realPath = StringUtil.replacePathSeparator(realPath);
375: ;
376: return realPath;
377: }
378: }
379:
380: @SuppressWarnings("unchecked")
381: protected String getLocalizedJSP(Locale locale, String path) {
382: String localizedPath = (String) bpLocalizedPaths.get(path + ":"
383: + locale);
384: if (localizedPath == null) {
385: localizedPath = CommonUtil.getLocalizedPath(path, locale,
386: bpContext);
387: bpLocalizedPaths.put(path + ":" + locale, localizedPath);
388: }
389: return localizedPath;
390: }
391:
392: }
|