001: /**
002: * $Id: JspPortlet.java,v 1.1 2004/06/23 00:27:42 jtb 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 Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.jspportlet;
014:
015: import javax.portlet.GenericPortlet;
016: import javax.portlet.RenderRequest;
017: import javax.portlet.ActionRequest;
018: import javax.portlet.ActionResponse;
019: import javax.portlet.RenderResponse;
020: import javax.portlet.PortletException;
021: import javax.portlet.PortletURL;
022: import javax.portlet.PortletPreferences;
023: import javax.portlet.PortletContext;
024: import javax.portlet.PortletRequestDispatcher;
025: import javax.portlet.PortletConfig;
026: import javax.portlet.WindowState;
027: import javax.portlet.PortletMode;
028:
029: import java.io.File;
030: import java.io.IOException;
031: import java.io.PrintWriter;
032: import java.io.InputStream;
033:
034: import java.util.Locale;
035: import java.util.Map;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.Collections;
039:
040: public class JspPortlet extends GenericPortlet {
041: private PortletContext pContext;
042:
043: public void init(PortletConfig config) throws PortletException {
044: super .init(config);
045: pContext = config.getPortletContext();
046: }
047:
048: public void doView(RenderRequest request, RenderResponse response)
049: throws PortletException, IOException {
050: String contentPage = getContentJSP(request);
051:
052: response.setContentType(request.getResponseContentType());
053: if (contentPage != null && contentPage.length() != 0) {
054: try {
055: PortletRequestDispatcher dispatcher = pContext
056: .getRequestDispatcher(contentPage);
057: dispatcher.include(request, response);
058: } catch (IOException e) {
059: throw new PortletException(
060: "JSPPortlet.doView exception", e);
061: }
062: }
063:
064: }
065:
066: public void doEdit(RenderRequest request, RenderResponse response)
067: throws PortletException {
068: String editPage = getEditJSP(request);
069:
070: response.setContentType(request.getResponseContentType());
071: if (editPage != null && editPage.length() != 0) {
072: try {
073: PortletRequestDispatcher dispatcher = pContext
074: .getRequestDispatcher(editPage);
075: dispatcher.include(request, response);
076: } catch (IOException e) {
077: throw new PortletException(
078: "JSPPortlet.doEdit exception", e);
079: }
080: }
081: }
082:
083: public void doHelp(RenderRequest request, RenderResponse response)
084: throws PortletException {
085: String helpPage = getHelpJSP(request);
086:
087: response.setContentType(request.getResponseContentType());
088: if (helpPage != null && helpPage.length() != 0) {
089: try {
090: PortletRequestDispatcher dispatcher = pContext
091: .getRequestDispatcher(helpPage);
092: dispatcher.include(request, response);
093: } catch (IOException e) {
094: throw new PortletException(
095: "JSPPortlet.doHelp exception", e);
096: }
097: }
098: }
099:
100: public void processAction(ActionRequest request,
101: ActionResponse actionResponse) throws PortletException,
102: java.io.IOException {
103: actionResponse.setRenderParameters(request.getParameterMap());
104: }
105:
106: protected String getContentJSP(RenderRequest request)
107: throws PortletException {
108: PortletPreferences pref = request.getPreferences();
109: String contentPage = pref.getValue("contentPage", "");
110: return getLocalizedJSP(request.getLocale(), contentPage);
111: }
112:
113: protected String getEditJSP(RenderRequest request)
114: throws PortletException {
115: PortletPreferences pref = request.getPreferences();
116: String editPage = pref.getValue("editPage", "");
117: return getLocalizedJSP(request.getLocale(), editPage);
118: }
119:
120: protected String getHelpJSP(RenderRequest request)
121: throws PortletException {
122: PortletPreferences pref = request.getPreferences();
123: String helpPage = pref.getValue("helpPage", "");
124: return getLocalizedJSP(request.getLocale(), helpPage);
125: }
126:
127: protected String getLocalizedJSP(Locale locale, String jspPath) {
128: String realJspPath = jspPath;
129:
130: if (locale != null) {
131: int separator = jspPath.lastIndexOf("/");
132: String jspBaseDir = jspPath.substring(0, separator);
133: String jspFileName = jspPath.substring(separator + 1);
134: PortletContext pContext = getPortletContext();
135:
136: String searchPath = getJSPPath(jspBaseDir, locale
137: .toString(), jspFileName);
138:
139: // search the requested JSP from the following location:
140: // <ctxt_root>/<portlet_base_dir>_<language>_<country>/<jsp_file_name>
141: if (pContext.getResourceAsStream(searchPath) != null) {
142: realJspPath = searchPath;
143: } else {
144: // if the country code is not empty, try to search the
145: // requested JSP from the following location:
146: // <ctxt_root>/<portlet_base_dir>_<language>/<jsp_file_name>
147: if (!locale.getCountry().equals("")) {
148: searchPath = getJSPPath(jspBaseDir, locale
149: .getLanguage(), jspFileName);
150:
151: if (pContext.getResourceAsStream(searchPath) != null) {
152: realJspPath = searchPath;
153: }
154: }
155: }
156: }
157: return realJspPath;
158: }
159:
160: private String getJSPPath(String jspBaseDir, String localeStr,
161: String jspFileName) {
162: StringBuffer sb = new StringBuffer();
163: sb.append(jspBaseDir).append("_").append(localeStr).append("/")
164: .append(jspFileName);
165: return sb.toString();
166: }
167: }
|