View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.container;
18  
19  import java.io.InputStream;
20  import java.util.Collection;
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.RequestDispatcher;
26  
27  import javax.portlet.PortletContext;
28  import javax.portlet.PortletRequestDispatcher;
29  
30  import org.apache.jetspeed.dispatcher.JetspeedRequestDispatcher;
31  import org.apache.jetspeed.om.common.JetspeedServiceReference;
32  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
33  import org.apache.jetspeed.services.JetspeedPortletServices;
34  import org.apache.jetspeed.services.PortletServices;
35  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
36  
37  /***
38   * Implements the Portlet API Portlet Context class
39   *
40   * TODO: on LOCAL apps, we need to merge in web.xml props. See PLT 10.3
41   *
42   * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
43   * @version $Id: JetspeedPortletContext.java 516448 2007-03-09 16:25:47Z ate $
44   */
45  public class JetspeedPortletContext implements PortletContext, InternalPortletContext
46  {
47      /***
48       * The path to the Local Portlet Apps directory
49       */
50      public static final String LOCAL_PA_ROOT = "/WEB-INF/apps";
51  
52      private ServletContext servletContext;
53      private MutablePortletApplication application;
54  
55      public JetspeedPortletContext(ServletContext servletContext, PortletApplicationDefinition application)
56      {
57          this.servletContext = servletContext;
58          this.application = (MutablePortletApplication)application;
59      }
60  
61      public int getMajorVersion()
62      {
63          return ContainerInfo.getMajorSpecificationVersion();
64      }
65  
66      public int getMinorVersion()
67      {
68          return ContainerInfo.getMinorSpecificationVersion();
69      }
70  
71      // Delegated methods
72  
73      public java.util.Set getResourcePaths(String path)
74      {
75          return servletContext.getResourcePaths(path);
76      }
77  
78      public javax.portlet.PortletRequestDispatcher getRequestDispatcher(String path)
79      {
80          String localizedPath = localizePath(path, this.application);
81          RequestDispatcher rd = null;
82          
83          try
84          {
85              rd = servletContext.getRequestDispatcher(localizedPath);
86          }
87          catch (Exception e)
88          {
89              // Portlet API says: return null
90          }
91  
92          // TODO: factory
93          if ( rd != null )
94          {
95              return new JetspeedRequestDispatcher(rd);
96          }
97          return null;
98      }
99  
100     public PortletRequestDispatcher getNamedDispatcher(String name)
101     {
102         // TODO: localize name
103 
104         RequestDispatcher rd = null;
105         
106         try
107         {
108             rd = servletContext.getNamedDispatcher(name);
109         }
110         catch (Exception e)
111         {
112             // Portlet API says: return null
113         }
114         
115         // TODO: factory
116 
117         if ( rd != null )
118         {
119             return new JetspeedRequestDispatcher(rd);
120         }
121         return null;
122     }
123 
124     public String getMimeType(String file)
125     {
126         return servletContext.getMimeType(file);
127     }
128 
129     public InputStream getResourceAsStream(String path)
130     {
131         return servletContext.getResourceAsStream(localizePath(path, this.application));
132     }
133 
134     public java.lang.Object getAttribute(java.lang.String name)
135     {
136         if ( name == null )
137         {
138             throw new IllegalArgumentException("Required parameter name is null");
139         }
140         
141         if (name.startsWith("cps:"))
142         {
143             String serviceName = name.substring("cps:".length());
144             
145             // validate service
146             Collection validServices = application.getJetspeedServices();
147             if (null == validServices)
148             {
149                 return null;
150             }
151             boolean found = false;
152             Iterator iterator = validServices.iterator();
153             while (iterator.hasNext())
154             {
155                 JetspeedServiceReference validService = (JetspeedServiceReference)iterator.next();
156                 if (validService.getName().equals(serviceName))
157                 {
158                     found = true;
159                     break;
160                 }
161             }
162             
163             if (!found)
164             {
165                 return null;
166             }
167             
168             // return the service
169             PortletServices services = JetspeedPortletServices.getSingleton();
170             return services.getService(serviceName);
171         }
172         return servletContext.getAttribute(name);
173     }
174 
175     public void log(java.lang.String msg)
176     {
177         // TODO: setup a logger for the portlet application
178         servletContext.log(msg);
179     }
180 
181     public void log(java.lang.String message, java.lang.Throwable throwable)
182     {
183         // TODO: setup a logger for the portlet application
184         servletContext.log(message, throwable);
185     }
186 
187     public String getRealPath(String path)
188     {
189         return servletContext.getRealPath(localizePath(path, this.application));
190     }
191 
192     public java.net.URL getResource(String path) throws java.net.MalformedURLException
193     {
194         return servletContext.getResource(localizePath(path, this.application));
195     }
196 
197     public Enumeration getAttributeNames()
198     {
199         return servletContext.getAttributeNames();
200     }
201 
202     public java.lang.String getInitParameter(java.lang.String name)
203     {
204         if ( name == null )
205         {
206             throw new IllegalArgumentException("Required parameter name is null");
207         }
208         return servletContext.getInitParameter(name);
209     }
210 
211     public java.util.Enumeration getInitParameterNames()
212     {
213         return servletContext.getInitParameterNames();
214     }
215 
216     public void removeAttribute(java.lang.String name)
217     {
218         if (name == null)
219         {
220             throw new IllegalArgumentException("Attribute name == null");
221         }
222 
223         servletContext.removeAttribute(name);
224     }
225 
226     public void setAttribute(java.lang.String name, java.lang.Object object)
227     {
228         if (name == null)
229         {
230             throw new IllegalArgumentException("Attribute name == null");
231         }
232         servletContext.setAttribute(name, object);
233     }
234 
235     public String getServerInfo()
236     {
237         return ContainerInfo.getServerInfo();
238     }
239 
240     // internal portlet context implementation
241 
242     public ServletContext getServletContext()
243     {
244         return this.servletContext;
245     }
246 
247     /***
248      * @see org.apache.jetspeed.container.InternalPortletContext#getApplication()
249      */
250     public PortletApplicationDefinition getApplication()
251     {
252         return this.application;
253     }
254 
255     public String getPortletContextName()
256     {
257         return servletContext.getServletContextName();
258     }
259 
260     private String localizePath(String path, MutablePortletApplication app)
261     {
262         if (path == null)
263         {
264             return "/";
265         }
266         return path;
267         // TODO: local PA with own/extra resource paths support
268 /*        
269         if (app.getApplicationType() == MutablePortletApplication.WEBAPP)
270         {
271             return path;
272         }
273 
274         StringBuffer pathBuffer = new StringBuffer(LOCAL_PA_ROOT);
275         pathBuffer.append(app.getWebApplicationDefinition().getContextRoot());
276         if (!path.startsWith("/"))
277         {
278             pathBuffer.append("/");
279         }
280         pathBuffer.append(path);
281         String result = pathBuffer.toString();
282         return result;
283 */        
284     }
285 }