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  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.util.Iterator;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.springframework.ws.WebServiceMessage;
27  import org.springframework.ws.transport.AbstractReceiverConnection;
28  import org.springframework.ws.transport.EndpointAwareWebServiceConnection;
29  import org.springframework.ws.transport.FaultAwareWebServiceConnection;
30  import org.springframework.ws.transport.WebServiceConnection;
31  import org.springframework.ws.transport.support.EnumerationIterator;
32  
33  /**
34   * Implementation of {@link WebServiceConnection} that is based on the Servlet API.
35   *
36   * @author Arjen Poutsma
37   * @since 1.0.0
38   */
39  public class HttpServletConnection extends AbstractReceiverConnection
40          implements EndpointAwareWebServiceConnection, FaultAwareWebServiceConnection {
41  
42      private final HttpServletRequest httpServletRequest;
43  
44      private final HttpServletResponse httpServletResponse;
45  
46      private boolean statusCodeSet = false;
47  
48      /**
49       * Constructs a new servlet connection with the given <code>HttpServletRequest</code> and
50       * <code>HttpServletResponse</code>.
51       */
52      protected HttpServletConnection(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
53          this.httpServletRequest = httpServletRequest;
54          this.httpServletResponse = httpServletResponse;
55      }
56  
57      /** Returns the <code>HttpServletRequest</code> for this connection. */
58      public HttpServletRequest getHttpServletRequest() {
59          return httpServletRequest;
60      }
61  
62      /** Returns the <code>HttpServletResponse</code> for this connection. */
63      public HttpServletResponse getHttpServletResponse() {
64          return httpServletResponse;
65      }
66  
67      public void endpointNotFound() {
68          getHttpServletResponse().setStatus(HttpTransportConstants.STATUS_NOT_FOUND);
69          statusCodeSet = true;
70      }
71  
72      public boolean hasError() throws IOException {
73          return false;
74      }
75  
76      public String getErrorMessage() throws IOException {
77          return null;
78      }
79  
80      /*
81       * Receiving request
82       */
83  
84      protected Iterator getRequestHeaderNames() throws IOException {
85          return new EnumerationIterator(getHttpServletRequest().getHeaderNames());
86      }
87  
88      protected Iterator getRequestHeaders(String name) throws IOException {
89          return new EnumerationIterator(getHttpServletRequest().getHeaders(name));
90      }
91  
92      protected InputStream getRequestInputStream() throws IOException {
93          return getHttpServletRequest().getInputStream();
94      }
95  
96      /*
97      * Sending response
98      */
99  
100     protected void addResponseHeader(String name, String value) throws IOException {
101         getHttpServletResponse().addHeader(name, value);
102     }
103 
104     protected OutputStream getResponseOutputStream() throws IOException {
105         return getHttpServletResponse().getOutputStream();
106     }
107 
108     protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
109         statusCodeSet = true;
110     }
111 
112     public void onClose() throws IOException {
113         if (!statusCodeSet) {
114             getHttpServletResponse().setStatus(HttpTransportConstants.STATUS_ACCEPTED);
115         }
116     }
117 
118     /*
119      * Faults
120      */
121 
122     public boolean hasFault() throws IOException {
123         return false;
124     }
125 
126     public void setFault(boolean fault) throws IOException {
127         if (fault) {
128             getHttpServletResponse().setStatus(HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR);
129         }
130         else {
131             getHttpServletResponse().setStatus(HttpTransportConstants.STATUS_OK);
132         }
133         statusCodeSet = true;
134     }
135 }