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.bookmarkportlet;
039:
040: import java.io.IOException;
041: import java.util.Vector;
042:
043: import javax.portlet.ActionRequest;
044: import javax.portlet.ActionResponse;
045: import javax.portlet.PortletConfig;
046: import javax.portlet.PortletException;
047: import javax.portlet.PortletMode;
048: import javax.portlet.PortletPreferences;
049: import javax.portlet.RenderRequest;
050: import javax.portlet.ValidatorException;
051:
052: import com.sun.portal.portlet.samples.jspportlet.JSPPortlet;
053:
054: public class BookmarkPortlet extends JSPPortlet {
055:
056: // file names
057: public static final String CANCEL_EDIT = "Cancel";
058: public static final String FINISHED_EDIT = "Finished";
059: public static final String ADD_RESOURCE = "Add Resource";
060: public static final String RESOURCE_COUNT = "resourceCount";
061: public static final String RESOURCE_NAME = "resourceName";
062: public static final String RESOURCE_URL = "resourceURL";
063: public static final String WINDOW_PREF = "windowPref";
064:
065: // name of JSP pages
066: public static final String BOOKMARK_CONTENT_PAGE = "bookmarkContentPage";
067: public static final String BOOKMARK_EDIT_PAGE = "bookmarkEditPage";
068: public static final String BOOKMARK_HELP_PAGE = "bookmarkHelpPage";
069:
070: public static final String TARGETS = "targets";
071: public static final String CONTENT_TEMPLATE = "display.template";
072: public static final String URL_WRAP_TEMPLATE = "urlWrapper.template";
073: public static final String IDENTIFIER = "BookmarkPortlet";
074: public static final String ENCODER_NAME_HTML = "com.sun.portal.portlet.samples.bookmarkportlet.HTMLEncoder";
075: public static final String ENCODER_NAME_FORMNAME = "com.sun.portal.portlet.samples.bookmarkportlet.FormNameEncoder";
076: public static TypeEncoder HTML_ENCODER = null;
077: public static TypeEncoder FORMNAME_ENCODER = null;
078: public static final String OWN_WINDOW = "Open each page in its own window.";
079: public static final String SINGLE_WINDOW = "Open all pages in a single browser window.";
080: public static final String MAIN_WINDOW = "Open all pages in the main desktop window.";
081: public static final String NON_ASCII_URL = "Resource URL must be ASCII characters!";
082: public static final String EMPTY_URL = "ERROR: Please enter a value for the URL: ";
083: public static final String EMPTY_NAME = "ERROR: Please enter a value for the Bookmark Name: ";
084: public static final String EMPTY_NAME_URL = "ERROR: Please enter a value for the Bookmark Name and URL";
085: public static final String REMOVE_ERROR = "ERROR: Please remove a bookmark by selecting the remove check box";
086: public static final String RENDER_PARAM_ERROR = "render_param_error";
087:
088: public void init(PortletConfig config) throws PortletException {
089: super .init(config);
090:
091: try {
092: if (HTML_ENCODER == null) {
093: HTML_ENCODER = (TypeEncoder) Class.forName(
094: ENCODER_NAME_HTML).newInstance();
095: }
096: if (FORMNAME_ENCODER == null) {
097: FORMNAME_ENCODER = (TypeEncoder) Class.forName(
098: ENCODER_NAME_FORMNAME).newInstance();
099: }
100: } catch (ClassNotFoundException cnfe) {
101: throw new PortletException(
102: "BookmarkPortlet static initializer", cnfe);
103: } catch (InstantiationException ie) {
104: throw new PortletException(
105: "BookmarkPortlet static initializer", ie);
106: } catch (IllegalAccessException iae) {
107: throw new PortletException(
108: "BookmarkPortlet static initializer", iae);
109: }
110: }
111:
112: public void processAction(ActionRequest request,
113: ActionResponse response) throws PortletException {
114:
115: boolean errorFlag = false;
116: Vector targets = new Vector();
117:
118: if (isAction(request, CANCEL_EDIT)) {
119: response.setPortletMode(PortletMode.VIEW);
120: } else {
121: // find out which book marks were checked to remove
122: //
123: String rc = request.getParameter(RESOURCE_COUNT);
124:
125: int resourceCount = Integer.parseInt(rc);
126:
127: for (int i = 0; i < resourceCount; i++) {
128: String checkBoxValue = request.getParameter("remove"
129: + i);
130: Integer removeFlag = (checkBoxValue == null) ? Integers
131: .get(0) : Integer.valueOf(checkBoxValue);
132: //
133: // if the box was NOT check then we will keep it
134: // in the bookmark list. Build the name and url
135: // vectors to store in the hashtable
136: //
137: if (removeFlag.intValue() != 1) {
138: String nameString = "name" + i;
139: String urlString = "url" + i;
140:
141: String name = request.getParameter(nameString);
142: String url = request.getParameter(urlString);
143:
144: // Error handle when one of the existing bookmarks
145: // is changed
146: if (!isParamValid(name) && !isParamValid(url)) {
147: response.setRenderParameter(RENDER_PARAM_ERROR,
148: REMOVE_ERROR);
149: errorFlag = true;
150: } else if (!isParamValid(name)) {
151: response.setRenderParameter(RENDER_PARAM_ERROR,
152: EMPTY_NAME + url);
153: errorFlag = true;
154: } else if (!isParamValid(url)) {
155: response.setRenderParameter(RENDER_PARAM_ERROR,
156: EMPTY_URL + name);
157: errorFlag = true;
158: }
159: if (!I18n.isAscii(url)) {
160: response.setRenderParameter(RENDER_PARAM_ERROR,
161: NON_ASCII_URL);
162: errorFlag = true;
163: }
164:
165: // if input error is detected, return to the edit page
166: // with error message
167: if (errorFlag) {
168: response.setPortletMode(PortletMode.EDIT);
169: } else {
170: Target target = new Target(name, url);
171:
172: if ((target.getValue().indexOf("://") == -1)
173: && (!target.getValue().startsWith("/"))) {
174: // add http if there's no protocol specificed
175: // and it doesn't start with a '/'
176: target.setValue("http://"
177: + target.getValue());
178: }
179: targets.add(target.toString());
180: }
181: }
182: }
183:
184: if (!errorFlag) {
185: //
186: // get the new bookmark information
187: //
188: String newName = request.getParameter(RESOURCE_NAME);
189: String newURL = request.getParameter(RESOURCE_URL);
190:
191: if (isAction(request, ADD_RESOURCE)) {
192: response.setPortletMode(PortletMode.EDIT);
193: }
194:
195: if (isParamValid(newName) && isParamValid(newURL)) {
196:
197: if (!I18n.isAscii(newURL)) {
198: response.setRenderParameter(RENDER_PARAM_ERROR,
199: NON_ASCII_URL);
200: errorFlag = true;
201: } else {
202: Target target = new Target(newName, newURL);
203:
204: if ((target.getValue().indexOf("://") == -1)
205: && (!target.getValue().startsWith("/"))) {
206:
207: // add http if there's no protocol specificed
208: // and it doesn't start with a '/'
209: target.setValue("http://"
210: + target.getValue());
211: }
212:
213: //
214: // add the new target to the vector of all targets
215: //
216: targets.add(target.toString());
217: }
218: } else {
219:
220: if (!isParamValid(newName) && !isParamValid(newURL)) {
221: if (isAction(request, ADD_RESOURCE)) {
222: response.setRenderParameter(
223: RENDER_PARAM_ERROR, EMPTY_NAME_URL);
224: errorFlag = true;
225: }
226: } else if (!isParamValid(newName)) {
227: response.setRenderParameter(RENDER_PARAM_ERROR,
228: EMPTY_NAME + newURL);
229: errorFlag = true;
230: } else {
231: response.setRenderParameter(RENDER_PARAM_ERROR,
232: EMPTY_URL + newName);
233: errorFlag = true;
234: }
235: }
236:
237: if (errorFlag) {
238:
239: // do necessary settings and return
240: response.setPortletMode(PortletMode.EDIT);
241: response.setRenderParameter(RESOURCE_NAME, newName);
242: response.setRenderParameter(RESOURCE_URL, newURL);
243: } else {
244: if (isAction(request, FINISHED_EDIT)) {
245: response.setPortletMode(PortletMode.VIEW);
246: }
247:
248: PortletPreferences pref = request.getPreferences();
249: //
250: // put the targets
251: //
252: String[] targetElements = new String[targets.size()];
253: pref.setValues(TARGETS, (String[]) targets
254: .toArray(targetElements));
255: //Window Preference
256: String storedWindowPref = pref.getValue(
257: WINDOW_PREF, "");
258: String windowPref = request
259: .getParameter(WINDOW_PREF);
260:
261: if ((windowPref != null)
262: && (!windowPref.equals(storedWindowPref))) {
263: pref.setValue(WINDOW_PREF, windowPref);
264: }
265:
266: // storing the preferences
267: try {
268: pref.store();
269: } catch (ValidatorException ve) {
270: throw new PortletException(
271: "BookmarkPortlet.processAction", ve);
272: } catch (IOException ioe) {
273: throw new PortletException(
274: "BookmarkPortlet.processAction", ioe);
275: }
276: }
277: }
278: }
279: }
280:
281: private boolean isAction(ActionRequest request, String action) {
282:
283: return (request.getParameter(action) != null);
284: }
285:
286: private boolean isParamValid(String paramValue) {
287: return ((paramValue != null) && (!paramValue.trim().equals("")));
288: }
289:
290: protected String getContentJSP(RenderRequest request)
291: throws PortletException {
292:
293: PortletPreferences pref = request.getPreferences();
294: String contentPage = pref.getValue(BOOKMARK_CONTENT_PAGE, "");
295: return getLocalizedJSP(request.getLocale(), contentPage);
296: }
297:
298: protected String getEditJSP(RenderRequest request)
299: throws PortletException {
300:
301: PortletPreferences pref = request.getPreferences();
302: String editPage = pref.getValue(BOOKMARK_EDIT_PAGE, "");
303: return getLocalizedJSP(request.getLocale(), editPage);
304: }
305:
306: protected String getHelpJSP(RenderRequest request)
307: throws PortletException {
308:
309: PortletPreferences pref = request.getPreferences();
310: String helpPage = pref.getValue(BOOKMARK_HELP_PAGE, "");
311: return getLocalizedJSP(request.getLocale(), helpPage);
312: }
313: }
|