001: package com.ecyrd.jspwiki.plugin;
002:
003: import com.ecyrd.jspwiki.*;
004: import java.util.*;
005: import java.text.SimpleDateFormat;
006: import java.io.*;
007: import java.net.*;
008:
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011: import javax.servlet.ServletContext;
012: import javax.servlet.RequestDispatcher;
013:
014: public class PortletThread extends Thread {
015: private HttpServletRequest hreq;
016: private HttpServletResponse hres;
017: private String queryString;
018: private String portletName;
019: private String content;
020: private String url;
021: private Exception exception;
022: private String portalURI;
023:
024: /*
025: public void prepare(HttpServletRequest req, HttpServletResponse res, String qs) {
026: this.hreq = req;
027: this.hres = res;
028: this.portletName = portletName;
029: this.url = url;
030: this.queryString = qs;
031: }
032: */
033:
034: public void prepare(HttpServletRequest req,
035: HttpServletResponse res, String portletName,
036: String portalURI) {
037: this .hreq = req;
038: this .hres = res;
039: this .portletName = portletName;
040: this .portalURI = portalURI;
041: }
042:
043: public String getContent() throws Exception {
044: if (exception != null)
045: throw exception;
046: return content.toString();
047: }
048:
049: public void run() {
050: try {
051: //content = getPortletContentByURL();
052: content = getPortletContentByDispatch();
053: } catch (Exception e) {
054: System.out.println(e); // XXX
055: e.printStackTrace();
056: exception = e;
057: }
058: }
059:
060: private String getPortletContentByURL() throws Exception {
061: String url = ".." + portalURI + "/dt?provider=" + portletName;
062: URLConnection uc = new URL(url).openConnection();
063: uc.setAllowUserInteraction(false);
064: uc.setUseCaches(false);
065: uc.setDoOutput(false);
066: uc.setDoInput(true);
067: uc.connect();
068: InputStreamReader is = new InputStreamReader(uc
069: .getInputStream(), "utf-8"); // XXX
070: StringBuffer sb = new StringBuffer();
071: int c;
072: while ((c = is.read()) != -1) {
073: sb.append((char) c);
074: }
075: is.close();
076: return sb.toString();
077: }
078:
079: private String getPortletContentByDispatch() throws Exception {
080:
081: // new req/res objects for dispatching to desktop
082: LocalParamHttpServletRequestWrapper req = new LocalParamHttpServletRequestWrapper(
083: hreq);
084: BufferedHttpServletResponseWrapper res = new BufferedHttpServletResponseWrapper(
085: hres);
086:
087: if (portletName != null) {
088: req.setQueryString("provider=" + portletName
089: + "&last=false");
090: } else if (queryString != null) {
091: req.setQueryString(queryString);
092: }
093:
094: // XXX shouldn't need a session
095: ServletContext sc = req.getSession(true).getServletContext();
096: RequestDispatcher rd = sc.getContext(portalURI)
097: .getRequestDispatcher("/dt");
098: rd.include(req, res);
099:
100: return res.getStringContent();
101: }
102:
103: }
|