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.impl;
17  
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  
21  import org.apache.http.HttpStatus;
22  import org.esigate.Driver;
23  import org.esigate.HttpErrorPage;
24  import org.esigate.UserContext;
25  import org.esigate.api.ContainerRequestContext;
26  import org.esigate.http.IncomingRequest;
27  import org.esigate.util.UriUtils;
28  
29  /**
30   * A request to be executed by a given {@link Driver} instance.
31   * 
32   * @author Francois-Xavier Bonnet
33   * 
34   */
35  public class DriverRequest {
36      private final IncomingRequest wrappedRequest;
37      private final Driver driver;
38      private final UserContext userContext;
39      private final URL baseUrl;
40      private final String visibleBaseUrl;
41      private final boolean external;
42      private String characterEncoding;
43  
44      /**
45       * 
46       * @param request
47       * @param driver
48       * @param relUrl
49       * @throws HttpErrorPage
50       */
51      public DriverRequest(IncomingRequest request, Driver driver, String relUrl) throws HttpErrorPage {
52          this.wrappedRequest = request;
53          this.driver = driver;
54          this.external = UriUtils.isAbsolute(relUrl);
55          this.userContext = new UserContext(request, driver.getConfiguration().getInstanceName());
56          try {
57              this.baseUrl = new URL(driver.getConfiguration().getBaseUrlRetrieveStrategy().getBaseURL(request));
58          } catch (MalformedURLException e) {
59              throw new HttpErrorPage(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal server error", e);
60          }
61          String visibleBase = driver.getConfiguration().getVisibleBaseURL();
62          if (visibleBase == null) {
63              String requestUri = request.getRequestLine().getUri();
64              requestUri = UriUtils.removeQuerystring(requestUri);
65              if (!this.external && requestUri.endsWith(relUrl)) {
66                  visibleBase = requestUri.substring(0, requestUri.length() - relUrl.length());
67              } else {
68                  visibleBase = this.baseUrl.toString();
69              }
70          }
71          this.visibleBaseUrl = UriUtils.rewriteURI(visibleBase, UriUtils.extractHost(request.getRequestLine().getUri()));
72      }
73  
74      /**
75       * Returns the driver instance that handles this request.
76       * 
77       * @return the driver instance
78       */
79      public Driver getDriver() {
80          return driver;
81      }
82  
83      /**
84       * Returns the user context associated with this request.
85       * 
86       * @return the user context
87       */
88      public UserContext getUserContext() {
89          return userContext;
90      }
91  
92      /**
93       * Returns the base url selected to send this request.
94       * 
95       * @return the base url
96       */
97      public URL getBaseUrl() {
98          return baseUrl;
99      }
100 
101     /**
102      * Returns the charset used to encode and decode the request parameters.
103      * 
104      * @return the charset
105      */
106     public String getCharacterEncoding() {
107         return characterEncoding;
108     }
109 
110     /**
111      * Sets the charset to use to encode and decode the parameters for this request.
112      * 
113      * @param characterEncoding
114      *            the charset name
115      */
116     public void setCharacterEncoding(String characterEncoding) {
117         this.characterEncoding = characterEncoding;
118     }
119 
120     /**
121      * Returns the context specifit to the container (servlet container for instance).
122      * 
123      * @return the context
124      */
125     public ContainerRequestContext getContext() {
126         return wrappedRequest.getContext();
127     }
128 
129     /**
130      * Returns the original request received from the client.
131      * 
132      * @return the original request
133      */
134     public IncomingRequest getOriginalRequest() {
135         return wrappedRequest;
136     }
137 
138     /**
139      * Returns true if the target url is not one the provider application.
140      * 
141      * @return true if the url is on the provider
142      */
143     public boolean isExternal() {
144         return external;
145     }
146 
147     /**
148      * Returns the base url as seen by the client.
149      * 
150      * @return the visible base url
151      */
152     public String getVisibleBaseUrl() {
153         return visibleBaseUrl;
154     }
155 
156 }