RequestFactory.java

  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. package org.esigate.servlet.impl;

  16. import java.io.IOException;
  17. import java.util.Enumeration;

  18. import javax.servlet.FilterChain;
  19. import javax.servlet.ServletContext;
  20. import javax.servlet.http.Cookie;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import javax.servlet.http.HttpSession;

  24. import org.apache.http.ProtocolVersion;
  25. import org.apache.http.impl.cookie.BasicClientCookie;
  26. import org.apache.http.message.BasicLineParser;
  27. import org.apache.http.message.BasicRequestLine;
  28. import org.esigate.http.IncomingRequest;
  29. import org.esigate.servlet.HttpServletRequestContext;
  30. import org.esigate.util.UriUtils;

  31. /**
  32.  * Converts the {@link HttpServletRequest} to an {@link IncomingRequest}.
  33.  *
  34.  * @author Francois-Xavier Bonnet
  35.  *
  36.  */
  37. public class RequestFactory {
  38.     private final ServletContext servletContext;

  39.     public RequestFactory(ServletContext servletContext) {
  40.         this.servletContext = servletContext;
  41.     }

  42.     public IncomingRequest create(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  43.             throws IOException {
  44.         HttpServletRequestContext context =
  45.                 new HttpServletRequestContext(request, response, servletContext, filterChain);
  46.         // create request line
  47.         String uri =
  48.                 UriUtils.createURI(request.getScheme(), request.getServerName(), request.getServerPort(),
  49.                         request.getRequestURI(), request.getQueryString(), null);
  50.         ProtocolVersion protocolVersion = BasicLineParser.parseProtocolVersion(request.getProtocol(), null);
  51.         IncomingRequest.Builder builder =
  52.                 IncomingRequest.builder(new BasicRequestLine(request.getMethod(), uri, protocolVersion));
  53.         builder.setContext(context);
  54.         // copy headers
  55.         @SuppressWarnings("rawtypes")
  56.         Enumeration names = request.getHeaderNames();
  57.         while (names.hasMoreElements()) {
  58.             String name = (String) names.nextElement();
  59.             @SuppressWarnings("rawtypes")
  60.             Enumeration values = request.getHeaders(name);
  61.             while (values.hasMoreElements()) {
  62.                 String value = (String) values.nextElement();
  63.                 builder.addHeader(name, value);
  64.             }
  65.         }
  66.         // create entity
  67.         HttpServletRequestEntity entity = new HttpServletRequestEntity(request);
  68.         builder.setEntity(entity);

  69.         builder.setRemoteAddr(request.getRemoteAddr());
  70.         builder.setRemoteUser(request.getRemoteUser());
  71.         HttpSession session = request.getSession(false);
  72.         if (session != null) {
  73.             builder.setSessionId(session.getId());
  74.         }
  75.         builder.setUserPrincipal(request.getUserPrincipal());

  76.         // Copy cookies
  77.         // As cookie header contains only name=value so we don't need to copy
  78.         // all attributes!
  79.         javax.servlet.http.Cookie[] src = request.getCookies();
  80.         if (src != null) {
  81.             for (Cookie c : src) {
  82.                 BasicClientCookie dest = new BasicClientCookie(c.getName(), c.getValue());
  83.                 builder.addCookie(dest);
  84.             }
  85.         }
  86.         builder.setSession(new HttpServletSession(request));
  87.         builder.setContextPath(request.getContextPath());
  88.         return builder.build();
  89.     }
  90. }