Search in sources :

Example 11 with Errors

use of org.folio.rest.jaxrs.model.Errors in project raml-module-builder by folio-org.

the class RestRoutingTest method isValidRequest.

private <T> T isValidRequest(T t, Class<T> clazz) {
    Errors errors = new Errors();
    Object[] o = RestRouting.isValidRequest(null, t, errors, List.of(), clazz);
    assertThat(errors.getErrors(), is(empty()));
    return (T) o[1];
}
Also used : Errors(org.folio.rest.jaxrs.model.Errors)

Example 12 with Errors

use of org.folio.rest.jaxrs.model.Errors in project raml-module-builder by folio-org.

the class RestRoutingTest method isValidRequestSuccess.

@Test
void isValidRequestSuccess() {
    Errors errors = new Errors();
    RestRouting.isValidRequest(null, new Foo("id", null), errors, List.of(), null);
    assertThat(errors.getErrors(), is(empty()));
}
Also used : Errors(org.folio.rest.jaxrs.model.Errors) Test(org.junit.jupiter.api.Test)

Example 13 with Errors

use of org.folio.rest.jaxrs.model.Errors in project raml-module-builder by folio-org.

the class RestRouting method parseNonAnnotated.

private static void parseNonAnnotated(RoutingContext rc, Buffer body, Object[] paramArray, Map<String, String> okapiHeaders, String valueType, int order) throws JsonProcessingException, ReflectiveOperationException {
    // this will also validate the json against the pojo created from the schema
    Class<?> entityClazz = Class.forName(valueType);
    HttpServerRequest request = rc.request();
    if (valueType.equals("io.vertx.ext.web.RoutingContext")) {
        paramArray[order] = rc;
    } else if (valueType.equals("java.util.Map")) {
        paramArray[order] = okapiHeaders;
    } else if (valueType.equals("io.vertx.core.Context")) {
        paramArray[order] = rc.vertx().getOrCreateContext();
    } else if (valueType.equals("io.vertx.core.Handler")) {
        // will set it later in invoke
        paramArray[order] = null;
    } else if (!valueType.equals("java.io.InputStream")) {
        // we have special handling for the Result Handler and context, it is also assumed that
        // an inputsteam parameter occurs when application/octet is declared in the raml
        // in which case the content will be streamed to he function
        String bodyContent = body == null ? null : body.toString();
        withRequestId(rc, () -> LOGGER.debug("{} -------- bodyContent -------- {}", rc.request().path(), bodyContent));
        if (bodyContent != null) {
            if ("java.io.Reader".equals(valueType)) {
                paramArray[order] = new StringReader(bodyContent);
            } else if ("java.lang.String".equals(valueType)) {
                paramArray[order] = bodyContent;
            } else if (bodyContent.length() > 0) {
                try {
                    paramArray[order] = MAPPER.readValue(bodyContent, entityClazz);
                } catch (UnrecognizedPropertyException e) {
                    withRequestId(rc, () -> LOGGER.error(e.getMessage(), e));
                    endRequestWithError(rc, HttpStatus.HTTP_UNPROCESSABLE_ENTITY.toInt(), true, JsonUtils.entity2String(ValidationHelper.createValidationErrorMessage("", "", e.getMessage())));
                    return;
                }
            }
        }
        Errors errorResp = new Errors();
        // is this request only to validate a field value and not an actual
        // request for additional processing
        List<String> field2validate = request.params().getAll("validate_field");
        Object[] resp = isValidRequest(rc, paramArray[order], errorResp, field2validate, entityClazz);
        boolean isValid = (boolean) resp[0];
        paramArray[order] = resp[1];
        if (!isValid) {
            endRequestWithError(rc, HttpStatus.HTTP_UNPROCESSABLE_ENTITY.toInt(), true, JsonUtils.entity2String(errorResp));
            return;
        }
        if (!field2validate.isEmpty()) {
            // valid request for the field to validate request made
            AsyncResponseResult arr = new AsyncResponseResult();
            ResponseImpl ri = new ResponseImpl();
            ri.setStatus(200);
            arr.setResult(ri);
            // right now this is the only flag available to stop
            // any additional responses for this request. to fix
            sendResponse(rc, arr, 0, null);
            return;
        }
        MetadataUtil.populateMetadata(paramArray[order], okapiHeaders);
    }
}
Also used : Errors(org.folio.rest.jaxrs.model.Errors) HttpServerRequest(io.vertx.core.http.HttpServerRequest) StringReader(java.io.StringReader) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) AsyncResponseResult(org.folio.rest.tools.utils.AsyncResponseResult) JsonObject(io.vertx.core.json.JsonObject) ResponseImpl(org.folio.rest.tools.utils.ResponseImpl)

Example 14 with Errors

use of org.folio.rest.jaxrs.model.Errors in project raml-module-builder by folio-org.

the class PgUtil method respond422.

static Future<Response> respond422(Method response422Method, String key, String value, String message) {
    try {
        Errors errors = ValidationHelper.createValidationErrorMessage(key, value, message);
        Response response = (Response) response422Method.invoke(null, errors);
        return Future.succeededFuture(response);
    } catch (IllegalAccessException | InvocationTargetException | NullPointerException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : Response(javax.ws.rs.core.Response) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Errors(org.folio.rest.jaxrs.model.Errors) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Errors (org.folio.rest.jaxrs.model.Errors)14 Test (org.junit.Test)7 PgException (io.vertx.pgclient.PgException)6 Method (java.lang.reflect.Method)6 Response (javax.ws.rs.core.Response)6 JsonObject (io.vertx.core.json.JsonObject)5 Error (org.folio.rest.jaxrs.model.Error)5 PostUsersResponse (org.folio.rest.jaxrs.model.Users.PostUsersResponse)5 ExpectedException (org.junit.rules.ExpectedException)5 Async (io.vertx.ext.unit.Async)4 AsyncResult (io.vertx.core.AsyncResult)3 Future (io.vertx.core.Future)3 Handler (io.vertx.core.Handler)3 Vertx (io.vertx.core.Vertx)3 Buffer (io.vertx.core.buffer.Buffer)3 Json (io.vertx.core.json.Json)3 TestContext (io.vertx.ext.unit.TestContext)3 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)3 RoutingContext (io.vertx.ext.web.RoutingContext)3 Arrays (java.util.Arrays)3