@SuppressWarnings("unchecked") protected void registerMethodHandlerFor(String endpoint, Class<? extends ServiceMethodHandler> handlerClass) { methodHandlers.put(endpoint, injector.getInstance(handlerClass)); }
protected void registerPostMethodHandlerHookFor(String endpoint, Class<? extends ServiceMethodPostHook<? extends Message>> handlerClass) { methodHandlers.addPostHook(endpoint, injector.getInstance(handlerClass)); }
protected void registerPreMethodHandlerHookFor(String endpoint, Class<? extends ServiceMethodPreHook<? extends Message>> handlerClass) { methodHandlers.addPreHook(endpoint, injector.getInstance(handlerClass)); }
@Before public void setup() throws RpcCallException { handlerDictionary = new MethodHandlerDictionary(); handlerDictionary.put("a", null); ServiceMethodHandlerUnderTest mockHandlerThrowsRpcCallEx = new ServiceMethodHandlerUnderTest(); handlerDictionary.put("jsonRpcWithException", mockHandlerThrowsRpcCallEx); metricRegistry = mock(MetricRegistry.class); when(metricRegistry.counter(anyString())).thenReturn(mock(Counter.class)); when(metricRegistry.timer(anyString())).thenReturn(mock(Timer.class)); handlerMetrics = mock(RpcHandlerMetrics.class); when(handlerMetrics.getMethodTimer(any(), any(), any())).thenReturn(mock(GoTimer.class)); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, new ServiceProperties(), null); }
/** * For backward-compatibility */ @Deprecated public ServiceMethodHandler<? extends Message, ? extends Message> get(String endpoint) { return getMethodHandler(endpoint); }
/** * Invoke in the following order: * <ol><li>Global pre-handler hooks</li> * <li>Pre-handler hooks for this methodName</li> * <li>The handler</li> * <li>Post-handler hooks for this methodName</li> * <li>Global post-handler hooks</li></ol> */ @SuppressWarnings("unchecked") protected Message invokeHandlerChain(String methodName, ServiceMethodHandler handler, Message request, OrangeContext context) throws RpcCallException { List<ServiceMethodPreHook<? extends Message>> preHooks = handlers.getPreHooksFor(methodName); for (ServiceMethodPreHook hook : preHooks) { request = hook.handleRequest(request, context); } Message response = handler.handleRequest(request, context); List<ServiceMethodPostHook<? extends Message>> postHooks = handlers.getPostHooksFor(methodName); for (ServiceMethodPostHook hook : postHooks) { response = hook.handleRequest(response, context); } return response; }
public Map<String, ServiceMethodHandler<? extends Message, ? extends Message>> getMethodHandlers() { return methodHandlers.getMethodHandlers(); }
protected JsonRpcRequest parseRpcRequest(String jsonRequestString) throws IllegalArgumentException { JsonObject jsonRpcRequest = null; try { jsonRpcRequest = new JsonParser().parse(jsonRequestString).getAsJsonObject(); } catch (Exception ex) { throw new IllegalArgumentException(ex.getMessage(), ex); } JsonElement methodElement = jsonRpcRequest.get(METHOD_FIELD); if (methodElement == null || methodElement.getAsString().isEmpty()) { throw new IllegalArgumentException("Missing method name"); } if (! handlers.hasMethodHandler(methodElement.getAsString())) { throw new IllegalArgumentException("No handler registered for method '" + methodElement.getAsString() + "'"); } JsonArray paramsArray = jsonRpcRequest.getAsJsonArray(PARAMS_FIELD); JsonElement idElement = jsonRpcRequest.get(JsonRpcRequest.ID_FIELD); return new JsonRpcRequest(idElement, methodElement.getAsString(), paramsArray); }
@Test public void testSetterAndGetter() { InjectionModule module = new InjectionModule(new ServiceProperties()); MethodHandlerDictionary dict = new MethodHandlerDictionary(); module.setMethodHandlerDictionary(dict); assertThat(module.getMethodHandlers()).isEqualTo(dict); }
/** * Reset counter how often a service method was called to 0 * * @param serviceMethodName The service method name e.g. Telematics.LockVehicle */ public void resetServiceMethodCallCount(String serviceMethodName) throws Exception { ServiceMethodProxy serviceMethodProxy = (ServiceMethodProxy) methodHandlers.getMethodHandler(serviceMethodName); if(serviceMethodProxy == null){ throw new Exception("No method handler found for service method '" + serviceMethodName + "'"); } serviceMethodProxy.resetMethodCallCounter(); }
@Override public void initialize(MethodHandlerDictionary methodHandlers) { registrationManager.setRegisteredHandlers(methodHandlers.getMethodHandlers()); registrationManager.register(); }
public Message getActualHandledRequest(final String command){ ServiceMethodProxy proxy = ((ServiceMethodProxy) methodHandlers.getMethodHandler(command)); if (proxy == null) { throw new RuntimeException("The method " + command + " for " + serviceName + " could not be found"); } return proxy.getRequest(); }
private void buildMethodHandlers(String serviceName) throws Exception { // get the generated proto classes List<String> protoClasses = rpcMethodScanner.getGeneratedProtoClasses(serviceName); List<RpcMethodDefinition> defs = rpcMethodScanner.getRpcMethodDefinitions(serviceName); for (RpcMethodDefinition def : defs) { ServiceMethodProxy handler = buildMethodHandler(def, protoClasses); methodHandlers.put(def.getMethodName(), handler); logger.info("Adding mock method handler for {}", def.getMethodName()); } }
private void initialize() throws Exception { buildMethodHandlers(serviceName.replaceAll("-", "_")); registrationManager.setRegisteredHandlers(methodHandlers.getMethodHandlers()); registrationManager.register(); messageHandler.setServiceName(serviceName); messageHandler.start(); while(! registrationManager.isRegistered()) { logger.info("Waiting for service registration of {}", serviceName); new Sleeper().sleepNoException(100); } healthCheckManager.initialize(); loadBalancerFactory = injector.getInstance(LoadBalancerFactory.class); loadBalancerFactory.getLoadBalancer(serviceName).waitForServiceInstance(); }
public Class<? extends Message> getResponseClassForMethod(String method) { ServiceMethodProxy proxy = ((ServiceMethodProxy) this.methodHandlers .getMethodHandler(method)); if (proxy == null) { throw new RuntimeException("The method " + method + " for " + this.serviceName + " could not be found"); } return proxy.getResponseType(); }
/** * Get counter how often a service method was called * * @param serviceMethodName The service method name e.g. Telematics.LockVehicle * @return Number of calls of a service method */ public int getServiceMethodCallCount(String serviceMethodName) throws Exception { ServiceMethodProxy serviceMethodProxy = (ServiceMethodProxy) methodHandlers.getMethodHandler(serviceMethodName); if(serviceMethodProxy == null){ throw new Exception("No method handler found for service method '" + serviceMethodName + "'"); } return serviceMethodProxy.getMethodCallCounter(); }
public ServiceImpersonator addMapping(CommandResponseMapping mapping) { ServiceMethodProxy proxy = ((ServiceMethodProxy) this.methodHandlers .getMethodHandler(mapping.getCommand())); if (proxy == null) { throw new RuntimeException("The method " + mapping.getCommand() + " for " + this.serviceName + " could not be found"); } logger.info("Adding mock response mapping for {}", mapping.getCommand()); proxy.setResponse(mapping.getResponse()); proxy.setException(mapping.getException()); return this; }
@SuppressWarnings("unchecked") private JsonRpcResponse dispatchJsonRpcRequest(JsonRpcRequest rpcRequest, OrangeContext cxt) { JsonRpcResponse jsonResponse = new JsonRpcResponse(rpcRequest.getId(), JsonNull.INSTANCE, JsonNull.INSTANCE, HttpServletResponse.SC_OK); try { ServiceMethodHandler handler = handlers.getMethodHandler(rpcRequest.getMethod()); Message innerRequest = convertJsonToProtobuf(handler, rpcRequest); JsonElement idElement = rpcRequest.getId(); if (idElement == null) { jsonResponse.setId(new JsonPrimitive(-1)); } Message innerResponse = invokeHandlerChain(rpcRequest.getMethod(), handler, innerRequest, cxt); jsonResponse.setResult(ProtobufUtil.protobufToJson(innerResponse)); } catch (RpcCallException rpcEx) { logger.debug("Error processing request", rpcEx); jsonResponse.setError(rpcEx.toJson()); jsonResponse.setStatusCode(rpcEx.getCategory().getHttpStatus()); } catch (Exception ex) { logger.warn("Error processing request", ex); if (ex.getMessage() != null) { jsonResponse.setError(new JsonPrimitive(ex.getMessage())); } jsonResponse.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return jsonResponse; }
ServiceMethodHandler handler = handlers.getMethodHandler(methodName); if (handler == null) { incrementFailureCounter(methodName, context.getRpcOriginService(),