1 16 17 package org.apache.jetspeed.portal.portlets; 18 19 import org.apache.jetspeed.util.JetspeedClearElement; 21 import org.apache.ecs.ConcreteElement; 22 23 import org.apache.jetspeed.portal.PortletConfig; 25 import org.apache.jetspeed.portal.PortletException; 26 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 27 import org.apache.jetspeed.services.logging.JetspeedLogger; 28 import org.apache.jetspeed.util.HTMLRewriter; 29 import org.apache.jetspeed.util.Base64; 30 31 import org.apache.turbine.util.RunData; 33 34 import java.io.InputStreamReader ; 36 import java.io.IOException ; 37 import java.io.Reader ; 38 import java.net.URL ; 39 import java.net.URLConnection ; 40 import java.util.StringTokenizer ; 41 42 51 public class WebPagePortlet extends AbstractInstancePortlet 52 { 53 56 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(WebPagePortlet.class.getName()); 57 58 protected HTMLRewriter rewriter = null; 59 protected boolean initDone = false; 60 protected boolean contentStale = true; 61 protected boolean cacheContent = false; 62 protected String username = null; 63 protected String password = null; 64 65 69 public void init() throws PortletException { 70 71 if (initDone) return; 73 74 PortletConfig config = this.getPortletConfig(); 75 76 try 77 { 78 rewriter = new HTMLRewriter( 80 ! config.getInitParameter("dont_remove_script","no") 81 .equalsIgnoreCase("yes"), 82 ! config.getInitParameter("dont_remove_style","no") 83 .equalsIgnoreCase("yes"), 84 ! config.getInitParameter("dont_remove_noscript","no") 85 .equalsIgnoreCase("yes"), 86 ! config.getInitParameter("dont_remove_meta","no") 87 .equalsIgnoreCase("yes"), 88 ! config.getInitParameter("dont_remove_applet","no") 89 .equalsIgnoreCase("yes"), 90 ! config.getInitParameter("dont_remove_object","no") 91 .equalsIgnoreCase("yes"), 92 ! config.getInitParameter("dont_remove_head","no") 93 .equalsIgnoreCase("yes"), 94 ! config.getInitParameter("dont_remove_onsomething","no") 95 .equalsIgnoreCase("yes"), 96 config.getInitParameter("open_in_popup","no") 97 .equalsIgnoreCase("yes") 98 ); 99 100 username = config.getInitParameter("username"); 102 password = config.getInitParameter("password"); 103 104 contentStale = true; 105 initDone = true; 106 } catch (Exception e) { 107 logger.info("Exception occurred:" + e.toString()); 108 e.printStackTrace(); 109 throw new PortletException( e.toString() ); 110 } 111 } 112 113 117 protected Reader getReader(String url) throws IOException 120 { 121 URL pageUrl = new URL (url); 122 123 URLConnection pageConn = pageUrl.openConnection(); 124 try 125 { 126 if (username != null && password !=null) 128 { 129 pageConn.setRequestProperty("Authorization", "Basic " + 130 Base64.encodeAsString(username + ":" + password)); 131 } 132 133 } 134 catch (Exception e) 135 { 136 logger.info("Exception occurred:" + e.toString(), e); 137 } 138 139 long pageExpiration = pageConn.getExpiration(); 140 String encoding = "iso-8859-1"; 141 String contentType = pageConn.getContentType(); 142 String tempString = null; 143 String noCache = "no-cache"; 144 145 if (contentType != null) 146 { 147 StringTokenizer st = new StringTokenizer (contentType, "; ="); 148 while (st.hasMoreTokens()) 149 { 150 if (st.nextToken().equalsIgnoreCase("charset")) 151 { 152 try 153 { 154 encoding = st.nextToken(); 155 break; 156 } 157 catch (Exception e) 158 { 159 break; 160 } 161 } 162 } 163 } 164 165 168 cacheContent = true; if (pageExpiration == 0) { 170 cacheContent = false; 171 } 172 tempString = pageConn.getHeaderField( "Cache-Control"); 174 if (tempString != null) { 175 if (tempString.toLowerCase().indexOf(noCache) >= 0) { 176 cacheContent = false; 177 } 178 } 179 tempString = pageConn.getHeaderField( "Pragma"); 181 if (tempString != null) { 182 if (tempString.toLowerCase().indexOf(noCache) >= 0) { 183 cacheContent = false; 184 } 185 } 186 187 Reader rdr = new InputStreamReader (pageConn.getInputStream(), 189 encoding ); 190 191 if (pageExpiration > System.currentTimeMillis() && (cacheContent == true)) 193 { 194 contentStale = false; 195 logger.debug( "WebPagePortlet caching URL: " + 196 url + 197 " Expiration: " + 198 pageExpiration + 199 ", " + 200 (pageExpiration - System.currentTimeMillis() ) + 201 " milliseconds into the future" ); 202 setExpirationMillis(pageExpiration); 203 } else { 204 contentStale = true; 205 } 206 207 return rdr; 208 } 209 210 211 218 public ConcreteElement getContent( RunData data ) 219 { 220 PortletConfig config = this.getPortletConfig(); 221 222 if (contentStale == true) 223 return getWebPageContent(data, config); 224 225 if (null == getExpirationMillis()) 226 return getContent( data, null, true); 227 228 if (getExpirationMillis().longValue() <= System.currentTimeMillis()) 229 return getWebPageContent(data, config); 230 231 return getContent( data, null , true ); 232 } 233 234 private ConcreteElement getWebPageContent( RunData data, PortletConfig config ) 235 { 236 237 String convertedString = null; JetspeedClearElement element = null; 239 240 String url = selectUrl( data, config ); 241 242 try 243 { 244 Reader htmlReader = getReader( url ); 245 convertedString = rewriter.convertURLs(htmlReader, url); 247 element = new JetspeedClearElement(convertedString); 248 249 this.clearContent(); this.setContent(element); 252 253 htmlReader.close(); 254 255 } catch (Exception e) { 256 logger.info("Exception occurred:" + e.toString(), e); 257 } 258 259 return element; 260 } 261 262 268 public void refresh() { 269 if (cacheContent == true) { 270 getWebPageContent(null, this.getPortletConfig()); 271 } 272 } 273 274 278 protected String selectUrl( RunData data, PortletConfig config ) 279 { 280 String url = config.getURL(); 281 282 return url; 283 284 } 286 } 287 | Popular Tags |