1 11 package org.eclipse.help.internal.protocols; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.HttpURLConnection ; 16 import java.net.URL ; 17 import java.net.URLConnection ; 18 import java.util.Date ; 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 import java.util.StringTokenizer ; 22 import java.util.Vector ; 23 24 import org.eclipse.core.runtime.IConfigurationElement; 25 import org.eclipse.core.runtime.IExtension; 26 import org.eclipse.core.runtime.IExtensionPoint; 27 import org.eclipse.core.runtime.IExtensionRegistry; 28 import org.eclipse.core.runtime.IProduct; 29 import org.eclipse.core.runtime.Platform; 30 import org.eclipse.help.internal.base.HelpBasePlugin; 31 import org.eclipse.help.internal.base.remote.RemoteHelp; 32 import org.eclipse.help.internal.util.ResourceLocator; 33 import org.eclipse.help.internal.util.URLCoder; 34 import org.osgi.framework.Bundle; 35 36 39 public class HelpURLConnection extends URLConnection { 40 41 private final static String PARAM_LANG = "lang"; private final static String PRODUCT_PLUGIN = "PRODUCT_PLUGIN"; public final static String PLUGINS_ROOT = "PLUGINS_ROOT/"; private final static String PATH_RTOPIC = "/rtopic"; protected static boolean cachingEnabled = true; 47 static { 48 String [] args = Platform.getCommandLineArgs(); 49 for (int i = 0; i < args.length; i++) { 50 if ("-dev".equals(args[i])) { cachingEnabled = false; 52 break; 53 } 54 } 55 } 56 57 protected String pluginAndFile; protected String query; protected HashMap arguments; 60 protected Bundle plugin; 61 protected String file; 63 protected String locale; 64 private static String appserverImplPluginId; 65 66 69 public HelpURLConnection(URL url) { 70 super(url); 71 72 String urlFile = url.getFile(); 73 74 int index = urlFile.indexOf(PLUGINS_ROOT); 76 if (index!= -1) 77 urlFile = urlFile.substring(index+PLUGINS_ROOT.length()); 78 if (urlFile.startsWith("/")) urlFile = urlFile.substring(1); 81 82 int indx = urlFile.indexOf("?"); if (indx != -1) { 84 query = urlFile.substring(indx + 1); 85 urlFile = urlFile.substring(0, indx); 86 } 87 this.pluginAndFile = urlFile; 88 parseQuery(); 89 90 setDefaultUseCaches(isCacheable()); 91 } 92 93 96 public void connect() throws IOException { 97 } 98 99 103 public InputStream getInputStream() throws IOException { 104 Bundle plugin = getPlugin(); 106 if (plugin != null && plugin.getSymbolicName().equals(getAppserverImplPluginId())) { 107 throw new IOException ("Resource not found."); } 110 111 if (getFile() == null || "".equals(getFile())) { throw new IOException ("Resource not found."); } 114 115 InputStream in = null; 116 if (plugin != null) { 117 in = ResourceLocator.openFromProducer(plugin, query == null ? getFile() 120 : getFile() + "?" + query, getLocale()); 122 123 if (in == null) { 124 in = ResourceLocator.openFromZip(plugin, "doc.zip", getFile(), getLocale()); 126 } 127 if (in == null) { 128 in = ResourceLocator.openFromPlugin(plugin, getFile(), getLocale()); 129 } 130 } 131 else { 132 in = openFromRemoteServer(getHref(), getLocale()); 133 } 134 if (in == null) { 135 throw new IOException ("Resource not found."); } 137 return in; 138 } 139 140 public long getExpiration() { 141 return isCacheable() ? new Date ().getTime() + 10000 : 0; 142 } 143 144 public static void parseQuery(String query, HashMap arguments) { 145 StringTokenizer stok = new StringTokenizer (query, "&"); while (stok.hasMoreTokens()) { 147 String aQuery = stok.nextToken(); 148 int equalsPosition = aQuery.indexOf("="); if (equalsPosition > -1) { String arg = aQuery.substring(0, equalsPosition); 151 String val = aQuery.substring(equalsPosition + 1); 152 Object existing = arguments.get(arg); 153 if (existing == null) 154 arguments.put(arg, val); 155 else if (existing instanceof Vector ) { 156 ((Vector ) existing).add(val); 157 arguments.put(arg, existing); 158 } else { 159 Vector v = new Vector (2); 160 v.add(existing); 161 v.add(val); 162 arguments.put(arg, v); 163 } 164 } 165 } 166 } 167 168 172 protected void parseQuery() { 173 if (query != null && !"".equals(query)) { if (arguments == null) { 175 arguments = new HashMap (5); 176 } 177 parseQuery(query, arguments); 178 } 179 } 180 181 public String getContentType() { 182 String file = pluginAndFile.toLowerCase(Locale.US); 184 if (file.endsWith(".html") || file.endsWith(".htm") || file.endsWith(".xhtml")) return "text/html"; else if (file.endsWith(".css")) return "text/css"; else if (file.endsWith(".gif")) return "image/gif"; else if (file.endsWith(".jpg")) return "image/jpeg"; else if (file.endsWith(".pdf")) return "application/pdf"; else if (file.endsWith(".xml")) return "application/xml"; else if (file.endsWith(".xsl")) return "application/xsl"; return "text/plain"; } 201 202 205 public Vector getMultiValue(String name) { 206 if (arguments != null) { 207 Object value = arguments.get(name); 208 if (value instanceof Vector ) 209 return (Vector ) value; 210 return null; 211 } 212 return null; 213 } 214 215 218 public String getValue(String name) { 219 if (arguments == null) 220 return null; 221 Object value = arguments.get(name); 222 String stringValue = null; 223 if (value instanceof String ) 224 stringValue = (String ) value; 225 else if (value instanceof Vector ) 226 stringValue = (String ) ((Vector ) value).firstElement(); 227 else 228 return null; 229 try { 230 return URLCoder.decode(stringValue); 231 } catch (Exception e) { 232 return null; 233 } 234 235 } 236 237 240 protected String getLocale() { 241 if (locale == null) { 242 locale = getValue(PARAM_LANG); 243 if (locale == null) { 244 locale = Platform.getNL(); 245 } 246 } 247 return locale; 248 } 249 250 protected String getFile() { 251 if (file == null) { 252 int start = pluginAndFile.indexOf("/") + 1; int end = pluginAndFile.indexOf("?"); if (end == -1) 257 end = pluginAndFile.indexOf("#"); if (end == -1) 259 end = pluginAndFile.length(); 260 file = pluginAndFile.substring(start, end); 261 file = URLCoder.decode(file); 262 } 263 return file; 264 } 265 266 protected Bundle getPlugin() { 267 if (plugin == null) { 268 int i = pluginAndFile.indexOf('/'); 270 String pluginId = i == -1 ? "" : pluginAndFile.substring(0, i); pluginId = URLCoder.decode(pluginId); 272 if (PRODUCT_PLUGIN.equals(pluginId)) { 273 IProduct product = Platform.getProduct(); 274 if (product != null) { 275 plugin = product.getDefiningBundle(); 276 return plugin; 277 } 278 } 279 plugin = Platform.getBundle(pluginId); 280 } 281 return plugin; 282 } 283 284 private String getHref() { 285 return '/' + pluginAndFile; 286 } 287 288 public boolean isCacheable() { 289 if (getValue("resultof") != null) return false; 291 return cachingEnabled; 292 } 293 294 public String toString() { 295 return pluginAndFile; 296 } 297 298 303 private static String getAppserverImplPluginId() { 304 if (appserverImplPluginId == null) { 305 306 308 IExtensionRegistry pluginRegistry = Platform.getExtensionRegistry(); 310 IExtensionPoint point = pluginRegistry.getExtensionPoint("org.eclipse.help.appserver.server"); if (point != null) { 312 IExtension[] extensions = point.getExtensions(); 313 if (extensions.length != 0) { 314 IConfigurationElement[] elements = extensions[0].getConfigurationElements(); 316 if (elements.length == 0) 317 return null; 318 IConfigurationElement serverElement = null; 319 for (int i = 0; i < elements.length; i++) { 320 String defaultValue = elements[i].getAttribute("default"); if (defaultValue == null || defaultValue.equals("false")) { serverElement = elements[i]; 323 break; 324 } 325 } 326 if (serverElement == null) { 328 serverElement = elements[0]; 329 } 330 332 appserverImplPluginId = serverElement.getContributor().getName(); 333 334 } 335 } 336 } 337 return appserverImplPluginId; 338 } 339 340 345 private InputStream openFromRemoteServer(String href, String locale) { 346 if (RemoteHelp.isEnabled()) { 347 try { 348 URL url = RemoteHelp.getURL(PATH_RTOPIC + href + '?' + PARAM_LANG + '=' + locale); 349 HttpURLConnection connection = (HttpURLConnection )url.openConnection(); 350 if (connection.getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND) { 351 return connection.getInputStream(); 352 } 353 } 354 catch (IOException e) { 355 String msg = "I/O error while trying to contact the remote help server"; HelpBasePlugin.logError(msg, e); 357 } 358 } 359 return null; 360 } 361 362 } 363 | Popular Tags |