1 33 34 package com.icesoft.faces.renderkit; 35 36 import com.icesoft.faces.renderkit.dom_html_basic.TextRenderer; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 import javax.faces.component.UIComponent; 41 import javax.faces.component.UIOutput; 42 import javax.faces.context.FacesContext; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpSession ; 45 import java.io.IOException ; 46 import java.io.InputStreamReader ; 47 import java.io.Reader ; 48 import java.io.StringWriter ; 49 import java.net.URI ; 50 import java.net.URL ; 51 import java.net.URLConnection ; 52 53 public class IncludeRenderer extends TextRenderer { 54 55 private static final Log log = LogFactory.getLog(IncludeRenderer.class); 56 57 public void encodeBegin(FacesContext context, UIComponent component) 58 throws IOException { 59 if (context == null || component == null) { 60 throw new NullPointerException ( 61 "Null Faces context or component parameter"); 62 } 63 if (!component.isRendered()) { 66 return; 67 } 68 69 String page = (String ) component.getAttributes().get("page"); 70 71 HttpServletRequest request = (HttpServletRequest ) 72 context.getExternalContext().getRequest(); 73 URI absoluteURI = null; 74 try { 75 absoluteURI = new URI ( 76 request.getScheme() + "://" + 77 request.getServerName() + ":" + 78 request.getServerPort() + 79 request.getRequestURI()); 80 URL includedURL = absoluteURI.resolve(page).toURL(); 81 URLConnection includedConnection = includedURL.openConnection(); 82 includedConnection 83 .setRequestProperty( "Cookie", 84 "JSESSIONID=" + ((HttpSession ) context.getExternalContext() 85 .getSession( false)).getId() ); 86 Reader contentsReader = new InputStreamReader ( 87 includedConnection.getInputStream()); 88 89 StringWriter includedContents = new StringWriter (); 90 char[] buf = new char[2000]; 91 int len = 0; 92 while ((len = contentsReader.read(buf)) > -1) { 93 includedContents.write(buf, 0, len); 94 } 95 96 ((UIOutput) component).setValue(includedContents.toString()); 97 98 } catch (Exception e) { 99 if (log.isDebugEnabled()) { 100 log.debug(e.getMessage()); 101 } 102 } 103 104 super.encodeBegin(context, component); 105 } 106 107 108 } 109 | Popular Tags |