Skip to content

Commit

Permalink
[HWKMETRICS-185] Port the exception mappers to JAX-RS 1.1 code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Negrea committed Aug 13, 2015
1 parent 6e27222 commit 303189e
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.exception.mappers;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.spi.BadRequestException;

/**
* Exception mapper for any exception thrown by RESTEasy when HTTP Bad Request (400) is encountered.
* <p>
* This mapper let us reply to the user with a pre-determined message format if, for example, a {@link
* javax.ws.rs.ext.ParamConverter} throws an {@link java.lang.IllegalArgumentException}.
*
* @author Thomas Segismont
*/
@Provider
public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException> {

@Override
public Response toResponse(BadRequestException exception) {
return ExceptionMapperUtils.buildResponse(exception, Response.Status.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.exception.mappers;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Throwables;

/**
* @author Jeeva Kandasamy
*/
public class ExceptionMapperUtils {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionMapperUtils.class);
private ExceptionMapperUtils(){

}

public static Response buildResponse(Throwable exception, Status status){
LOG.trace("RestEasy exception,", exception);
return Response.status(status)
.entity(new ApiError(Throwables.getRootCause(exception).getMessage()))
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.exception.mappers;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.spi.NotAcceptableException;

/**
* Exception mapper for any exception thrown by RESTEasy when HTTP Not Acceptable (406) is encountered.
* <p>
* This mapper let us reply to the user with a pre-determined message format if, for example, receive a
* HTTP GET request with unsupported media type.
*
* @author Jeeva Kandasamy
*/
@Provider
public class NotAcceptableExceptionMapper implements ExceptionMapper<NotAcceptableException> {

@Override
public Response toResponse(NotAcceptableException exception) {
return ExceptionMapperUtils.buildResponse(exception, Response.Status.NOT_ACCEPTABLE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.exception.mappers;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.spi.MethodNotAllowedException;

/**
* Exception mapper for any exception thrown by RESTEasy when HTTP Method Not Allowed (405) is encountered.
* <p>
* This mapper let us reply to the user with a pre-determined message format if, for example, receive
* a HTTP POST request for a resource which does not support for HTTP POST method.
*
* @author Jeeva Kandasamy
*/
@Provider
public class NotAllowedExceptionMapper implements ExceptionMapper<MethodNotAllowedException> {

@Override
public Response toResponse(MethodNotAllowedException exception) {
return ExceptionMapperUtils.buildResponse(exception, Response.Status.BAD_REQUEST);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.exception.mappers;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.spi.NotFoundException;

/**
* Exception mapper for any exception thrown by RESTEasy when HTTP Not Found (404) is encountered.
* Also checks the case chain, if NumberFormatException is present building response with HTTP Bad Request (400).
* <p>
* This mapper let us reply to the user with a pre-determined message format if, for example, receive
* a request for unavailable resource.
*
* @author Jeeva Kandasamy
*/
@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {

@Override
public Response toResponse(NotFoundException exception) {
if (exception.getCause() instanceof NumberFormatException) {
return ExceptionMapperUtils.buildResponse(exception, Response.Status.BAD_REQUEST);
} else {
return ExceptionMapperUtils.buildResponse(exception, Response.Status.NOT_FOUND);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.exception.mappers;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.hawkular.metrics.api.jaxrs.interceptor.EmptyPayloadException;
import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.jboss.resteasy.spi.ReaderException;

import com.google.common.base.Throwables;

/**
* Exception mapper for any exception thrown by a body reader chain.
* <p>
* This mapper let us reply to the user with a pre-determined message format if, for example, a JSON entity cannot be
* parsed.
*
* @author Thomas Segismont
*/
@Provider
public class ReaderExceptionMapper implements ExceptionMapper<ReaderException> {

@Override
public Response toResponse(ReaderException exception) {
Throwable rootCause = Throwables.getRootCause(exception);
if (rootCause instanceof EmptyPayloadException) {
return ApiUtils.emptyPayload();
}
return ExceptionMapperUtils.buildResponse(rootCause, Response.Status.BAD_REQUEST);
}
}

0 comments on commit 303189e

Please sign in to comment.