1 16 package org.apache.cocoon.components.url; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.util.Iterator ; 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 39 public class SourceProtocolHandler extends AbstractParsedURLProtocolHandler { 40 41 42 protected final static InheritableThreadLocal localResolver = new InheritableThreadLocal (); 43 44 45 protected final static ParsedURLProtocolHandler defaultHandler; 46 47 58 static { 59 defaultHandler = ParsedURL.getHandler(null); 61 62 ParsedURL.registerHandler(new SourceProtocolHandler(null)); 64 65 ImageTagRegistry.getRegistry().register(new StreamJDKRegistryEntry()); 67 } 68 69 72 public static void setup(SourceResolver resolver) { 73 localResolver.set(resolver); 74 } 75 76 79 public static SourceResolver getSourceResolver() 80 { 81 SourceResolver resolver = (SourceResolver)localResolver.get(); 82 return resolver; 83 } 84 85 87 public SourceProtocolHandler(String protocol) { 88 super(protocol); 89 } 90 91 public ParsedURLData parseURL(ParsedURL baseURL, String urlStr) { 92 SourceResolver resolver = (SourceResolver)localResolver.get(); 93 if (resolver == null) { 94 return defaultHandler == null ? null : defaultHandler.parseURL(baseURL, urlStr); 96 } else { 97 return new SourceParsedURLData(urlStr, resolver); 98 } 99 } 100 101 public ParsedURLData parseURL(String 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 113 static class SourceParsedURLData extends ParsedURLData { 114 public String url; 115 116 private Source source; 117 private SourceResolver resolver; 118 119 public SourceParsedURLData(String urlStr, SourceResolver resolver) { 120 this.url = urlStr; 121 this.resolver = resolver; 122 123 int pidx=0, idx; 126 127 idx = urlStr.indexOf(':'); 128 if (idx != -1) { 129 this.protocol = urlStr.substring(pidx, idx); 131 if (this.protocol.indexOf('/') == -1) { 132 pidx = idx+1; 133 } else { 134 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 152 try { 154 this.source = resolver.resolveURI(this.url); 155 } catch(Exception e) { 156 throw new CascadingRuntimeException("Cannot resolve " + this.url, e); 157 } 158 159 161 this.contentType = this.source.getMimeType(); 163 164 if (this.contentType == null) { 165 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 getPortStr() { 181 String portStr = protocol+":"; 182 if (host != null) portStr += host; 183 portStr += ","; 184 return portStr; 185 } 186 187 public String toString() { 188 return this.url; 189 } 190 191 195 protected InputStream openStreamInternal (String userAgent, Iterator mimeTypes, Iterator encodingTypes) 196 throws IOException { 197 198 try { 199 return source.getInputStream(); 200 } catch(Exception 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 |