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