-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HWKMETRICS-185] Port the exception mappers to JAX-RS 1.1 code.
- Loading branch information
Stefan Negrea
committed
Aug 13, 2015
1 parent
6e27222
commit 303189e
Showing
6 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...main/java/org/hawkular/metrics/api/jaxrs/exception/mappers/BadRequestExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
.../src/main/java/org/hawkular/metrics/api/jaxrs/exception/mappers/ExceptionMapperUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...n/java/org/hawkular/metrics/api/jaxrs/exception/mappers/NotAcceptableExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...main/java/org/hawkular/metrics/api/jaxrs/exception/mappers/NotAllowedExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...c/main/java/org/hawkular/metrics/api/jaxrs/exception/mappers/NotFoundExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...src/main/java/org/hawkular/metrics/api/jaxrs/exception/mappers/ReaderExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |