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 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class CapabilityValveImpl implements CapabilityValve |
39 |
|
{ |
40 |
0 |
private static final Log log = LogFactory.getLog(CapabilityValveImpl.class); |
41 |
|
String resourceDefault; |
42 |
|
private Capabilities capabilities; |
43 |
|
|
44 |
|
public CapabilityValveImpl( Capabilities capabilities ) |
45 |
0 |
{ |
46 |
0 |
this.capabilities = capabilities; |
47 |
0 |
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
public void initialize() throws PipelineException |
53 |
|
{ |
54 |
|
|
55 |
0 |
} |
56 |
|
|
57 |
|
public void invoke( RequestContext request, ValveContext context ) throws PipelineException |
58 |
|
{ |
59 |
0 |
String agent = request.getRequest().getHeader("User-Agent"); |
60 |
|
|
61 |
|
|
62 |
|
CapabilityMap cm; |
63 |
|
try |
64 |
|
{ |
65 |
0 |
cm = capabilities.getCapabilityMap(agent); |
66 |
|
} |
67 |
0 |
catch (UnableToBuildCapabilityMapException e) |
68 |
|
{ |
69 |
0 |
throw new PipelineException("Falied to create capabilitied: "+e.getMessage(), e); |
70 |
0 |
} |
71 |
|
|
72 |
0 |
MediaType mediaType = cm.getPreferredMediaType(); |
73 |
0 |
MimeType mimeType = cm.getPreferredType(); |
74 |
|
|
75 |
0 |
if (mediaType == null) |
76 |
|
{ |
77 |
0 |
log.error("CapabilityMap returned a null media type"); |
78 |
0 |
throw new PipelineException("CapabilityMap returned a null media type"); |
79 |
|
} |
80 |
|
|
81 |
0 |
if (mimeType == null) |
82 |
|
{ |
83 |
0 |
log.error("CapabilityMap returned a null mime type"); |
84 |
0 |
throw new PipelineException("CapabilityMap returned a null mime type"); |
85 |
|
} |
86 |
|
|
87 |
0 |
String encoding = request.getRequest().getCharacterEncoding(); |
88 |
|
|
89 |
0 |
if (encoding == null) |
90 |
|
{ |
91 |
0 |
if (mediaType != null && mediaType.getCharacterSet() != class="keyword">null) |
92 |
|
{ |
93 |
0 |
encoding = mediaType.getCharacterSet(); |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
0 |
if (log.isDebugEnabled()) |
98 |
|
{ |
99 |
0 |
log.debug("MediaType: " + mediaType.getName()); |
100 |
0 |
log.debug("Encoding: " + encoding); |
101 |
0 |
log.debug("Mimetype: " + mimeType.getName()); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
0 |
request.setCharacterEncoding(encoding); |
106 |
|
|
107 |
|
|
108 |
0 |
request.setCapabilityMap(cm); |
109 |
|
|
110 |
|
|
111 |
0 |
request.setMediaType(mediaType.getName()); |
112 |
|
|
113 |
|
|
114 |
0 |
request.setMimeType(mimeType.getName()); |
115 |
|
|
116 |
|
|
117 |
0 |
StringBuffer contentType = new StringBuffer(mimeType.getName()); |
118 |
0 |
if (encoding != null) |
119 |
|
{ |
120 |
0 |
contentType.append("; charset=" + encoding); |
121 |
|
} |
122 |
0 |
String type = contentType.toString(); |
123 |
0 |
request.getResponse().setContentType(type); |
124 |
|
|
125 |
|
|
126 |
0 |
context.invokeNext(request); |
127 |
0 |
} |
128 |
|
|
129 |
0 |
static String[][] MIME_MAP = |
130 |
|
{ |
131 |
|
{".pdf", "application/pdf"} |
132 |
|
}; |
133 |
|
|
134 |
|
protected String mapContentType(RequestContext request, String contentType) |
135 |
|
{ |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
0 |
String path = request.getPath(); |
140 |
0 |
if (path != null) |
141 |
|
{ |
142 |
0 |
for (int ix=0; ix < MIME_MAP.length; ix++) |
143 |
|
{ |
144 |
0 |
if (path.endsWith(MIME_MAP[ix][0])) |
145 |
|
{ |
146 |
0 |
return MIME_MAP[ix][1]; |
147 |
|
} |
148 |
|
} |
149 |
|
} |
150 |
0 |
return contentType; |
151 |
|
} |
152 |
|
|
153 |
|
public String toString() |
154 |
|
{ |
155 |
0 |
return "CapabilityValveImpl"; |
156 |
|
} |
157 |
|
} |