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 public void onClose() {
61 connection.disconnect();
62 }
63
64
65
66
67
68 public URI getUri() throws URISyntaxException {
69 return new URI(StringUtils.replace(connection.getURL().toString(), " ", "%20"));
70 }
71
72
73
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
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
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 }