1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.nsa.dmaap;
25 import javax.inject.Singleton;
26 import javax.ws.rs.BadRequestException;
27 import javax.ws.rs.InternalServerErrorException;
28 import javax.ws.rs.NotAllowedException;
29 import javax.ws.rs.NotAuthorizedException;
30 import javax.ws.rs.NotFoundException;
31 import javax.ws.rs.ServiceUnavailableException;
32 import javax.ws.rs.WebApplicationException;
33 import javax.ws.rs.core.Response;
34 import javax.ws.rs.ext.ExceptionMapper;
35 import javax.ws.rs.ext.Provider;
37 import org.apache.http.HttpStatus;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import org.springframework.beans.factory.annotation.Autowired;
42 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
43 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
44 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
47 * Exception Mapper class to handle
49 * @author rajashree.khare
54 public class DMaaPWebExceptionMapper implements ExceptionMapper<WebApplicationException>{
60 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DMaaPWebExceptionMapper.class);
64 private ErrorResponse errRes;
69 private DMaaPErrorMessages msgs;
72 * Contructor for DMaaPWebExceptionMapper
74 public DMaaPWebExceptionMapper() {
76 LOGGER.info("WebException Mapper Created..");
80 * The toResponse method is called when
81 * an exception of type WebApplicationException
82 * is thrown.This method will send a custom error
83 * response to the client
86 public Response toResponse(WebApplicationException ex) {
87 //System.out.println("--------------------------------------------------"+ex);
88 LOGGER.info("Reached WebException Mapper");
93 if(ex instanceof NotFoundException)
95 errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,DMaaPResponseCode.RESOURCE_NOT_FOUND.
96 getResponseCode(),msgs.getNotFound());
98 LOGGER.info(errRes.toString());
99 Response response = Response.status(errRes.getHttpStatusCode()).header("exception",
100 errRes.getErrMapperStr()).build();
106 * Internal Server Error
108 if(ex instanceof InternalServerErrorException)
111 int errCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
112 int dmaapErrCode = DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode();
113 String errMsg = msgs.getServerUnav();
116 if(ex.getCause().toString().contains("Json")) {
117 errCode = HttpStatus.SC_BAD_REQUEST;
118 dmaapErrCode = DMaaPResponseCode.INCORRECT_JSON.getResponseCode();
119 errMsg = ex.getCause().getMessage().substring(0, ex.getCause().getMessage().indexOf("[Source")-3);
121 else if (ex.getCause().toString().contains("UnrecognizedPropertyException")) {
122 errCode = HttpStatus.SC_BAD_REQUEST;
123 dmaapErrCode = DMaaPResponseCode.INCORRECT_JSON.getResponseCode();
124 errMsg = ex.getCause().getMessage().substring(0, ex.getCause().getMessage().indexOf("[Source")-3);
126 errRes = new ErrorResponse(errCode,dmaapErrCode,errMsg);
128 LOGGER.info(errRes.toString());
129 Response response = Response.status(errRes.getHttpStatusCode()).header("exception",
130 errRes.getErrMapperStr()).build();
138 if(ex instanceof NotAuthorizedException)
140 errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,DMaaPResponseCode.ACCESS_NOT_PERMITTED.
141 getResponseCode(),msgs.getAuthFailure());
143 LOGGER.info(errRes.toString());
144 Response response = Response.status(errRes.getHttpStatusCode()).header("exception",
145 errRes.getErrMapperStr()).build();
152 if(ex instanceof BadRequestException)
154 errRes = new ErrorResponse(HttpStatus.SC_BAD_REQUEST,DMaaPResponseCode.INCORRECT_JSON.
155 getResponseCode(),msgs.getBadRequest());
157 LOGGER.info(errRes.toString());
158 Response response = Response.status(errRes.getHttpStatusCode()).header("exception",
159 errRes.getErrMapperStr()).build();
164 * HTTP Method not allowed
166 if(ex instanceof NotAllowedException)
168 errRes = new ErrorResponse(HttpStatus.SC_METHOD_NOT_ALLOWED,DMaaPResponseCode.METHOD_NOT_ALLOWED.
169 getResponseCode(),msgs.getMethodNotAllowed());
171 LOGGER.info(errRes.toString());
172 Response response = Response.status(errRes.getHttpStatusCode()).header("exception",
173 errRes.getErrMapperStr()).build();
181 if(ex instanceof ServiceUnavailableException)
183 errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,DMaaPResponseCode.SERVER_UNAVAILABLE.
184 getResponseCode(),msgs.getServerUnav());
186 LOGGER.info(errRes.toString());
187 Response response = Response.status(errRes.getHttpStatusCode()).header("exception",
188 errRes.getErrMapperStr()).build();
195 return Response.serverError().build();