001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jetspeed.capabilities.impl;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.jetspeed.capabilities.Capabilities;
023: import org.apache.jetspeed.capabilities.CapabilityMap;
024: import org.apache.jetspeed.capabilities.MediaType;
025: import org.apache.jetspeed.capabilities.MimeType;
026: import org.apache.jetspeed.capabilities.UnableToBuildCapabilityMapException;
027: import org.apache.jetspeed.pipeline.PipelineException;
028: import org.apache.jetspeed.pipeline.valve.CapabilityValve;
029: import org.apache.jetspeed.pipeline.valve.ValveContext;
030: import org.apache.jetspeed.request.RequestContext;
031:
032: /**
033: * Invokes the capability mapper in the request pipeline
034: *
035: * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann </a>
036: * @version $Id: CapabilityValveImpl.java 517719 2007-03-13 15:05:48Z ate $
037: */
038: public class CapabilityValveImpl implements CapabilityValve {
039: private static final Log log = LogFactory
040: .getLog(CapabilityValveImpl.class);
041: String resourceDefault; // the default name for a resource
042: private Capabilities capabilities;
043:
044: public CapabilityValveImpl(Capabilities capabilities) {
045: this .capabilities = capabilities;
046: }
047:
048: /**
049: * Initialize the valve before using in a pipeline.
050: */
051: public void initialize() throws PipelineException {
052:
053: }
054:
055: public void invoke(RequestContext request, ValveContext context)
056: throws PipelineException {
057: String agent = request.getRequest().getHeader("User-Agent");
058:
059: // Get capability map
060: CapabilityMap cm;
061: try {
062: cm = capabilities.getCapabilityMap(agent);
063: } catch (UnableToBuildCapabilityMapException e) {
064: throw new PipelineException(
065: "Falied to create capabilitied: " + e.getMessage(),
066: e);
067: }
068:
069: MediaType mediaType = cm.getPreferredMediaType();
070: MimeType mimeType = cm.getPreferredType();
071:
072: if (mediaType == null) {
073: log.error("CapabilityMap returned a null media type");
074: throw new PipelineException(
075: "CapabilityMap returned a null media type");
076: }
077:
078: if (mimeType == null) {
079: log.error("CapabilityMap returned a null mime type");
080: throw new PipelineException(
081: "CapabilityMap returned a null mime type");
082: }
083:
084: String encoding = request.getRequest().getCharacterEncoding();
085:
086: if (encoding == null) {
087: if (mediaType != null
088: && mediaType.getCharacterSet() != null) {
089: encoding = mediaType.getCharacterSet();
090: }
091: }
092:
093: if (log.isDebugEnabled()) {
094: log.debug("MediaType: " + mediaType.getName());
095: log.debug("Encoding: " + encoding);
096: log.debug("Mimetype: " + mimeType.getName());
097: }
098:
099: // Put the encoding in the request
100: request.setCharacterEncoding(encoding);
101:
102: // Put the CapabilityMap into the request
103: request.setCapabilityMap(cm);
104:
105: // Put the Media Type into the request
106: request.setMediaType(mediaType.getName());
107:
108: // Put the Mime Type into the request
109: request.setMimeType(mimeType.getName());
110:
111: // Put the Mime Type and Charset into the response
112: StringBuffer contentType = new StringBuffer(mimeType.getName());
113: if (encoding != null) {
114: contentType.append("; charset=" + encoding);
115: }
116: String type = contentType.toString(); //mapContentType(request, contentType.toString());
117: request.getResponse().setContentType(type);
118:
119: // Pass control to the next Valve in the Pipeline
120: context.invokeNext(request);
121: }
122:
123: static String[][] MIME_MAP = { { ".pdf", "application/pdf" } };
124:
125: protected String mapContentType(RequestContext request,
126: String contentType) {
127: // TODO: get path from servlet request
128: // this code below fails since the path is not yet parsed
129: // to far up in the pipeline
130: String path = request.getPath();
131: if (path != null) {
132: for (int ix = 0; ix < MIME_MAP.length; ix++) {
133: if (path.endsWith(MIME_MAP[ix][0])) {
134: return MIME_MAP[ix][1];
135: }
136: }
137: }
138: return contentType;
139: }
140:
141: public String toString() {
142: return "CapabilityValveImpl";
143: }
144: }
|