1
2
3
4
5
6
7
8
9
10
11
12
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
38
39
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
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
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
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
86
87
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 }