001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.transport.http;
018:
019: import java.io.FilterInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.util.Iterator;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.springframework.ws.WebServiceMessage;
028: import org.springframework.ws.transport.AbstractReceiverConnection;
029: import org.springframework.ws.transport.EndpointAwareWebServiceConnection;
030: import org.springframework.ws.transport.FaultAwareWebServiceConnection;
031: import org.springframework.ws.transport.WebServiceConnection;
032: import org.springframework.ws.transport.support.EnumerationIterator;
033:
034: /**
035: * Implementation of {@link WebServiceConnection} that is based on the Servlet API.
036: *
037: * @author Arjen Poutsma
038: * @since 1.0.0
039: */
040: public class HttpServletConnection extends AbstractReceiverConnection
041: implements EndpointAwareWebServiceConnection,
042: FaultAwareWebServiceConnection {
043:
044: private final HttpServletRequest httpServletRequest;
045:
046: private final HttpServletResponse httpServletResponse;
047:
048: private boolean statusCodeSet = false;
049:
050: /**
051: * Constructs a new servlet connection with the given <code>HttpServletRequest</code> and
052: * <code>HttpServletResponse</code>.
053: */
054: protected HttpServletConnection(
055: HttpServletRequest httpServletRequest,
056: HttpServletResponse httpServletResponse) {
057: this .httpServletRequest = httpServletRequest;
058: this .httpServletResponse = httpServletResponse;
059: }
060:
061: /** Returns the <code>HttpServletRequest</code> for this connection. */
062: public HttpServletRequest getHttpServletRequest() {
063: return httpServletRequest;
064: }
065:
066: /** Returns the <code>HttpServletResponse</code> for this connection. */
067: public HttpServletResponse getHttpServletResponse() {
068: return httpServletResponse;
069: }
070:
071: public void endpointNotFound() {
072: getHttpServletResponse().setStatus(
073: HttpTransportConstants.STATUS_NOT_FOUND);
074: statusCodeSet = true;
075: }
076:
077: public boolean hasError() throws IOException {
078: return false;
079: }
080:
081: public String getErrorMessage() throws IOException {
082: return null;
083: }
084:
085: /*
086: * Receiving request
087: */
088:
089: protected Iterator getRequestHeaderNames() throws IOException {
090: return new EnumerationIterator(getHttpServletRequest()
091: .getHeaderNames());
092: }
093:
094: protected Iterator getRequestHeaders(String name)
095: throws IOException {
096: return new EnumerationIterator(getHttpServletRequest()
097: .getHeaders(name));
098: }
099:
100: protected InputStream getRequestInputStream() throws IOException {
101: return new FilterInputStream(getHttpServletRequest()
102: .getInputStream()) {
103:
104: public void close() throws IOException {
105: // defer close, some SAAJ implementations (Axis 1) lazy-initialize the SOAPMessage
106: }
107: };
108: }
109:
110: /*
111: * Sending response
112: */
113:
114: protected void addResponseHeader(String name, String value)
115: throws IOException {
116: getHttpServletResponse().addHeader(name, value);
117: }
118:
119: protected OutputStream getResponseOutputStream() throws IOException {
120: return getHttpServletResponse().getOutputStream();
121: }
122:
123: protected void onSendAfterWrite(WebServiceMessage message)
124: throws IOException {
125: statusCodeSet = true;
126: }
127:
128: public void close() throws IOException {
129: if (!statusCodeSet) {
130: getHttpServletResponse().setStatus(
131: HttpTransportConstants.STATUS_ACCEPTED);
132: }
133: }
134:
135: /*
136: * Faults
137: */
138:
139: public boolean hasFault() throws IOException {
140: return false;
141: }
142:
143: public void setFault(boolean fault) throws IOException {
144: if (fault) {
145: getHttpServletResponse()
146: .setStatus(
147: HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR);
148: } else {
149: getHttpServletResponse().setStatus(
150: HttpTransportConstants.STATUS_OK);
151: }
152: statusCodeSet = true;
153: }
154: }
|