KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > http > HttpContext


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.environment.http;
17
18 import java.io.InputStream JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import javax.servlet.RequestDispatcher JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25
26 import org.apache.avalon.framework.CascadingRuntimeException;
27 import org.apache.cocoon.environment.Context;
28
29 /**
30  *
31  * Implements the {@link org.apache.cocoon.environment.Context} interface
32  * @author ?
33  * @version CVS $Id: HttpContext.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35
36 public final class HttpContext implements Context JavaDoc {
37
38     /** The ServletContext */
39     private final ServletContext JavaDoc servletContext;
40
41     /**
42      * Constructs a HttpContext object from a ServletContext object
43      */

44     public HttpContext (ServletContext JavaDoc servletContext) {
45         this.servletContext = servletContext;
46     }
47
48     public Object JavaDoc getAttribute(String JavaDoc name) {
49         return servletContext.getAttribute(name);
50     }
51
52     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
53         servletContext.setAttribute(name, value);
54     }
55
56     public void removeAttribute(String JavaDoc name) {
57         servletContext.removeAttribute(name);
58     }
59
60     public Enumeration JavaDoc getAttributeNames() {
61         return servletContext.getAttributeNames();
62     }
63
64     public URL JavaDoc getResource(String JavaDoc path)
65        throws MalformedURLException JavaDoc {
66        return servletContext.getResource(path);
67     }
68
69     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
70     return servletContext.getResourceAsStream(path);
71     }
72
73     public String JavaDoc getRealPath(String JavaDoc path) {
74         if (path.equals("/")) {
75             String JavaDoc value = servletContext.getRealPath(path);
76             if (value == null) {
77                 // Try to figure out the path of the root from that of WEB-INF
78
try {
79                 value = this.servletContext.getResource("/WEB-INF").toString();
80                 } catch (MalformedURLException JavaDoc mue) {
81                     throw new CascadingRuntimeException("Cannot determine the base URL for " + path, mue);
82                 }
83                 value = value.substring(0,value.length()-"WEB-INF".length());
84             }
85             return value;
86         }
87         return servletContext.getRealPath(path);
88     }
89
90     public String JavaDoc getMimeType(String JavaDoc file) {
91       return servletContext.getMimeType(file);
92     }
93
94     public String JavaDoc getInitParameter(String JavaDoc name) {
95         return servletContext.getInitParameter(name);
96     }
97
98     /*
99      * These methods are not in Cocoon's Context interface, but in the
100      * ServletContext. To use them you have to downcast Cocoon's Context
101      * to this HttpContext until we decide to add them to the Context
102      * interface too.
103      *
104      * The following methods are deprecated since Servlet API 2.0 or 2.1
105      * and will not be implemented here:
106      * - public Servlet getServlet(String name)
107      * - public Enumeration getServletNames()
108      * - public Enumeration getServlets()
109      * - public void log(Exception exception, String msg)
110      */

111
112     public ServletContext JavaDoc getContext(String JavaDoc uripath) {
113         return this.servletContext.getContext(uripath);
114     }
115
116     public Enumeration JavaDoc getInitParameterNames() {
117         return this.servletContext.getInitParameterNames();
118     }
119
120     public int getMajorVersion() {
121         return this.servletContext.getMajorVersion();
122     }
123
124     public int getMinorVersion() {
125         return this.servletContext.getMinorVersion();
126     }
127
128     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc name) {
129         return this.servletContext.getNamedDispatcher(name);
130     }
131
132     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
133         return this.servletContext.getRequestDispatcher(path);
134     }
135
136     public String JavaDoc getServerInfo() {
137         return this.servletContext.getServerInfo();
138     }
139
140     public void log(String JavaDoc msg) {
141         this.servletContext.log(msg);
142     }
143
144     public void log(String JavaDoc msg, Throwable JavaDoc throwable) {
145         this.servletContext.log(msg, throwable);
146     }
147 }
148
Popular Tags