001: /*
002: * (C) Copyright 2000 - 2005 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: package com.nabhinc.portlet.portletadmin;
020:
021: import java.io.IOException;
022: import java.util.HashMap;
023: import java.util.Hashtable;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletException;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import com.nabhinc.core.MimeTypes;
032: import com.nabhinc.portal.config.PortletType;
033: import com.nabhinc.portal.container.PortletRequestImpl;
034: import com.nabhinc.portal.container.PortletResponseImpl;
035: import com.nabhinc.portal.core.PortalServlet;
036: import com.nabhinc.portal.core.PortletConfigInfo;
037: import com.nabhinc.portlet.portletadmin.PortletUtil;
038: import com.nabhinc.portlet.base.BasePortlet;
039: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
040: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
041: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
042: import com.nabhinc.util.StringUtil;
043:
044: /**
045: *
046: *
047: * @author Padmanabh Dabke
048: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
049: * @since 2.4.1
050: */
051: public class BasePortletCreator extends BaseRequestProcessor implements
052: ActionProcessor {
053:
054: /* (non-Javadoc)
055: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
056: */
057: public String process(ActionRequest request,
058: ActionResponse response, ActionConfig actionConfig)
059: throws PortletException, IOException {
060:
061: final String PORTLET_TYPE = "com.nabhinc.portlet.base.BasePortlet";
062: final String VIEW_MODE = "view";
063: final String EDIT_MODE = "edit";
064: final String HELP_MODE = "help";
065:
066: boolean isEdit = "true".equals(request.getParameter("edit"));
067: String portletIndex = request.getParameter("portlet_index");
068: int pIndex = portletIndex == null ? -1 : Integer
069: .parseInt(portletIndex);
070: String requestType = request.getParameter("request_type");
071:
072: //HTML mime type is supported by default
073: boolean isHTMLMimeTypeSupported = true; //rReq.getParameter("html_mime_type") != null;
074: boolean isHTMLEditSupported = request
075: .getParameter("html_edit_mode") != null;
076: boolean isHTMLHelpSupported = request
077: .getParameter("html_help_mode") != null;
078: boolean isXHTMLMimeTypeSupported = true;
079: boolean isWMLMimeTypeSupported = true;
080:
081: String pName = request.getParameter("pname");
082: String title = request.getParameter("title");
083: String keywordsStr = request.getParameter("keywords");
084: String shortTitle = request.getParameter("short_title");
085: String expCache = request.getParameter("exp_cache");
086: String resourceBundleName = request
087: .getParameter("resource_bundle");
088: String supportedLocales = request
089: .getParameter("supported_locales");
090:
091: String totalSecRoles = request.getParameter("sec_roles_total");
092: String totalDescrLang = request
093: .getParameter("descr_lang_total");
094: String totalDispLang = request
095: .getParameter("display_lang_total");
096:
097: int numOfSecRoles = Integer.parseInt(totalSecRoles);
098: int numOfDescrLangs = Integer.parseInt(totalDescrLang);
099: int numOfDispLangs = Integer.parseInt(totalDispLang);
100:
101: String contentPath = request.getParameter("content_path");
102: String editPath = request.getParameter("edit_path");
103: String helpPath = request.getParameter("help_path");
104:
105: String error = "";
106: if (pName == null || "".equals(pName.trim())) {
107: error += "You must provide portlet name.<br/>";
108: } else if (PortalServlet.getInstance().isPortletExists(pName)) {
109: if (!isEdit
110: || !pName.equals(((PortletConfigInfo) PortalServlet
111: .getInstance().getPortletConfigInfoList()
112: .elementAt(pIndex)).name)) {
113: error += "Duplicate portlet name.<br/>";
114: }
115: }
116: if (title == null || "".equals(title.trim())) {
117: error += "You must provide portlet title.<br/>";
118: }
119: if (contentPath == null || "".equals(contentPath.trim())) {
120: error += "View page path is required.<br/>";
121: }
122: if (expCache != null && !expCache.trim().equals("")) {
123: try {
124: Integer.parseInt(expCache);
125: } catch (Exception ex) {
126: error += "Expiration cache must be a number.<br/>";
127: }
128: }
129:
130: if (!error.equals("")) {
131: response.setRenderParameter("error", error);
132: if (isEdit)
133: response.setRenderParameter("edit", "true");
134: return "failure";
135: }
136:
137: PortletType pType = PortletUtil.createPortletType(pName,
138: PORTLET_TYPE);
139: PortletUtil.setTitle(title, pType);
140: PortletUtil.setShortTitle(shortTitle, pType);
141: PortletUtil.setResourceBundle(resourceBundleName, pType);
142:
143: HashMap descrMap = new HashMap();
144: for (int i = 0; i < numOfDescrLangs; i++) {
145: String lang = request.getParameter("descr_lang" + i);
146: String descr = request.getParameter("descr" + i);
147: if (descrMap.containsKey(lang))
148: continue;
149: descrMap.put(lang, descr);
150: }
151: PortletUtil.setDescription(descrMap, pType);
152:
153: HashMap displayMap = new HashMap();
154: for (int i = 0; i < numOfDispLangs; i++) {
155: String lang = request.getParameter("display_lang" + i);
156: String displayName = request.getParameter("display_name"
157: + i);
158: if (displayMap.containsKey(lang))
159: continue;
160: displayMap.put(lang, displayName);
161: }
162: PortletUtil.setDisplayName(displayMap, pType);
163:
164: if (expCache != null && !expCache.trim().equals(""))
165: PortletUtil.setExpirationCache(Integer.parseInt(expCache),
166: pType);
167:
168: Hashtable initParams = new Hashtable();
169: if (contentPath.indexOf("://") == -1
170: && !contentPath.startsWith("/")) {
171: contentPath = "/" + contentPath;
172: }
173: initParams.put(BasePortlet.CONTENT_JSP_PARAM, contentPath);
174:
175: if (editPath != null && !editPath.trim().equals("")) {
176: if (editPath.indexOf("://") == -1
177: && !editPath.startsWith("/")) {
178: editPath = "/" + editPath;
179: }
180: initParams.put(BasePortlet.EDIT_JSP_PARAM, editPath);
181: isHTMLEditSupported = true;
182: }
183:
184: if (helpPath != null && !helpPath.trim().equals("")) {
185: if (helpPath.indexOf("://") == -1
186: && !helpPath.startsWith("/")) {
187: helpPath = "/" + helpPath;
188: }
189: initParams.put(BasePortlet.HELP_JSP_PARAM, helpPath);
190: isHTMLHelpSupported = true;
191: }
192:
193: PortletUtil.setInitParams(initParams, pType);
194: PortletUtil.setKeywords(keywordsStr, pType);
195:
196: if (isHTMLMimeTypeSupported) {
197: String[] supportedModes = null;
198: if (isHTMLEditSupported && isHTMLHelpSupported) {
199: supportedModes = new String[] { EDIT_MODE, HELP_MODE };
200:
201: } else if (isHTMLEditSupported) {
202: supportedModes = new String[] { EDIT_MODE };
203:
204: } else if (isHTMLHelpSupported) {
205: supportedModes = new String[] { HELP_MODE };
206:
207: }
208:
209: PortletUtil.setSupports(supportedModes, MimeTypes.HTML,
210: pType);
211: }
212:
213: if (isXHTMLMimeTypeSupported) {
214: String[] supportedModes = new String[0];
215: PortletUtil.setSupports(supportedModes, MimeTypes.XHTML_MP,
216: pType);
217: }
218:
219: if (isWMLMimeTypeSupported) {
220: String[] supportedModes = new String[0];
221: PortletUtil.setSupports(supportedModes, MimeTypes.WML,
222: pType);
223: }
224:
225: if (supportedLocales != null
226: && !"".equals(supportedLocales.trim())) {
227: String[] locales = StringUtil.split(supportedLocales, ",");
228: PortletUtil.setSupportedLocales(locales, pType);
229: }
230:
231: HashMap secRoleMap = new HashMap();
232: for (int i = 0; i < numOfSecRoles; i++) {
233: String roleName = request.getParameter("role_name" + i);
234: if (roleName != null && !"".equals(roleName.trim())) {
235: secRoleMap.put(roleName, request
236: .getParameter("role_link" + i));
237: }
238: }
239: PortletUtil.setSecurityRoleReference(secRoleMap, pType);
240:
241: try {
242: if (isEdit) {
243: HttpServletRequest req = ((PortletRequestImpl) request)
244: .getHttpServletRequest();
245: HttpServletResponse res = ((PortletResponseImpl) response)
246: .getHttpServletResponse();
247: PortalServlet.getInstance().replacePortlet(pType,
248: pIndex, req, res);
249: } else {
250: PortalServlet.getInstance().addPortlet(pType);
251: }
252: PortalServlet.getInstance().savePortletConfiguration();
253:
254: } catch (Exception ex) {
255: throw new PortletException("Failed to create JSP portlet.",
256: ex);
257: }
258:
259: return "success";
260: }
261:
262: }
|