View Javadoc

1   /*
2    * Copyright 2003,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  /* 
17  
18   */
19  
20  package org.apache.pluto.core.impl;
21  
22  import java.net.MalformedURLException;
23  import java.util.Map;
24  import java.util.StringTokenizer;
25  
26  import javax.portlet.PortletContext;
27  import javax.servlet.RequestDispatcher;
28  
29  import org.apache.pluto.Environment;
30  import org.apache.pluto.core.InternalPortletContext;
31  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
32  
33  public class PortletContextImpl implements PortletContext, InternalPortletContext
34  {
35      private PortletApplicationDefinition portletApplicationDefinition;
36      private javax.servlet.ServletContext servletContext;
37  
38      public PortletContextImpl(javax.servlet.ServletContext servletContext,
39                                PortletApplicationDefinition portletApplicationDefinition)
40      {
41          this.servletContext = servletContext;
42          this.portletApplicationDefinition = portletApplicationDefinition;
43      }
44  
45      // javax.portlet.PortletContext implementation ------------------------------------------------
46      public String getServerInfo()
47      {
48          return Environment.getServerInfo();
49      }
50  
51      public javax.portlet.PortletRequestDispatcher getRequestDispatcher(String path)
52      {
53          Map parms = parseQueryParams(path);
54  		try {
55  	        RequestDispatcher rd = servletContext.getRequestDispatcher(path);
56              return rd == null?null:new PortletRequestDispatcherImpl(rd, parms);
57          } catch (Exception e) {
58      		// need to catch exception because of tomcat 4.x bug
59      		// tomcat throws an exception instead of return null
60      		// if the path was not found
61      		return null;
62      	}
63  
64      }
65  
66      public javax.portlet.PortletRequestDispatcher getNamedDispatcher(String name)
67      {
68         	javax.servlet.RequestDispatcher rd = servletContext.getNamedDispatcher(name);
69         	return rd != null ? new PortletRequestDispatcherImpl(rd)
70             	              : null;
71      }
72  
73      public java.io.InputStream getResourceAsStream(String path)
74      {
75          return servletContext.getResourceAsStream(path);
76      }
77      
78      public int getMajorVersion()
79      {
80          return Environment.getMajorSpecificationVersion();
81      }
82  
83      public int getMinorVersion()
84      {
85          return Environment.getMinorSpecificationVersion();
86      }
87  
88      public String getMimeType(String file)
89      {
90          return servletContext.getMimeType(file);
91      }
92  
93      public String getRealPath(String path)
94      {
95          return servletContext.getRealPath(path);
96      }
97  
98      public java.util.Set getResourcePaths(String path)
99      {
100         return servletContext.getResourcePaths(path);
101     }
102 
103     public java.net.URL getResource(String path) throws java.net.MalformedURLException
104     {
105         if (path == null || !path.startsWith("/"))
106         {
107             throw new MalformedURLException("path must start with a '/'");
108         }
109         return servletContext.getResource(path);
110     }
111 
112     public java.lang.Object getAttribute(java.lang.String name)
113     {
114         if (name == null)
115         {
116             throw new IllegalArgumentException("Attribute name == null");
117         }
118 
119         return servletContext.getAttribute(name);
120     }
121 
122     public java.util.Enumeration getAttributeNames()
123     {
124         return servletContext.getAttributeNames();
125     }
126 
127     public java.lang.String getInitParameter(java.lang.String name)
128     {
129         if (name == null)
130         {
131             throw new IllegalArgumentException("Parameter name == null");
132         }
133 
134         return servletContext.getInitParameter(name);
135     }
136 
137     public java.util.Enumeration getInitParameterNames()
138     {
139         return servletContext.getInitParameterNames();
140     }
141 
142     public void log(java.lang.String msg)
143     {
144         servletContext.log(msg);
145     }
146 
147     public void log(java.lang.String message, java.lang.Throwable throwable)
148     {
149         servletContext.log(message, throwable);
150     }
151 
152     public void removeAttribute(java.lang.String name)
153     {
154         if (name == null)
155         {
156             throw new IllegalArgumentException("Attribute name == null");
157         }
158 
159         servletContext.removeAttribute(name);
160     }
161 
162     public void setAttribute(java.lang.String name, java.lang.Object object)
163     {
164         if (name == null)
165         {
166             throw new IllegalArgumentException("Attribute name == null");
167         }
168 
169         servletContext.setAttribute(name, object);
170     }
171 
172     public String getPortletContextName()
173     {
174         return servletContext.getServletContextName();
175     }
176     // --------------------------------------------------------------------------------------------
177 
178     // org.apache.pluto.core.InternalPortletContext implementation --------------------------------
179     public javax.servlet.ServletContext getServletContext()
180     {
181         return servletContext;
182     }
183 
184     public PortletApplicationDefinition getInternalPortletApplicationDefinition()
185     {
186         return portletApplicationDefinition;
187     }
188     // --------------------------------------------------------------------------------------------
189 
190     private Map parseQueryParams(String path) {
191         Map map = new java.util.HashMap();
192         int idx = path.indexOf("?");
193         if(idx < 0) {
194             return null;
195         }
196         String parms = path.substring(idx+1);
197         StringTokenizer st = new StringTokenizer(parms, "&");
198         while(st.hasMoreTokens()) {
199             String pair = st.nextToken();
200             if(pair.indexOf("=")>0) {
201                 String key = pair.substring(0,pair.indexOf("="));
202                 String val = pair.substring(pair.indexOf("=")+1);
203                 map.put(key, new String[] {val});
204             }
205         }
206         return map;
207     }
208 }
209