001: package com.ecyrd.jspwiki.url;
002:
003: import com.sun.portal.app.wiki.WikiPortletContext;
004:
005: import java.util.Properties;
006: import java.io.UnsupportedEncodingException;
007: import javax.servlet.http.HttpServletRequest;
008:
009: import javax.portlet.*;
010: import java.net.URLDecoder;
011: import java.util.regex.*;
012:
013: import com.ecyrd.jspwiki.InternalWikiException;
014: import com.ecyrd.jspwiki.TextUtil;
015: import com.ecyrd.jspwiki.WikiContext;
016: import com.ecyrd.jspwiki.WikiEngine;
017: import com.ecyrd.jspwiki.url.*;
018:
019: import com.sun.portal.community.CommunityId;
020: import com.sun.portal.community.urlmanager.CommunityURLManager;
021: import com.sun.portal.community.urlmanager.CommunityURLManagerFactory;
022:
023: import java.util.StringTokenizer;
024: import org.apache.log4j.Logger;
025:
026: public class PortalURLConstructor extends DefaultURLConstructor {
027:
028: static private Pattern channelPattern = Pattern
029: .compile(".*jdo__([^_]*)_Container.*");
030:
031: private static final Logger log = Logger
032: .getLogger(PortalURLConstructor.class);
033:
034: //
035: // modify the portlet url to switch the community if necessary
036: // XXX would prefer to use a single wiki container, but this'll do for now
037: //
038: static String switchCommunity(String pagename, PortletURL url) {
039: String result = null;
040: // we skip all of this if not currently in a community context XXX portlet pref?
041: // thus, community portlets will not show in dev sample, but the page content will
042: Matcher m = channelPattern.matcher(url.toString());
043: if (!m.matches())
044: return url.toString();
045: String currentcty = m.group(1);
046:
047: // the community name is the same as the wiki name
048: String newcty = WikiEngine.getWikiName(pagename); // could be null or ""
049:
050: /* XXX this code has extra redirect to pivk up visible args from
051: * remote portlet (eg, menu container tab) - not using right now,
052: * simply hardcoding menu tab instead
053: *
054: // Are we switching to a different wiki/community? (display global pages in current cty)
055: if ( newcty != null && newcty.length() > 0 && !newcty.equalsIgnoreCase(currentcty) )
056: {
057: // Switch the user, portlet, etc, to the new community
058: try
059: {
060: // set up the cty url args
061: String newctyid = "jdo__"+newcty;
062: String ctyurl = CommunityURLManagerFactory.getInstance().getCommunityURLManager().getCommunityURL(WikiContext.getHttpRequestForThread(), new CommunityId(newctyid));
063: String ctyargs = ctyurl.substring(ctyurl.indexOf("?") + 1);
064: /*for (StringTokenizer st = new StringTokenizer(ctyargs, "&"); st.hasMoreTokens(); ) {
065: String argval = st.nextToken();
066: int edx = argval.indexOf("=");
067: if(edx != -1) {
068: String arg = URLDecoder.decode(argval.substring(0, edx), "UTF-8");
069: String val = URLDecoder.decode(argval.substring(edx + 1), "UTF-8");
070: if (!arg.startsWith("dt."))
071: arg = "dt." + arg;
072: url.setParameter(arg, val);
073: }
074: }*/
075: /*
076: // add the cty parent container args
077: url.setParameter("dt.newargs", "dt.CommunityParentContainer.selectedChannel=" + newctyid + "_Container");
078: //url.setParameter("_makevisible_", "1");
079:
080: // switch the portlet url container names, etc, to the new cty
081: String targeturl = url.toString();
082: if (currentcty != null && currentcty.length() > 0) {
083: String currentctyid = "jdo__"+currentcty;
084: targeturl = targeturl.replaceAll(currentctyid, newctyid);
085: }
086:
087: PortletURL nextURL = WikiPortletContext.getRenderResponse().createActionURL();
088: for (StringTokenizer st = new StringTokenizer(ctyargs, "&"); st.hasMoreTokens(); ) {
089: String argval = st.nextToken();
090: int edx = argval.indexOf("=");
091: if(edx != -1) {
092: String arg = URLDecoder.decode(argval.substring(0, edx), "UTF-8");
093: String val = URLDecoder.decode(argval.substring(edx + 1), "UTF-8");
094: if (!arg.startsWith("dt."))
095: arg = "dt." + arg;
096: nextURL.setParameter(arg, val);
097: }
098: }
099: nextURL.setParameter("_makevisible_", "1");
100: nextURL.setParameter("_redirect_", targeturl);
101:
102: String nexturl = nextURL.toString();
103: if (currentcty != null && currentcty.length() > 0) {
104: String currentctyid = "jdo__"+currentcty;
105: nexturl = nexturl.replaceAll(currentctyid, newctyid);
106: }
107:
108: // first we come back to ourself and check the membership,
109: // if not member: redirect to info portlet and request to join
110: // if member: redirect to the target wiki in the target community
111: PortletURL selfURL = WikiPortletContext.getRenderResponse().createActionURL();
112: selfURL.setParameter("_checkmembership_", "1");
113: selfURL.setParameter("_redirect_", nexturl);
114:
115: result = selfURL.toString();
116: }
117: catch(Exception e) {
118: log.error("Error in url construction", e);
119: }
120: */
121:
122: // Are we switching to a different wiki/community? (display global pages in current cty)
123: if (newcty != null && newcty.length() > 0
124: && !newcty.equalsIgnoreCase(currentcty)) {
125: // Switch the user, portlet, etc, to the new community
126: try {
127: // set up the cty url args
128: String newctyid = "jdo__" + newcty;
129: /*String ctyurl = CommunityURLManagerFactory.getInstance().getCommunityURLManager().getCommunityURL(WikiContext.getHttpRequestForThread(), new CommunityId(newctyid));
130: String ctyargs = ctyurl.substring(ctyurl.indexOf("?") + 1);
131: for (StringTokenizer st = new StringTokenizer(ctyargs, "&"); st.hasMoreTokens(); ) {
132: String argval = st.nextToken();
133: int edx = argval.indexOf("=");
134: if(edx != -1) {
135: String arg = URLDecoder.decode(argval.substring(0, edx), "UTF-8");
136: String val = URLDecoder.decode(argval.substring(edx + 1), "UTF-8");
137: if (!arg.startsWith("dt."))
138: arg = "dt." + arg;
139: url.setParameter(arg, val);
140: }
141: }*/
142: // hard code this to avoid unnecessary api calls (IS??) and url args (XXX still need to check membership)
143: // switch the display profile to new cty
144: url.setParameter("dt.community", newctyid);
145:
146: // switch the desktop view
147: // add the cty parent container args and switch the menu tab (even if it's not a menu... XXX)
148: url.setParameter("dt.newargs",
149: "dt.CommunityParentContainer.selectedChannel="
150: + newctyid + "_Container" + "&"
151: + newctyid + "_Container.setSelected="
152: + newctyid + "_Container/Wiki");
153:
154: // switch the portlet url container names, etc, to the new cty
155: String targeturl = url.toString();
156: if (currentcty != null && currentcty.length() > 0) {
157: String currentctyid = "jdo__" + currentcty;
158: targeturl = targeturl.replaceAll(currentctyid,
159: newctyid);
160: }
161:
162: /* XXX not checking membership for now
163: // first we come back to ourself and check the membership,
164: // if not member: redirect to info portlet and request to join
165: // if member: redirect to the target wiki in the target community
166: PortletURL selfURL = WikiPortletContext.getRenderResponse().createActionURL();
167: selfURL.setParameter("_checkmembership_", "1");
168: selfURL.setParameter("_redirect_", targeturl);
169:
170: result = selfURL.toString();
171: */
172:
173: result = targeturl;
174:
175: } catch (Exception e) {
176: log.error("Error in url construction", e);
177: }
178:
179: } else
180: result = url.toString();
181: return result;
182: }
183:
184: /**
185: * Returns the pattern used for each URL style.
186: *
187: * @param context
188: * @param name
189: * @return A pattern for replacement.
190: */
191: public static String getURLPattern(String context, String name) {
192: if (context.equals(WikiContext.VIEW)) {
193: if (WikiPortletContext.isActive()) {
194: PortletURL url = WikiPortletContext.getRenderResponse()
195: .createActionURL();
196: try {
197: url.setPortletMode(PortletMode.VIEW);
198: } catch (PortletModeException e) {
199: System.out.println(e); // XXX
200: e.printStackTrace();
201: }
202: url.setParameter("page", name);
203: url.setParameter("wikijsp", "Wiki.jsp");
204: return switchCommunity(name, url);
205: } else {
206: if (name == null)
207: return "%uWiki.jsp"; // FIXME
208: else
209: return "%uWiki.jsp?page=%n";
210: }
211: } else if (context.equals(WikiContext.EDIT)) {
212: if (WikiPortletContext.isActive()) {
213: PortletURL url = WikiPortletContext.getRenderResponse()
214: .createActionURL();
215: try {
216: url.setPortletMode(PortletMode.VIEW);
217: } catch (PortletModeException e) {
218: System.out.println(e); // XXX
219: e.printStackTrace();
220: }
221: url.setParameter("page", name);
222: url.setParameter("wikijsp", "Edit.jsp");
223: return switchCommunity(name, url);
224: } else
225: return "%uEdit.jsp?page=%n";
226: } else if (context.equals(WikiContext.ATTACH)) {
227: return "%uattach/%n";
228: } else if (context.equals(WikiContext.INFO)) {
229: if (WikiPortletContext.isActive()) {
230: PortletURL url = WikiPortletContext.getRenderResponse()
231: .createActionURL();
232: try {
233: url.setPortletMode(PortletMode.VIEW);
234: } catch (PortletModeException e) {
235: System.out.println(e); // XXX
236: e.printStackTrace();
237: }
238: url.setParameter("page", name);
239: url.setParameter("wikijsp", "PageInfo.jsp");
240: return url.toString();
241: } else
242: return "%uPageInfo.jsp?page=%n";
243: } else if (context.equals(WikiContext.DIFF)) {
244: if (WikiPortletContext.isActive()) {
245: PortletURL url = WikiPortletContext.getRenderResponse()
246: .createActionURL();
247: try {
248: url.setPortletMode(PortletMode.VIEW);
249: } catch (PortletModeException e) {
250: System.out.println(e); // XXX
251: e.printStackTrace();
252: }
253: url.setParameter("page", name);
254: url.setParameter("wikijsp", "Diff.jsp");
255: return url.toString();
256: } else
257: return "%uDiff.jsp?page=%n";
258: } else if (context.equals(WikiContext.NONE)) {
259: return "%u/%n";
260: } else if (context.equals(WikiContext.UPLOAD)) {
261: if (WikiPortletContext.isActive()) {
262: PortletURL url = WikiPortletContext.getRenderResponse()
263: .createActionURL();
264: try {
265: url.setPortletMode(PortletMode.VIEW);
266: } catch (PortletModeException e) {
267: System.out.println(e); // XXX
268: e.printStackTrace();
269: }
270: url.setParameter("page", name);
271: url.setParameter("wikijsp", "Upload.jsp");
272: return url.toString();
273: } else
274: return "%uUpload.jsp?page=%n";
275: } else if (context.equals(WikiContext.COMMENT)) {
276: if (WikiPortletContext.isActive()) {
277: PortletURL url = WikiPortletContext.getRenderResponse()
278: .createActionURL();
279: try {
280: url.setPortletMode(PortletMode.VIEW);
281: } catch (PortletModeException e) {
282: System.out.println(e); // XXX
283: e.printStackTrace();
284: }
285: url.setParameter("page", name);
286: url.setParameter("wikijsp", "Comment.jsp");
287: return url.toString();
288: } else
289: return "%uComment.jsp?page=%n";
290: } else if (context.equals(WikiContext.ERROR)) {
291: if (WikiPortletContext.isActive()) {
292: PortletURL url = WikiPortletContext.getRenderResponse()
293: .createActionURL();
294: try {
295: url.setPortletMode(PortletMode.VIEW);
296: } catch (PortletModeException e) {
297: System.out.println(e); // XXX
298: e.printStackTrace();
299: }
300: url.setParameter("wikijsp", "Error.jsp");
301: return url.toString();
302: } else
303: return "%uError.jsp";
304: }
305: throw new InternalWikiException(
306: "Requested unsupported context " + context);
307: }
308:
309: /**
310: * Constructs the actual URL based on the context.
311: */
312: private String makeURL(String context, String name, boolean absolute) {
313: if (context.equals(WikiContext.VIEW)) {
314: if (name == null)
315: return makeURL("%uWiki.jsp", "", absolute); // FIXME
316: }
317:
318: return doReplacement(getURLPattern(context, name), name,
319: absolute);
320: }
321:
322: /**
323: * Constructs the URL with a bunch of parameters.
324: * @param parameters If null or empty, no parameters are added.
325: */
326: public String makeURL(String context, String name,
327: boolean absolute, String parameters) {
328: if (parameters != null && parameters.length() > 0) {
329: if (context.equals(WikiContext.ATTACH)) {
330: parameters = "?" + parameters;
331: } else {
332: parameters = "&" + parameters;
333: }
334: } else {
335: parameters = "";
336: }
337: return makeURL(context, name, absolute) + parameters;
338: }
339:
340: }
|