Java Code Examples for io.vertx.ext.web.impl.Utils#isJsonContentType()

The following examples show how to use io.vertx.ext.web.impl.Utils#isJsonContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenAPI3RequestValidationHandlerImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
private void parseRequestBody(RequestBody requestBody) {
  if (requestBody != null && requestBody.getContent() != null) {
    for (Map.Entry<String, ? extends MediaType> mediaType : requestBody.getContent().entrySet()) {
      if (Utils.isJsonContentType(mediaType.getKey()) && mediaType.getValue().getSchema() != null) {
        this.setEntireBodyValidator(JsonTypeValidator.JsonTypeValidatorFactory
          .createJsonTypeValidator(OpenApi3Utils.generateSanitizedJsonSchemaNode(mediaType.getValue().getSchema(), this.spec)));
      } else if (mediaType.getKey().equals("application/x-www-form-urlencoded") && mediaType.getValue().getSchema()
        != null) {
        for (Map.Entry<String, ? extends Schema> paramSchema : ((Map<String, Schema>) mediaType.getValue().getSchema().getProperties())
          .entrySet()) {
          this.addFormParamRule(ParameterValidationRuleImpl.ParameterValidationRuleFactory
            .createValidationRuleWithCustomTypeValidator(paramSchema.getKey(), this
              .resolveSchemaTypeValidatorFormEncoded(paramSchema.getValue()), !OpenApi3Utils.isRequiredParam
              (mediaType.getValue().getSchema(), paramSchema.getKey()), false, ParameterLocation.BODY_FORM));
        }
      } else if (mediaType.getKey().equals("multipart/form-data") && mediaType.getValue().getSchema() != null &&
        mediaType.getValue().getSchema().getType().equals("object")) {
        for (Map.Entry<String, ? extends Schema> multipartProperty : ((Map<String, Schema>) mediaType.getValue().getSchema().getProperties())
          .entrySet()) {
          Encoding encodingProperty = null;
          if (mediaType.getValue().getEncoding() != null)
            encodingProperty = mediaType.getValue().getEncoding().get(multipartProperty.getKey());
          String contentTypeRegex = null;
          if (encodingProperty != null && encodingProperty.getContentType() != null)
            contentTypeRegex = OpenApi3Utils.resolveContentTypeRegex(encodingProperty.getContentType());

          handleMultimapParameter(multipartProperty.getKey(), contentTypeRegex, multipartProperty.getValue(),
            mediaType.getValue().getSchema());
        }
      } else {
        this.addBodyFileRule(mediaType.getKey());
      }
    }
    this.bodyRequired = safeBoolean.apply(requestBody.getRequired());
  }
}
 
Example 2
Source File: JsonBodyProcessorImpl.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canProcess(String contentType) {
  return Utils.isJsonContentType(contentType);
}
 
Example 3
Source File: JsonBodyProcessorGenerator.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canGenerate(String mediaTypeName, JsonObject mediaTypeObject) {
  return Utils.isJsonContentType(mediaTypeName);
}
 
Example 4
Source File: BaseValidationHandler.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(RoutingContext routingContext) {
  try {
    RequestParametersImpl parsedParameters = new RequestParametersImpl();

    parsedParameters.setPathParameters(validatePathParams(routingContext));
    parsedParameters.setQueryParameters(validateQueryParams(routingContext));
    parsedParameters.setHeaderParameters(validateHeaderParams(routingContext));
    parsedParameters.setCookieParameters(validateCookieParams(routingContext));

    //Run custom validators
    for (CustomValidator customValidator : customValidators) {
      customValidator.validate(routingContext);
    }

    String contentType = routingContext.request().getHeader(HttpHeaders.CONTENT_TYPE);
    if (contentType != null && contentType.length() != 0) {
      boolean isMultipart = contentType.contains("multipart/form-data");

      if (multipartFileRules.size() != 0 && !isMultipart) {
        throw ValidationException.ValidationExceptionFactory.generateWrongContentTypeExpected(contentType,
          "multipart/form-data");
      }
      if (contentType.contains("application/x-www-form-urlencoded")) {
        parsedParameters.setFormParameters(validateFormParams(routingContext));
      } else if (isMultipart) {
        parsedParameters.setFormParameters(validateFormParams(routingContext));
        validateFileUpload(routingContext);
      } else if (Utils.isJsonContentType(contentType) || Utils.isXMLContentType(contentType)) {
        parsedParameters.setBody(validateEntireBody(routingContext));
      } else if (bodyRequired && !checkContentType(contentType)) {
        throw ValidationException.ValidationExceptionFactory.generateWrongContentTypeExpected(contentType, null);
      } // If content type is valid or body is not required, do nothing!
    } else if (bodyRequired) {
      throw ValidationException.ValidationExceptionFactory.generateWrongContentTypeExpected(contentType, null);
    }

    if (routingContext.data().containsKey("parsedParameters")) {
      ((RequestParametersImpl)routingContext.get("parsedParameters")).merge(parsedParameters);
    } else {
      routingContext.put("parsedParameters", parsedParameters);
    }
    routingContext.next();

  } catch (ValidationException e) {
    routingContext.fail(400, e);
  }
}