summaryrefslogtreecommitdiff
path: root/bkucommon/src/main/java/at/gv/egiz/bku/binding
diff options
context:
space:
mode:
authormcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2008-12-04 10:00:31 +0000
committermcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2008-12-04 10:00:31 +0000
commit3aadcf8f877a560bed75af7e0db918aa26ef2a03 (patch)
tree66e382b6f87b0034046e4fb906497ed6aed74198 /bkucommon/src/main/java/at/gv/egiz/bku/binding
parent99134c1be5db0fedadc051922e70c9bf563ce16d (diff)
downloadmocca-3aadcf8f877a560bed75af7e0db918aa26ef2a03.tar.gz
mocca-3aadcf8f877a560bed75af7e0db918aa26ef2a03.tar.bz2
mocca-3aadcf8f877a560bed75af7e0db918aa26ef2a03.zip
Refactoring of infobox implementation.
git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@232 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4
Diffstat (limited to 'bkucommon/src/main/java/at/gv/egiz/bku/binding')
-rw-r--r--bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrl.java1
-rw-r--r--bkucommon/src/main/java/at/gv/egiz/bku/binding/LegacyDataUrlConnectionImpl.java230
2 files changed, 231 insertions, 0 deletions
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrl.java b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrl.java
index d462ac60..531772cf 100644
--- a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrl.java
+++ b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrl.java
@@ -65,5 +65,6 @@ public class DataUrl {
public static void setConfiguration(Properties props) {
configuration = props;
+ defaultDataUrlConnection.setConfiguration(configuration);
}
} \ No newline at end of file
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/binding/LegacyDataUrlConnectionImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/binding/LegacyDataUrlConnectionImpl.java
new file mode 100644
index 00000000..5339d689
--- /dev/null
+++ b/bkucommon/src/main/java/at/gv/egiz/bku/binding/LegacyDataUrlConnectionImpl.java
@@ -0,0 +1,230 @@
+package at.gv.egiz.bku.binding;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.StringWriter;
+import java.net.HttpURLConnection;
+import java.net.SocketTimeoutException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.security.cert.X509Certificate;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import at.gv.egiz.bku.slcommands.SLResult;
+import at.gv.egiz.bku.slcommands.SLResult.SLResultType;
+import at.gv.egiz.bku.slexceptions.SLRuntimeException;
+import at.gv.egiz.bku.utils.binding.Protocol;
+
+/**
+ * not thread-safe thus newInsance always returns a new object
+ *
+ */
+public class LegacyDataUrlConnectionImpl implements DataUrlConnectionSPI {
+
+ private final static Log log = LogFactory.getLog(DataUrlConnectionImpl.class);
+
+ public final static Protocol[] SUPPORTED_PROTOCOLS = { Protocol.HTTP,
+ Protocol.HTTPS };
+ protected X509Certificate serverCertificate;
+ protected Protocol protocol;
+ protected URL url;
+ private HttpURLConnection connection;
+ protected Map<String, String> requestHttpHeaders;
+ protected Map<String, String> formParams;
+ protected String boundary;
+ protected Properties config = null;
+
+ protected DataUrlResponse result;
+
+ public String getProtocol() {
+ if (protocol == null) {
+ return null;
+ }
+ return protocol.toString();
+ }
+
+ /**
+ * opens a connection sets the headers gets the server certificate
+ *
+ * @throws java.net.SocketTimeoutException
+ * @throws java.io.IOException
+ * @pre url != null
+ * @pre httpHeaders != null
+ */
+ public void connect() throws SocketTimeoutException, IOException {
+ connection = (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ Set<String> headers = requestHttpHeaders.keySet();
+ Iterator<String> headerIt = headers.iterator();
+ while (headerIt.hasNext()) {
+ String name = headerIt.next();
+ connection.setRequestProperty(name, requestHttpHeaders.get(name));
+ }
+ log.trace("Connecting to: "+url);
+ connection.connect();
+ if (connection instanceof HttpsURLConnection) {
+ HttpsURLConnection ssl = (HttpsURLConnection) connection;
+ X509Certificate[] certs = (X509Certificate[]) ssl.getServerCertificates();
+ if ((certs != null) && (certs.length >= 1)) {
+ log.trace("Server certificate: "+certs[0]);
+ serverCertificate = certs[0];
+ }
+ }
+ }
+
+ public X509Certificate getServerCertificate() {
+ return serverCertificate;
+ }
+
+ public void setHTTPHeader(String name, String value) {
+ if (name != null && value != null) {
+ requestHttpHeaders.put(name, value);
+ }
+ }
+
+ public void setHTTPFormParameter(String name, InputStream data,
+ String contentType, String charSet, String transferEncoding) {
+ StringBuilder sb = new StringBuilder();
+ try {
+ InputStreamReader reader = new InputStreamReader(data, (charSet != null) ? charSet : "UTF-8");
+ char[] c = new char[512];
+ for (int l; (l = reader.read(c)) != -1;) {
+ sb.append(c, 0, l);
+ }
+ } catch (IOException e) {
+ throw new SLRuntimeException("Failed to set HTTP form parameter.", e);
+ }
+ formParams.put(name, sb.toString());
+ }
+
+ /**
+ * send all formParameters
+ *
+ * @throws java.io.IOException
+ */
+ public void transmit(SLResult slResult) throws IOException {
+ StringWriter writer = new StringWriter();
+ slResult.writeTo(new StreamResult(writer));
+ formParams.put(
+ (slResult.getResultType() == SLResultType.XML)
+ ? DataUrlConnection.FORMPARAM_XMLRESPONSE
+ : DataUrlConnection.FORMPARAM_BINARYRESPONSE,
+ writer.toString());
+
+ OutputStream os = connection.getOutputStream();
+ OutputStreamWriter streamWriter = new OutputStreamWriter(os, HttpUtil.DEFAULT_CHARSET);
+
+ log.trace("Sending data");
+ Iterator<String> keys = formParams.keySet().iterator();
+ while(keys.hasNext()) {
+ String key = keys.next();
+ streamWriter.write(URLEncoder.encode(key, "UTF-8"));
+ streamWriter.write("=");
+ streamWriter.write(URLEncoder.encode(formParams.get(key), "UTF-8"));
+ if (keys.hasNext()) {
+ streamWriter.write("&");
+ }
+ }
+ streamWriter.flush();
+ os.close();
+
+ // MultipartRequestEntity PostMethod
+ InputStream is = null;
+ try {
+ is = connection.getInputStream();
+ } catch (IOException iox) {
+ log.info(iox);
+ }
+ log.trace("Reading response");
+ result = new DataUrlResponse(url.toString(), connection.getResponseCode(), is);
+ Map<String, String> responseHttpHeaders = new HashMap<String, String>();
+ Map<String, List<String>> httpHeaders = connection.getHeaderFields();
+ for (Iterator<String> keyIt = httpHeaders.keySet().iterator(); keyIt
+ .hasNext();) {
+ String key = keyIt.next();
+ StringBuffer value = new StringBuffer();
+ for (String val : httpHeaders.get(key)) {
+ value.append(val);
+ value.append(HttpUtil.SEPERATOR[0]);
+ }
+ String valString = value.substring(0, value.length() - 1);
+ if ((key != null) && (value.length() > 0)) {
+ responseHttpHeaders.put(key, valString);
+ }
+ }
+ result.setResponseHttpHeaders(responseHttpHeaders);
+ }
+
+ @Override
+ public DataUrlResponse getResponse() throws IOException {
+ return result;
+ }
+
+ /**
+ * inits protocol, url, httpHeaders, formParams
+ *
+ * @param url
+ * must not be null
+ */
+ @Override
+ public void init(URL url) {
+
+ for (int i = 0; i < SUPPORTED_PROTOCOLS.length; i++) {
+ if (SUPPORTED_PROTOCOLS[i].toString().equalsIgnoreCase(url.getProtocol())) {
+ protocol = SUPPORTED_PROTOCOLS[i];
+ break;
+ }
+ }
+ if (protocol == null) {
+ throw new SLRuntimeException("Protocol " + url.getProtocol()
+ + " not supported for data url");
+ }
+ this.url = url;
+ requestHttpHeaders = new HashMap<String, String>();
+ if ((config != null)
+ && (config.getProperty(USER_AGENT_PROPERTY_KEY) != null)) {
+ requestHttpHeaders.put(HttpUtil.HTTP_HEADER_USER_AGENT, config
+ .getProperty(USER_AGENT_PROPERTY_KEY));
+ } else {
+ requestHttpHeaders
+ .put(HttpUtil.HTTP_HEADER_USER_AGENT, DEFAULT_USERAGENT);
+
+ }
+ requestHttpHeaders.put(HttpUtil.HTTP_HEADER_CONTENT_TYPE,
+ HttpUtil.APPLICATION_URL_ENCODED);
+
+ formParams = new HashMap<String, String>();
+ }
+
+ @Override
+ public DataUrlConnectionSPI newInstance() {
+ DataUrlConnectionSPI uc = new LegacyDataUrlConnectionImpl();
+ uc.setConfiguration(config);
+ return uc;
+ }
+
+ @Override
+ public URL getUrl() {
+ return url;
+ }
+
+ @Override
+ public void setConfiguration(Properties config) {
+ this.config = config;
+ }
+} \ No newline at end of file