View Javadoc
1   /*
2    * Copyright 2008 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 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   * @author Arjen Poutsma
36   * @since 1.5.0
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  }