001: package com.sun.portal.app.wiki;
002:
003: //import com.ecyrd.jspwiki.plugin.PortletThread;
004:
005: import javax.portlet.*;
006:
007: import javax.servlet.*;
008: import javax.servlet.http.*;
009:
010: import java.util.*;
011: import java.io.*;
012:
013: // XXX Need to ditch JspPortlet and switch to res bundles
014: public class WikiPortlet extends JspPortlet {
015:
016: // edit form literals
017: public static String EDIT_FINISHED = "finished";
018: public static String EDIT_CANCEL = "cancel";
019: public static String PREF_PREFIX = "pref."; // XXX because some arg names are swallowed by the desktop
020:
021: private PortletContext pContext;
022:
023: public void init(PortletConfig config) throws PortletException {
024: super .init(config);
025: pContext = config.getPortletContext();
026: }
027:
028: public void doView(RenderRequest request, RenderResponse response)
029: throws PortletException, IOException {
030:
031: response.setContentType(request.getResponseContentType());
032:
033: try {
034:
035: PortletPreferences prefs = request.getPreferences();
036:
037: PortletSession ps = request.getPortletSession(true);
038:
039: if ("true".equals(ps.getAttribute("wiki.clearstate"))) {
040: // an action has been performed - clear all sticky page state
041: ps.setAttribute("wiki.redirect", null);
042: ps.setAttribute("wiki.clearstate", null);
043: }
044:
045: String dispatchURL;
046:
047: String contextPath = request.getContextPath();
048: String redirect = (String) ps.getAttribute("wiki.redirect");
049: if (redirect != null) {
050: // redirects from the webapp are treated as sticky
051: if (redirect.indexOf(contextPath) != -1) {
052: // strip the servlet context prefix for dispatch
053: redirect = redirect.substring(redirect
054: .indexOf(contextPath)
055: + contextPath.length());
056: }
057: dispatchURL = redirect;
058: } else {
059: // no redirect - use the current navigation args
060: String page = request.getParameter("page");
061: if (page == null)
062: page = request.getParameter("wikipage"); // wikipage deprecated in 71u1
063: if (page == null)
064: page = prefs.getValue("wikiPage", "Main");
065: String wikijsp = request.getParameter("wikijsp");
066: if (wikijsp == null)
067: wikijsp = "Wiki.jsp";
068: dispatchURL = "/" + wikijsp + "?page=" + page;
069: }
070:
071: // set up the wiki portlet context which makes
072: // porlet and prefs info available to the JSPWiki webapp
073: WikiPortletContext.init(request, response);
074:
075: // hopefully we can simplify this and just add the prefs map to the wikicontext
076: WikiPortletContext.put("wikiTemplate", prefs.getValue(
077: "wikiTemplate", null));
078: WikiPortletContext.put("editorType", prefs.getValue(
079: "editorType", null));
080: WikiPortletContext.put("editorOptions", prefs.getValues(
081: "editorOptions", null));
082: WikiPortletContext.put("availablePortlets", prefs
083: .getValues("availablePortlets", null));
084:
085: // dispatch to jspwiki
086: PortletRequestDispatcher dispatcher = pContext
087: .getRequestDispatcher(dispatchURL);
088: dispatcher = pContext.getRequestDispatcher(dispatchURL);
089: dispatcher.include(request, response);
090:
091: // check if there is a new redirect fomr jspwiki
092: String newredirect = (String) WikiPortletContext
093: .get("wiki.redirect");
094: if (newredirect != null) {
095: if (newredirect.indexOf(contextPath) != -1) {
096: // strip the servlet context prefix for dispatch
097: newredirect = newredirect.substring(newredirect
098: .indexOf(contextPath)
099: + contextPath.length());
100: if (newredirect.indexOf("%3A") != -1) {
101: // due to bug in portlet request dispatcher not decoding its args
102: newredirect = newredirect
103: .replaceAll("%3A", ":");
104: }
105: }
106: dispatchURL = newredirect;
107:
108: // make the redirect sticky
109: // XXX there should be a way to reset render args...
110: ps.setAttribute("wiki.redirect", dispatchURL);
111:
112: dispatcher = pContext.getRequestDispatcher(dispatchURL);
113: dispatcher.include(request, response);
114:
115: }
116:
117: WikiPortletContext.cleanup();
118: } catch (Exception e) {
119: e.printStackTrace();
120: throw new PortletException(e);
121: }
122:
123: }
124:
125: public void processAction(ActionRequest request,
126: ActionResponse response) throws PortletException,
127: java.io.IOException {
128:
129: try {
130:
131: // There are two types of action...
132:
133: // ... 1) saving preferences from the edit page
134: if (request.getPortletMode().equals(PortletMode.EDIT)) {
135: savePreferences(request, response);
136: response.setPortletMode(PortletMode.VIEW);
137: return;
138: }
139:
140: // ... 2) a portlet action url
141: response.setRenderParameters(request.getParameterMap());
142: // tell render to clear its state
143: PortletSession ps = request.getPortletSession(true);
144: ps.setAttribute("wiki.clearstate", "true");
145: ps.setAttribute("wiki.redirect", null);
146: } catch (Exception e) {
147: System.out.println(e); // XXX
148: throw new PortletException(e);
149: }
150:
151: }
152:
153: void savePreferences(PortletRequest request,
154: PortletResponse response) throws PortletException {
155:
156: try {
157:
158: String finished = request.getParameter(EDIT_FINISHED);
159: String cancel = request.getParameter(EDIT_CANCEL);
160:
161: if (cancel != null) {
162: // user cancelled - do nothing
163: return;
164: } else if (finished != null) {
165:
166: PortletPreferences prefs = request.getPreferences();
167: boolean needstore = false;
168: // check if any prefs have changed
169: for (Enumeration e = request.getParameterNames(); e
170: .hasMoreElements();) {
171: String paramname = (String) e.nextElement();
172: if (!paramname.startsWith(PREF_PREFIX))
173: continue;
174: String name = paramname.substring(PREF_PREFIX
175: .length());
176: String[] newvalues = request
177: .getParameterValues(paramname);
178: String[] oldvalues = prefs.getValues(name,
179: new String[] {});
180: String[] newPrefs = compareValues(newvalues,
181: oldvalues);
182: if (newPrefs != null) {
183: prefs.setValues(name, newPrefs);
184: needstore = true;
185: }
186: }
187: if (needstore) {
188: // prefs have changed - save them
189: prefs.store();
190: }
191: return;
192: } else {
193: // shouldn't happen
194: return;
195: }
196: } catch (Exception e) {
197: System.out.println("exception: " + e); // XXX
198: throw new PortletException(e);
199: }
200: }
201:
202: // returns a new String[] if prefs changed, or null if no change
203: private String[] compareValues(String[] n, String[] o) {
204:
205: // remove holes from n
206: List vl = new ArrayList();
207: for (int i = 0; i < n.length; ++i) {
208: if (n[i] != null && !n[i].equals("")) {
209: vl.add(n[i]);
210: }
211: }
212: n = (String[]) vl.toArray(new String[0]);
213:
214: // remove holes from o
215: vl = new ArrayList();
216: for (int i = 0; i < o.length; ++i) {
217: if (o[i] != null && !o[i].equals("")) {
218: vl.add(o[i]);
219: }
220: }
221: o = (String[]) vl.toArray(new String[0]);
222:
223: if (n.length != o.length) {
224: return n;
225: }
226:
227: for (int i = 0; i < n.length; ++i) {
228: if (!n[i].equals(o[i])) {
229: return n;
230: }
231: }
232: return null;
233: }
234:
235: }
|