View Javadoc

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