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.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 {@link 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     protected Iterator getRequestHeaderNames() throws IOException {
101         return new EnumerationIterator(getHttpServletRequest().getHeaderNames());
102     }
103 
104     protected Iterator getRequestHeaders(String name) throws IOException {
105         return new EnumerationIterator(getHttpServletRequest().getHeaders(name));
106     }
107 
108     protected InputStream getRequestInputStream() throws IOException {
109         return getHttpServletRequest().getInputStream();
110     }
111 
112     /*
113     * Sending response
114     */
115 
116     protected void addResponseHeader(String name, String value) throws IOException {
117         getHttpServletResponse().addHeader(name, value);
118     }
119 
120     protected OutputStream getResponseOutputStream() throws IOException {
121         return getHttpServletResponse().getOutputStream();
122     }
123 
124     protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
125         statusCodeSet = true;
126     }
127 
128     public void onClose() throws IOException {
129         if (!statusCodeSet) {
130             getHttpServletResponse().setStatus(HttpTransportConstants.STATUS_ACCEPTED);
131         }
132     }
133 
134     /*
135      * Faults
136      */
137 
138     public boolean hasFault() throws IOException {
139         return false;
140     }
141 
142     public void setFault(boolean fault) throws IOException {
143         if (fault) {
144             getHttpServletResponse().setStatus(HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR);
145         }
146         else {
147             getHttpServletResponse().setStatus(HttpTransportConstants.STATUS_OK);
148         }
149         statusCodeSet = true;
150     }
151 }