View Javadoc

1   /*
2    * Copyright 2007 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.IOException;
20  
21  import com.sun.net.httpserver.HttpExchange;
22  import com.sun.net.httpserver.HttpHandler;
23  
24  import org.springframework.ws.transport.support.SimpleWebServiceMessageReceiverObjectSupport;
25  
26  /**
27   * [email protected] HttpHandler} that can be used to handle incoming [email protected] HttpExchange} service requests. Designed for Sun's JRE
28   * 1.6 HTTP server.
29   * <p/>
30   * Requires a [email protected] org.springframework.ws.WebServiceMessageFactory} which is used to convert the incoming [email protected]
31   * HttpExchange} into a [email protected] org.springframework.ws.WebServiceMessage}, and passes that to the [email protected]
32   * org.springframework.ws.transport.WebServiceMessageReceiver} [email protected] #setMessageReceiver(org.springframework.ws.transport.WebServiceMessageReceiver)
33   * registered}.
34   *
35   * @author Arjen Poutsma
36   * @see org.springframework.remoting.support.SimpleHttpServerFactoryBean
37   * @since 1.5.0
38   */
39  public class WebServiceMessageReceiverHttpHandler extends SimpleWebServiceMessageReceiverObjectSupport
40          implements HttpHandler {
41  
42      private boolean chunkedEncoding = false;
43  
44      /** Enables chunked encoding on response bodies. Defaults to <code>false</code>. */
45      public void setChunkedEncoding(boolean chunkedEncoding) {
46          this.chunkedEncoding = chunkedEncoding;
47      }
48  
49      public void handle(HttpExchange httpExchange) throws IOException {
50          if (HttpTransportConstants.METHOD_POST.equals(httpExchange.getRequestMethod())) {
51              HttpExchangeConnection connection = new HttpExchangeConnection(httpExchange);
52              connection.setChunkedEncoding(chunkedEncoding);
53              try {
54                  handleConnection(connection);
55              }
56              catch (Exception ex) {
57                  logger.error(ex);
58              }
59          }
60          else {
61              httpExchange.sendResponseHeaders(HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED, -1);
62              httpExchange.close();
63          }
64      }
65  }