KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > url > SourceProtocolHandler


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.url;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.avalon.framework.CascadingRuntimeException;
23 import org.apache.batik.ext.awt.image.spi.ImageTagRegistry;
24 import org.apache.batik.util.AbstractParsedURLProtocolHandler;
25 import org.apache.batik.util.ParsedURL;
26 import org.apache.batik.util.ParsedURLData;
27 import org.apache.batik.util.ParsedURLProtocolHandler;
28 import org.apache.cocoon.CascadingIOException;
29 import org.apache.excalibur.source.Source;
30 import org.apache.excalibur.source.SourceResolver;
31
32 /**
33  * A Batik protocol handler that handles all Cocoon sources. This allows
34  * <svg:image xlink:href="..."/> to use any of the protocols handled by Cocoon.
35  *
36  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
37  * @version CVS $Id: SourceProtocolHandler.java 123965 2005-01-03 13:42:33Z antonio $
38  */

39 public class SourceProtocolHandler extends AbstractParsedURLProtocolHandler {
40
41     /** Thread-local source resolver */
42     protected final static InheritableThreadLocal JavaDoc localResolver = new InheritableThreadLocal JavaDoc();
43
44     /** Batik's original default handler */
45     protected final static ParsedURLProtocolHandler defaultHandler;
46
47     /**
48      * Change the default handler used by Batik to resolve URLs to a handler
49      * based on <code>SourceResolver</code> and <code>SourceHandler</code>.
50      * <p>
51      * Note : Batik handlers are defined statically, and are thus shared by
52      * all its users. However, this shouldn't be a problem since different
53      * web applications live in different classloaders.
54      *
55      * @param manager the component manager used to get the <code>SourceHandler</code>
56      * @param logger the logger for logging.
57      */

58     static {
59         // Keep the default handler, if any
60
defaultHandler = ParsedURL.getHandler(null);
61         
62         // Set the default handler to our handler
63
ParsedURL.registerHandler(new SourceProtocolHandler(null));
64
65         // Add a new image registry entry to handle image streams
66
ImageTagRegistry.getRegistry().register(new StreamJDKRegistryEntry());
67     }
68
69     /**
70      * Set the resolver to be used within the current thread.
71      */

72     public static void setup(SourceResolver resolver) {
73         localResolver.set(resolver);
74     }
75
76     /**
77      * Get the thread-local resolver.
78      */

79     public static SourceResolver getSourceResolver()
80     {
81         SourceResolver resolver = (SourceResolver)localResolver.get();
82         return resolver;
83     }
84
85     //-------------------------------------------------------------------------
86

87     public SourceProtocolHandler(String JavaDoc protocol) {
88         super(protocol);
89     }
90
91     public ParsedURLData parseURL(ParsedURL baseURL, String JavaDoc urlStr) {
92         SourceResolver resolver = (SourceResolver)localResolver.get();
93         if (resolver == null) {
94             // Fall back to the previous default handler
95
return defaultHandler == null ? null : defaultHandler.parseURL(baseURL, urlStr);
96         } else {
97             return new SourceParsedURLData(urlStr, resolver);
98         }
99     }
100
101     public ParsedURLData parseURL(String JavaDoc urlStr) {
102         SourceResolver resolver = (SourceResolver)localResolver.get();
103         if (resolver == null) {
104             return defaultHandler == null ? null : defaultHandler.parseURL(urlStr);
105         } else {
106             return new SourceParsedURLData(urlStr, resolver);
107         }
108     }
109
110     /**
111      * Reimplementation of some methods of ParsedURLData since we cannot use <code>java.net.URL</code>.
112      */

113     static class SourceParsedURLData extends ParsedURLData {
114         public String JavaDoc url;
115
116         private Source source;
117         private SourceResolver resolver;
118
119         public SourceParsedURLData(String JavaDoc urlStr, SourceResolver resolver) {
120             this.url = urlStr;
121             this.resolver = resolver;
122             
123             // ParsedURLData has some public members which seems to be required to
124
// have a value. This sucks.
125
int pidx=0, idx;
126
127             idx = urlStr.indexOf(':');
128             if (idx != -1) {
129                 // May have a protocol spec...
130
this.protocol = urlStr.substring(pidx, idx);
131                 if (this.protocol.indexOf('/') == -1) {
132                     pidx = idx+1;
133                 } else {
134                     // Got a slash in protocol probably means
135
// no protocol given, (host and port?)
136
this.protocol = null;
137                     pidx = 0;
138                 }
139             }
140
141             idx = urlStr.indexOf(',',pidx);
142             if (idx != -1) {
143                 this.host = urlStr.substring(pidx, idx);
144                 pidx = idx+1;
145             }
146             if (pidx != urlStr.length()) {
147                 this.path = urlStr.substring(pidx);
148             }
149
150             // Now do the real job
151

152             // Setup source
153
try {
154                 this.source = resolver.resolveURI(this.url);
155             } catch(Exception JavaDoc e) {
156                 throw new CascadingRuntimeException("Cannot resolve " + this.url, e);
157             }
158
159             // Get Mime-type
160

161             // First try the source itself
162
this.contentType = this.source.getMimeType();
163
164             if (this.contentType == null) {
165                 // Guess it from the URL extension
166
if (url.endsWith(".gif")) {
167                     this.contentType = "image/gif";
168                 } else if (url.endsWith(".jpeg") || url.endsWith(".jpg")) {
169                     this.contentType = "image/jpeg";
170                 } else if (url.endsWith(".png")) {
171                     this.contentType = "image/png";
172                 }
173             }
174         }
175
176         public boolean complete() {
177             return (this.url != null);
178         }
179
180         public String JavaDoc getPortStr() {
181             String JavaDoc portStr = protocol+":";
182             if (host != null) portStr += host;
183             portStr += ",";
184             return portStr;
185         }
186
187         public String JavaDoc toString() {
188             return this.url;
189         }
190
191         /**
192          * Open a stream for the data. If the thread-local <code>SourceResolver</code> exists,
193          * then use it, otherwise fall back to <code>SourceHandler</code>.
194          */

195         protected InputStream JavaDoc openStreamInternal (String JavaDoc userAgent, Iterator JavaDoc mimeTypes, Iterator JavaDoc encodingTypes)
196             throws IOException JavaDoc {
197
198             try {
199                 return source.getInputStream();
200             } catch(Exception JavaDoc e) {
201                 throw new CascadingIOException("Cannot open URL " + this.url, e);
202             } finally {
203                 this.resolver.release(this.source);
204             }
205         }
206     }
207 }
208
Popular Tags