1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */1718packageorg.apache.jetspeed.capabilities.impl;
1920import org.apache.commons.logging.Log;
21import org.apache.commons.logging.LogFactory;
22import org.apache.jetspeed.capabilities.Capabilities;
23import org.apache.jetspeed.capabilities.CapabilityMap;
24import org.apache.jetspeed.capabilities.MediaType;
25import org.apache.jetspeed.capabilities.MimeType;
26import org.apache.jetspeed.capabilities.UnableToBuildCapabilityMapException;
27import org.apache.jetspeed.pipeline.PipelineException;
28import org.apache.jetspeed.pipeline.valve.CapabilityValve;
29import org.apache.jetspeed.pipeline.valve.ValveContext;
30import org.apache.jetspeed.request.RequestContext;
3132/***33 * Invokes the capability mapper in the request pipeline34 * 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 */38publicclassCapabilityValveImpl implements CapabilityValve39 {
40privatestaticfinal Log log = LogFactory.getLog(CapabilityValveImpl.class);
41 String resourceDefault; // the default name for a resource42private Capabilities capabilities;
4344publicCapabilityValveImpl( Capabilities capabilities )
45 {
46this.capabilities = capabilities;
47 }
4849/***50 * Initialize the valve before using in a pipeline.51 */52publicvoid initialize() throws PipelineException
53 {
5455 }
5657publicvoid invoke( RequestContext request, ValveContext context ) throws PipelineException
58 {
59 String agent = request.getRequest().getHeader("User-Agent");
6061// Get capability map62 CapabilityMap cm;
63try64 {
65 cm = capabilities.getCapabilityMap(agent);
66 }
67catch (UnableToBuildCapabilityMapException e)
68 {
69thrownew PipelineException("Falied to create capabilitied: "+e.getMessage(), e);
70 }
7172 MediaType mediaType = cm.getPreferredMediaType();
73 MimeType mimeType = cm.getPreferredType();
7475if (mediaType == null)
76 {
77 log.error("CapabilityMap returned a null media type");
78thrownew PipelineException("CapabilityMap returned a null media type");
79 }
8081if (mimeType == null)
82 {
83 log.error("CapabilityMap returned a null mime type");
84thrownew PipelineException("CapabilityMap returned a null mime type");
85 }
8687 String encoding = request.getRequest().getCharacterEncoding();
8889if (encoding == null)
90 {
91if (mediaType != null && mediaType.getCharacterSet() != null)
92 {
93 encoding = mediaType.getCharacterSet();
94 }
95 }
9697if (log.isDebugEnabled())
98 {
99 log.debug("MediaType: " + mediaType.getName());
100 log.debug("Encoding: " + encoding);
101 log.debug("Mimetype: " + mimeType.getName());
102 }
103104// Put the encoding in the request105 request.setCharacterEncoding(encoding);
106107// Put the CapabilityMap into the request108 request.setCapabilityMap(cm);
109110// Put the Media Type into the request111 request.setMediaType(mediaType.getName());
112113// Put the Mime Type into the request114 request.setMimeType(mimeType.getName());
115116// Put the Mime Type and Charset into the response117 StringBuffer contentType = new StringBuffer(mimeType.getName());
118if (encoding != null)
119 {
120 contentType.append("; charset=" + encoding);
121 }
122 String type = contentType.toString(); //mapContentType(request, contentType.toString());123 request.getResponse().setContentType(type);
124125// Pass control to the next Valve in the Pipeline126 context.invokeNext(request);
127 }
128129static String[][] MIME_MAP =
130 {
131 {".pdf", "application/pdf"}
132 };
133134protected String mapContentType(RequestContext request, String contentType)
135 {
136// TODO: get path from servlet request137// this code below fails since the path is not yet parsed138// to far up in the pipeline139 String path = request.getPath();
140if (path != null)
141 {
142for (int ix=0; ix < MIME_MAP.length; ix++)
143 {
144if (path.endsWith(MIME_MAP[ix][0]))
145 {
146return MIME_MAP[ix][1];
147 }
148 }
149 }
150return contentType;
151 }
152153public String toString()
154 {
155return"CapabilityValveImpl";
156 }
157 }