View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.http;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.HttpURLConnection;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Set;
30  
31  import org.springframework.util.Assert;
32  import org.springframework.util.StringUtils;
33  import org.springframework.ws.WebServiceMessage;
34  import org.springframework.ws.transport.WebServiceConnection;
35  
36  /**
37   * Implementation of the {@link WebServiceConnection} interface that uses a {@link HttpURLConnection}.
38   *
39   * @author Arjen Poutsma
40   * @since 1.0.0
41   */
42  public class HttpUrlConnection extends AbstractHttpSenderConnection {
43  
44      private final HttpURLConnection connection;
45  
46      /**
47       * Creates a new instance of the <code>HttpUrlConnection</code> with the given <code>HttpURLConnection</code>.
48       *
49       * @param connection the <code>HttpURLConnection</code>
50       */
51      protected HttpUrlConnection(HttpURLConnection connection) {
52          Assert.notNull(connection, "connection must not be null");
53          this.connection = connection;
54      }
55  
56      public HttpURLConnection getConnection() {
57          return connection;
58      }
59  
60      public void onClose() {
61          connection.disconnect();
62      }
63  
64      /*
65       * URI
66       */
67  
68      public URI getUri() throws URISyntaxException {
69          return new URI(StringUtils.replace(connection.getURL().toString(), " ", "%20"));
70      }
71  
72      /*
73       * Sending request
74       */
75  
76      protected void addRequestHeader(String name, String value) throws IOException {
77          connection.addRequestProperty(name, value);
78      }
79  
80      protected OutputStream getRequestOutputStream() throws IOException {
81          return connection.getOutputStream();
82      }
83  
84      protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
85          connection.connect();
86      }
87  
88      /*
89       * Receiving response
90       */
91  
92      protected long getResponseContentLength() throws IOException {
93          return connection.getContentLength();
94      }
95  
96      protected Iterator getResponseHeaderNames() throws IOException {
97          List headerNames = new ArrayList();
98          // Header field 0 is the status line, so we start at 1
99          int i = 1;
100         while (true) {
101             String headerName = connection.getHeaderFieldKey(i);
102             if (!StringUtils.hasLength(headerName)) {
103                 break;
104             }
105             headerNames.add(headerName);
106             i++;
107         }
108         return headerNames.iterator();
109     }
110 
111     protected Iterator getResponseHeaders(String name) throws IOException {
112         String headerField = connection.getHeaderField(name);
113         if (headerField == null) {
114             return Collections.EMPTY_LIST.iterator();
115         }
116         else {
117             Set tokens = StringUtils.commaDelimitedListToSet(headerField);
118             return tokens.iterator();
119         }
120     }
121 
122     protected int getResponseCode() throws IOException {
123         return connection.getResponseCode();
124     }
125 
126     protected String getResponseMessage() throws IOException {
127         return connection.getResponseMessage();
128     }
129 
130     protected InputStream getRawResponseInputStream() throws IOException {
131         if (connection.getResponseCode() / 100 != 2) {
132             return connection.getErrorStream();
133         }
134         else {
135             return connection.getInputStream();
136         }
137     }
138 }