001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/enterprise/WebUtils.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.enterprise;
044:
045: import java.net.URL;
046:
047: import javax.servlet.http.HttpServletRequest;
048:
049: import org.apache.commons.httpclient.HostConfiguration;
050: import org.apache.commons.httpclient.HttpClient;
051: import org.deegree.framework.util.CharsetUtils;
052: import org.deegree.framework.util.StringTools;
053:
054: /**
055: *
056: *
057: *
058: * @version $Revision: 9338 $
059: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
060: * @author last edited by: $Author: apoth $
061: *
062: * @version 1.0. $Revision: 9338 $, $Date: 2007-12-27 04:31:31 -0800 (Thu, 27 Dec 2007) $
063: *
064: * @since 2.0
065: */
066: public class WebUtils {
067:
068: /**
069: * returns the root path (address - port - context) of a web application read from the passed
070: * request<br>
071: * e.g. http://myserver:8080/deegree/services?request=GetCapabilites ... <br>
072: * will return 'http://myserver:8080/deegree'
073: *
074: * @param request
075: * @return the root path (address - port - context) of a web application read from the passed
076: * request
077: */
078: public static String getAbsoluteContextPath(
079: HttpServletRequest request) {
080:
081: String s = request.getRequestURL().toString();
082: String[] parts = org.deegree.framework.util.StringTools
083: .toArray(s, "/", false);
084: return parts[0] + "//" + parts[1] + request.getContextPath();
085:
086: }
087:
088: /**
089: * reads proxyHost and proxyPort from system parameters and sets them to the passed HttpClient
090: * instance
091: *
092: * @see HostConfiguration of the passed
093: * @see HttpClient
094: * @param client
095: * @param url
096: * @return HttpClient with proxy configuration
097: */
098: public static synchronized HttpClient enableProxyUsage(
099: HttpClient client, URL url) {
100: String host = url.getHost();
101: String protocol = url.getProtocol().toLowerCase();
102: HostConfiguration hc = client.getHostConfiguration();
103: if (System.getProperty("http.proxyHost") != null
104: && "http".equals(protocol)) {
105: String nop = System.getProperty("http.noProxyHosts");
106: if (nop != null && nop.indexOf(host) < 0) {
107: hc.setProxy(System.getProperty("http.proxyHost"),
108: Integer.parseInt(System
109: .getProperty("http.proxyPort")));
110: }
111: } else if (System.getProperty("https.proxyHost") != null
112: && "https".equals(protocol)) {
113: String nop = System.getProperty("https.noProxyHosts");
114: if (nop != null && nop.indexOf(host) < 0) {
115: hc.setProxy(System.getProperty("https.proxyHost"),
116: Integer.parseInt(System
117: .getProperty("https.proxyPort")));
118: }
119: } else if (System.getProperty("ftp.proxyHost") != null
120: && "ftp".equals(protocol)) {
121: String nop = System.getProperty("ftp.noProxyHosts");
122: if (nop != null && nop.indexOf(host) < 0) {
123: hc.setProxy(System.getProperty("ftp.proxyHost"),
124: Integer.parseInt(System
125: .getProperty("https.proxyPort")));
126: }
127: } else if (System.getProperty("proxyHost") != null) {
128: String nop = System.getProperty("noProxyHosts");
129: if (nop != null && nop.indexOf(host) < 0) {
130: hc.setProxy(System.getProperty("proxyHost"), Integer
131: .parseInt(System.getProperty("proxyPort")));
132: }
133: }
134: client.setHostConfiguration(hc);
135:
136: return client;
137: }
138:
139: /**
140: * returns the charset read from the content type of the passed {@link HttpServletRequest}. If
141: * no content type or no charset is defined the charset read from {@link CharsetUtils} will be
142: * returned instead. So it is gauranteed that the method will not return <code>null</code>
143: *
144: * @param request
145: * @return charset
146: */
147: public static String readCharsetFromContentType(
148: HttpServletRequest request) {
149:
150: String contentType = request.getHeader("Content-Type");
151: return readCharsetFromContentType(contentType);
152: }
153:
154: /**
155: * @see #readCharsetFromContentType(HttpServletRequest)
156: * @param contentType
157: * @return charset
158: */
159: public static String readCharsetFromContentType(String contentType) {
160: String charset = null;
161: if (contentType != null) {
162: String[] tmp = StringTools.toArray(contentType, ";", false);
163: if (tmp.length == 2) {
164: charset = tmp[1].substring(tmp[1].indexOf('=') + 1,
165: tmp[1].length());
166: } else {
167: charset = CharsetUtils.getSystemCharset();
168: }
169: } else {
170: charset = CharsetUtils.getSystemCharset();
171: }
172: return charset;
173: }
174:
175: }
|