package org.apache.maven.archiva.web.util;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

/**
 * ContextUtils 
 *
 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
 * @version $Id$
 */
public class ContextUtils
{
    private static final Map defaultSchemePortMap;

    static
    {
        defaultSchemePortMap = new HashMap();
        defaultSchemePortMap.put( "http", new Integer( 80 ) );
        defaultSchemePortMap.put( "https", new Integer( 443 ) );
    }

    /**
     * Using the page context, get the base url.
     * 
     * @param pageContext the page context to use
     * @return the base url with module name.
     */
    public static String getBaseURL( PageContext pageContext )
    {
        return getBaseURL( pageContext, null );
    }

    /**
     * Using the page context, get the base url and append an optional resource name to the end of the provided url.
     * 
     * @param pageContext the page context to use
     * @param resource the resource name (or null if no resource name specified)
     * @return the base url with resource name.
     */
    public static String getBaseURL( PageContext pageContext, String resource )
    {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        return getBaseURL( request, resource );
    }
    
    /**
     * Using the http servlet request, get the base url and append an optional resource name to the end of the url.
     * 
     * @param request the request to use
     * @param resource the resource name (or null if not resource name should be appended)
     * @return the base url with resource name.
     */
    public static String getBaseURL( HttpServletRequest request, String resource )
    {
        StringBuffer baseUrl = new StringBuffer();
        
        baseUrl.append( request.getScheme() ).append( "://" );
        baseUrl.append( request.getServerName() );
        int portnum = request.getServerPort();

        // Only add port if non-standard.
        Integer defaultPortnum = (Integer) defaultSchemePortMap.get( request.getScheme() );
        if ( ( defaultPortnum == null ) || ( defaultPortnum.intValue() != portnum ) )
        {
            baseUrl.append( ":" ).append( String.valueOf( portnum ) );
        }
        baseUrl.append( request.getContextPath() );

        if ( StringUtils.isNotBlank( resource ) )
        {
            if ( !baseUrl.toString().endsWith( "/" ) )
            {
                baseUrl.append( "/" );
            }

            baseUrl.append( resource );
        }

        return baseUrl.toString();
    }
}

@@ -2815,35 +2815,590 @@
 nd( 
-request.getServerName() );%0A
+getServerName( request ) );%0A        baseUrl.append( request.getContextPath() );%0A%0A        if ( StringUtils.isNotBlank( resource ) )%0A        %7B%0A            if ( !baseUrl.toString().endsWith( %22/%22 ) )%0A            %7B%0A                baseUrl.append( %22/%22 );%0A            %7D%0A%0A            baseUrl.append( resource );%0A        %7D%0A%0A        return baseUrl.toString();%0A    %7D%0A%0A    private static String getServerName( HttpServletRequest request )%0A    %7B%0A        String name = request.getHeader( %22X-Forwarded-Host%22 );%0A        if ( name == null )%0A        %7B%0A            name = request.getServerName();%0A    
     
@@ -3449,16 +3449,20 @@
         
+  
+  
 // Only 
@@ -3487,16 +3487,20 @@
 andard.%0A
+    
         
@@ -3587,24 +3587,28 @@
  );%0A        
+  
+  
 if ( ( defau
@@ -3673,32 +3673,36 @@
 num ) )%0A        
+    
 %7B%0A            ba
@@ -3703,37 +3703,31 @@
     
-baseUrl.append( %22:%22 ).append(
+    name = name + %22:%22 +
  Str
@@ -3746,26 +3746,24 @@
  portnum
- )
  );%0A    
     %7D%0A  
@@ -3758,22 +3758,16 @@
     
-    %7D%0A
         
 base
@@ -3766,274 +3766,33 @@
     
-baseUrl.append( request.getContextPath() );%0A%0A        if ( StringUtils.isNotBlank( resource ) )%0A        %7B%0A            if ( !baseUrl.toString().endsWith( %22/%22 ) )%0A            %7B%0A                baseUrl.append( %22/%22 );%0A            %7D%0A%0A            baseUrl.append( resource )
+%7D%0A            return name
 ;%0A  
@@ -3791,33 +3791,32 @@
 name;%0A        %7D%0A
-%0A
         return b
@@ -3814,34 +3814,20 @@
  return 
-baseUrl.toString()
+name
 ;%0A    %7D%0A

use Apache 2.0 mod_proxy header if available for those that don't want to use ProxyPreserveHost