001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.framework;
011:
012: import java.util.*;
013: import java.net.*;
014: import java.io.*;
015: import javax.servlet.http.*;
016: import javax.xml.transform.stream.StreamResult;
017: import javax.xml.transform.stream.StreamSource;
018: import javax.xml.transform.Source;
019: import javax.xml.transform.Result;
020: import org.mmbase.util.functions.*;
021: import org.mmbase.util.*;
022:
023: import org.mmbase.util.logging.Logger;
024: import org.mmbase.util.logging.Logging;
025:
026: /**
027: * A Renderer implementation based on an external connection.
028: *
029: * @author Michiel Meeuwissen
030: * @version $Id: ConnectionRenderer.java,v 1.4 2008/02/23 12:44:03 michiel Exp $
031: * @since MMBase-1.9
032: */
033: public class ConnectionRenderer extends AbstractRenderer {
034: private static final Logger log = Logging
035: .getLoggerInstance(ConnectionRenderer.class);
036:
037: protected URL url;
038: protected int timeOut = 2000;
039: protected String xsl = null;
040: protected boolean decorate = true;
041:
042: public ConnectionRenderer(String t, Block parent) {
043: super (t, parent);
044: }
045:
046: public void setUrl(String u) throws MalformedURLException {
047: url = new URL(u);
048: }
049:
050: public void setXslt(String x) throws MalformedURLException {
051: xsl = x;
052: }
053:
054: public void setTimeOut(int t) {
055: timeOut = t;
056: }
057:
058: public void setDecorate(boolean d) {
059: decorate = d;
060: }
061:
062: public Parameter[] getParameters() {
063: return new Parameter[] {};
064: }
065:
066: public void render(Parameters blockParameters,
067: Parameters frameworkParameters, Writer w, WindowState state)
068: throws FrameworkException {
069:
070: try {
071: if (decorate) {
072: HttpServletRequest request = blockParameters
073: .get(Parameter.REQUEST);
074: decorateIntro(request, w, null);
075: }
076: HttpURLConnection connection = (HttpURLConnection) url
077: .openConnection();
078: connection.setConnectTimeout(timeOut);
079: connection.setReadTimeout(timeOut);
080: int responseCode = connection.getResponseCode();
081: String contentType = connection.getContentType();
082: InputStream inputStream = connection.getInputStream();
083: if (responseCode == 200) {
084: if (xsl == null) {
085: String encoding = GenericResponseWrapper
086: .getEncoding(contentType);
087: BufferedReader r = new BufferedReader(
088: new InputStreamReader(inputStream, encoding));
089: char[] buf = new char[1000];
090: int c;
091: while ((c = r.read(buf, 0, 1000)) > 0) {
092: w.write(buf, 0, c);
093: }
094: } else {
095: /// convert using the xsl and spit out that.
096: Source xml = new StreamSource(inputStream);
097: URL x = ResourceLoader.getConfigurationRoot()
098: .getResource(xsl);
099:
100: Result res = new StreamResult(w);
101: XSLTransformer.transform(xml, x, res,
102: new HashMap<String, Object>());
103: }
104:
105: } else {
106: throw new FrameworkException("" + responseCode);
107: }
108: } catch (java.net.ConnectException ce) {
109: throw new FrameworkException(ce.getMessage(), ce);
110: } catch (java.net.SocketTimeoutException ste) {
111: throw new FrameworkException(ste.getMessage(), ste);
112: } catch (IOException ioe) {
113: throw new FrameworkException(ioe.getMessage(), ioe);
114: } catch (javax.xml.transform.TransformerException te) {
115: throw new FrameworkException(te.getMessage(), te);
116: } finally {
117: if (decorate) {
118: try {
119: HttpServletRequest request = blockParameters
120: .get(Parameter.REQUEST);
121: decorateOutro(request, w);
122: } catch (Exception e) {
123: }
124: }
125: }
126:
127: }
128:
129: public String toString() {
130: return url.toString();
131: }
132:
133: public java.net.URI getUri() {
134: try {
135: return url.toURI();
136: } catch (URISyntaxException use) {
137: log.warn(use);
138: return null;
139: }
140: }
141: }
|