1 16 package org.apache.jetspeed.portal.portlets; 17 18 import org.apache.ecs.ConcreteElement; 19 import org.apache.ecs.StringElement; 20 import org.apache.jetspeed.portal.portlets.AbstractPortlet; 21 import org.apache.turbine.util.RunData; 22 23 import java.io.BufferedInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 29 30 49 50 51 public class ServletProxyPortlet extends AbstractPortlet 52 { 53 private final static int BUFFER_SIZE = 2048; 54 55 56 public static final String URL_PARAMETER_NAME = "URL"; 57 58 public static final String PROTOCOL_PARAMETER_NAME = "protocol"; 59 62 public static final String SESSION_TOKEN_PARAMETER_NAME = "session_token"; 63 64 65 public static final String DEFAULT_PROTOCOL = "http"; 66 67 public static final String DEFAULT_SESSION_TOKEN = "jsessionid"; 68 69 74 public ConcreteElement getContent(RunData rundata) 75 { 76 String servletURL = processURL(rundata); 77 if(servletURL == null) 78 { 79 return new StringElement("ServletInvokerPortlet: Must specify a URL using the URL parameter"); 80 } 81 String content; 82 83 try 85 { 86 87 URL url = new URL (servletURL); 88 URLConnection connection = url.openConnection(); 89 InputStream stream = connection.getInputStream(); 90 BufferedInputStream in = new BufferedInputStream (stream); 91 int length = 0; 92 byte[] buf = new byte[BUFFER_SIZE]; 93 ByteArrayOutputStream out = new ByteArrayOutputStream (); 94 while ((in != null) && ((length = in.read(buf)) != -1)) 95 { 96 out.write(buf, 0, length); 98 } 99 content = out.toString(); 100 return new StringElement(content); 101 } 102 catch (Exception e) 103 { 104 String message = "ServletInvokerPortlet: Error invoking " + servletURL + ": " + e.getMessage(); 105 return new StringElement(message); 106 } 107 108 } 109 110 115 protected String processURL(RunData rundata) 116 { 117 String servletURL = getPortletConfig().getInitParameter(URL_PARAMETER_NAME); 118 if( servletURL == null) { 120 return null; 121 } 122 String protocol = getPortletConfig().getInitParameter(PROTOCOL_PARAMETER_NAME); 123 if(protocol == null) 124 { 125 protocol = DEFAULT_PROTOCOL; 126 } 127 String token = getPortletConfig().getInitParameter(SESSION_TOKEN_PARAMETER_NAME); 128 if(token == null) 129 { 130 token = DEFAULT_SESSION_TOKEN; 131 } 132 133 134 String queryString = new String (); 135 int queryIndex = servletURL.indexOf("?"); 136 if(queryIndex > 0) 137 { 138 queryString = servletURL.substring(queryIndex); 139 servletURL = servletURL.substring(0, queryIndex); 140 } 141 servletURL = protocol + "://" + rundata.getServerName() + ":" + rundata.getServerPort() + rundata.getContextPath() + servletURL + ";" + token + "=" + rundata.getSession().getId() + queryString; 142 return servletURL; 143 } 144 145 } 146 | Popular Tags |