Search in sources :

Example 1 with VaadinEndpointData

use of dev.hilla.EndpointRegistry.VaadinEndpointData in project flow by vaadin.

the class EndpointInvoker method invoke.

/**
 * Invoke the given endpoint method with the given parameters if the user
 * has access to do so.
 *
 * @param endpointName
 *            the name of the endpoint
 * @param methodName
 *            the name of the method in the endpoint
 * @param body
 *            optional request body, that should be specified if the method
 *            called has parameters
 * @param request
 *            the HTTP request which should not be here in the end
 * @return the return value of the invoked endpoint method, wrapped in a
 *         response entity
 */
public ResponseEntity<String> invoke(String endpointName, String methodName, ObjectNode body, HttpServletRequest request) {
    VaadinEndpointData vaadinEndpointData = endpointRegistry.get(endpointName);
    if (vaadinEndpointData == null) {
        getLogger().debug("Endpoint '{}' not found", endpointName);
        return ResponseEntity.notFound().build();
    }
    Method methodToInvoke = vaadinEndpointData.getMethod(methodName).orElse(null);
    if (methodToInvoke == null) {
        getLogger().debug("Method '{}' not found in endpoint '{}'", methodName, endpointName);
        return ResponseEntity.notFound().build();
    }
    try {
        // Put a VaadinRequest in the instances object so as the request is
        // available in the end-point method
        VaadinServletService service = (VaadinServletService) VaadinService.getCurrent();
        CurrentInstance.set(VaadinRequest.class, new VaadinServletRequest(request, service));
        return invokeVaadinEndpointMethod(endpointName, methodName, methodToInvoke, body, vaadinEndpointData, request);
    } catch (JsonProcessingException e) {
        String errorMessage = String.format("Failed to serialize endpoint '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", endpointName, methodName, EndpointController.VAADIN_ENDPOINT_MAPPER_BEAN_QUALIFIER);
        getLogger().error(errorMessage, e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(createResponseErrorObject(errorMessage));
    } finally {
        CurrentInstance.set(VaadinRequest.class, null);
    }
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Method(java.lang.reflect.Method) VaadinEndpointData(dev.hilla.EndpointRegistry.VaadinEndpointData) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinEndpointData (dev.hilla.EndpointRegistry.VaadinEndpointData)1 Method (java.lang.reflect.Method)1