001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.http;
018:
019: import java.io.InputStream;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.Enumeration;
023:
024: import javax.servlet.RequestDispatcher;
025: import javax.servlet.ServletContext;
026:
027: import org.apache.avalon.framework.CascadingRuntimeException;
028: import org.apache.cocoon.environment.Context;
029:
030: /**
031: *
032: * Implements the {@link org.apache.cocoon.environment.Context} interface
033: * @author ?
034: * @version CVS $Id: HttpContext.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036:
037: public final class HttpContext implements Context {
038:
039: /** The ServletContext */
040: private final ServletContext servletContext;
041:
042: /**
043: * Constructs a HttpContext object from a ServletContext object
044: */
045: public HttpContext(ServletContext servletContext) {
046: this .servletContext = servletContext;
047: }
048:
049: public Object getAttribute(String name) {
050: return servletContext.getAttribute(name);
051: }
052:
053: public void setAttribute(String name, Object value) {
054: servletContext.setAttribute(name, value);
055: }
056:
057: public void removeAttribute(String name) {
058: servletContext.removeAttribute(name);
059: }
060:
061: public Enumeration getAttributeNames() {
062: return servletContext.getAttributeNames();
063: }
064:
065: public URL getResource(String path) throws MalformedURLException {
066: return servletContext.getResource(path);
067: }
068:
069: public InputStream getResourceAsStream(String path) {
070: return servletContext.getResourceAsStream(path);
071: }
072:
073: public String getRealPath(String path) {
074: if (path.equals("/")) {
075: String value = servletContext.getRealPath(path);
076: if (value == null) {
077: // Try to figure out the path of the root from that of WEB-INF
078: try {
079: value = this .servletContext.getResource("/WEB-INF")
080: .toString();
081: } catch (MalformedURLException mue) {
082: throw new CascadingRuntimeException(
083: "Cannot determine the base URL for " + path,
084: mue);
085: }
086: value = value.substring(0, value.length()
087: - "WEB-INF".length());
088: }
089: return value;
090: }
091: return servletContext.getRealPath(path);
092: }
093:
094: public String getMimeType(String file) {
095: return servletContext.getMimeType(file);
096: }
097:
098: public String getInitParameter(String name) {
099: return servletContext.getInitParameter(name);
100: }
101:
102: /*
103: * These methods are not in Cocoon's Context interface, but in the
104: * ServletContext. To use them you have to downcast Cocoon's Context
105: * to this HttpContext until we decide to add them to the Context
106: * interface too.
107: *
108: * The following methods are deprecated since Servlet API 2.0 or 2.1
109: * and will not be implemented here:
110: * - public Servlet getServlet(String name)
111: * - public Enumeration getServletNames()
112: * - public Enumeration getServlets()
113: * - public void log(Exception exception, String msg)
114: */
115:
116: public ServletContext getContext(String uripath) {
117: return this .servletContext.getContext(uripath);
118: }
119:
120: public Enumeration getInitParameterNames() {
121: return this .servletContext.getInitParameterNames();
122: }
123:
124: public int getMajorVersion() {
125: return this .servletContext.getMajorVersion();
126: }
127:
128: public int getMinorVersion() {
129: return this .servletContext.getMinorVersion();
130: }
131:
132: public RequestDispatcher getNamedDispatcher(String name) {
133: return this .servletContext.getNamedDispatcher(name);
134: }
135:
136: public RequestDispatcher getRequestDispatcher(String path) {
137: return this .servletContext.getRequestDispatcher(path);
138: }
139:
140: public String getServerInfo() {
141: return this .servletContext.getServerInfo();
142: }
143:
144: public void log(String msg) {
145: this .servletContext.log(msg);
146: }
147:
148: public void log(String msg, Throwable throwable) {
149: this.servletContext.log(msg, throwable);
150: }
151: }
|