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 org.esigate.Parameters;
19  import org.esigate.impl.DriverRequest;
20  import org.esigate.util.UriUtils;
21  
22  /**
23   * Utility class to generate URL and path for Resources.
24   * 
25   * @author Francois-Xavier Bonnet
26   */
27  public final class ResourceUtils {
28  
29      /**
30       * Private constructor
31       */
32      private ResourceUtils() {
33  
34      }
35  
36      private static String buildQueryString(DriverRequest originalRequest, boolean proxy) {
37          StringBuilder queryString = new StringBuilder(Parameters.SMALL_BUFFER_SIZE);
38  
39          String originalQuerystring =
40                  UriUtils.getRawQuery(originalRequest.getOriginalRequest().getRequestLine().getUri());
41          if (proxy && originalQuerystring != null) {
42              // Remove jsessionid from request if it is present
43              // As we are in a java application, the container might add
44              // jsessionid to the querystring. We must not forward it to
45              // included applications.
46              String jsessionid;
47              jsessionid = originalRequest.getOriginalRequest().getSessionId();
48              if (jsessionid != null) {
49                  originalQuerystring = UriUtils.removeSessionId(jsessionid, originalQuerystring);
50              }
51              queryString.append(originalQuerystring);
52          }
53          return queryString.toString();
54      }
55  
56      private static String concatUrl(String baseUrl, String relUrl) {
57          StringBuilder url = new StringBuilder(Parameters.SMALL_BUFFER_SIZE);
58          if (baseUrl != null && relUrl != null && (baseUrl.endsWith("/") || baseUrl.endsWith("\\"))
59                  && relUrl.startsWith("/")) {
60              url.append(baseUrl.substring(0, baseUrl.length() - 1)).append(relUrl);
61          } else {
62              url.append(baseUrl).append(relUrl);
63          }
64          return url.toString();
65      }
66  
67      public static String getHttpUrlWithQueryString(String url, DriverRequest originalRequest, boolean proxy) {
68          if (!url.startsWith("http://") && !url.startsWith("https://")) {
69              // Relative URL, we need to add the driver base url
70              String baseUrl = originalRequest.getBaseUrl().toString();
71              if (baseUrl != null) {
72                  url = concatUrl(baseUrl, url);
73              }
74          }
75          String queryString = ResourceUtils.buildQueryString(originalRequest, proxy);
76          if (queryString.length() == 0) {
77              return url;
78          } else {
79              return url + "?" + queryString;
80          }
81      }
82  
83  }