1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40
41
42 public class HttpUrlConnection extends AbstractHttpSenderConnection {
43
44 private final HttpURLConnection connection;
45
46
47
48
49
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
67
68
69 public URI getUri() throws URISyntaxException {
70 return new URI(StringUtils.replace(connection.getURL().toString(), " ", "%20"));
71 }
72
73
74
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
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
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 }