001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037:
038: package com.sun.portal.portlet.samples.jspportlet;
039:
040: import javax.portlet.GenericPortlet;
041: import javax.portlet.RenderRequest;
042: import javax.portlet.ActionRequest;
043: import javax.portlet.ActionResponse;
044: import javax.portlet.RenderResponse;
045: import javax.portlet.PortletException;
046: import javax.portlet.PortletURL;
047: import javax.portlet.PortletPreferences;
048: import javax.portlet.PortletContext;
049: import javax.portlet.PortletRequestDispatcher;
050: import javax.portlet.PortletConfig;
051: import javax.portlet.WindowState;
052: import javax.portlet.PortletMode;
053:
054: import java.io.File;
055: import java.io.IOException;
056: import java.io.PrintWriter;
057: import java.io.InputStream;
058:
059: import java.util.Locale;
060: import java.util.Map;
061: import java.util.HashMap;
062: import java.util.Iterator;
063: import java.util.Collections;
064:
065: public class JSPPortlet extends GenericPortlet {
066:
067: private PortletContext pContext;
068:
069: public void init(PortletConfig config) throws PortletException {
070: super .init(config);
071: pContext = config.getPortletContext();
072: }
073:
074: public void doView(RenderRequest request, RenderResponse response)
075: throws PortletException, IOException {
076: String contentPage = getContentJSP(request);
077:
078: response.setContentType(request.getResponseContentType());
079: if (contentPage != null && contentPage.length() != 0) {
080: try {
081: PortletRequestDispatcher dispatcher = pContext
082: .getRequestDispatcher(contentPage);
083: dispatcher.include(request, response);
084: } catch (IOException e) {
085: throw new PortletException(
086: "JSPPortlet.doView exception", e);
087: }
088: }
089:
090: }
091:
092: public void doEdit(RenderRequest request, RenderResponse response)
093: throws PortletException {
094: String editPage = getEditJSP(request);
095:
096: response.setContentType(request.getResponseContentType());
097: if (editPage != null && editPage.length() != 0) {
098: try {
099: PortletRequestDispatcher dispatcher = pContext
100: .getRequestDispatcher(editPage);
101: dispatcher.include(request, response);
102: } catch (IOException e) {
103: throw new PortletException(
104: "JSPPortlet.doEdit exception", e);
105: }
106: }
107: }
108:
109: public void doHelp(RenderRequest request, RenderResponse response)
110: throws PortletException {
111: String helpPage = getHelpJSP(request);
112:
113: response.setContentType(request.getResponseContentType());
114: if (helpPage != null && helpPage.length() != 0) {
115: try {
116: PortletRequestDispatcher dispatcher = pContext
117: .getRequestDispatcher(helpPage);
118: dispatcher.include(request, response);
119: } catch (IOException e) {
120: throw new PortletException(
121: "JSPPortlet.doHelp exception", e);
122: }
123: }
124: }
125:
126: public void processAction(ActionRequest request,
127: ActionResponse actionResponse) throws PortletException,
128: java.io.IOException {
129: actionResponse.setRenderParameters(request.getParameterMap());
130: }
131:
132: protected String getContentJSP(RenderRequest request)
133: throws PortletException {
134: PortletPreferences pref = request.getPreferences();
135: String contentPage = pref.getValue("contentPage", "");
136: return getLocalizedJSP(request.getLocale(), contentPage);
137: }
138:
139: protected String getEditJSP(RenderRequest request)
140: throws PortletException {
141: PortletPreferences pref = request.getPreferences();
142: String editPage = pref.getValue("editPage", "");
143: return getLocalizedJSP(request.getLocale(), editPage);
144: }
145:
146: protected String getHelpJSP(RenderRequest request)
147: throws PortletException {
148: PortletPreferences pref = request.getPreferences();
149: String helpPage = pref.getValue("helpPage", "");
150: return getLocalizedJSP(request.getLocale(), helpPage);
151: }
152:
153: protected String getLocalizedJSP(Locale locale, String jspPath) {
154: String realJspPath = jspPath;
155:
156: if (locale != null) {
157: int separator = jspPath.lastIndexOf("/");
158: String jspBaseDir = jspPath.substring(0, separator);
159: String jspFileName = jspPath.substring(separator + 1);
160: PortletContext pContext = getPortletContext();
161:
162: String searchPath = getJSPPath(jspBaseDir, locale
163: .toString(), jspFileName);
164:
165: // search the requested JSP from the following location:
166: // <ctxt_root>/<portlet_base_dir>_<language>_<country>/<jsp_file_name>
167: if (pContext.getResourceAsStream(searchPath) != null) {
168: realJspPath = searchPath;
169: } else {
170: // if the country code is not empty, try to search the
171: // requested JSP from the following location:
172: // <ctxt_root>/<portlet_base_dir>_<language>/<jsp_file_name>
173: if (!locale.getCountry().equals("")) {
174: searchPath = getJSPPath(jspBaseDir, locale
175: .getLanguage(), jspFileName);
176:
177: if (pContext.getResourceAsStream(searchPath) != null) {
178: realJspPath = searchPath;
179: }
180: }
181: }
182: }
183: return realJspPath;
184: }
185:
186: private String getJSPPath(String jspBaseDir, String localeStr,
187: String jspFileName) {
188: StringBuffer sb = new StringBuffer();
189: sb.append(jspBaseDir).append("_").append(localeStr).append("/")
190: .append(jspFileName);
191: return sb.toString();
192: }
193: }
|