1 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 32 public class ControlServlet extends HttpServlet { 33 private static final long serialVersionUID = 1L; 34 public static final String UPDATE_PLUGIN_ID = "org.eclipse.update.core"; 36 public static final String CMD_DISPLAYHELP = "displayHelp"; 38 public static final String CMD_SHUTDOWN = "shutdown"; 40 public static final String CMD_INSTALL = "install"; 42 public static final String CMD_UPDATE = "update"; 44 public static final String CMD_ENABLE = "enable"; 46 public static final String CMD_DISABLE = "disable"; 48 public static final String CMD_UNINSTALL = "uninstall"; 50 public static final String CMD_SEARCH = "search"; 52 public static final String CMD_LIST = "listFeatures"; 54 public static final String CMD_ADDSITE = "addSite"; 56 public static final String CMD_APPLY = "apply"; 58 public static final String CMD_REMOVESITE = "removeSite"; 60 public static final String PACKAGE_PREFIX = "org.eclipse.update.standalone."; 62 public static final String CLASS_INSTALL = PACKAGE_PREFIX 63 + "InstallCommand"; 65 public static final String CLASS_UPDATE = PACKAGE_PREFIX + "UpdateCommand"; 67 public static final String CLASS_ENABLE = PACKAGE_PREFIX + "EnableCommand"; 69 public static final String CLASS_DISABLE = PACKAGE_PREFIX 70 + "DisableCommand"; 72 public static final String CLASS_UNINSTALL = PACKAGE_PREFIX 73 + "UninstallCommand"; 75 public static final String CLASS_SEARCH = PACKAGE_PREFIX + "SearchCommand"; 77 public static final String CLASS_LIST = PACKAGE_PREFIX 78 + "ListFeaturesCommand"; 80 public static final String CLASS_ADDSITE = PACKAGE_PREFIX 81 + "AddSiteCommand"; 83 public static final String CLASS_REMOVESITE = PACKAGE_PREFIX 84 + "RemoveSiteCommand"; 86 public static final String PARAM_FEATUREID = "featureId"; 88 public static final String PARAM_VERSION = "version"; 90 public static final String PARAM_FROM = "from"; 92 public static final String PARAM_TO = "to"; 94 public static final String PARAM_VERIFYONLY = "verifyOnly"; 96 private HelpDisplay helpDisplay = null; 97 98 private boolean shuttingDown = false; 99 100 104 public void init() throws ServletException { 105 super.init(); 106 if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_STANDALONE) { 107 helpDisplay = BaseHelpSystem.getHelpDisplay(); 108 } 109 } 110 111 115 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 116 throws ServletException, IOException { 117 processRequest(req, resp); 118 } 119 120 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"); 134 resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("Pragma", "no-cache"); resp.setDateHeader("Expires", 0); 141 if (!UrlUtil.isLocalRequest(req)) { 142 return; 144 } 145 if (!"/help".equals(req.getContextPath()) || !"/control".equals(req.getServletPath())) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, ""); return; 150 } 151 152 if (shuttingDown) { 153 return; 154 } 155 156 String command = req.getParameter("command"); if (command == null) { 158 resp.getWriter().print("No command."); 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 resp.getWriter().print("Unrecognized command."); } 184 } 185 186 private void updateDocs(String command, HttpServletRequest req) { 187 Bundle bundle = Platform.getBundle(UPDATE_PLUGIN_ID); 188 if (bundle == null) { 189 return; 191 } 192 try { 193 String className = getStandaloneClassName(command); 194 if (className == null) { 195 System.out.println("No class name for command " + command); return; 197 } 198 Class c = bundle.loadClass(className); 199 if (c == null) { 200 System.out.println("No class for command " + command); return; 202 } 203 Class [] parameterTypes = getParameterTypes(className); 204 Constructor constr = c.getConstructor(parameterTypes); 205 if (constr == null) { 206 System.out.println("No expected constructor for command " + command); 208 return; 209 } 210 Method m; 211 if (!CMD_APPLY.equalsIgnoreCase(command)) { 212 m = c.getMethod("run", new Class []{}); } else { 214 m = c.getMethod("applyChangesNow", new Class []{}); } 216 Object [] initargs = getInitArgs(className, req); 217 Object o = constr.newInstance(initargs); 218 Object ret = m.invoke(o, new Object []{}); 219 if (!CMD_APPLY.equalsIgnoreCase(command) &&((Boolean )ret).equals(Boolean.FALSE)){ 220 System.out.println("Command not executed."); } else { 222 System.out.println("Command executed."); } 224 } catch (Exception e) { 225 Throwable t = e; 226 if (e instanceof InvocationTargetException) { 227 t = ((InvocationTargetException) e).getTargetException(); 228 } 229 System.out.println(t.getLocalizedMessage()); 230 } 232 } 233 234 private String getStandaloneClassName(String 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 [] getParameterTypes(String className) { 259 if (CLASS_INSTALL.equals(className)) 260 return new Class []{String .class, String .class, String .class, 261 String .class, String .class}; 262 else if (CLASS_UPDATE.equals(className)) 263 return new Class []{String .class, String .class, String .class}; 264 else if (CLASS_ENABLE.equals(className) 265 || CLASS_DISABLE.equals(className) 266 || CLASS_UNINSTALL.equals(className)) 267 return new Class []{String .class, String .class, String .class, 268 String .class}; 269 else 270 return new Class []{String .class}; 271 } 272 273 private Object [] getInitArgs(String className, HttpServletRequest req) { 274 String featureId = req.getParameter(PARAM_FEATUREID); 275 String version = req.getParameter(PARAM_VERSION); 276 String fromSite = req.getParameter(PARAM_FROM); 277 String toSite = req.getParameter(PARAM_TO); 278 String verifyOnly = req.getParameter(PARAM_VERIFYONLY); 279 if (CLASS_INSTALL.equals(className)) 280 return new Object []{featureId, version, fromSite, toSite, 281 verifyOnly}; 282 else if (CLASS_UPDATE.equals(className)) 283 return new Object []{featureId, version, verifyOnly}; 284 else if (CLASS_ENABLE.equals(className) 285 || CLASS_DISABLE.equals(className) 286 || CLASS_UNINSTALL.equals(className)) 287 return new Object []{featureId, version, toSite, verifyOnly}; 288 else if (CLASS_REMOVESITE.equals(className)) 289 return new Object []{toSite}; 290 else 291 return new Object []{fromSite}; 292 } 293 294 297 private void shutdown() { 298 shuttingDown = true; 299 HelpApplication.stopHelp(); 300 } 301 302 309 private void displayHelp(HttpServletRequest req) { 310 String href = req.getParameter("href"); if (href != null) { 312 helpDisplay.displayHelpResource(href, false); 313 } else { 314 helpDisplay.displayHelp(false); 315 } 316 } 317 } 318 | Popular Tags |