001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/WebUtils.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2006 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: 7809 $ 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: 7809 $, $Date: 2007-07-23 13:48:49 +0200 (Mo, 23 Jul 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( HttpServletRequest request ) { 079 080 String s = request.getRequestURL().toString(); 081 String[] parts = org.deegree.framework.util.StringTools.toArray( s, "/", false ); 082 return parts[0] + "//" + parts[1] + request.getContextPath(); 083 084 } 085 086 /** 087 * reads proxyHost and proxyPort from system parameters and sets them to the 088 * 089 * @see HostConfiguration of the passed 090 * @see HttpClient 091 * @param client 092 * @param url 093 * @return HttpClient with proxy configuration 094 */ 095 public static synchronized HttpClient enableProxyUsage( HttpClient client, URL url ) { 096 String host = url.getHost(); 097 String protocol = url.getProtocol().toLowerCase(); 098 HostConfiguration hc = client.getHostConfiguration(); 099 if ( System.getProperty( "http.proxyHost" ) != null && "http".equals( protocol ) ) { 100 String nop = System.getProperty( "http.noProxyHosts" ); 101 if ( nop != null && nop.indexOf( host ) < 0 ) { 102 hc.setProxy( System.getProperty( "http.proxyHost" ), 103 Integer.parseInt( System.getProperty( "http.proxyPort" ) ) ); 104 } 105 } else if ( System.getProperty( "https.proxyHost" ) != null && "https".equals( protocol ) ) { 106 String nop = System.getProperty( "https.noProxyHosts" ); 107 if ( nop != null && nop.indexOf( host ) < 0 ) { 108 hc.setProxy( System.getProperty( "https.proxyHost" ), 109 Integer.parseInt( System.getProperty( "https.proxyPort" ) ) ); 110 } 111 } else if ( System.getProperty( "ftp.proxyHost" ) != null && "ftp".equals( protocol ) ) { 112 String nop = System.getProperty( "ftp.noProxyHosts" ); 113 if ( nop != null && nop.indexOf( host ) < 0 ) { 114 hc.setProxy( System.getProperty( "ftp.proxyHost" ), 115 Integer.parseInt( System.getProperty( "https.proxyPort" ) ) ); 116 } 117 } else if ( System.getProperty( "proxyHost" ) != null ) { 118 String nop = System.getProperty( "noProxyHosts" ); 119 if ( nop != null && nop.indexOf( host ) < 0 ) { 120 hc.setProxy( System.getProperty( "proxyHost" ), Integer.parseInt( System.getProperty( "proxyPort" ) ) ); 121 } 122 } 123 client.setHostConfiguration( hc ); 124 125 return client; 126 } 127 128 /** 129 * returns the charset read from the content type of the passed {@link HttpServletRequest}. If 130 * no content type or no charset is defined the charset read from {@link CharsetUtils} will be 131 * returned instead. So it is gauranteed that the method will not return <code>null</code> 132 * 133 * @param request 134 * @return charset 135 */ 136 public static String readCharsetFromContentType( HttpServletRequest request ) { 137 138 String contentType = request.getHeader( "Content-Type" ); 139 return readCharsetFromContentType( contentType ); 140 } 141 142 /** 143 * @see #readCharsetFromContentType(HttpServletRequest) 144 * @param contentType 145 * @return charset 146 */ 147 public static String readCharsetFromContentType( String contentType ) { 148 String charset = null; 149 if ( contentType != null ) { 150 String[] tmp = StringTools.toArray( contentType, ";", false ); 151 if ( tmp.length == 2 ) { 152 charset = tmp[1].substring( tmp[1].indexOf( '=' ) + 1, tmp[1].length() ); 153 } else { 154 charset = CharsetUtils.getSystemCharset(); 155 } 156 } else { 157 charset = CharsetUtils.getSystemCharset(); 158 } 159 return charset; 160 } 161 162 163 }