Search in sources :

Example 1 with MalformedValueException

use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.

the class ParameterProcessorUnitTest method testParsingFailure.

@Test
public void testParsingFailure() {
    ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, false, mockedParser, mockedValidator);
    when(mockedParser.parseParameter(any())).thenThrow(new MalformedValueException("bla"));
    assertThatCode(() -> processor.process(new HashMap<>())).isInstanceOf(ParameterProcessorException.class).hasFieldOrPropertyWithValue("errorType", ParameterProcessorException.ParameterProcessorErrorType.PARSING_ERROR).hasFieldOrPropertyWithValue("location", ParameterLocation.QUERY).hasFieldOrPropertyWithValue("parameterName", "myParam").hasCauseInstanceOf(MalformedValueException.class);
}
Also used : ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) Test(org.junit.jupiter.api.Test)

Example 2 with MalformedValueException

use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.

the class FormBodyProcessorImpl method process.

@Override
public Future<RequestParameter> process(RoutingContext requestContext) {
    try {
        MultiMap multiMap = requestContext.request().formAttributes();
        JsonObject object = new JsonObject();
        for (String key : multiMap.names()) {
            List<String> serialized = multiMap.getAll(key);
            Map.Entry<String, Object> parsed = parseField(key, serialized);
            if (parsed != null)
                object.put(parsed.getKey(), parsed.getValue());
        }
        return valueValidator.validate(object).recover(err -> Future.failedFuture(BodyProcessorException.createValidationError(requestContext.parsedHeaders().contentType().value(), err)));
    } catch (MalformedValueException e) {
        return Future.failedFuture(BodyProcessorException.createParsingError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), e));
    }
}
Also used : MultiMap(io.vertx.core.MultiMap) JsonObject(io.vertx.core.json.JsonObject) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) JsonObject(io.vertx.core.json.JsonObject) MultiMap(io.vertx.core.MultiMap) Map(java.util.Map)

Example 3 with MalformedValueException

use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.

the class JsonBodyProcessorImpl method process.

@Override
public Future<RequestParameter> process(RoutingContext requestContext) {
    try {
        Buffer body = requestContext.getBody();
        if (body == null) {
            throw BodyProcessorException.createParsingError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), new MalformedValueException("Null body"));
        }
        Object json = Json.decodeValue(body);
        return valueValidator.validate(json).recover(err -> Future.failedFuture(BodyProcessorException.createValidationError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), err)));
    } catch (DecodeException e) {
        throw BodyProcessorException.createParsingError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), e);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) DecodeException(io.vertx.core.json.DecodeException)

Example 4 with MalformedValueException

use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.

the class SplitterCharObjectParser method parse.

@Override
public JsonObject parse(String serialized) throws MalformedValueException {
    Map<String, Object> result = new HashMap<>();
    String[] values = serialized.split(separator, -1);
    // Key value pairs -> odd length not allowed
    if (values.length % 2 != 0)
        throw new MalformedValueException("Key value pair Object must have odd number of deserialized values");
    for (int i = 0; i < values.length; i += 2) {
        // empty key not allowed!
        if (values[i].length() == 0) {
            throw new MalformedValueException("Empty key not allowed");
        } else {
            Map.Entry<String, Object> parsed = parseField(values[i], values[i + 1]);
            if (parsed != null)
                result.put(parsed.getKey(), parsed.getValue());
        }
    }
    return new JsonObject(result);
}
Also used : HashMap(java.util.HashMap) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with MalformedValueException

use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.

the class AnyOfOneOfSingleParameterParser method parseParameter.

@Override
@Nullable
public Object parseParameter(Map<String, List<String>> parameterValue) throws MalformedValueException {
    List<String> extractedList = parameterValue.remove(parameterName);
    if (extractedList == null)
        return null;
    String extracted = extractedList.get(0);
    MalformedValueException lastException = new MalformedValueException("Cannot deserialize parameter " + parameterName + " with value " + extracted);
    for (ValueParser<String> p : valueParsers) {
        try {
            return p.parse(extracted);
        } catch (MalformedValueException e) {
            lastException = e;
        }
    }
    throw lastException;
}
Also used : MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) Nullable(io.vertx.codegen.annotations.Nullable)

Aggregations

MalformedValueException (io.vertx.ext.web.validation.MalformedValueException)5 JsonObject (io.vertx.core.json.JsonObject)2 Map (java.util.Map)2 Nullable (io.vertx.codegen.annotations.Nullable)1 MultiMap (io.vertx.core.MultiMap)1 Buffer (io.vertx.core.buffer.Buffer)1 DecodeException (io.vertx.core.json.DecodeException)1 ParameterProcessor (io.vertx.ext.web.validation.impl.parameter.ParameterProcessor)1 ParameterProcessorImpl (io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1