io.telicent.smart.cache.server.jaxrs.errors.NotFoundExceptionMapper Maven / Gradle / Ivy
/**
* 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 jakarta.ws.rs.NotFoundException;
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;
/**
* Maps 404 errors into RFC 7807 Problem responses
*/
@Provider
public class NotFoundExceptionMapper implements ExceptionMapper {
@Context
private UriInfo uri;
@Override
public Response toResponse(NotFoundException exception) {
return new Problem("NotFound",
null,
Response.Status.NOT_FOUND.getStatusCode(),
String.format("/%s is not a valid URL in this API",
this.uri.getPath()),
null).toResponse();
}
}