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.InputStream;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.opensaml.ws.security.ServletRequestX509CredentialAdapter;
27  import org.opensaml.xml.security.credential.Credential;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Adapts an {@link HttpServletRequest} to an {@link HTTPInTransport}.
33   */
34  public class HttpServletRequestAdapter implements HTTPInTransport {
35  
36      /** Adapted servlet request. */
37      private HttpServletRequest httpServletRequest;
38  
39      /** Class logger. */
40      private final Logger log = LoggerFactory.getLogger(HttpServletRequestAdapter.class);
41  
42      /** Whether the peer endpoint has been authenticated. */
43      private boolean peerAuthenticated;
44  
45      /** Storage for peer credential adapted from HTTP servlet request. */
46      private Credential peerCredential;
47  
48      /**
49       * Constructor.
50       * 
51       * @param request servlet request to adap
52       */
53      public HttpServletRequestAdapter(HttpServletRequest request) {
54          httpServletRequest = request;
55      }
56  
57      /** {@inheritDoc} */
58      public Object getAttribute(String name) {
59          return httpServletRequest.getAttribute(name);
60      }
61  
62      /** {@inheritDoc} */
63      public String getCharacterEncoding() {
64          return httpServletRequest.getCharacterEncoding();
65      }
66  
67      /** {@inheritDoc} */
68      public String getHeaderValue(String name) {
69          // This appears to be necessary for at least some HttpServletRequest impls
70          if (name.equalsIgnoreCase("Content-Type")) {
71              return httpServletRequest.getContentType();
72          } else if (name.equalsIgnoreCase("Content-Length")) {
73              return Integer.toString(httpServletRequest.getContentLength());
74          }
75          return httpServletRequest.getHeader(name);
76      }
77  
78      /** {@inheritDoc} */
79      public String getHTTPMethod() {
80          return httpServletRequest.getMethod();
81      }
82  
83      /** {@inheritDoc} */
84      public InputStream getIncomingStream() {
85          try {
86              return httpServletRequest.getInputStream();
87          } catch (IOException e) {
88              log.error("Unable to recover input stream from adapted HttpServletRequest", e);
89              return null;
90          }
91      }
92  
93      /** {@inheritDoc} */
94      public Credential getLocalCredential() {
95          // TODO Auto-generated method stub
96          return null;
97      }
98  
99      /** {@inheritDoc} */
100     public String getParameterValue(String name) {
101         return httpServletRequest.getParameter(name);
102 
103     }
104 
105     /** {@inheritDoc} */
106     public List<String> getParameterValues(String name) {
107         ArrayList<String> valuesList = new ArrayList<String>();
108         String[] values = httpServletRequest.getParameterValues(name);
109         if (values != null) {
110             for (String value : values) {
111                 valuesList.add(value);
112             }
113         }
114 
115         return valuesList;
116     }
117 
118     /** {@inheritDoc} */
119     public String getPeerAddress() {
120         return httpServletRequest.getRemoteAddr();
121     }
122 
123     /** {@inheritDoc} */
124     public Credential getPeerCredential() {
125         if (peerCredential == null) {
126             try {
127                 peerCredential = new ServletRequestX509CredentialAdapter(httpServletRequest);
128             } catch (IllegalArgumentException e) {
129                 log.info("Wrapped HTTP servlet request did not contain a client certificate");
130             }
131         }
132         return peerCredential;
133     }
134 
135     /** {@inheritDoc} */
136     public String getPeerDomainName() {
137         return httpServletRequest.getRemoteHost();
138     }
139 
140     /**
141      * {@inheritDoc}
142      * 
143      * This method is not supported for this transport implementation. It always returns -1;
144      */
145     public int getStatusCode() {
146         return 1;
147     }
148 
149     /**
150      * {@inheritDoc}
151      * 
152      * This method is not supported for this transport implementation. It always returns null;
153      */
154     public HTTP_VERSION getVersion() {
155         // unsupported options
156         return null;
157     }
158 
159     /**
160      * Gets the adapted request.
161      * 
162      * @return adapted request
163      */
164     public HttpServletRequest getWrappedRequest() {
165         return httpServletRequest;
166     }
167 
168     /** {@inheritDoc} */
169     public boolean isAuthenticated() {
170         return peerAuthenticated;
171     }
172 
173     /** {@inheritDoc} */
174     public boolean isConfidential() {
175         return httpServletRequest.isSecure();
176     }
177 
178     /** {@inheritDoc} */
179     public void setAuthenticated(boolean isAuthenticated) {
180         peerAuthenticated = isAuthenticated;
181     }
182 
183     /**
184      * {@inheritDoc}
185      * 
186      * This method is not supported for this transport implementation.
187      */
188     public void setConfidential(boolean isConfidential) {
189 
190     }
191     
192     /** {@inheritDoc} */
193     public boolean isIntegrityProtected() {
194         return httpServletRequest.isSecure();
195     }
196     
197     /** {@inheritDoc} */
198     public void setIntegrityProtected(boolean isIntegrityProtected) {
199         
200     }
201 }