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.ByteArrayOutputStream;
20 import java.io.IOException;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.stream.StreamResult;
23
24 import com.sun.net.httpserver.Headers;
25 import com.sun.net.httpserver.HttpExchange;
26 import com.sun.net.httpserver.HttpHandler;
27
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.util.Assert;
30 import org.springframework.util.FileCopyUtils;
31 import org.springframework.ws.wsdl.WsdlDefinition;
32 import org.springframework.xml.transform.TransformerObjectSupport;
33
34
35
36
37
38 public class WsdlDefinitionHttpHandler extends TransformerObjectSupport implements HttpHandler, InitializingBean {
39
40 private static final String CONTENT_TYPE = "text/xml";
41
42 private WsdlDefinition definition;
43
44 public WsdlDefinitionHttpHandler() {
45 }
46
47 public WsdlDefinitionHttpHandler(WsdlDefinition definition) {
48 this.definition = definition;
49 }
50
51 public void setDefinition(WsdlDefinition definition) {
52 this.definition = definition;
53 }
54
55 public void afterPropertiesSet() throws Exception {
56 Assert.notNull(definition, "'definition' is required");
57 }
58
59 public void handle(HttpExchange httpExchange) throws IOException {
60 try {
61 if (HttpTransportConstants.METHOD_GET.equals(httpExchange.getRequestMethod())) {
62 Headers headers = httpExchange.getResponseHeaders();
63 headers.set(HttpTransportConstants.HEADER_CONTENT_TYPE, CONTENT_TYPE);
64 ByteArrayOutputStream os = new ByteArrayOutputStream();
65 transform(definition.getSource(), new StreamResult(os));
66 byte[] buf = os.toByteArray();
67 httpExchange.sendResponseHeaders(HttpTransportConstants.STATUS_OK, buf.length);
68 FileCopyUtils.copy(buf, httpExchange.getResponseBody());
69 }
70 else {
71 httpExchange.sendResponseHeaders(HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED, -1);
72 }
73 }
74 catch (TransformerException ex) {
75 logger.error(ex, ex);
76 }
77 finally {
78 httpExchange.close();
79 }
80 }
81 }