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.commons.httpclient.Header;
33  import org.apache.commons.httpclient.HttpClient;
34  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
35  import org.apache.commons.httpclient.URIException;
36  import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
37  import org.apache.commons.httpclient.methods.PostMethod;
38  
39  /**
40   * Implementation of [email protected] WebServiceConnection} that is based on Jakarta Commons HttpClient. Exposes a [email protected]
41   * PostMethod}.
42   *
43   * @author Arjen Poutsma
44   * @since 1.0.0
45   * @deprecated In favor of [email protected] HttpComponentsConnection}
46   */
47  @Deprecated
48  public class CommonsHttpConnection extends AbstractHttpSenderConnection {
49  
50      private final HttpClient httpClient;
51  
52      private final PostMethod postMethod;
53  
54      private ByteArrayOutputStream requestBuffer;
55  
56      private MultiThreadedHttpConnectionManager connectionManager;
57  
58      protected CommonsHttpConnection(HttpClient httpClient, PostMethod postMethod) {
59          Assert.notNull(httpClient, "httpClient must not be null");
60          Assert.notNull(postMethod, "postMethod must not be null");
61          this.httpClient = httpClient;
62          this.postMethod = postMethod;
63      }
64  
65      public PostMethod getPostMethod() {
66          return postMethod;
67      }
68  
69      @Override
70      public void onClose() throws IOException {
71          postMethod.releaseConnection();
72          if (connectionManager != null) {
73              connectionManager.shutdown();
74          }
75      }
76  
77      /*
78       * URI
79       */
80  
81      public URI getUri() throws URISyntaxException {
82          try {
83              return new URI(postMethod.getURI().toString());
84          }
85          catch (URIException ex) {
86              throw new URISyntaxException("", ex.getMessage());
87          }
88      }
89  
90      /*
91       * Sending request
92       */
93  
94      @Override
95      protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
96          requestBuffer = new ByteArrayOutputStream();
97      }
98  
99      @Override
100     protected void addRequestHeader(String name, String value) throws IOException {
101         postMethod.addRequestHeader(name, value);
102     }
103 
104     @Override
105     protected OutputStream getRequestOutputStream() throws IOException {
106         return requestBuffer;
107     }
108 
109     @Override
110     protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
111         postMethod.setRequestEntity(new ByteArrayRequestEntity(requestBuffer.toByteArray()));
112         requestBuffer = null;
113         try {
114             httpClient.executeMethod(postMethod);
115         } catch (IllegalStateException ex) {
116             if ("Connection factory has been shutdown.".equals(ex.getMessage())) {
117                 // The application context has been closed, resulting in a connection factory shutdown and an ISE.
118                 // Let's create a new connection factory for this connection only.
119                 connectionManager = new MultiThreadedHttpConnectionManager();
120                 httpClient.setHttpConnectionManager(connectionManager);
121                 httpClient.executeMethod(postMethod);
122             } else {
123                 throw ex;
124             }
125         }
126     }
127 
128     /*
129      * Receiving response
130      */
131 
132     @Override
133     protected int getResponseCode() throws IOException {
134         return postMethod.getStatusCode();
135     }
136 
137     @Override
138     protected String getResponseMessage() throws IOException {
139         return postMethod.getStatusText();
140     }
141 
142     @Override
143     protected long getResponseContentLength() throws IOException {
144         return postMethod.getResponseContentLength();
145     }
146 
147     @Override
148     protected InputStream getRawResponseInputStream() throws IOException {
149         return postMethod.getResponseBodyAsStream();
150     }
151 
152     @Override
153     protected Iterator<String> getResponseHeaderNames() throws IOException {
154         Header[] headers = postMethod.getResponseHeaders();
155         String[] names = new String[headers.length];
156         for (int i = 0; i < headers.length; i++) {
157             names[i] = headers[i].getName();
158         }
159         return Arrays.asList(names).iterator();
160     }
161 
162     @Override
163     protected Iterator<String> getResponseHeaders(String name) throws IOException {
164         Header[] headers = postMethod.getResponseHeaders(name);
165         String[] values = new String[headers.length];
166         for (int i = 0; i < headers.length; i++) {
167             values[i] = headers[i].getValue();
168         }
169         return Arrays.asList(values).iterator();
170     }
171 
172 }