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.rewriter.Rewriter; 29 import org.apache.jetspeed.util.rewriter.HTMLRewriter; 30 import org.apache.jetspeed.util.Base64; 31 import org.apache.jetspeed.util.template.JetspeedLink; 32 import org.apache.jetspeed.util.template.JetspeedLinkFactory; 33 34 import org.apache.turbine.util.RunData; 36 37 import java.io.InputStreamReader ; 39 import java.io.IOException ; 40 import java.io.Reader ; 41 import java.net.URL ; 42 import java.net.URLConnection ; 43 44 50 public class WebPagePortlet2 extends AbstractInstancePortlet 51 { 52 53 56 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(WebPagePortlet2.class.getName()); 57 58 protected Rewriter 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 72 if (initDone) return; 74 75 PortletConfig config = this.getPortletConfig(); 76 77 try 78 { 79 rewriter = new HTMLRewriter(); 80 81 username = config.getInitParameter("username"); 83 password = config.getInitParameter("password"); 84 85 contentStale = true; 86 initDone = true; 87 } 88 catch (Exception e) 89 { 90 logger.info("Exception occurred:" + e.toString(), e); 91 throw new PortletException( e.toString() ); 92 } 93 } 94 95 99 100 103 protected Reader getReader(String url) throws IOException 104 { 105 URL pageUrl = new URL (url); 106 107 URLConnection pageConn = pageUrl.openConnection(); 108 try 109 { 110 if (username != null && password !=null) 112 { 113 pageConn.setRequestProperty("Authorization", "Basic " + 114 Base64.encodeAsString(username + ":" + password)); 115 } 116 117 } 118 catch (Exception e) 119 { 120 logger.info("Exception occurred:" + e.toString(), e); 121 } 122 123 long pageExpiration = pageConn.getExpiration(); 124 String encoding = pageConn.getContentEncoding(); 125 String tempString = null; 126 String noCache = "no-cache"; 127 128 if(encoding == null) 129 { 130 encoding = "iso-8859-1"; 132 } 133 134 137 cacheContent = true; if (pageExpiration == 0) 139 { 140 cacheContent = false; 141 } 142 tempString = pageConn.getHeaderField( "Cache-Control"); 144 if (tempString != null) 145 { 146 if (tempString.toLowerCase().indexOf(noCache) >= 0) 147 { 148 cacheContent = false; 149 } 150 } 151 tempString = pageConn.getHeaderField( "Pragma"); 153 if (tempString != null) 154 { 155 if (tempString.toLowerCase().indexOf(noCache) >= 0) 156 { 157 cacheContent = false; 158 } 159 } 160 161 Reader rdr = new InputStreamReader (pageConn.getInputStream(), 163 encoding ); 164 165 if (pageExpiration > System.currentTimeMillis() && (cacheContent == true)) 167 { 168 contentStale = false; 169 if ( logger.isDebugEnabled() ) 170 { 171 logger.debug( "WebPagePortlet caching URL: " + 172 url + 173 " Expiration: " + 174 pageExpiration + 175 ", " + 176 (pageExpiration - System.currentTimeMillis() ) + 177 " milliseconds into the future" ); 178 } 179 setExpirationMillis(pageExpiration); 180 } 181 else 182 { 183 contentStale = true; 184 } 185 186 return rdr; 187 } 188 189 190 197 public ConcreteElement getContent( RunData data ) 198 { 199 PortletConfig config = this.getPortletConfig(); 200 201 if (contentStale == true) 202 return getWebPageContent(data, config); 203 204 if (null == getExpirationMillis()) 205 return getContent( data, null, true); 206 207 if (getExpirationMillis().longValue() <= System.currentTimeMillis()) 208 return getWebPageContent(data, config); 209 210 return getContent( data, null , true ); 211 } 212 213 private ConcreteElement getWebPageContent( RunData data, PortletConfig config ) 214 { 215 216 String convertedString = null; JetspeedClearElement element = null; 218 219 String url = selectUrl( data, config ); 220 221 try 222 { 223 JetspeedLink jsLink = JetspeedLinkFactory.getInstance(data); 224 Reader htmlReader = getReader( url ); 225 convertedString = rewriter.rewrite(htmlReader, jsLink.toString()); 226 element = new JetspeedClearElement(convertedString); 227 228 System.out.println("js link = " + jsLink.toString()); 229 JetspeedLinkFactory.putInstance(jsLink); 230 231 this.clearContent(); this.setContent(element); 234 235 htmlReader.close(); 236 } 237 catch (Exception e) 238 { 239 logger.info("Exception occurred:" + e.toString(), e); 240 } 241 242 return element; 243 } 244 245 251 public void refresh() 252 { 253 if (cacheContent == true) 254 { 255 getWebPageContent(null, this.getPortletConfig()); 256 } 257 } 258 259 263 protected String selectUrl( RunData data, PortletConfig config ) 264 { 265 String url = config.getURL(); 266 267 return url; 268 269 } 271 } 272 273 | Popular Tags |