01: /*
02: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
03: *
04: * This file is part of TransferCM.
05: *
06: * TransferCM is free software; you can redistribute it and/or modify it under the
07: * terms of the GNU General Public License as published by the Free Software
08: * Foundation; either version 2 of the License, or (at your option) any later
09: * 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;
24: import java.io.File;
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 getRelativeUrl(HttpServletRequest request) {
42:
43: String baseUrl = null;
44:
45: if ((request.getServerPort() == 80)
46: || (request.getServerPort() == 443))
47: baseUrl = request.getScheme() + "://"
48: + request.getServerName()
49: + request.getContextPath();
50: else
51: baseUrl = request.getScheme() + "://"
52: + request.getServerName() + ":"
53: + request.getServerPort()
54: + request.getContextPath();
55:
56: StringBuffer buf = request.getRequestURL();
57:
58: if (request.getQueryString() != null) {
59: buf.append("?");
60: buf.append(request.getQueryString());
61: }
62:
63: return buf.substring(baseUrl.length());
64: }
65:
66: /**
67: * NOT UNIT TESTED Returns the base url (e.g, <tt>http://myhost:8080/myapp</tt>) suitable for
68: * using in a base tag or building reliable urls.
69: */
70: public static String getBaseUrl(HttpServletRequest request) {
71: if ((request.getServerPort() == 80)
72: || (request.getServerPort() == 443))
73: return request.getScheme() + "://"
74: + request.getServerName()
75: + request.getContextPath();
76: else
77: return request.getScheme() + "://"
78: + request.getServerName() + ":"
79: + request.getServerPort()
80: + request.getContextPath();
81: }
82:
83: /**
84: * Returns the file specified by <tt>path</tt> as returned by
85: * <tt>ServletContext.getRealPath()</tt>.
86: */
87: public static File getRealFile(HttpServletRequest request,
88: String path) {
89:
90: return new File(request.getSession().getServletContext()
91: .getRealPath(path));
92: }
93:
94: // properties ///////////////////////////////////////////////////////////////
95:
96: // attributes ///////////////////////////////////////////////////////////////
97: }
|