001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.components.util;
019:
020: import java.io.BufferedReader;
021: import java.io.InputStreamReader;
022: import java.io.IOException;
023: import java.io.StringWriter;
024: import java.io.Writer;
025: import java.net.HttpURLConnection;
026: import java.net.URL;
027: import java.net.URLConnection;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Map;
031:
032: import de.finix.contelligent.AbstractComponent;
033: import de.finix.contelligent.CallData;
034: import de.finix.contelligent.Component;
035: import de.finix.contelligent.ComponentManager;
036: import de.finix.contelligent.ComponentPath;
037: import de.finix.contelligent.components.Folder;
038: import de.finix.contelligent.exception.CircularRenderException;
039: import de.finix.contelligent.exception.ContelligentException;
040: import de.finix.contelligent.logging.LoggingService;
041: import de.finix.contelligent.render.ParameterDescription;
042: import de.finix.contelligent.render.Renderable;
043: import de.finix.contelligent.render.Renderer;
044:
045: public class HTTPRenderer extends Folder implements Renderable,
046: Renderer {
047:
048: final static org.apache.log4j.Logger log = LoggingService
049: .getLogger(HTTPRenderer.class);
050:
051: final static public String URL_COMPONENT_NAME = "url";
052:
053: final static public String SOAP_CONTENT_COMPONENT_NAME = "soapContent";
054:
055: private Collection sensitiveCategories = new ArrayList();
056:
057: private String url;
058:
059: private String soapAction;
060:
061: private int connectTimeout = 1000;
062:
063: private int readTimeout = 1000;
064:
065: public String getUrl() {
066: return url;
067: }
068:
069: public void setUrl(String url) {
070: this .url = url;
071: }
072:
073: public String getSoapAction() {
074: return soapAction;
075: }
076:
077: public void setSoapAction(String soapAction) {
078: this .soapAction = soapAction;
079: }
080:
081: public int getConnectTimeout() {
082: return connectTimeout;
083: }
084:
085: public void setConnectTimeout(int connectTimeout) {
086: this .connectTimeout = connectTimeout;
087: }
088:
089: public int getReadTimeout() {
090: return readTimeout;
091: }
092:
093: public void setReadTimeout(int readTimeout) {
094: this .readTimeout = readTimeout;
095: }
096:
097: public Renderer getRenderer() {
098: return this ;
099: }
100:
101: public ParameterDescription[] getParameterDescription() {
102: return new ParameterDescription[0];
103: }
104:
105: protected String retrieveRenderedSubcomponent(String componentName,
106: Map parameterMap, CallData callData)
107: throws ContelligentException, IOException {
108: ComponentManager cm = callData.getActualManager();
109: Component comp = cm.getSubcomponent(this , componentName,
110: callData);
111: if (comp instanceof Renderable) {
112: StringWriter localWriter = new StringWriter();
113: Renderer renderer = ((Renderable) comp).getRenderer();
114:
115: // Circular render check
116: ComponentPath realPath = comp.getComponentContext()
117: .getPath();
118: if (callData.checkRenderStack(realPath)) {
119: throw new CircularRenderException();
120: }
121:
122: callData.pushRenderStack(realPath);
123: renderer.render(localWriter, parameterMap, callData);
124: callData.popRenderStack();
125:
126: return localWriter.toString();
127:
128: } else {
129: throw new ContelligentException("Subcomponent "
130: + URL_COMPONENT_NAME + " is not renderable.");
131: }
132: }
133:
134: public void render(Writer writer, Map parameterMap,
135: CallData callData) throws ContelligentException,
136: IOException {
137: HttpURLConnection conn;
138: if ((url != null) && (!url.equals(""))) {
139: conn = (HttpURLConnection) (new URL(url)).openConnection();
140: } else {
141: conn = (HttpURLConnection) (new URL(
142: retrieveRenderedSubcomponent(URL_COMPONENT_NAME,
143: parameterMap, callData))).openConnection();
144: }
145: conn.setConnectTimeout(connectTimeout);
146: conn.setReadTimeout(readTimeout);
147: if ((soapAction != null) && (!soapAction.equals(""))) {
148: try {
149: byte[] content = retrieveRenderedSubcomponent(
150: SOAP_CONTENT_COMPONENT_NAME, parameterMap,
151: callData).getBytes("UTF-8");
152: conn.setRequestProperty("Content-Length", String
153: .valueOf(content.length));
154: conn.setRequestProperty("Content-Type",
155: "text/xml; charset=utf-8");
156: conn.setRequestProperty("SOAPAction", soapAction);
157: conn.setRequestMethod("POST");
158: conn.setDoOutput(true);
159: conn.setDoInput(true);
160: conn.getOutputStream().write(content);
161: } catch (ContelligentException ce) {
162: log.error("Unable to render content.", ce);
163: }
164: }
165: conn.connect();
166: //log.info("Ciph: "+conn.getCipherSuite());
167: String charset = callData.getSystem().getDefaultEncoding();
168: String type = conn.getContentType();
169: if (type != null) {
170: log.debug("Content-Type header: " + type);
171: int i = type.indexOf("; charset=");
172: if (i != -1) {
173: charset = type.substring(i + 10);
174: }
175: } else {
176: log.debug("No Content-Type header available");
177: }
178: log.debug("Guessed charset: " + charset);
179:
180: //log.info("RESPONSE("+conn.getResponseCode()+"): "+conn.getResponseMessage());
181:
182: BufferedReader inputReader;
183: //try {
184: inputReader = new BufferedReader(new InputStreamReader(conn
185: .getInputStream(), charset));
186: //} catch (IOException ioe) {
187: // //log.info("error?");
188: // inputReader = new BufferedReader(new InputStreamReader(conn.getErrorStream(), charset));
189: //}
190: String line;
191: StringBuffer content = new StringBuffer();
192: while ((line = inputReader.readLine()) != null) {
193: content.append(line);
194: content.append("\n");
195: }
196: writer.write(content.toString());
197: }
198:
199: public Collection getSensitiveCategories() {
200: return sensitiveCategories;
201: }
202: }
|