001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2005-08-10 16:00:46 +0200 (Mi, 10 Aug 2005) $
025: * $Author: til132 $
026: * $Revision: 155 $
027: */
028: package net.sf.regain.util.sharedtag.taglib;
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.net.MalformedURLException;
033: import java.net.URL;
034: import java.util.Enumeration;
035: import java.util.Locale;
036:
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.jsp.PageContext;
039:
040: import net.sf.regain.RegainException;
041: import net.sf.regain.util.sharedtag.PageRequest;
042:
043: /**
044: * Adapter from a JSP page context to a SharedTag PageRequest.
045: *
046: * @author Til Schneider, www.murfman.de
047: */
048: public class JspPageRequest extends PageRequest {
049:
050: /** The JSP page context to adapt. */
051: private PageContext mPageContext;
052:
053: /** The base URL where the JSP files and resources are located. */
054: private static URL mBaseUrl;
055:
056: /** The working directory of the web server. */
057: private static File mWorkingDir;
058:
059: /**
060: * Creates a new instance of JspPageRequest.
061: *
062: * @param pageContext The JSP page context to adapt.
063: */
064: public JspPageRequest(PageContext pageContext) {
065: mPageContext = pageContext;
066: }
067:
068: /**
069: * Gets a request parameter that was given to page via GET or POST.
070: *
071: * @param name The name of the parameter.
072: * @return The given parameter or <code>null</code> if no such parameter was
073: * given.
074: */
075: public String getParameter(String name) {
076: return mPageContext.getRequest().getParameter(name);
077: }
078:
079: /**
080: * Gets all request parameters with the given name that were given to the page
081: * via GET or POST.
082: *
083: * @param name The name of the parameter.
084: * @return The parameters or <code>null</code> if no such parameter was
085: * given.
086: * @throws RegainException If getting the parameter failed.
087: */
088: public String[] getParameters(String name) throws RegainException {
089: return mPageContext.getRequest().getParameterValues(name);
090: }
091:
092: /**
093: * Gets the names of the given parameters.
094: *
095: * @return The names of the given parameters.
096: */
097: public Enumeration getParameterNames() {
098: return mPageContext.getRequest().getParameterNames();
099: }
100:
101: /**
102: * Gets the header with the given name.
103: *
104: * @param name The name of the header.
105: * @return The header or <code>null</code> if no such header exists.
106: * @throws RegainException If getting the header failed.
107: */
108: public String getHeader(String name) throws RegainException {
109: HttpServletRequest request = (HttpServletRequest) mPageContext
110: .getRequest();
111: return request.getHeader(name);
112: }
113:
114: /**
115: * Gets the header with the given name as date.
116: *
117: * @param name The name of the header.
118: * @return The date header or <code>-1</code> if no such header exists.
119: * @throws RegainException If getting the header failed.
120: */
121: public long getHeaderAsDate(String name) throws RegainException {
122: HttpServletRequest request = (HttpServletRequest) mPageContext
123: .getRequest();
124: return request.getDateHeader(name);
125: }
126:
127: /**
128: * Gets the locale of the client.
129: *
130: * @return The locale.
131: * @throws RegainException If getting the locale failed.
132: */
133: public Locale getLocale() throws RegainException {
134: HttpServletRequest request = (HttpServletRequest) mPageContext
135: .getRequest();
136: return request.getLocale();
137: }
138:
139: /**
140: * Sets an attribute at the page context.
141: *
142: * @param name The name of the attribute to set.
143: * @param value The value of the attribute to set.
144: */
145: public void setContextAttribute(String name, Object value) {
146: mPageContext.setAttribute(name, value);
147: }
148:
149: /**
150: * Gets an attribute from the page context.
151: *
152: * @param name The name of the attribute to get.
153: * @return The attribute's value or <code>null</code> if there is no such
154: * attribute.
155: */
156: public Object getContextAttribute(String name) {
157: return mPageContext.getAttribute(name);
158: }
159:
160: /**
161: * Sets an attribute at the session.
162: *
163: * @param name The name of the attribute to set.
164: * @param value The value of the attribute to set.
165: */
166: public void setSessionAttribute(String name, Object value) {
167: mPageContext.getSession().setAttribute(name, value);
168: }
169:
170: /**
171: * Gets an attribute from the session.
172: *
173: * @param name The name of the attribute to get.
174: * @return The attribute's value or <code>null</code> if there is no such
175: * attribute.
176: */
177: public Object getSessionAttribute(String name) {
178: return mPageContext.getSession().getAttribute(name);
179: }
180:
181: /**
182: * Gets an init parameter.
183: *
184: * @param name The name of the init parameter.
185: * @return The value of the init parameter.
186: */
187: public String getInitParameter(String name) {
188: return mPageContext.getServletContext().getInitParameter(name);
189: }
190:
191: /**
192: * Gets the base URL where the JSP files and resources are located.
193: *
194: * @return The base URL where the JSP files and resources are located.
195: * @throws RegainException If getting the base URL failed.
196: */
197: public URL getResourceBaseUrl() throws RegainException {
198: if (mBaseUrl == null) {
199: try {
200: mBaseUrl = mPageContext.getServletContext()
201: .getResource("/");
202: System.out.println("mBaseUrl: " + mBaseUrl);
203: } catch (MalformedURLException exc) {
204: throw new RegainException("Getting base URL failed",
205: exc);
206: }
207: }
208: return mBaseUrl;
209: }
210:
211: /**
212: * Gets the working directory of the web server.
213: *
214: * @return The working directory of the web server.
215: * @throws RegainException If getting the working directory failed.
216: */
217: public File getWorkingDir() throws RegainException {
218: if (mWorkingDir == null) {
219: // Check whether we get a realpath
220: String realpath = mPageContext.getServletContext()
221: .getRealPath(".");
222: if (realpath != null) {
223: // Use the parent directory of the realpath
224: // E.g. "c:\tomcat\webapps\regain\." -> "c:\tomcat\webapps"
225: // NOTE: getCanonicalFile is needed to get rid of "\." is a save way
226: try {
227: mWorkingDir = new File(realpath).getCanonicalFile()
228: .getParentFile();
229: } catch (IOException exc) {
230: throw new RegainException(
231: "Getting the working directory of "
232: + "the web server failed", exc);
233: }
234: } else {
235: // We got no real path -> Return the current directory
236: mWorkingDir = new File(".");
237: }
238: }
239: return mWorkingDir;
240: }
241:
242: }
|