View Javadoc

1   /*
2    * Copyright 2005-2010 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      @Override
61      public void onClose() {
62          connection.disconnect();
63      }
64  
65      /*
66       * URI
67       */
68  
69      public URI getUri() throws URISyntaxException {
70          return new URI(StringUtils.replace(connection.getURL().toString(), " ", "%20"));
71      }
72  
73      /*
74       * Sending request
75       */
76  
77      @Override
78      protected void addRequestHeader(String name, String value) throws IOException {
79          connection.addRequestProperty(name, value);
80      }
81  
82      @Override
83      protected OutputStream getRequestOutputStream() throws IOException {
84          return connection.getOutputStream();
85      }
86  
87      @Override
88      protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
89          connection.connect();
90      }
91  
92      /*
93       * Receiving response
94       */
95  
96      @Override
97      protected long getResponseContentLength() throws IOException {
98          return connection.getContentLength();
99      }
100 
101     @Override
102     protected Iterator<String> getResponseHeaderNames() throws IOException {
103         List<String> headerNames = new ArrayList<String>();
104         // Header field 0 is the status line, so we start at 1
105         int i = 1;
106         while (true) {
107             String headerName = connection.getHeaderFieldKey(i);
108             if (!StringUtils.hasLength(headerName)) {
109                 break;
110             }
111             headerNames.add(headerName);
112             i++;
113         }
114         return headerNames.iterator();
115     }
116 
117     @Override
118     protected Iterator<String> getResponseHeaders(String name) throws IOException {
119         String headerField = connection.getHeaderField(name);
120         if (headerField == null) {
121             return Collections.<String>emptyList().iterator();
122         }
123         else {
124             Set<String> tokens = StringUtils.commaDelimitedListToSet(headerField);
125             return tokens.iterator();
126         }
127     }
128 
129     @Override
130     protected int getResponseCode() throws IOException {
131         return connection.getResponseCode();
132     }
133 
134     @Override
135     protected String getResponseMessage() throws IOException {
136         return connection.getResponseMessage();
137     }
138 
139     @Override
140     protected InputStream getRawResponseInputStream() throws IOException {
141         if (connection.getResponseCode() / 100 != 2) {
142             return connection.getErrorStream();
143         }
144         else {
145             return connection.getInputStream();
146         }
147     }
148 }