KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > servlet > ControlServlet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.webapp.servlet;
12
13 import java.io.*;
14 import java.lang.reflect.*;
15
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.help.internal.base.*;
21 import org.eclipse.help.internal.webapp.data.*;
22 import org.osgi.framework.*;
23
24 /**
25  * Servlet to control Eclipse helpApplication from standalone application.
26  * Accepts the following parameters: command=displayHelp | shutdown
27  * | install | update | enable | disable | uninstall | search | listFeatures
28  * | addSite | removeSite | apply.
29  * href - may be provided if comand==displayHelp.
30  * featureId, version, from, to, verifyOnly may be provided for update commands
31  */

32 public class ControlServlet extends HttpServlet {
33     private static final long serialVersionUID = 1L;
34     public static final String JavaDoc UPDATE_PLUGIN_ID = "org.eclipse.update.core"; //$NON-NLS-1$
35

36     public static final String JavaDoc CMD_DISPLAYHELP = "displayHelp"; //$NON-NLS-1$
37

38     public static final String JavaDoc CMD_SHUTDOWN = "shutdown"; //$NON-NLS-1$
39

40     public static final String JavaDoc CMD_INSTALL = "install"; //$NON-NLS-1$
41

42     public static final String JavaDoc CMD_UPDATE = "update"; //$NON-NLS-1$
43

44     public static final String JavaDoc CMD_ENABLE = "enable"; //$NON-NLS-1$
45

46     public static final String JavaDoc CMD_DISABLE = "disable"; //$NON-NLS-1$
47

48     public static final String JavaDoc CMD_UNINSTALL = "uninstall"; //$NON-NLS-1$
49

50     public static final String JavaDoc CMD_SEARCH = "search"; //$NON-NLS-1$
51

52     public static final String JavaDoc CMD_LIST = "listFeatures"; //$NON-NLS-1$
53

54     public static final String JavaDoc CMD_ADDSITE = "addSite"; //$NON-NLS-1$
55

56     public static final String JavaDoc CMD_APPLY = "apply"; //$NON-NLS-1$
57

58     public static final String JavaDoc CMD_REMOVESITE = "removeSite"; //$NON-NLS-1$
59

60     public static final String JavaDoc PACKAGE_PREFIX = "org.eclipse.update.standalone."; //$NON-NLS-1$
61

62     public static final String JavaDoc CLASS_INSTALL = PACKAGE_PREFIX
63             + "InstallCommand"; //$NON-NLS-1$
64

65     public static final String JavaDoc CLASS_UPDATE = PACKAGE_PREFIX + "UpdateCommand"; //$NON-NLS-1$
66

67     public static final String JavaDoc CLASS_ENABLE = PACKAGE_PREFIX + "EnableCommand"; //$NON-NLS-1$
68

69     public static final String JavaDoc CLASS_DISABLE = PACKAGE_PREFIX
70             + "DisableCommand"; //$NON-NLS-1$
71

72     public static final String JavaDoc CLASS_UNINSTALL = PACKAGE_PREFIX
73             + "UninstallCommand"; //$NON-NLS-1$
74

75     public static final String JavaDoc CLASS_SEARCH = PACKAGE_PREFIX + "SearchCommand"; //$NON-NLS-1$
76

77     public static final String JavaDoc CLASS_LIST = PACKAGE_PREFIX
78             + "ListFeaturesCommand"; //$NON-NLS-1$
79

80     public static final String JavaDoc CLASS_ADDSITE = PACKAGE_PREFIX
81             + "AddSiteCommand"; //$NON-NLS-1$
82

83     public static final String JavaDoc CLASS_REMOVESITE = PACKAGE_PREFIX
84             + "RemoveSiteCommand"; //$NON-NLS-1$
85

86     public static final String JavaDoc PARAM_FEATUREID = "featureId"; //$NON-NLS-1$
87

88     public static final String JavaDoc PARAM_VERSION = "version"; //$NON-NLS-1$
89

90     public static final String JavaDoc PARAM_FROM = "from"; //$NON-NLS-1$
91

92     public static final String JavaDoc PARAM_TO = "to"; //$NON-NLS-1$
93

94     public static final String JavaDoc PARAM_VERIFYONLY = "verifyOnly"; //$NON-NLS-1$
95

96     private HelpDisplay helpDisplay = null;
97
98     private boolean shuttingDown = false;
99
100     /**
101      * Called by the servlet container to indicate to a servlet that the servlet
102      * is being placed into service.
103      */

104     public void init() throws ServletException {
105         super.init();
106         if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_STANDALONE) {
107             helpDisplay = BaseHelpSystem.getHelpDisplay();
108         }
109     }
110
111     /**
112      * Called by the server (via the <code>service</code> method) to allow a
113      * servlet to handle a GET request.
114      */

115     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
116             throws ServletException, IOException {
117         processRequest(req, resp);
118     }
119
120     /**
121      * Called by the server (via the <code>service</code> method) to allow a
122      * servlet to handle a POST request.
123      */

124     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
125             throws ServletException, IOException {
126         processRequest(req, resp);
127     }
128
129     private void processRequest(HttpServletRequest req, HttpServletResponse resp)
130             throws ServletException, IOException {
131
132         req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
133

134         // for HTTP 1.1
135
resp.setHeader("Cache-Control", "no-cache"); //$NON-NLS-1$ //$NON-NLS-2$
136
// for HTTP 1.0
137
resp.setHeader("Pragma", "no-cache"); //$NON-NLS-1$ //$NON-NLS-2$
138
resp.setDateHeader("Expires", 0); //$NON-NLS-1$
139
//prevents caching at the proxy server
140

141         if (!UrlUtil.isLocalRequest(req)) {
142             // do not allow remote clients to execute this servlet
143
return;
144         }
145         if (!"/help".equals(req.getContextPath()) //$NON-NLS-1$
146
|| !"/control".equals(req.getServletPath())) { //$NON-NLS-1$
147
// do not allow arbitrary URLs to execute this servlet
148
resp.sendError(HttpServletResponse.SC_FORBIDDEN, ""); //$NON-NLS-1$
149
return;
150         }
151
152         if (shuttingDown) {
153             return;
154         }
155
156         String JavaDoc command = req.getParameter("command"); //$NON-NLS-1$
157
if (command == null) {
158             // this should never happen and is invisible to the user
159
resp.getWriter().print("No command."); //$NON-NLS-1$
160
return;
161         }
162
163         if (CMD_SHUTDOWN.equalsIgnoreCase(command)) {
164             shutdown();
165         } else if (CMD_DISPLAYHELP.equalsIgnoreCase(command)) {
166             if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_STANDALONE) {
167                 displayHelp(req);
168             }
169         } else if (CMD_INSTALL.equalsIgnoreCase(command)
170                 || CMD_ENABLE.equalsIgnoreCase(command)
171                 || CMD_UPDATE.equalsIgnoreCase(command)
172                 || CMD_DISABLE.equalsIgnoreCase(command)
173                 || CMD_UNINSTALL.equalsIgnoreCase(command)
174                 || CMD_SEARCH.equalsIgnoreCase(command)
175                 || CMD_LIST.equalsIgnoreCase(command)
176                 || CMD_ADDSITE.equalsIgnoreCase(command)
177                 || CMD_REMOVESITE.equalsIgnoreCase(command)
178                 || CMD_APPLY.equalsIgnoreCase(command)) {
179             updateDocs(command, req);
180         } else {
181             // this should never happen and is invisible to the user
182
resp.getWriter().print("Unrecognized command."); //$NON-NLS-1$
183
}
184     }
185
186     private void updateDocs(String JavaDoc command, HttpServletRequest req) {
187         Bundle bundle = Platform.getBundle(UPDATE_PLUGIN_ID);
188         if (bundle == null) {
189             // no update plugin present
190
return;
191         }
192         try {
193             String JavaDoc className = getStandaloneClassName(command);
194             if (className == null) {
195                 System.out.println("No class name for command " + command); //$NON-NLS-1$
196
return;
197             }
198             Class JavaDoc c = bundle.loadClass(className);
199             if (c == null) {
200                 System.out.println("No class for command " + command); //$NON-NLS-1$
201
return;
202             }
203             Class JavaDoc[] parameterTypes = getParameterTypes(className);
204             Constructor constr = c.getConstructor(parameterTypes);
205             if (constr == null) {
206                 System.out.println("No expected constructor for command " //$NON-NLS-1$
207
+ command);
208                 return;
209             }
210             Method m;
211             if (!CMD_APPLY.equalsIgnoreCase(command)) {
212                 m = c.getMethod("run", new Class JavaDoc[]{}); //$NON-NLS-1$
213
} else {
214                 m = c.getMethod("applyChangesNow", new Class JavaDoc[]{}); //$NON-NLS-1$
215
}
216             Object JavaDoc[] initargs = getInitArgs(className, req);
217             Object JavaDoc o = constr.newInstance(initargs);
218             Object JavaDoc ret = m.invoke(o, new Object JavaDoc[]{});
219             if (!CMD_APPLY.equalsIgnoreCase(command) &&((Boolean JavaDoc)ret).equals(Boolean.FALSE)){
220                 System.out.println("Command not executed."); //$NON-NLS-1$
221
} else {
222                 System.out.println("Command executed."); //$NON-NLS-1$
223
}
224         } catch (Exception JavaDoc e) {
225             Throwable JavaDoc t = e;
226             if (e instanceof InvocationTargetException) {
227                 t = ((InvocationTargetException) e).getTargetException();
228             }
229             System.out.println(t.getLocalizedMessage());
230             //t.printStackTrace();
231
}
232     }
233
234     private String JavaDoc getStandaloneClassName(String JavaDoc command) {
235         if (CMD_INSTALL.equalsIgnoreCase(command))
236             return CLASS_INSTALL;
237         else if (CMD_UPDATE.equalsIgnoreCase(command))
238             return CLASS_UPDATE;
239         else if (CMD_ENABLE.equalsIgnoreCase(command))
240             return CLASS_ENABLE;
241         else if (CMD_DISABLE.equalsIgnoreCase(command))
242             return CLASS_DISABLE;
243         else if (CMD_UNINSTALL.equalsIgnoreCase(command))
244             return CLASS_UNINSTALL;
245         else if (CMD_SEARCH.equalsIgnoreCase(command))
246             return CLASS_SEARCH;
247         else if (CMD_LIST.equalsIgnoreCase(command)
248                 || CMD_APPLY.equalsIgnoreCase(command))
249             return CLASS_LIST;
250         else if (CMD_ADDSITE.equalsIgnoreCase(command))
251             return CLASS_ADDSITE;
252         else if (CMD_REMOVESITE.equalsIgnoreCase(command))
253             return CLASS_REMOVESITE;
254         else
255             return null;
256     }
257
258     private Class JavaDoc[] getParameterTypes(String JavaDoc className) {
259         if (CLASS_INSTALL.equals(className))
260             return new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class,
261                     String JavaDoc.class, String JavaDoc.class};
262         else if (CLASS_UPDATE.equals(className))
263             return new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class};
264         else if (CLASS_ENABLE.equals(className)
265                 || CLASS_DISABLE.equals(className)
266                 || CLASS_UNINSTALL.equals(className))
267             return new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class,
268                     String JavaDoc.class};
269         else
270             return new Class JavaDoc[]{String JavaDoc.class};
271     }
272
273     private Object JavaDoc[] getInitArgs(String JavaDoc className, HttpServletRequest req) {
274         String JavaDoc featureId = req.getParameter(PARAM_FEATUREID);
275         String JavaDoc version = req.getParameter(PARAM_VERSION);
276         String JavaDoc fromSite = req.getParameter(PARAM_FROM);
277         String JavaDoc toSite = req.getParameter(PARAM_TO);
278         String JavaDoc verifyOnly = req.getParameter(PARAM_VERIFYONLY);
279         if (CLASS_INSTALL.equals(className))
280             return new Object JavaDoc[]{featureId, version, fromSite, toSite,
281                     verifyOnly};
282         else if (CLASS_UPDATE.equals(className))
283             return new Object JavaDoc[]{featureId, version, verifyOnly};
284         else if (CLASS_ENABLE.equals(className)
285                 || CLASS_DISABLE.equals(className)
286                 || CLASS_UNINSTALL.equals(className))
287             return new Object JavaDoc[]{featureId, version, toSite, verifyOnly};
288         else if (CLASS_REMOVESITE.equals(className))
289             return new Object JavaDoc[]{toSite};
290         else
291             return new Object JavaDoc[]{fromSite};
292     }
293
294     /**
295      * Shuts-down Eclipse helpApplication.
296      */

297     private void shutdown() {
298         shuttingDown = true;
299         HelpApplication.stopHelp();
300     }
301
302     /**
303      * Displays help.
304      *
305      * @param req
306      * HttpServletRequest that might contain href parameter, which is
307      * the resource to display
308      */

309     private void displayHelp(HttpServletRequest req) {
310         String JavaDoc href = req.getParameter("href"); //$NON-NLS-1$
311
if (href != null) {
312             helpDisplay.displayHelpResource(href, false);
313         } else {
314             helpDisplay.displayHelp(false);
315         }
316     }
317 }
318
Popular Tags