1 package org.apache.maven.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;
33 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
36 public class ContextUtils
38 private static final Map defaultSchemePortMap;
42 defaultSchemePortMap = new HashMap();
43 defaultSchemePortMap.put( "http", new Integer( 80 ) );
44 defaultSchemePortMap.put( "https", new Integer( 443 ) );
48 * Using the page context, get the base url.
50 * @param pageContext the page context to use
51 * @return the base url with module name.
53 public static String getBaseURL( PageContext pageContext )
55 return getBaseURL( pageContext, null );
59 * Using the page context, get the base url and append an optional resource name to the end of the provided url.
61 * @param pageContext the page context to use
62 * @param resource the resource name (or null if no resource name specified)
63 * @return the base url with resource name.
65 public static String getBaseURL( PageContext pageContext, String resource )
67 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
68 return getBaseURL( request, resource );
72 * Using the http servlet request, get the base url and append an optional resource name to the end of the url.
74 * @param request the request to use
75 * @param resource the resource name (or null if not resource name should be appended)
76 * @return the base url with resource name.
78 public static String getBaseURL( HttpServletRequest request, String resource )
80 StringBuffer baseUrl = new StringBuffer();
82 baseUrl.append( request.getScheme() ).append( "://" );
83 baseUrl.append( request.getServerName() );
84 int portnum = request.getServerPort();
86 // Only add port if non-standard.
87 Integer defaultPortnum = (Integer) defaultSchemePortMap.get( request.getScheme() );
88 if ( ( defaultPortnum == null ) || ( defaultPortnum.intValue() != portnum ) )
90 baseUrl.append( ":" ).append( String.valueOf( portnum ) );
92 baseUrl.append( request.getContextPath() );
94 if ( StringUtils.isNotBlank( resource ) )
96 if ( !baseUrl.toString().endsWith( "/" ) )
98 baseUrl.append( "/" );
101 baseUrl.append( resource );
104 return baseUrl.toString();