001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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 static java.lang.Integer.parseInt; 046 import static java.lang.System.getProperty; 047 import static java.util.Arrays.asList; 048 import static org.deegree.framework.log.LoggerFactory.getLogger; 049 050 import java.net.URL; 051 import java.util.TreeSet; 052 053 import javax.servlet.http.HttpServletRequest; 054 055 import org.apache.commons.httpclient.HostConfiguration; 056 import org.apache.commons.httpclient.HttpClient; 057 import org.deegree.framework.log.ILogger; 058 import org.deegree.framework.util.CharsetUtils; 059 import org.deegree.framework.util.StringTools; 060 061 /** 062 * 063 * 064 * 065 * @version $Revision: 11814 $ 066 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 067 * @author last edited by: $Author: aschmitz $ 068 * 069 * @version 1.0. $Revision: 11814 $, $Date: 2008-05-20 10:53:17 +0200 (Di, 20 Mai 2008) $ 070 * 071 * @since 2.0 072 */ 073 public class WebUtils { 074 075 private static final ILogger LOG = getLogger( WebUtils.class ); 076 077 /** 078 * returns the root path (address - port - context) of a web application read from the passed 079 * request<br> 080 * e.g. http://myserver:8080/deegree/services?request=GetCapabilites ... <br> 081 * will return 'http://myserver:8080/deegree' 082 * 083 * @param request 084 * @return the root path (address - port - context) of a web application read from the passed 085 * request 086 */ 087 public static String getAbsoluteContextPath( HttpServletRequest request ) { 088 089 String s = request.getRequestURL().toString(); 090 String[] parts = org.deegree.framework.util.StringTools.toArray( s, "/", false ); 091 return parts[0] + "//" + parts[1] + request.getContextPath(); 092 093 } 094 095 private static void handleProxies( String protocol, HttpClient client, String host ) { 096 TreeSet<String> nops = new TreeSet<String>(); 097 098 String proxyHost = getProperty( ( protocol == null ? "" : protocol + "." ) + "proxyHost" ); 099 100 if ( proxyHost != null ) { 101 String nop = getProperty( ( protocol == null ? "" : protocol + "." ) + "noProxyHosts" ); 102 if ( nop != null && !nop.equals( "" ) ) { 103 nops.addAll( asList( nop.split( "\\|" ) ) ); 104 } 105 nop = getProperty( ( protocol == null ? "" : protocol + "." ) + "nonProxyHosts" ); 106 if ( nop != null && !nop.equals( "" ) ) { 107 nops.addAll( asList( nop.split( "\\|" ) ) ); 108 } 109 110 int proxyPort = parseInt( getProperty( ( protocol == null ? "" : protocol + "." ) + "proxyPort" ) ); 111 112 HostConfiguration hc = client.getHostConfiguration(); 113 114 if ( LOG.isDebug() ) { 115 LOG.logDebug( "Found the following no- and nonProxyHosts", nops ); 116 } 117 118 if ( !nops.contains( host ) ) { 119 if ( LOG.isDebug() ) { 120 LOG.logDebug( "Using proxy " + proxyHost + ":" + proxyPort ); 121 if ( protocol == null ) { 122 LOG.logDebug( "This overrides the protocol specific settings, if there were any." ); 123 } 124 } 125 hc.setProxy( proxyHost, proxyPort ); 126 client.setHostConfiguration( hc ); 127 } else { 128 if ( LOG.isDebug() ) { 129 LOG.logDebug( "Proxy was set, but " + host + " was contained in the no-/nonProxyList!" ); 130 if ( protocol == null ) { 131 LOG.logDebug( "If a protocol specific proxy has been set, it will be used anyway!" ); 132 } 133 } 134 } 135 } 136 137 if ( protocol != null ) { 138 handleProxies( null, client, host ); 139 } 140 } 141 142 /** 143 * reads proxyHost and proxyPort from system parameters and sets them to the passed HttpClient 144 * instance 145 * 146 * @see HostConfiguration of the passed 147 * @see HttpClient 148 * @param client 149 * @param url 150 * @return HttpClient with proxy configuration 151 */ 152 public static HttpClient enableProxyUsage( HttpClient client, URL url ) { 153 String host = url.getHost(); 154 String protocol = url.getProtocol().toLowerCase(); 155 handleProxies( protocol, client, host ); 156 return client; 157 } 158 159 /** 160 * returns the charset read from the content type of the passed {@link HttpServletRequest}. If 161 * no content type or no charset is defined the charset read from {@link CharsetUtils} will be 162 * returned instead. So it is gauranteed that the method will not return <code>null</code> 163 * 164 * @param request 165 * @return charset 166 */ 167 public static String readCharsetFromContentType( HttpServletRequest request ) { 168 169 String contentType = request.getHeader( "Content-Type" ); 170 return readCharsetFromContentType( contentType ); 171 } 172 173 /** 174 * @see #readCharsetFromContentType(HttpServletRequest) 175 * @param contentType 176 * @return charset 177 */ 178 public static String readCharsetFromContentType( String contentType ) { 179 String charset = null; 180 if ( contentType != null ) { 181 String[] tmp = StringTools.toArray( contentType, ";", false ); 182 if ( tmp.length == 2 ) { 183 charset = tmp[1].substring( tmp[1].indexOf( '=' ) + 1, tmp[1].length() ); 184 } else { 185 charset = CharsetUtils.getSystemCharset(); 186 } 187 } else { 188 charset = CharsetUtils.getSystemCharset(); 189 } 190 return charset; 191 } 192 193 }