View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.servlet.impl;
17  
18  import java.io.IOException;
19  import java.util.Enumeration;
20  
21  import javax.servlet.FilterChain;
22  import javax.servlet.ServletContext;
23  import javax.servlet.http.Cookie;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpSession;
27  
28  import org.apache.http.ProtocolVersion;
29  import org.apache.http.impl.cookie.BasicClientCookie;
30  import org.apache.http.message.BasicLineParser;
31  import org.apache.http.message.BasicRequestLine;
32  import org.esigate.http.IncomingRequest;
33  import org.esigate.servlet.HttpServletRequestContext;
34  import org.esigate.util.UriUtils;
35  
36  /**
37   * Converts the {@link HttpServletRequest} to an {@link IncomingRequest}.
38   * 
39   * @author Francois-Xavier Bonnet
40   * 
41   */
42  public class RequestFactory {
43      private final ServletContext servletContext;
44  
45      public RequestFactory(ServletContext servletContext) {
46          this.servletContext = servletContext;
47      }
48  
49      public IncomingRequest create(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
50              throws IOException {
51          HttpServletRequestContext context =
52                  new HttpServletRequestContext(request, response, servletContext, filterChain);
53          // create request line
54          String uri =
55                  UriUtils.createURI(request.getScheme(), request.getServerName(), request.getServerPort(),
56                          request.getRequestURI(), request.getQueryString(), null);
57          ProtocolVersion protocolVersion = BasicLineParser.parseProtocolVersion(request.getProtocol(), null);
58          IncomingRequest.Builder builder =
59                  IncomingRequest.builder(new BasicRequestLine(request.getMethod(), uri, protocolVersion));
60          builder.setContext(context);
61          // copy headers
62          @SuppressWarnings("rawtypes")
63          Enumeration names = request.getHeaderNames();
64          while (names.hasMoreElements()) {
65              String name = (String) names.nextElement();
66              @SuppressWarnings("rawtypes")
67              Enumeration values = request.getHeaders(name);
68              while (values.hasMoreElements()) {
69                  String value = (String) values.nextElement();
70                  builder.addHeader(name, value);
71              }
72          }
73          // create entity
74          HttpServletRequestEntity entity = new HttpServletRequestEntity(request);
75          builder.setEntity(entity);
76  
77          builder.setRemoteAddr(request.getRemoteAddr());
78          builder.setRemoteUser(request.getRemoteUser());
79          HttpSession session = request.getSession(false);
80          if (session != null) {
81              builder.setSessionId(session.getId());
82          }
83          builder.setUserPrincipal(request.getUserPrincipal());
84  
85          // Copy cookies
86          // As cookie header contains only name=value so we don't need to copy
87          // all attributes!
88          javax.servlet.http.Cookie[] src = request.getCookies();
89          if (src != null) {
90              for (Cookie c : src) {
91                  BasicClientCookie dest = new BasicClientCookie(c.getName(), c.getValue());
92                  builder.addCookie(dest);
93              }
94          }
95          builder.setSession(new HttpServletSession(request));
96          builder.setContextPath(request.getContextPath());
97          return builder.build();
98      }
99  }