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.http;
17  
18  import java.security.Principal;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.http.HttpEntity;
25  import org.apache.http.HttpVersion;
26  import org.apache.http.RequestLine;
27  import org.apache.http.cookie.Cookie;
28  import org.apache.http.message.BasicHttpEntityEnclosingRequest;
29  import org.apache.http.message.BasicRequestLine;
30  import org.esigate.api.ContainerRequestContext;
31  import org.esigate.api.Session;
32  
33  /**
34   * Represents an incoming http request.
35   * 
36   * @author fxbonnet
37   * 
38   */
39  public final class IncomingRequest extends BasicHttpEntityEnclosingRequest {
40  
41      private final Map<String, Object> attributes = new HashMap<>();
42      private ContainerRequestContext context;
43      private String remoteUser;
44      private String remoteAddr;
45      private String sessionId;
46      private Principal userPrincipal;
47      private List<Cookie> cookies = new ArrayList<>();
48      private List<Cookie> newCookies = new ArrayList<>();
49      private Session session;
50      private String contextPath;
51  
52      public static final class Builder {
53          private final IncomingRequest result;
54  
55          private Builder(RequestLine requestline) {
56              result = new IncomingRequest(requestline);
57          }
58  
59          public IncomingRequest build() {
60              return result;
61          }
62  
63          public Builder setContext(ContainerRequestContext context) {
64              result.context = context;
65              return this;
66          }
67  
68          public Builder setRemoteAddr(String remoteAddr) {
69              result.remoteAddr = remoteAddr;
70              return this;
71          }
72  
73          public Builder setRemoteUser(String remoteUser) {
74              result.remoteUser = remoteUser;
75              return this;
76          }
77  
78          public Builder setSessionId(String sessionId) {
79              result.sessionId = sessionId;
80              return this;
81          }
82  
83          public Builder setUserPrincipal(Principal userPrincipal) {
84              result.userPrincipal = userPrincipal;
85              return this;
86          }
87  
88          public Builder addCookie(Cookie cookie) {
89              result.cookies.add(cookie);
90              return this;
91          }
92  
93          public Builder setSession(Session session) {
94              result.session = session;
95              return this;
96          }
97  
98          public Builder addHeader(String name, String value) {
99              result.addHeader(name, value);
100             return this;
101         }
102 
103         public Builder setEntity(HttpEntity entity) {
104             result.setEntity(entity);
105             return this;
106         }
107 
108         /**
109          * A String to remove before mapping at the begining of the path, typically used for the context path in a
110          * servlet engine.
111          * 
112          * @param contextPath
113          *            the context path
114          * @return the {@link Builder}
115          */
116         public Builder setContextPath(String contextPath) {
117             result.contextPath = contextPath;
118             return this;
119         }
120 
121     }
122 
123     public static Builder builder(RequestLine requestline) {
124         return new Builder(requestline);
125     }
126 
127     public static Builder builder(String uri) {
128         return new Builder(new BasicRequestLine("GET", uri, HttpVersion.HTTP_1_1));
129     }
130 
131     private IncomingRequest(RequestLine requestline) {
132         super(requestline);
133     }
134 
135     public <T> T getAttribute(String name) {
136         return (T) attributes.get(name);
137     }
138 
139     public void setAttribute(String name, Object value) {
140         attributes.put(name, value);
141     }
142 
143     public ContainerRequestContext getContext() {
144         return context;
145     }
146 
147     public String getRemoteUser() {
148         return remoteUser;
149     }
150 
151     public String getRemoteAddr() {
152         return remoteAddr;
153     }
154 
155     public String getSessionId() {
156         return sessionId;
157     }
158 
159     public Principal getUserPrincipal() {
160         return userPrincipal;
161     }
162 
163     public Cookie[] getCookies() {
164         return cookies.toArray(new Cookie[cookies.size()]);
165     }
166 
167     public Cookie[] getNewCookies() {
168         return newCookies.toArray(new Cookie[newCookies.size()]);
169     }
170 
171     public void addNewCookie(Cookie cookie) {
172         newCookies.add(cookie);
173     }
174 
175     public Session getSession() {
176         return session;
177     }
178 
179     public String getContextPath() {
180         return contextPath;
181     }
182 
183 }