1 package org.apache.archiva.web.util;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
24 import java.util.HashMap;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.jsp.PageContext;
35 public class ContextUtils
37 private static final Map<String, Integer> defaultSchemePortMap;
41 defaultSchemePortMap = new HashMap<String, Integer>();
42 defaultSchemePortMap.put( "http", Integer.valueOf( 80 ) );
43 defaultSchemePortMap.put( "https", Integer.valueOf( 443 ) );
47 * Using the page context, get the base url.
49 * @param pageContext the page context to use
50 * @return the base url with module name.
52 public static String getBaseURL( PageContext pageContext )
54 return getBaseURL( pageContext, null );
58 * Using the page context, get the base url and append an optional resource name to the end of the provided url.
60 * @param pageContext the page context to use
61 * @param resource the resource name (or null if no resource name specified)
62 * @return the base url with resource name.
64 public static String getBaseURL( PageContext pageContext, String resource )
66 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
67 return getBaseURL( request, resource );
71 * Using the http servlet request, get the base url and append an optional resource name to the end of the url.
73 * @param request the request to use
74 * @param resource the resource name (or null if not resource name should be appended)
75 * @return the base url with resource name.
77 public static String getBaseURL( HttpServletRequest request, String resource )
79 StringBuilder baseUrl = new StringBuilder();
81 baseUrl.append( request.getScheme() ).append( "://" );
82 baseUrl.append( getServerName( request ) );
83 baseUrl.append( request.getContextPath() );
85 if ( StringUtils.isNotBlank( resource ) )
87 if ( !baseUrl.toString().endsWith( "/" ) )
89 baseUrl.append( "/" );
92 baseUrl.append( resource );
95 return baseUrl.toString();
98 private static String getServerName( HttpServletRequest request )
100 String name = request.getHeader( "X-Forwarded-Host" );
103 name = request.getServerName();
104 int portnum = request.getServerPort();
106 // Only add port if non-standard.
107 Integer defaultPortnum = (Integer) defaultSchemePortMap.get( request.getScheme() );
108 if ( ( defaultPortnum == null ) || ( defaultPortnum.intValue() != portnum ) )
110 name = name + ":" + String.valueOf( portnum );
116 // respect chains of proxies, return first one (as it's the outermost visible one)
117 String[] hosts = name.split( "," );
118 name = hosts[0].trim();