1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
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
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
105 request.setCharacterEncoding(encoding);
106
107
108 request.setCapabilityMap(cm);
109
110
111 request.setMediaType(mediaType.getName());
112
113
114 request.setMimeType(mimeType.getName());
115
116
117 StringBuffer contentType = new StringBuffer(mimeType.getName());
118 if (encoding != null)
119 {
120 contentType.append("; charset=" + encoding);
121 }
122 String type = contentType.toString();
123 request.getResponse().setContentType(type);
124
125
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
137
138
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 }