diff options
Diffstat (limited to 'bkucommon/src/main/java')
5 files changed, 263 insertions, 232 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 8eaeacbd..a8477ece 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 @@ -16,13 +16,14 @@  */  package at.gv.egiz.bku.binding;
 -import java.net.MalformedURLException;
 -import java.net.URL;
 -
 -import org.apache.commons.logging.Log;
 -import org.apache.commons.logging.LogFactory;
 -
 -import at.gv.egiz.bku.slexceptions.SLRuntimeException;
 +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import at.gv.egiz.bku.slexceptions.SLRuntimeException;  /**
   * Used to handle DataUrl connections as specified in the CCE's HTTP protocol binding. 
 @@ -30,7 +31,8 @@ import at.gv.egiz.bku.slexceptions.SLRuntimeException;   */
  public class DataUrl {
    private static DataUrlConnectionSPI defaultDataUrlConnection = new DataUrlConnectionImpl();
 -  private static Log log = LogFactory.getLog(DataUrl.class);
 +  private static Log log = LogFactory.getLog(DataUrl.class); +  private static Properties configuration;
    private URL url;
 @@ -58,5 +60,9 @@ public class DataUrl {        log.error(e);
        throw new SLRuntimeException("Cannot instantiate a dataurlconnection:",e);
      }
 +  } +   +  public static void setConfiguration(Properties props) { +    configuration = props;    }
  }
\ No newline at end of file diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnection.java b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnection.java index 6d654639..c6ffa32a 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnection.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnection.java @@ -34,14 +34,16 @@ import at.gv.egiz.bku.slcommands.SLResult;   */
  public interface DataUrlConnection {
 -    public static final String DEFAULT_USERAGENT = "citizen-card-environment/1.2 BKU2 1.0";
 +    public static final String DEFAULT_USERAGENT = "citizen-card-environment/1.2 MOCCA Unknown";
      public static final String FORMPARAM_RESPONSETYPE = "ResponseType";
      public static final String DEFAULT_RESPONSETYPE = "HTTP-Security-Layer-RESPONSE";
      public static final String FORMPARAM_XMLRESPONSE = "XMLResponse";
      public static final String FORMPARAM_BINARYRESPONSE = "BinaryResponse";
      public static final String XML_RESPONSE_ENCODING = "UTF-8";
 -
 + +    public final static String USER_AGENT_PROPERTY_KEY="UserAgent"; +    
      public String getProtocol();      public URL getUrl();
 diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionImpl.java index 9f5d70cb..bb0314b5 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionImpl.java @@ -1,221 +1,235 @@  /* -* Copyright 2008 Federal Chancellery Austria and -* Graz University of Technology -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -*     http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -package at.gv.egiz.bku.binding;
 -
 -import java.io.FileOutputStream;
 -import java.io.IOException;
 -import java.io.InputStream;
 -import java.io.OutputStream;
 -import java.net.HttpURLConnection;
 -import java.net.SocketTimeoutException;
 -import java.net.URL;
 -import java.security.cert.X509Certificate;
 -import java.util.ArrayList;
 -import java.util.HashMap;
 -import java.util.Iterator;
 -import java.util.List;
 -import java.util.Map;
 -import java.util.Set;
 -
 -import javax.net.ssl.HttpsURLConnection;
 -
 -import org.apache.commons.httpclient.methods.multipart.FilePart;
 -import org.apache.commons.httpclient.methods.multipart.Part;
 -import org.apache.commons.httpclient.methods.multipart.StringPart;
 -
 -import at.gv.egiz.bku.binding.multipart.InputStreamPartSource;
 -import at.gv.egiz.bku.binding.multipart.SLResultPart;
 -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.StreamUtil;
 -import at.gv.egiz.bku.utils.binding.Protocol;
 -
 -/**
 - * not thread-safe thus newInsance always returns a new object
 - * 
 - */
 -public class DataUrlConnectionImpl implements DataUrlConnectionSPI {
 -
 -  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 ArrayList<Part> formParams;
 -  protected String boundary;
 -
 -  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();
 -
 + * Copyright 2008 Federal Chancellery Austria and + * Graz University of Technology + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + *     http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package at.gv.egiz.bku.binding; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.SocketTimeoutException; +import java.net.URL; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +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 org.apache.commons.httpclient.methods.multipart.FilePart; +import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.StringPart; + +import at.gv.egiz.bku.binding.multipart.InputStreamPartSource; +import at.gv.egiz.bku.binding.multipart.SLResultPart; +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 DataUrlConnectionImpl implements DataUrlConnectionSPI { + +  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 ArrayList<Part> 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(); +      // FIXXME move this to config.
 -    HttpURLConnection.setFollowRedirects(false);
 -    
 -    
 -    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));
 -    }
 -    connection.connect();
 -    if (connection instanceof HttpsURLConnection) {
 -      HttpsURLConnection ssl = (HttpsURLConnection) connection;
 -      X509Certificate[] certs = (X509Certificate[]) ssl.getServerCertificates();
 -      if ((certs != null) && (certs.length >= 1)) {
 -        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) {
 -    InputStreamPartSource source = new InputStreamPartSource(null, data);
 -    FilePart formParam = new FilePart(name, source, contentType, charSet);
 -    if (transferEncoding != null) {
 -      formParam.setTransferEncoding(transferEncoding);
 -    } else {
 -      formParam.setTransferEncoding(null);
 -    }
 -    formParams.add(formParam);
 -  }
 -
 -  /**
 -   * send all formParameters
 -   * 
 -   * @throws java.io.IOException
 -   */
 -  public void transmit(SLResult slResult) throws IOException {
 -    SLResultPart slResultPart = new SLResultPart(slResult,
 -        XML_RESPONSE_ENCODING);
 -    if (slResult.getResultType() == SLResultType.XML) {
 -      slResultPart.setTransferEncoding(null);
 -      slResultPart.setContentType(slResult.getMimeType());
 -      slResultPart.setCharSet(XML_RESPONSE_ENCODING);
 -    } else {
 -      slResultPart.setTransferEncoding(null);
 -      slResultPart.setContentType(slResult.getMimeType());
 -    }
 -    formParams.add(slResultPart);
 -
 -    OutputStream os = connection.getOutputStream();
 -    
 -    Part[] parts = new Part[formParams.size()];
 -    Part.sendParts(os, formParams.toArray(parts), boundary.getBytes());
 -    os.close();
 +    HttpURLConnection.setFollowRedirects(false); + +    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)); +    } +    connection.connect(); +    if (connection instanceof HttpsURLConnection) { +      HttpsURLConnection ssl = (HttpsURLConnection) connection; +      X509Certificate[] certs = (X509Certificate[]) ssl.getServerCertificates(); +      if ((certs != null) && (certs.length >= 1)) { +        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) { +    InputStreamPartSource source = new InputStreamPartSource(null, data); +    FilePart formParam = new FilePart(name, source, contentType, charSet); +    if (transferEncoding != null) { +      formParam.setTransferEncoding(transferEncoding); +    } else { +      formParam.setTransferEncoding(null); +    } +    formParams.add(formParam); +  } + +  /** +   * send all formParameters +   *  +   * @throws java.io.IOException +   */ +  public void transmit(SLResult slResult) throws IOException { +    SLResultPart slResultPart = new SLResultPart(slResult, +        XML_RESPONSE_ENCODING); +    if (slResult.getResultType() == SLResultType.XML) { +      slResultPart.setTransferEncoding(null); +      slResultPart.setContentType(slResult.getMimeType()); +      slResultPart.setCharSet(XML_RESPONSE_ENCODING); +    } else { +      slResultPart.setTransferEncoding(null); +      slResultPart.setContentType(slResult.getMimeType()); +    } +    formParams.add(slResultPart); + +    OutputStream os = connection.getOutputStream(); + +    Part[] parts = new Part[formParams.size()]; +    Part.sendParts(os, formParams.toArray(parts), boundary.getBytes()); +    os.close();      // MultipartRequestEntity PostMethod
 -    result = new DataUrlResponse(url.toString(), connection.getResponseCode(),
 -        connection.getInputStream());
 -    
 -    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;
 -    boundary = "--" + IdFactory.getInstance().createId().toString();
 -    requestHttpHeaders = new HashMap<String, String>();
 -    requestHttpHeaders.put(HttpUtil.HTTP_HEADER_USER_AGENT, DEFAULT_USERAGENT);
 -    requestHttpHeaders.put(HttpUtil.HTTP_HEADER_CONTENT_TYPE,
 -        HttpUtil.MULTIPART_FOTMDATA + HttpUtil.SEPERATOR[0]
 -            + HttpUtil.MULTIPART_FOTMDATA_BOUNDARY + "=" + boundary);
 -
 -    formParams = new ArrayList<Part>();
 -    StringPart responseType = new StringPart(FORMPARAM_RESPONSETYPE,
 -        DEFAULT_RESPONSETYPE);
 -    responseType.setCharSet("UTF-8");
 -    responseType.setTransferEncoding(null);
 -    formParams.add(responseType);
 -  }
 -
 -  @Override
 -  public DataUrlConnectionSPI newInstance() {
 -    return new DataUrlConnectionImpl();
 +    result = new DataUrlResponse(url.toString(), connection.getResponseCode(), +        connection.getInputStream()); + +    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 URL getUrl() { -		return url; -	}
 +  @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; +    boundary = "--" + IdFactory.getInstance().createId().toString(); +    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.MULTIPART_FOTMDATA + HttpUtil.SEPERATOR[0] +            + HttpUtil.MULTIPART_FOTMDATA_BOUNDARY + "=" + boundary); + +    formParams = new ArrayList<Part>(); +    StringPart responseType = new StringPart(FORMPARAM_RESPONSETYPE, +        DEFAULT_RESPONSETYPE); +    responseType.setCharSet("UTF-8"); +    responseType.setTransferEncoding(null); +    formParams.add(responseType); +  } + +  @Override +  public DataUrlConnectionSPI newInstance() { +    DataUrlConnectionSPI uc = new DataUrlConnectionImpl(); +    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 diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionSPI.java b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionSPI.java index 9e5a66f8..80cc3a0b 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionSPI.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/binding/DataUrlConnectionSPI.java @@ -17,6 +17,7 @@  package at.gv.egiz.bku.binding;
  import java.net.URL;
 +import java.util.Properties;  /**
   * Prototype of a DataurlconnectionSPI
 @@ -36,7 +37,13 @@ public interface DataUrlConnectionSPI extends DataUrlConnection {     * Initializes the DataUrlConnection
     * @param url
     */
 -  public void init(URL url);
 +  public void init(URL url); +   +  /** +   * Sets configuration parameters for this connection +   * @param config +   */ +  public void setConfiguration(Properties config);
  }
 diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java index 555f83bd..fb624211 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/ErrorResultImpl.java @@ -16,12 +16,14 @@  */  package at.gv.egiz.bku.slcommands.impl;
 -import at.buergerkarte.namespaces.securitylayer._1.ErrorResponseType;
 -import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory;
 -import at.gv.egiz.bku.slcommands.ErrorResult;
 -import at.gv.egiz.bku.slexceptions.SLException;
 -
 -import javax.xml.transform.Result;
 +import java.util.Locale; + +import javax.xml.transform.Result; + +import at.buergerkarte.namespaces.securitylayer._1.ErrorResponseType; +import at.buergerkarte.namespaces.securitylayer._1.ObjectFactory; +import at.gv.egiz.bku.slcommands.ErrorResult; +import at.gv.egiz.bku.slexceptions.SLException;  /**
   * This class implements the security layer result <code>ErrorResponse</code>.
 | 
