@Test public void should_Return500_When_ServiceMethodThrowsIllegalAccessException() throws Exception { int inputValue = 222; Method serviceMethodMock = createServiceMethodMockThatThrows(inputValue, new IllegalAccessException("OOPS")); VaadinConnectController controller = createVaadinController(TEST_SERVICE); controller.vaadinServices.get(TEST_SERVICE_NAME.toLowerCase()).methods .put(TEST_METHOD.getName().toLowerCase(), serviceMethodMock); ResponseEntity<String> response = controller.serveVaadinService( TEST_SERVICE_NAME, TEST_METHOD.getName(), createRequestParameters(String.format("{\"value\": %s}", inputValue))); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); String responseBody = response.getBody(); assertServiceInfoPresent(responseBody); assertTrue(String.format("Invalid response body: '%s'", responseBody), responseBody.contains("access failure")); verify(serviceMethodMock, times(1)).invoke(TEST_SERVICE, inputValue); verify(serviceMethodMock, times(1)).getParameters(); }
@PostMapping(path = "/{service}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity<String> serveVaadinService( @PathVariable("service") String serviceName, @PathVariable("method") String methodName, @RequestBody(required = false) ObjectNode body) { getLogger().debug("Service: {}, method: {}, request body: {}", serviceName, methodName, body); VaadinServiceData vaadinServiceData = vaadinServices .get(serviceName.toLowerCase(Locale.ENGLISH)); if (vaadinServiceData == null) { getLogger().debug("Service '{}' not found", serviceName); return ResponseEntity.notFound().build(); } Method methodToInvoke = vaadinServiceData .getMethod(methodName.toLowerCase(Locale.ENGLISH)).orElse(null); if (methodToInvoke == null) { getLogger().debug("Method '{}' not found in service '{}'", methodName, serviceName); return ResponseEntity.notFound().build(); } try { return invokeVaadinServiceMethod(serviceName, methodName, methodToInvoke, body, vaadinServiceData); } catch (JsonProcessingException e) { String errorMessage = String.format( "Failed to serialize service '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", serviceName, methodName, VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER); getLogger().error(errorMessage, e); try { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(createResponseErrorObject(errorMessage)); } catch (JsonProcessingException unexpected) { throw new IllegalStateException(String.format( "Unexpected: Failed to serialize a plain Java string '%s' into a JSON. " + "Double check the provided mapper's configuration.", errorMessage), unexpected); } } }
VaadinConnectController { @PostMapping(path = "/{service}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity<String> serveVaadinService( @PathVariable("service") String serviceName, @PathVariable("method") String methodName, @RequestBody(required = false) ObjectNode body) { getLogger().debug("Service: {}, method: {}, request body: {}", serviceName, methodName, body); VaadinServiceData vaadinServiceData = vaadinServices .get(serviceName.toLowerCase(Locale.ENGLISH)); if (vaadinServiceData == null) { getLogger().debug("Service '{}' not found", serviceName); return ResponseEntity.notFound().build(); } Method methodToInvoke = vaadinServiceData .getMethod(methodName.toLowerCase(Locale.ENGLISH)).orElse(null); if (methodToInvoke == null) { getLogger().debug("Method '{}' not found in service '{}'", methodName, serviceName); return ResponseEntity.notFound().build(); } try { return invokeVaadinServiceMethod(serviceName, methodName, methodToInvoke, body, vaadinServiceData); } catch (JsonProcessingException e) { String errorMessage = String.format( "Failed to serialize service '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", serviceName, methodName, VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER); getLogger().error(errorMessage, e); try { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(createResponseErrorObject(errorMessage)); } catch (JsonProcessingException unexpected) { throw new IllegalStateException(String.format( "Unexpected: Failed to serialize a plain Java string '%s' into a JSON. " + "Double check the provided mapper's configuration.", errorMessage), unexpected); } } } }
VaadinConnectController { @PostMapping(path = "/{service}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity<String> serveVaadinService( @PathVariable("service") String serviceName, @PathVariable("method") String methodName, @RequestBody(required = false) ObjectNode body) { getLogger().debug("Service: {}, method: {}, request body: {}", serviceName, methodName, body); VaadinServiceData vaadinServiceData = vaadinServices .get(serviceName.toLowerCase(Locale.ENGLISH)); if (vaadinServiceData == null) { getLogger().debug("Service '{}' not found", serviceName); return ResponseEntity.notFound().build(); } Method methodToInvoke = vaadinServiceData .getMethod(methodName.toLowerCase(Locale.ENGLISH)).orElse(null); if (methodToInvoke == null) { getLogger().debug("Method '{}' not found in service '{}'", methodName, serviceName); return ResponseEntity.notFound().build(); } try { return invokeVaadinServiceMethod(serviceName, methodName, methodToInvoke, body, vaadinServiceData); } catch (JsonProcessingException e) { String errorMessage = String.format( "Failed to serialize service '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", serviceName, methodName, VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER); getLogger().error(errorMessage, e); try { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(createResponseErrorObject(errorMessage)); } catch (JsonProcessingException unexpected) { throw new IllegalStateException(String.format( "Unexpected: Failed to serialize a plain Java string '%s' into a JSON. " + "Double check the provided mapper's configuration.", errorMessage), unexpected); } } } VaadinConnectController(
      @Autowired(required = false) @Qualifier(VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER) ObjectMapper vaadinServiceMapper,
      VaadinConnectAccessChecker accessChecker,
      VaadinServiceNameChecker serviceNameChecker, ApplicationContext context); }
VaadinConnectController { @PostMapping(path = "/{service}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity<String> serveVaadinService( @PathVariable("service") String serviceName, @PathVariable("method") String methodName, @RequestBody(required = false) ObjectNode body) { getLogger().debug("Service: {}, method: {}, request body: {}", serviceName, methodName, body); VaadinServiceData vaadinServiceData = vaadinServices .get(serviceName.toLowerCase(Locale.ENGLISH)); if (vaadinServiceData == null) { getLogger().debug("Service '{}' not found", serviceName); return ResponseEntity.notFound().build(); } Method methodToInvoke = vaadinServiceData .getMethod(methodName.toLowerCase(Locale.ENGLISH)).orElse(null); if (methodToInvoke == null) { getLogger().debug("Method '{}' not found in service '{}'", methodName, serviceName); return ResponseEntity.notFound().build(); } try { return invokeVaadinServiceMethod(serviceName, methodName, methodToInvoke, body, vaadinServiceData); } catch (JsonProcessingException e) { String errorMessage = String.format( "Failed to serialize service '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", serviceName, methodName, VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER); getLogger().error(errorMessage, e); try { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(createResponseErrorObject(errorMessage)); } catch (JsonProcessingException unexpected) { throw new IllegalStateException(String.format( "Unexpected: Failed to serialize a plain Java string '%s' into a JSON. " + "Double check the provided mapper's configuration.", errorMessage), unexpected); } } } VaadinConnectController(
      @Autowired(required = false) @Qualifier(VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER) ObjectMapper vaadinServiceMapper,
      VaadinConnectAccessChecker accessChecker,
      VaadinServiceNameChecker serviceNameChecker, ApplicationContext context); @PostMapping(path = "/{service}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) ResponseEntity<String> serveVaadinService(
      @PathVariable("service") String serviceName,
      @PathVariable("method") String methodName,
      @RequestBody(required = false) ObjectNode body); }
VaadinConnectController { @PostMapping(path = "/{service}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity<String> serveVaadinService( @PathVariable("service") String serviceName, @PathVariable("method") String methodName, @RequestBody(required = false) ObjectNode body) { getLogger().debug("Service: {}, method: {}, request body: {}", serviceName, methodName, body); VaadinServiceData vaadinServiceData = vaadinServices .get(serviceName.toLowerCase(Locale.ENGLISH)); if (vaadinServiceData == null) { getLogger().debug("Service '{}' not found", serviceName); return ResponseEntity.notFound().build(); } Method methodToInvoke = vaadinServiceData .getMethod(methodName.toLowerCase(Locale.ENGLISH)).orElse(null); if (methodToInvoke == null) { getLogger().debug("Method '{}' not found in service '{}'", methodName, serviceName); return ResponseEntity.notFound().build(); } try { return invokeVaadinServiceMethod(serviceName, methodName, methodToInvoke, body, vaadinServiceData); } catch (JsonProcessingException e) { String errorMessage = String.format( "Failed to serialize service '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", serviceName, methodName, VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER); getLogger().error(errorMessage, e); try { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(createResponseErrorObject(errorMessage)); } catch (JsonProcessingException unexpected) { throw new IllegalStateException(String.format( "Unexpected: Failed to serialize a plain Java string '%s' into a JSON. " + "Double check the provided mapper's configuration.", errorMessage), unexpected); } } } VaadinConnectController(
      @Autowired(required = false) @Qualifier(VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER) ObjectMapper vaadinServiceMapper,
      VaadinConnectAccessChecker accessChecker,
      VaadinServiceNameChecker serviceNameChecker, ApplicationContext context); @PostMapping(path = "/{service}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) ResponseEntity<String> serveVaadinService(
      @PathVariable("service") String serviceName,
      @PathVariable("method") String methodName,
      @RequestBody(required = false) ObjectNode body); static final String VAADIN_SERVICE_MAPPER_BEAN_QUALIFIER; }