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