All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.telicent.smart.cache.server.jaxrs.errors.ConstraintViolationMapper Maven / Gradle / Ivy

There is a newer version: 0.24.1
Show newest version
/**
 * Copyright (C) Telicent Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.telicent.smart.cache.server.jaxrs.errors;

import io.telicent.smart.cache.server.jaxrs.model.Problem;
import io.telicent.smart.cache.server.jaxrs.utils.ParamInfo;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import org.glassfish.jersey.server.validation.internal.ValidationHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Maps constraint violation errors into RFC 7807 Problem responses
 * 

* Constraint violations occur when an API is called with parameter values that do not meet the constraints imposed by * Jakarta Bean Validation annotations. *

*/ @Provider public class ConstraintViolationMapper implements ExceptionMapper { private static final Logger LOGGER = LoggerFactory.getLogger(ConstraintViolationMapper.class); @Context UriInfo uri; @Override public Response toResponse(ConstraintViolationException exception) { StringBuilder detail = new StringBuilder(); for (ConstraintViolation violation : exception.getConstraintViolations()) { if (!detail.isEmpty()) { detail.append('\n'); } ParamInfo info = ParamInfo.fromViolation(violation); LOGGER.debug("Mapped parameter violation at path '{}' to real parameter name '{}' with type '{}'", violation.getPropertyPath(), info.getName(), info.getType()); detail.append('/').append(this.uri.getPath()).append(" received "); if (info.getType() != null) { detail.append(info.getType()).append(' '); } detail.append("Parameter '") .append(info.getName()) .append("' with invalid value: ") .append(violation.getMessage()); } Response.Status status = ValidationHelper.getResponseStatus(exception); String problemType = exception.getConstraintViolations().isEmpty() ? "InvalidRequestParameter" : "InvalidRequestParameters"; return new Problem(problemType, null, status.getStatusCode(), detail.toString(), null).toResponse(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy