KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > protocols > HelpURLConnection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.protocols;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.HttpURLConnection JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLConnection JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import java.util.Vector JavaDoc;
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 /**
37  * URLConnection to help documents in plug-ins
38  */

39 public class HelpURLConnection extends URLConnection JavaDoc {
40
41     private final static String JavaDoc PARAM_LANG = "lang"; //$NON-NLS-1$
42
private final static String JavaDoc PRODUCT_PLUGIN = "PRODUCT_PLUGIN"; //$NON-NLS-1$
43
public final static String JavaDoc PLUGINS_ROOT = "PLUGINS_ROOT/"; //$NON-NLS-1$
44
private final static String JavaDoc PATH_RTOPIC = "/rtopic"; //$NON-NLS-1$
45
// document caching - disabled if running in dev mode
46
protected static boolean cachingEnabled = true;
47     static {
48         String JavaDoc[] args = Platform.getCommandLineArgs();
49         for (int i = 0; i < args.length; i++) {
50             if ("-dev".equals(args[i])) { //$NON-NLS-1$
51
cachingEnabled = false;
52                 break;
53             }
54         }
55     }
56
57     protected String JavaDoc pluginAndFile; // plugin/file
58
protected String JavaDoc query; // after ?
59
protected HashMap JavaDoc arguments;
60     protected Bundle plugin;
61     // file in a plug-in
62
protected String JavaDoc file;
63     protected String JavaDoc locale;
64     private static String JavaDoc appserverImplPluginId;
65
66     /**
67      * Constructor for HelpURLConnection
68      */

69     public HelpURLConnection(URL JavaDoc url) {
70         super(url);
71
72         String JavaDoc urlFile = url.getFile();
73
74         // Strip off everything before and including the PLUGINS_ROOT
75
int index = urlFile.indexOf(PLUGINS_ROOT);
76         if (index!= -1)
77             urlFile = urlFile.substring(index+PLUGINS_ROOT.length());
78         // Strip off the leading "/" and the query
79
if (urlFile.startsWith("/")) //$NON-NLS-1$
80
urlFile = urlFile.substring(1);
81
82         int indx = urlFile.indexOf("?"); //$NON-NLS-1$
83
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     /**
94      * @see URLConnection#connect()
95      */

96     public void connect() throws IOException JavaDoc {
97     }
98
99     /**
100      * see URLConnection#getInputStream(); Note: this method can throw IOException, but should never
101      * return null
102      */

103     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
104         // must override parent implementation, since it does nothing.
105
Bundle plugin = getPlugin();
106         if (plugin != null && plugin.getSymbolicName().equals(getAppserverImplPluginId())) {
107             // Do not return documents from app server implementation plug-in
108
throw new IOException JavaDoc("Resource not found."); //$NON-NLS-1$
109
}
110
111         if (getFile() == null || "".equals(getFile())) { //$NON-NLS-1$
112
throw new IOException JavaDoc("Resource not found."); //$NON-NLS-1$
113
}
114
115         InputStream JavaDoc in = null;
116         if (plugin != null) {
117             // first try using content provider, then try to find the file
118
// inside doc.zip, and finally try the file system
119
in = ResourceLocator.openFromProducer(plugin, query == null ? getFile()
120                     : getFile() + "?" + query, //$NON-NLS-1$
121
getLocale());
122     
123             if (in == null) {
124                 in = ResourceLocator.openFromZip(plugin, "doc.zip", //$NON-NLS-1$
125
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 JavaDoc("Resource not found."); //$NON-NLS-1$
136
}
137         return in;
138     }
139
140     public long getExpiration() {
141         return isCacheable() ? new Date JavaDoc().getTime() + 10000 : 0;
142     }
143
144     public static void parseQuery(String JavaDoc query, HashMap JavaDoc arguments) {
145         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(query, "&"); //$NON-NLS-1$
146
while (stok.hasMoreTokens()) {
147             String JavaDoc aQuery = stok.nextToken();
148             int equalsPosition = aQuery.indexOf("="); //$NON-NLS-1$
149
if (equalsPosition > -1) { // well formed name/value pair
150
String JavaDoc arg = aQuery.substring(0, equalsPosition);
151                 String JavaDoc val = aQuery.substring(equalsPosition + 1);
152                 Object JavaDoc existing = arguments.get(arg);
153                 if (existing == null)
154                     arguments.put(arg, val);
155                 else if (existing instanceof Vector JavaDoc) {
156                     ((Vector JavaDoc) existing).add(val);
157                     arguments.put(arg, existing);
158                 } else {
159                     Vector JavaDoc v = new Vector JavaDoc(2);
160                     v.add(existing);
161                     v.add(val);
162                     arguments.put(arg, v);
163                 }
164             }
165         }
166     }
167
168     /**
169      * NOTE: need to add support for multi-valued parameters (like filtering) Multiple values are
170      * added as vectors
171      */

172     protected void parseQuery() {
173         if (query != null && !"".equals(query)) { //$NON-NLS-1$
174
if (arguments == null) {
175                 arguments = new HashMap JavaDoc(5);
176             }
177             parseQuery(query, arguments);
178         }
179     }
180
181     public String JavaDoc getContentType() {
182         // Check if the file is hypertext or plain text
183
String JavaDoc file = pluginAndFile.toLowerCase(Locale.US);
184         if (file.endsWith(".html") || file.endsWith(".htm") //$NON-NLS-1$ //$NON-NLS-2$
185
|| file.endsWith(".xhtml")) //$NON-NLS-1$
186
return "text/html"; //$NON-NLS-1$
187
else if (file.endsWith(".css")) //$NON-NLS-1$
188
return "text/css"; //$NON-NLS-1$
189
else if (file.endsWith(".gif")) //$NON-NLS-1$
190
return "image/gif"; //$NON-NLS-1$
191
else if (file.endsWith(".jpg")) //$NON-NLS-1$
192
return "image/jpeg"; //$NON-NLS-1$
193
else if (file.endsWith(".pdf")) //$NON-NLS-1$
194
return "application/pdf"; //$NON-NLS-1$
195
else if (file.endsWith(".xml")) //$NON-NLS-1$
196
return "application/xml"; //$NON-NLS-1$
197
else if (file.endsWith(".xsl")) //$NON-NLS-1$
198
return "application/xsl"; //$NON-NLS-1$
199
return "text/plain"; //$NON-NLS-1$
200
}
201
202     /**
203      *
204      */

205     public Vector JavaDoc getMultiValue(String JavaDoc name) {
206         if (arguments != null) {
207             Object JavaDoc value = arguments.get(name);
208             if (value instanceof Vector JavaDoc)
209                 return (Vector JavaDoc) value;
210             return null;
211         }
212         return null;
213     }
214
215     /**
216      *
217      */

218     public String JavaDoc getValue(String JavaDoc name) {
219         if (arguments == null)
220             return null;
221         Object JavaDoc value = arguments.get(name);
222         String JavaDoc stringValue = null;
223         if (value instanceof String JavaDoc)
224             stringValue = (String JavaDoc) value;
225         else if (value instanceof Vector JavaDoc)
226             stringValue = (String JavaDoc) ((Vector JavaDoc) value).firstElement();
227         else
228             return null;
229         try {
230             return URLCoder.decode(stringValue);
231         } catch (Exception JavaDoc e) {
232             return null;
233         }
234
235     }
236
237     /**
238      * Returns the locale specified by client.
239      */

240     protected String JavaDoc 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 JavaDoc getFile() {
251         if (file == null) {
252             // Strip the plugin id
253
int start = pluginAndFile.indexOf("/") + 1; //$NON-NLS-1$
254
// Strip query string or anchor bookmark
255
int end = pluginAndFile.indexOf("?"); //$NON-NLS-1$
256
if (end == -1)
257                 end = pluginAndFile.indexOf("#"); //$NON-NLS-1$
258
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             // Assume the url is pluginID/path_to_topic.html
269
int i = pluginAndFile.indexOf('/');
270             String JavaDoc pluginId = i == -1 ? "" : pluginAndFile.substring(0, i); //$NON-NLS-1$
271
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 JavaDoc getHref() {
285         return '/' + pluginAndFile;
286     }
287     
288     public boolean isCacheable() {
289         if (getValue("resultof") != null) //$NON-NLS-1$
290
return false;
291         return cachingEnabled;
292     }
293
294     public String JavaDoc toString() {
295         return pluginAndFile;
296     }
297
298     /**
299      * Obtains ID of plugin that contributes appserver implementation. *
300      *
301      * @return plug-in ID or null
302      */

303     private static String JavaDoc getAppserverImplPluginId() {
304         if (appserverImplPluginId == null) {
305
306             // This part mimics AppserverPlugin.createWebappServer()
307

308             // get the app server extension from the system plugin registry
309
IExtensionRegistry pluginRegistry = Platform.getExtensionRegistry();
310             IExtensionPoint point = pluginRegistry.getExtensionPoint("org.eclipse.help.appserver.server"); //$NON-NLS-1$
311
if (point != null) {
312                 IExtension[] extensions = point.getExtensions();
313                 if (extensions.length != 0) {
314                     // We need to pick up the non-default configuration
315
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 JavaDoc defaultValue = elements[i].getAttribute("default"); //$NON-NLS-1$
321
if (defaultValue == null || defaultValue.equals("false")) { //$NON-NLS-1$
322
serverElement = elements[i];
323                             break;
324                         }
325                     }
326                     // if all the servers are default, then pick the first one
327
if (serverElement == null) {
328                         serverElement = elements[0];
329                     }
330                     //
331

332                     appserverImplPluginId = serverElement.getContributor().getName();
333
334                 }
335             }
336         }
337         return appserverImplPluginId;
338     }
339
340     /*
341      * Opens a connection to the document on the remote help server, if one
342      * was specified. If the document doesn't exist on the remote server,
343      * returns null.
344      */

345     private InputStream JavaDoc openFromRemoteServer(String JavaDoc href, String JavaDoc locale) {
346         if (RemoteHelp.isEnabled()) {
347             try {
348                 URL JavaDoc url = RemoteHelp.getURL(PATH_RTOPIC + href + '?' + PARAM_LANG + '=' + locale);
349                 HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)url.openConnection();
350                 if (connection.getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND) {
351                     return connection.getInputStream();
352                 }
353             }
354             catch (IOException JavaDoc e) {
355                 String JavaDoc msg = "I/O error while trying to contact the remote help server"; //$NON-NLS-1$
356
HelpBasePlugin.logError(msg, e);
357             }
358         }
359         return null;
360     }
361
362 }
363
Popular Tags