01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.ows.util;
06:
07: import java.net.URI;
08: import java.net.URISyntaxException;
09:
10: import javax.servlet.http.HttpServletRequest;
11:
12: import org.geoserver.platform.GeoServerExtensions;
13:
14: /**
15: * Utility class performing operations related to http requests.
16: *
17: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
18: *
19: * TODO: this class needs to be merged with org.vfny.geoserver.Requests.
20: */
21: public class RequestUtils {
22: /**
23: * Returns the url which is hte base of schemas stored / served by
24: * geoserver.
25: * <p>
26: * This method returns:
27: * <pre>
28: * <code>
29: * baseURL( req ) + "schemas/"
30: * </code>
31: * </pre>
32: * </p>
33: *
34: * @return A String of the form "<scheme>://<server>:<port>/<context>/schemas/"
35: */
36: public static String schemaBaseURL(HttpServletRequest req) {
37: return baseURL(req) + "schemas/";
38: }
39:
40: /**
41: * Pulls out the base url ( from the client point of view ), from the
42: * given request object.
43: *
44: * @return A String of the form "<scheme>://<server>:<port>/<context>/"
45: *
46: */
47: public static String baseURL(HttpServletRequest req) {
48: String url = req.getScheme() + "://" + req.getServerName()
49: + ":" + req.getServerPort() + req.getContextPath()
50: + "/";
51:
52: return url;
53: }
54:
55: /**
56: * Given a base URL and a proxy url (which may or may-not be null)
57: * this method grafts the two together so that the proper 'proxified' or 'non-proxified' url is returned
58: *
59: */
60: public static String proxifiedBaseURL(String baseUrl,
61: String proxyBase) {
62: if (proxyBase == null || proxyBase.trim().length() == 0) {
63: if (!baseUrl.endsWith("/"))
64: baseUrl += "/";
65: return baseUrl;
66: }
67:
68: try {
69: URI baseUri = new URI(baseUrl);
70: if (proxyBase.endsWith("/"))
71: proxyBase = proxyBase.substring(0,
72: proxyBase.length() - 1);
73:
74: String proxifiedBaseUrl = proxyBase + baseUri.getPath();
75: if (!proxifiedBaseUrl.endsWith("/"))
76: proxifiedBaseUrl += "/";
77:
78: return proxifiedBaseUrl;
79: } catch (URISyntaxException urise) {
80: //hmm...guess the proxy base must be invalid
81: throw new RuntimeException(
82: "Invalid Proxy Base URL property is set in your GeoServer installation.",
83: urise);
84: }
85: }
86: }
|