View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.tapestry;
14  
15  import java.net.URL;
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  import javax.activation.FileTypeMap;
22  
23  import org.apache.tapestry.describe.DescriptionReceiver;
24  import org.apache.tapestry.web.WebContext;
25  
26  /**
27   * {@link WebContext} interface implementation.
28   *
29   *
30   * @author Daniel Sendula
31   */
32  public class DanubeContext implements WebContext {
33  
34      /** Contexct attributes */
35      protected Map<String, Object> attributes = new HashMap<String, Object>();
36  
37      /** Reference to handler */
38      protected TapestryConnectionHandler handler;
39  
40      /**
41       * Constructor
42       * @param handler handler
43       */
44      public DanubeContext(TapestryConnectionHandler handler) {
45          this.handler = handler;
46      }
47  
48      /**
49       * Returns resource as URL. It uses this classes class loader.
50       * @param path resource path. If starts with &quot;/&quot; then it is removed.
51       * @return resource as URL.
52       */
53      public URL getResource(String path) {
54          if (path.startsWith("/")) {
55              path = path.substring(1);
56          }
57          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
58          URL url = classLoader.getResource(path);
59          return url;
60      }
61  
62      /**
63       * Returns mime type of given resource path
64       * @param resourcePath resource path
65       * @return mime type of given resource path
66       */
67      public String getMimeType(String resourcePath) {
68          if (resourcePath.endsWith(".js")) {
69              return "text/javascript";
70          }
71  
72          if (resourcePath.endsWith(".css")) {
73              return "text/css";
74          }
75  
76          FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
77          // URL resource = getResource(resourcePath);
78          String mimeType = map.getContentType(resourcePath);
79          return mimeType; // TODO
80      }
81  
82      /**
83       * Returns attribute names
84       * @return attribute names as a list
85       */
86      @SuppressWarnings("unchecked")
87      public List getAttributeNames() {
88          return new ArrayList<String>(attributes.keySet());
89      }
90  
91      /**
92       * Returns attribute
93       * @param name attribute name
94       * @return attribute
95       */
96      public Object getAttribute(String name) {
97          return attributes.get(name);
98      }
99  
100     /**
101      * Sets attribute
102      * @param name attribte name
103      * @param attribute attribute value
104      */
105     public void setAttribute(String name, Object attribute) {
106         attributes.put(name, attribute);
107     }
108 
109     /**
110      * Returns initial parameters names.
111      * @return list of initial parameter names
112      */
113     @SuppressWarnings("unchecked")
114     public List getInitParameterNames() {
115         ArrayList<String> names = new ArrayList<String>(handler.getInitialParameters().keySet());
116         return names;
117     }
118 
119     /**
120      * Gets initial parameter's value
121      * @param name attribute name
122      * @return initial parameter's value
123      */
124     public String getInitParameterValue(String name) {
125         return handler.getInitialParameters().get(name);
126     }
127 
128     /**
129      * Does nothing
130      * @param receiver receiver
131      */
132     public void describeTo(DescriptionReceiver receiver) {
133     }
134 
135 }