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