001: package com.ecyrd.jspwiki.plugin;
002:
003: import org.apache.log4j.Logger;
004: import com.ecyrd.jspwiki.*;
005: import java.util.*;
006: import java.text.SimpleDateFormat;
007: import java.io.*;
008: import java.net.*;
009:
010: import com.sun.portal.app.wiki.WikiPortletContext;
011: import javax.portlet.RenderRequest;
012:
013: import javax.servlet.*;
014: import javax.servlet.http.HttpServletRequest;
015:
016: public class Portlet implements WikiPlugin {
017: private static Logger log = Logger
018: .getLogger(CurrentTimePlugin.class);
019:
020: public String execute(WikiContext context, Map params)
021: throws PluginException {
022: try {
023: String portlet = (String) params.get("name"); // can be any channel
024: String wraparg = (String) params.get("wrap");
025: String widtharg = (String) params.get("width");
026:
027: //return getPortletContentByURL(context, portlet);
028: //return getPortletContentByDispatch(context, portlet);
029:
030: // find the portal server URI
031: WikiEngine wiki = context.getEngine();
032: String portalURI = wiki.getWikiProperties().getProperty(
033: "jspwiki.plugin.portlet.portaluri");
034: if (portalURI == null)
035: portalURI = "/portal"; // not much point in this...
036:
037: String portletContent;
038: /*
039: // Need to use a new thread because desktop is not reentrant (threadlocals aka global variables...)
040: PortletThread pthread = new PortletThread(); // XXX need thread pool here
041: pthread.prepare(context.getHttpRequest(), context.getHttpResponse(), portlet, portalURI);
042: //pthread.prepare((HttpServletRequest)WikiPortletContext.get("httpServletRequest"), context.getHttpResponse(), portlet, portalURI);
043: pthread.start();
044: pthread.join();
045: portletContent = pthread.getContent();
046:
047: */
048: portletContent = "<channel name='" + portlet + "'>";
049:
050: boolean wrap = "true".equalsIgnoreCase(wraparg);
051: String htmlwidth = "";
052: if (widtharg != null)
053: htmlwidth = " width=\"" + widtharg + "\"";
054:
055: if (wrap) {
056: // XXX Need to sort this out - use provider/container?
057: // not being used for now (original spec said we should not have this)
058: // obviously this can't remain hard-coded if we actually use it
059: return "<table border=\"1\"" + htmlwidth
060: + "><tr><td class=\"portlet\">"
061: + portletContent + "</td></tr></table>";
062: } else
063: return portletContent;
064: } catch (IllegalArgumentException e) {
065: e.printStackTrace(); // XXX
066: throw new PluginException("Illegal Argument: "
067: + e.getMessage());
068: } catch (Exception e) {
069: e.printStackTrace(); // XXX
070: throw new PluginException("Plugin Exception: "
071: + e.getMessage());
072: }
073: }
074:
075: private String getPortletContentByURL(WikiContext context,
076: String name) throws Exception {
077: String url = "../portal/dt?provider=" + name; // XXX (not used currently)
078: URLConnection uc = new URL(url).openConnection();
079: uc.setAllowUserInteraction(false);
080: uc.setUseCaches(false);
081: uc.setDoOutput(false);
082: uc.setDoInput(true);
083: uc.connect();
084: InputStreamReader is = new InputStreamReader(uc
085: .getInputStream(), "utf-8"); // XXX
086: StringBuffer sb = new StringBuffer();
087: int c;
088: while ((c = is.read()) != -1) {
089: sb.append((char) c);
090: }
091: is.close();
092: return sb.toString();
093: }
094:
095: private String getPortletContentByDispatch(WikiContext context,
096: String name) throws Exception {
097:
098: // new req/res objects for dispatching to desktop
099: LocalParamHttpServletRequestWrapper req = new LocalParamHttpServletRequestWrapper(
100: context.getHttpRequest());
101: BufferedHttpServletResponseWrapper res = new BufferedHttpServletResponseWrapper(
102: context.getHttpResponse());
103:
104: req.getParameterMap().put("provider", name);
105: req.getParameterMap().put("last", "false");
106: req.setQueryString("provider=" + name + "&last=false");
107:
108: ServletContext sc = context.getEngine().getServletContext();
109: RequestDispatcher rd = sc.getContext("/portal")
110: .getRequestDispatcher("/dt"); // XXX (not currently used)
111: rd.include(req, res);
112:
113: return res.getStringContent();
114: }
115:
116: }
|