View Javadoc

1   /*
2    * Copyright 2005-2012 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.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  import java.util.Arrays;
26  import java.util.Iterator;
27  
28  import org.springframework.util.Assert;
29  import org.springframework.ws.WebServiceMessage;
30  import org.springframework.ws.transport.WebServiceConnection;
31  
32  import org.apache.http.Header;
33  import org.apache.http.HttpEntity;
34  import org.apache.http.HttpResponse;
35  import org.apache.http.client.HttpClient;
36  import org.apache.http.client.methods.HttpPost;
37  import org.apache.http.entity.ByteArrayEntity;
38  import org.apache.http.protocol.HttpContext;
39  import org.apache.http.util.EntityUtils;
40  
41  /**
42   * Implementation of [email protected] WebServiceConnection} that is based on Apache HttpClient. Exposes a [email protected] HttpPost} and
43   * [email protected] HttpResponse}.
44   *
45   * @author Alan Stewart
46   * @author Barry Pitman
47   * @author Arjen Poutsma
48   * @since 2.1.0
49   */
50  public class HttpComponentsConnection extends AbstractHttpSenderConnection {
51  
52      private final HttpClient httpClient;
53  
54      private final HttpPost httpPost;
55  
56      private final HttpContext httpContext;
57  
58      private HttpResponse httpResponse;
59  
60      private ByteArrayOutputStream requestBuffer;
61  
62      protected HttpComponentsConnection(HttpClient httpClient, HttpPost httpPost, HttpContext httpContext) {
63          Assert.notNull(httpClient, "httpClient must not be null");
64          Assert.notNull(httpPost, "httpPost must not be null");
65          this.httpClient = httpClient;
66          this.httpPost = httpPost;
67          this.httpContext = httpContext;
68      }
69  
70      public HttpPost getHttpPost() {
71          return httpPost;
72      }
73  
74      public HttpResponse getHttpResponse() {
75          return httpResponse;
76      }
77  
78      @Override
79      public void onClose() throws IOException {
80          if (httpResponse != null && httpResponse.getEntity() != null) {
81              EntityUtils.consume(httpResponse.getEntity());
82          }
83      }
84  
85      /*
86        * URI
87        */
88      public URI getUri() throws URISyntaxException {
89          return new URI(httpPost.getURI().toString());
90      }
91  
92      /*
93        * Sending request
94        */
95  
96      @Override
97      protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
98          requestBuffer = new ByteArrayOutputStream();
99      }
100 
101     @Override
102     protected void addRequestHeader(String name, String value) throws IOException {
103         httpPost.addHeader(name, value);
104     }
105 
106     @Override
107     protected OutputStream getRequestOutputStream() throws IOException {
108         return requestBuffer;
109     }
110 
111     @Override
112     protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
113         httpPost.setEntity(new ByteArrayEntity(requestBuffer.toByteArray()));
114         requestBuffer = null;
115         if (httpContext != null) {
116             httpResponse = httpClient.execute(httpPost, httpContext);
117         }
118         else {
119             httpResponse = httpClient.execute(httpPost);
120         }
121     }
122 
123     /*
124      * Receiving response
125      */
126 
127     @Override
128     protected int getResponseCode() throws IOException {
129         return httpResponse.getStatusLine().getStatusCode();
130     }
131 
132     @Override
133     protected String getResponseMessage() throws IOException {
134         return httpResponse.getStatusLine().getReasonPhrase();
135     }
136 
137     @Override
138     protected long getResponseContentLength() throws IOException {
139         HttpEntity entity = httpResponse.getEntity();
140         if (entity != null) {
141             return entity.getContentLength();
142         }
143         return 0;
144     }
145 
146     @Override
147     protected InputStream getRawResponseInputStream() throws IOException {
148         HttpEntity entity = httpResponse.getEntity();
149         if (entity != null) {
150             return entity.getContent();
151         }
152         throw new IllegalStateException("Response has no enclosing response entity, cannot create input stream");
153     }
154 
155     @Override
156     protected Iterator<String> getResponseHeaderNames() throws IOException {
157         Header[] headers = httpResponse.getAllHeaders();
158         String[] names = new String[headers.length];
159         for (int i = 0; i < headers.length; i++) {
160             names[i] = headers[i].getName();
161         }
162         return Arrays.asList(names).iterator();
163     }
164 
165     @Override
166     protected Iterator<String> getResponseHeaders(String name) throws IOException {
167         Header[] headers = httpResponse.getHeaders(name);
168         String[] values = new String[headers.length];
169         for (int i = 0; i < headers.length; i++) {
170             values[i] = headers[i].getValue();
171         }
172         return Arrays.asList(values).iterator();
173     }
174 }