1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
24 package org.onap.dmaap;
27 import javax.inject.Singleton;
28 import javax.ws.rs.BadRequestException;
29 import javax.ws.rs.InternalServerErrorException;
30 import javax.ws.rs.NotAllowedException;
31 import javax.ws.rs.NotAuthorizedException;
32 import javax.ws.rs.NotFoundException;
33 import javax.ws.rs.ServiceUnavailableException;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.ext.ExceptionMapper;
37 import javax.ws.rs.ext.Provider;
39 import org.apache.http.HttpStatus;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import org.springframework.beans.factory.annotation.Autowired;
44 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
45 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
46 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
49 * Exception Mapper class to handle
51 * @author rajashree.khare
56 public class DMaaPWebExceptionMapper implements ExceptionMapper<WebApplicationException>{
62 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DMaaPWebExceptionMapper.class);
66 private ErrorResponse errRes;
71 private DMaaPErrorMessages msgs;
74 * Contructor for DMaaPWebExceptionMapper
76 public DMaaPWebExceptionMapper() {
78 LOGGER.info("WebException Mapper Created..");
82 * The toResponse method is called when
83 * an exception of type WebApplicationException
84 * is thrown.This method will send a custom error
85 * response to the client
88 public Response toResponse(WebApplicationException ex) {
89 LOGGER.info("Reached WebException Mapper");
94 if(ex instanceof NotFoundException)
96 errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,DMaaPResponseCode.RESOURCE_NOT_FOUND.
97 getResponseCode(),msgs.getNotFound());
99 LOGGER.info(errRes.toString());
100 Response response = Response.status(errRes.getHttpStatusCode()).header("exception",
101 errRes.getErrMapperStr()).build();
107 * Internal Server Error
109 if(ex instanceof InternalServerErrorException)
112 int errCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
113 int dmaapErrCode = DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode();
114 String errMsg = msgs.getServerUnav();
117 if(ex.getCause().toString().contains("Json")) {
118 errCode = HttpStatus.SC_BAD_REQUEST;
119 dmaapErrCode = DMaaPResponseCode.INCORRECT_JSON.getResponseCode();
120 errMsg = ex.getCause().getMessage().substring(0, ex.getCause().getMessage().indexOf("[Source")-3);
122 else if (ex.getCause().toString().contains("UnrecognizedPropertyException")) {
123 errCode = HttpStatus.SC_BAD_REQUEST;
124 dmaapErrCode = DMaaPResponseCode.INCORRECT_JSON.getResponseCode();
125 errMsg = ex.getCause().getMessage().substring(0, ex.getCause().getMessage().indexOf("[Source")-3);
127 errRes = new ErrorResponse(errCode,dmaapErrCode,errMsg);
129 LOGGER.info(errRes.toString());
130 return Response.status(errRes.getHttpStatusCode()).header("exception",
131 errRes.getErrMapperStr()).build();
136 if(ex instanceof NotAuthorizedException)
138 errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,DMaaPResponseCode.ACCESS_NOT_PERMITTED.
139 getResponseCode(),msgs.getAuthFailure());
141 LOGGER.info(errRes.toString());
142 return Response.status(errRes.getHttpStatusCode()).header("exception",
143 errRes.getErrMapperStr()).build();
148 if(ex instanceof BadRequestException)
150 errRes = new ErrorResponse(HttpStatus.SC_BAD_REQUEST,DMaaPResponseCode.INCORRECT_JSON.
151 getResponseCode(),msgs.getBadRequest());
153 LOGGER.info(errRes.toString());
154 return Response.status(errRes.getHttpStatusCode()).header("exception",
155 errRes.getErrMapperStr()).build();
158 * HTTP Method not allowed
160 if(ex instanceof NotAllowedException)
162 errRes = new ErrorResponse(HttpStatus.SC_METHOD_NOT_ALLOWED,DMaaPResponseCode.METHOD_NOT_ALLOWED.
163 getResponseCode(),msgs.getMethodNotAllowed());
165 LOGGER.info(errRes.toString());
166 return Response.status(errRes.getHttpStatusCode()).header("exception",
167 errRes.getErrMapperStr()).build();
173 if(ex instanceof ServiceUnavailableException)
175 errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,DMaaPResponseCode.SERVER_UNAVAILABLE.
176 getResponseCode(),msgs.getServerUnav());
178 LOGGER.info(errRes.toString());
179 return Response.status(errRes.getHttpStatusCode()).header("exception",
180 errRes.getErrMapperStr()).build();
185 return Response.serverError().build();