Home | History | Annotate | Download | only in transport
      1 /* Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany
      2  * Copyright (c) 2006, James Seigel, Calgary, AB., Canada
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a copy
      5  * of this software and associated documentation files (the "Software"), to deal
      6  * in the Software without restriction, including without limitation the rights
      7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or
      8  * sell copies of the Software, and to permit persons to whom the Software is
      9  * furnished to do so, subject to the following conditions:
     10  *
     11  * The  above copyright notice and this permission notice shall be included in
     12  * all copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     20  * IN THE SOFTWARE. */
     21 
     22 package org.ksoap2.transport;
     23 
     24 import org.ksoap2.HeaderProperty;
     25 
     26 import java.io.IOException;
     27 import java.io.InputStream;
     28 import java.io.OutputStream;
     29 import java.net.HttpURLConnection;
     30 import java.net.Proxy;
     31 import java.net.URL;
     32 import java.util.*;
     33 
     34 /**
     35  * Connection for J2SE environments.
     36  */
     37 public class ServiceConnectionSE implements ServiceConnection {
     38 
     39     private HttpURLConnection connection;
     40 
     41     /**
     42      * Constructor taking the url to the endpoint for this soap communication
     43      *
     44      * @param url the url to open the connection to.
     45      */
     46     public ServiceConnectionSE(String url) throws IOException {
     47         this(null, url, ServiceConnection.DEFAULT_TIMEOUT);
     48     }
     49 
     50     public ServiceConnectionSE(Proxy proxy, String url) throws IOException {
     51         this(proxy, url, ServiceConnection.DEFAULT_TIMEOUT);
     52     }
     53 
     54     /**
     55      * Constructor taking the url to the endpoint for this soap communication
     56      *
     57      * @param url     the url to open the connection to.
     58      * @param timeout the connection and read timeout for the http connection in milliseconds
     59      * @throws IOException // 20 seconds
     60      */
     61     public ServiceConnectionSE(String url, int timeout) throws IOException {
     62         this(null, url, timeout);
     63     }
     64 
     65     public ServiceConnectionSE(Proxy proxy, String url, int timeout) throws IOException {
     66         connection = (proxy == null)
     67                 ? (HttpURLConnection) new URL(url).openConnection()
     68                 : (HttpURLConnection) new URL(url).openConnection(proxy);
     69         connection.setUseCaches(false);
     70         connection.setDoOutput(true);
     71         connection.setDoInput(true);
     72         connection.setConnectTimeout(timeout);
     73         connection.setReadTimeout(
     74                 timeout); // even if we connect fine we want to time out if we cant read anything..
     75     }
     76 
     77     public void connect() throws IOException {
     78         connection.connect();
     79     }
     80 
     81     public void disconnect() {
     82         connection.disconnect();
     83     }
     84 
     85     public List getResponseProperties() throws IOException {
     86         List retList = new LinkedList();
     87 
     88         Map properties = connection.getHeaderFields();
     89         if (properties != null) {
     90             Set keys = properties.keySet();
     91             for (Iterator i = keys.iterator(); i.hasNext(); ) {
     92                 String key = (String) i.next();
     93                 List values = (List) properties.get(key);
     94 
     95                 for (int j = 0; j < values.size(); j++) {
     96                     retList.add(new HeaderProperty(key, (String) values.get(j)));
     97                 }
     98             }
     99         }
    100 
    101         return retList;
    102     }
    103 
    104     public int getResponseCode() throws IOException {
    105         return connection.getResponseCode();
    106     }
    107 
    108     public void setRequestProperty(String string, String soapAction) {
    109         connection.setRequestProperty(string, soapAction);
    110     }
    111 
    112     public void setRequestMethod(String requestMethod) throws IOException {
    113         connection.setRequestMethod(requestMethod);
    114     }
    115 
    116     /**
    117      * If the length of a HTTP request body is known ahead, sets fixed length
    118      * to enable streaming without buffering. Sets after connection will cause an exception.
    119      *
    120      * @param contentLength the fixed length of the HTTP request body
    121      * @see http://developer.android.com/reference/java/net/HttpURLConnection.html
    122      **/
    123     public void setFixedLengthStreamingMode(int contentLength) {
    124         connection.setFixedLengthStreamingMode(contentLength);
    125     }
    126 
    127     public void setChunkedStreamingMode() {
    128         connection.setChunkedStreamingMode(0);
    129     }
    130 
    131     public OutputStream openOutputStream() throws IOException {
    132         return connection.getOutputStream();
    133     }
    134 
    135     public InputStream openInputStream() throws IOException {
    136         return connection.getInputStream();
    137     }
    138 
    139     public InputStream getErrorStream() {
    140         return connection.getErrorStream();
    141     }
    142 
    143     public String getHost() {
    144         return connection.getURL().getHost();
    145     }
    146 
    147     public int getPort() {
    148         return connection.getURL().getPort();
    149     }
    150 
    151     public String getPath() {
    152         return connection.getURL().getPath();
    153     }
    154 }
    155