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.jetspeed.capabilities.impl;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.jetspeed.capabilities.Capabilities;
23  import org.apache.jetspeed.capabilities.CapabilityMap;
24  import org.apache.jetspeed.capabilities.MediaType;
25  import org.apache.jetspeed.capabilities.MimeType;
26  import org.apache.jetspeed.capabilities.UnableToBuildCapabilityMapException;
27  import org.apache.jetspeed.pipeline.PipelineException;
28  import org.apache.jetspeed.pipeline.valve.CapabilityValve;
29  import org.apache.jetspeed.pipeline.valve.ValveContext;
30  import org.apache.jetspeed.request.RequestContext;
31  
32  /***
33   * Invokes the capability mapper in the request pipeline
34   * 
35   * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann </a>
36   * @version $Id: CapabilityValveImpl.java 517719 2007-03-13 15:05:48Z ate $
37   */
38  public class CapabilityValveImpl implements CapabilityValve
39  {
40      private static final Log log = LogFactory.getLog(CapabilityValveImpl.class);
41      String resourceDefault; // the default name for a resource
42      private Capabilities capabilities;
43  
44      public CapabilityValveImpl( Capabilities capabilities )
45      {
46          this.capabilities = capabilities;
47      }
48  
49      /***
50       * Initialize the valve before using in a pipeline.
51       */
52      public void initialize() throws PipelineException
53      {
54  
55      }
56  
57      public void invoke( RequestContext request, ValveContext context ) throws PipelineException
58      {
59          String agent = request.getRequest().getHeader("User-Agent");
60  
61          // Get capability map
62          CapabilityMap cm;
63          try
64          {
65              cm = capabilities.getCapabilityMap(agent);
66          }
67          catch (UnableToBuildCapabilityMapException e)
68          {
69             throw new PipelineException("Falied to create capabilitied:  "+e.getMessage(), e);
70          }
71          
72          MediaType mediaType = cm.getPreferredMediaType();
73          MimeType mimeType = cm.getPreferredType();
74  
75          if (mediaType == null)
76          {
77              log.error("CapabilityMap returned a null media type");
78              throw new PipelineException("CapabilityMap returned a null media type");
79          }
80  
81          if (mimeType == null)
82          {
83              log.error("CapabilityMap returned a null mime type");
84              throw new PipelineException("CapabilityMap returned a null mime type");
85          }
86  
87          String encoding = request.getRequest().getCharacterEncoding();
88  
89          if (encoding == null)
90          {
91              if (mediaType != null && mediaType.getCharacterSet() != null)
92              {
93                  encoding = mediaType.getCharacterSet();
94              }
95          }
96  
97          if (log.isDebugEnabled())
98          {
99              log.debug("MediaType: " + mediaType.getName());
100             log.debug("Encoding: " + encoding);
101             log.debug("Mimetype: " + mimeType.getName());
102         }
103 
104         // Put the encoding in the request
105         request.setCharacterEncoding(encoding);
106 
107         // Put the CapabilityMap into the request
108         request.setCapabilityMap(cm);
109 
110         // Put the Media Type into the request
111         request.setMediaType(mediaType.getName());
112 
113         // Put the Mime Type into the request
114         request.setMimeType(mimeType.getName());
115 
116         // Put the Mime Type and Charset into the response
117         StringBuffer contentType = new StringBuffer(mimeType.getName());
118         if (encoding != null)
119         {
120             contentType.append("; charset=" + encoding);
121         }
122         String type =  contentType.toString(); //mapContentType(request, contentType.toString());
123         request.getResponse().setContentType(type);
124 
125         // Pass control to the next Valve in the Pipeline
126         context.invokeNext(request);
127     }
128 
129     static String[][] MIME_MAP =
130     {     
131         {".pdf", "application/pdf"}       
132     };
133     
134     protected String mapContentType(RequestContext request, String contentType)
135     {
136         // TODO: get path from servlet request
137         // this code below fails since the path is not yet parsed
138         // to far up in the pipeline
139         String path = request.getPath();
140         if (path != null)
141         {
142             for (int ix=0; ix < MIME_MAP.length; ix++)
143             {
144                 if (path.endsWith(MIME_MAP[ix][0]))
145                 {
146                     return MIME_MAP[ix][1];
147                 }
148             }            
149         }
150         return contentType;
151     }
152     
153     public String toString()
154     {
155         return "CapabilityValveImpl";
156     }
157 }