KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > util > ServletUtils


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.util;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import java.io.File JavaDoc;
25
26 public class ServletUtils {
27
28   // constructors /////////////////////////////////////////////////////////////
29

30   // constants ////////////////////////////////////////////////////////////////
31

32   // classes //////////////////////////////////////////////////////////////////
33

34   // methods //////////////////////////////////////////////////////////////////
35

36   /**
37    * NOT UNIT TESTED Returns the URL (including query parameters) minus the scheme, host, and
38    * context path. This method probably be moved to a more general purpose
39    * class.
40    */

41   public static String JavaDoc getRelativeUrl(
42     HttpServletRequest JavaDoc request ) {
43
44     String JavaDoc baseUrl = null;
45
46     if ( ( request.getServerPort() == 80 ) ||
47          ( request.getServerPort() == 443 ) )
48       baseUrl =
49         request.getScheme() + "://" +
50         request.getServerName() +
51         request.getContextPath();
52     else
53       baseUrl =
54         request.getScheme() + "://" +
55         request.getServerName() + ":" + request.getServerPort() +
56         request.getContextPath();
57
58     StringBuffer JavaDoc buf = request.getRequestURL();
59
60     if ( request.getQueryString() != null ) {
61       buf.append( "?" );
62       buf.append( request.getQueryString() );
63     }
64
65     return buf.substring( baseUrl.length() );
66   }
67
68   /**
69    * NOT UNIT TESTED Returns the base url (e.g, <tt>http://myhost:8080/myapp</tt>) suitable for
70    * using in a base tag or building reliable urls.
71    */

72   public static String JavaDoc getBaseUrl( HttpServletRequest JavaDoc request ) {
73     if ( ( request.getServerPort() == 80 ) ||
74          ( request.getServerPort() == 443 ) )
75       return request.getScheme() + "://" +
76              request.getServerName() +
77              request.getContextPath();
78     else
79       return request.getScheme() + "://" +
80              request.getServerName() + ":" + request.getServerPort() +
81              request.getContextPath();
82   }
83
84   /**
85    * Returns the file specified by <tt>path</tt> as returned by
86    * <tt>ServletContext.getRealPath()</tt>.
87    */

88   public static File JavaDoc getRealFile(
89     HttpServletRequest JavaDoc request,
90     String JavaDoc path ) {
91
92     return
93       new File JavaDoc( request.getSession().getServletContext().getRealPath( path ) );
94   }
95
96   // properties ///////////////////////////////////////////////////////////////
97

98   // attributes ///////////////////////////////////////////////////////////////
99
}
100
Popular Tags