001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.renderkit;
035:
036: import com.icesoft.faces.renderkit.dom_html_basic.TextRenderer;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: import javax.faces.component.UIComponent;
041: import javax.faces.component.UIOutput;
042: import javax.faces.context.FacesContext;
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpSession;
045: import java.io.IOException;
046: import java.io.InputStreamReader;
047: import java.io.Reader;
048: import java.io.StringWriter;
049: import java.net.URI;
050: import java.net.URL;
051: import java.net.URLConnection;
052:
053: public class IncludeRenderer extends TextRenderer {
054:
055: private static final Log log = LogFactory
056: .getLog(IncludeRenderer.class);
057:
058: public void encodeBegin(FacesContext context, UIComponent component)
059: throws IOException {
060: if (context == null || component == null) {
061: throw new NullPointerException(
062: "Null Faces context or component parameter");
063: }
064: // suppress rendering if "rendered" property on the component is
065: // false.
066: if (!component.isRendered()) {
067: return;
068: }
069:
070: String page = (String) component.getAttributes().get("page");
071:
072: HttpServletRequest request = (HttpServletRequest) context
073: .getExternalContext().getRequest();
074: URI absoluteURI = null;
075: try {
076: absoluteURI = new URI(request.getScheme() + "://"
077: + request.getServerName() + ":"
078: + request.getServerPort() + request.getRequestURI());
079: URL includedURL = absoluteURI.resolve(page).toURL();
080: URLConnection includedConnection = includedURL
081: .openConnection();
082: includedConnection.setRequestProperty("Cookie",
083: "JSESSIONID="
084: + ((HttpSession) context
085: .getExternalContext().getSession(
086: false)).getId());
087: Reader contentsReader = new InputStreamReader(
088: includedConnection.getInputStream());
089:
090: StringWriter includedContents = new StringWriter();
091: char[] buf = new char[2000];
092: int len = 0;
093: while ((len = contentsReader.read(buf)) > -1) {
094: includedContents.write(buf, 0, len);
095: }
096:
097: ((UIOutput) component)
098: .setValue(includedContents.toString());
099:
100: } catch (Exception e) {
101: if (log.isDebugEnabled()) {
102: log.debug(e.getMessage());
103: }
104: }
105:
106: super.encodeBegin(context, component);
107: }
108:
109: }
|