2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

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  
18  package org.apache.shale.test.mock;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.Hashtable;
28  import java.util.Set;
29  
30  import javax.portlet.PortletContext;
31  import javax.portlet.PortletRequestDispatcher;
32  
33  /***
34   * <p>Mock implementation of <code>PortletContext</code>.</p>
35   *
36   * $Id: MockPortletContext.java 516091 2007-03-08 16:25:17Z greddin $
37   */
38  public class MockPortletContext implements PortletContext {
39  
40      // ----------------------------------------------------- Mock Object Methods
41  
42      /***
43       * <p>Add a context initialization parameter to the set of parameters
44       * recognized by this instance.</p>
45       *
46       * @param name Parameter name
47       * @param value Parameter value
48       */
49      public void addInitParameter(String name, String value) {
50  
51          parameters.put(name, value);
52  
53      }
54  
55  
56      /***
57       * <p>Add a new MIME type mapping to the set of mappings recognized by this
58       * instance.</p>
59       * 
60       * @param extension Extension to check for (without the period)
61       * @param contentType Corresponding content type
62       */
63      public void addMimeType(String extension, String contentType) {
64  
65          mimeTypes.put(extension, contentType);
66  
67      }
68  
69  
70      /***
71       * <p>Set the document root for <code>getRealPath()</code> resolution.
72       * This parameter <strong>MUST</strong> represent a directory.</p>
73       *
74       * @param documentRoot The new base directory
75       */
76      public void setDocumentRoot(File documentRoot) {
77  
78          this.documentRoot = documentRoot;
79  
80      }
81  
82      // ------------------------------------------------------ Instance Variables
83  
84      private Hashtable attributes = new Hashtable();
85      private File documentRoot = null;
86      private Hashtable mimeTypes = new Hashtable();
87      private Hashtable parameters = new Hashtable();
88  
89  
90      // -------------------------------------------------- PortletContext Methods
91  
92  
93      /*** {@inheritDoc} */
94      public Object getAttribute(String name) {
95  
96          return attributes.get(name);
97  
98      }
99  
100 
101     /*** {@inheritDoc} */
102     public Enumeration getAttributeNames() {
103 
104         return attributes.keys();
105 
106     }
107 
108 
109     /*** {@inheritDoc} */
110     public String getInitParameter(String name) {
111 
112         return (String) parameters.get(name);
113 
114     }
115 
116 
117     /*** {@inheritDoc} */
118     public Enumeration getInitParameterNames() {
119 
120         return parameters.keys();
121 
122     }
123 
124 
125     /*** {@inheritDoc} */
126     public int getMajorVersion() {
127 
128         return 1;
129 
130     }
131 
132 
133     /*** {@inheritDoc} */
134     public String getMimeType(String path) {
135 
136         int period = path.lastIndexOf('.');
137         if (period < 0) {
138             return null;
139         }
140         String extension = path.substring(period + 1);
141         return (String) mimeTypes.get(extension);
142 
143     }
144 
145 
146     public int getMinorVersion() {
147 
148         // TODO Auto-generated method stub
149         return 0;
150     }
151 
152 
153     public PortletRequestDispatcher getNamedDispatcher(String arg0) {
154 
155         throw new UnsupportedOperationException();
156 
157     }
158 
159 
160     /*** {@inheritDoc} */
161     public String getPortletContextName() {
162 
163         return "MockPortletContext";
164 
165     }
166 
167 
168     /*** {@inheritDoc} */
169     public String getRealPath(String path) {
170 
171         if (documentRoot != null) {
172             if (!path.startsWith("/")) {
173                 throw new IllegalArgumentException("The specified path ('"
174                         + path + "') does not start with a '/' character");
175             }
176             File resolved = new File(documentRoot, path.substring(1));
177             try {
178                 return resolved.getCanonicalPath();
179             } catch (IOException e) {
180                 return resolved.getAbsolutePath();
181             }
182         } else {
183             return null;
184         }
185 
186     }
187 
188 
189     /*** {@inheritDoc} */
190     public PortletRequestDispatcher getRequestDispatcher(String arg0) {
191 
192         throw new UnsupportedOperationException();
193 
194     }
195 
196 
197     /*** {@inheritDoc} */
198     public URL getResource(String path) throws MalformedURLException {
199 
200         if (documentRoot != null) {
201             if (!path.startsWith("/")) {
202                 throw new MalformedURLException("The specified path ('" + path
203                         + "') does not start with a '/' character");
204             }
205             File resolved = new File(documentRoot, path.substring(1));
206             if (resolved.exists()) {
207                 return resolved.toURL();
208             } else {
209                 return null;
210             }
211         } else {
212             return null;
213         }
214 
215     }
216 
217 
218     /*** {@inheritDoc} */
219     public InputStream getResourceAsStream(String path) {
220 
221         try {
222             URL url = getResource(path);
223             if (url != null) {
224                 return url.openStream();
225             }
226         } catch (Exception e) {
227             ;
228         }
229         return null;
230 
231     }
232 
233 
234     /*** {@inheritDoc} */
235     public Set getResourcePaths(String path) {
236 
237         if (documentRoot == null) {
238             return null;
239         }
240 
241         // Enforce the leading slash restriction
242         if (!path.startsWith("/")) {
243             throw new IllegalArgumentException("The specified path ('" + path
244                     + "') does not start with a '/' character");
245         }
246 
247         // Locate the File node for this path's directory (if it exists)
248         File node = new File(documentRoot, path.substring(1));
249         if (!node.exists()) {
250             return null;
251         }
252         if (!node.isDirectory()) {
253             return null;
254         }
255 
256         // Construct a Set containing the paths to the contents of this
257         // directory
258         Set set = new HashSet();
259         String[] files = node.list();
260         if (files == null) {
261             return null;
262         }
263         for (int i = 0; i < files.length; i++) {
264             String subfile = path + files[i];
265             File subnode = new File(node, files[i]);
266             if (subnode.isDirectory()) {
267                 subfile += "/";
268             }
269             set.add(subfile);
270         }
271 
272         // Return the completed set
273         return set;
274 
275     }
276 
277 
278     /*** {@inheritDoc} */
279     public String getServerInfo() {
280 
281         return "MockPortletContext";
282     }
283 
284 
285     /*** {@inheritDoc} */
286     public void log(String message) {
287 
288         System.out.println(message);
289 
290     }
291 
292 
293     /*** {@inheritDoc} */
294     public void log(String message, Throwable exception) {
295 
296         System.out.println(message);
297         exception.printStackTrace();
298 
299     }
300 
301 
302     /*** {@inheritDoc} */
303     public void removeAttribute(String name) {
304 
305         if (attributes.containsKey(name)) {
306             attributes.remove(name);
307         }
308 
309     }
310 
311 
312     /*** {@inheritDoc} */
313     public void setAttribute(String name, Object value) {
314 
315         if (name == null) {
316             throw new IllegalArgumentException("Attribute name cannot be null");
317         }
318         if (value == null) {
319             removeAttribute(name);
320             return;
321         }
322         attributes.put(name, value);
323 
324     }
325 
326 }