View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
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.opensaml.ws.transport.http;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.util.List;
22  
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.opensaml.xml.security.credential.Credential;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Adapts an {@link HttpServletResponse} to an {@link HTTPOutTransport}.
31   */
32  public class HttpServletResponseAdapter implements HTTPOutTransport {
33  
34      /** Adapted servlet response. */
35      private HttpServletResponse httpServletResponse;
36  
37      /** Class logger. */
38      private final Logger log = LoggerFactory.getLogger(HttpServletResponseAdapter.class);
39  
40      /** Whether the peer endpoint has been authenticated. */
41      private boolean peerAuthenticated;
42  
43      /** Whether the HTTP connection is over SSL/TLS. */
44      private boolean secure;
45  
46      /**
47       * Constructor.
48       * 
49       * @param response servlet response to adapt
50       * @param isSecure whether the outbound connection is protected by SSL/TLS
51       */
52      public HttpServletResponseAdapter(HttpServletResponse response, boolean isSecure) {
53          httpServletResponse = response;
54          secure = isSecure;
55      }
56  
57      /**
58       * {@inheritDoc}
59       * 
60       * This method is not supported for this transport implementation and always returns null.
61       */
62      public Object getAttribute(String name) {
63          return null;
64      }
65  
66      /** {@inheritDoc} */
67      public String getCharacterEncoding() {
68          return httpServletResponse.getCharacterEncoding();
69      }
70  
71      /**
72       * {@inheritDoc}
73       * 
74       * This method is not supported for this transport implementation.
75       * 
76       */
77      public String getHeaderValue(String name) {
78          return null;
79      }
80  
81      /**
82       * {@inheritDoc}
83       * 
84       * This method is not supported for this transport implementation.
85       */
86      public String getHTTPMethod() {
87          return null;
88      }
89  
90      /** {@inheritDoc} */
91      public Credential getLocalCredential() {
92          // TODO Auto-generated method stub
93          return null;
94      }
95  
96      /** {@inheritDoc} */
97      public OutputStream getOutgoingStream() {
98          try {
99              return httpServletResponse.getOutputStream();
100         } catch (IOException e) {
101             log.error("Unable to recover input stream from adapted HttpServletResponse", e);
102             return null;
103         }
104     }
105 
106     /**
107      * {@inheritDoc}
108      * 
109      * This method is not supported for this transport implementation.
110      */
111     public String getParameterValue(String name) {
112         return null;
113     }
114 
115     /** {@inheritDoc} */
116     public List<String> getParameterValues(String name) {
117         return null;
118     }
119 
120     /** {@inheritDoc} */
121     public Credential getPeerCredential() {
122         return null;
123     }
124 
125     /**
126      * {@inheritDoc}
127      * 
128      * This method is not supported for this transport implementation.
129      */
130     public int getStatusCode() {
131         return -1;
132     }
133 
134     /**
135      * {@inheritDoc}
136      * 
137      * This method is not supported for this transport implementation.
138      */
139     public HTTP_VERSION getVersion() {
140         return null;
141     }
142 
143     /**
144      * Gets the adapted response.
145      * 
146      * @return adapted response
147      */
148     public HttpServletResponse getWrappedResponse() {
149         return httpServletResponse;
150     }
151 
152     /** {@inheritDoc} */
153     public boolean isAuthenticated() {
154         return peerAuthenticated;
155     }
156 
157     /**
158      * {@inheritDoc}
159      * 
160      * This method is not supported for this transport implementation and always returns false.
161      */
162     public boolean isConfidential() {
163         return secure;
164     }
165 
166     /** {@inheritDoc} */
167     public void sendRedirect(String location) {
168         try {
169             httpServletResponse.sendRedirect(location);
170         } catch (IOException e) {
171             log.error("Unable to send redirect message", e);
172         }
173     }
174 
175     /**
176      * {@inheritDoc}
177      * 
178      * This method is not supported for this transport implementation.
179      */
180     public void setAttribute(String name, Object value) {
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     public void setAuthenticated(boolean isAuthenticated) {
187         peerAuthenticated = isAuthenticated;
188     }
189 
190     /** {@inheritDoc} */
191     public void setCharacterEncoding(String encoding) {
192         httpServletResponse.setCharacterEncoding(encoding);
193     }
194 
195     /**
196      * {@inheritDoc}
197      * 
198      * This method is not supported for this transport implementation.
199      */
200     public void setConfidential(boolean isConfidential) {
201     }
202 
203     /** {@inheritDoc} */
204     public void setHeader(String name, String value) {
205         if (name == null) {
206             return;
207         }
208 
209         // HttpServletRequest requires certain headers be set by special methods
210         if (name.equalsIgnoreCase("Content-Type")) {
211             httpServletResponse.setContentType(value);
212         } else if (name.equalsIgnoreCase("Content-Length")) {
213             httpServletResponse.setContentLength(Integer.parseInt(value));
214         }
215 
216         httpServletResponse.setHeader(name, value);
217     }
218 
219     /**
220      * {@inheritDoc}
221      * 
222      * This method is not supported for this transport implementation.
223      */
224     public void addParameter(String name, String value) {
225     }
226 
227     /** {@inheritDoc} */
228     public void setStatusCode(int code) {
229         httpServletResponse.setStatus(code);
230     }
231 
232     /**
233      * {@inheritDoc}
234      * 
235      * This method is not supported for this transport implementation.
236      */
237     public void setVersion(HTTP_VERSION version) {
238     }
239 
240     /** {@inheritDoc} */
241     public boolean isIntegrityProtected() {
242         return secure;
243     }
244 
245     /** {@inheritDoc} */
246     public void setIntegrityProtected(boolean isIntegrityProtected) {
247 
248     }
249 }