private void invokeRestCall( HttpServletRequest request, HttpServletResponse response, WsOutbound clientStream, WsOutbound provenanceStream) throws IOException { URL original = getURL(request); String processed = request.getPathInfo(); processed = processed.substring(1); final String modName = processed.substring(0, processed.indexOf('/')); final String methodName = processed.substring(processed.indexOf('/') + 1); log.debug("module name = " + modName); log.debug("method name = " + methodName); final Module module = ModuleManagerFactory.getInstance().getManager().getModuleByName(modName); final ClientRequest logger = new ClientRequest( module.getClass().getName(), request.getParameterMap(), original, clientStream, provenanceStream, ModuleManagerFactory.getInstance().getManager()); try { Method m = getMethod(module, methodName); logger.debug("Invoking " + methodName + " of " + modName); final long start = System.currentTimeMillis(); String result = null; if (m.isAnnotationPresent(QueryMethod.class)) { result = (String) m.invoke(module, logger); } else if (m.isAnnotationPresent(HierarchicalMethod.class)) { result = invokeHierarchyMethod(response, logger, module, m); } else { response.sendError(HttpServletResponse.SC_FORBIDDEN, Messages.MODULE_INVALID); return; } if (result == null) { return; } log.debug("Response time: " + (System.currentTimeMillis() - start) + " ms"); logger.debug("Returning response to client"); PrintStream ps = new PrintStream( response.getOutputStream(), true, SemantEcoConfiguration.get().getEncoding()); ps.print(result); ps.close(); } catch (SecurityException e) { logger.error("Unable to execute specified method", e); } catch (NoSuchMethodException e) { response.sendError(HttpServletResponse.SC_FORBIDDEN, Messages.MODULE_INVALID); logger.error("Invalid method", e); } catch (IllegalArgumentException e) { logger.error("Illegal argument", e); } catch (IllegalAccessException e) { logger.error("Illegal access", e); } catch (InvocationTargetException e) { logger.error("Invalid target for invocation", e); } }
private String invokeHierarchyMethod( HttpServletResponse response, ClientRequest logger, Module module, Method m) throws IOException, IllegalAccessException, InvocationTargetException { Object mode = logger.getParam("mode"); if (!(mode instanceof String)) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, Messages.MODE_NOTVALID); return null; } HierarchyVerb verb = null; try { verb = HierarchyVerb.valueOf((String) mode); } catch (IllegalArgumentException e) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, Messages.MODE_NOTVALID); return null; } @SuppressWarnings("unchecked") Collection<HierarchyEntry> entries = (Collection<HierarchyEntry>) m.invoke(module, logger, verb); return serializeHierarchyEntries(entries); }