update the package name
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / DMaaPWebExceptionMapper.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package com.att.nsa.dmaap;
23
24
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;
36
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;
41
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;
45
46 /**
47  * Exception Mapper class to handle
48  * Web Exceptions
49  * @author rajashree.khare
50  *
51  */
52 @Provider
53 @Singleton
54 public class DMaaPWebExceptionMapper implements ExceptionMapper<WebApplicationException>{
55         
56         /**
57          * Logger obj
58          */
59
60         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DMaaPWebExceptionMapper.class);
61         /**
62          * Error response obj
63          */
64         private ErrorResponse errRes;
65         /**
66          * Error msg obj
67          */
68         @Autowired
69         private DMaaPErrorMessages msgs;
70         
71         /**
72          * Contructor for DMaaPWebExceptionMapper
73          */
74         public DMaaPWebExceptionMapper() {
75                 super();
76                 LOGGER.info("WebException Mapper Created..");
77         }
78
79         /**
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
84          */
85         @Override
86         public Response toResponse(WebApplicationException ex) {
87                 //System.out.println("--------------------------------------------------"+ex);
88                 LOGGER.info("Reached WebException Mapper");
89                 
90                 /**
91                  * Resource Not Found
92                  */
93                 if(ex instanceof NotFoundException)
94                 {
95                         errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,DMaaPResponseCode.RESOURCE_NOT_FOUND.
96                                         getResponseCode(),msgs.getNotFound());
97                         
98                         LOGGER.info(errRes.toString());
99                         Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
100                                         errRes.getErrMapperStr()).build();
101                         
102                         return response;
103                         
104                 }
105                 /**
106                  * Internal Server Error
107                  */
108                 if(ex instanceof InternalServerErrorException)
109                 {
110                 
111                         int errCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
112                         int dmaapErrCode = DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode();
113                         String errMsg = msgs.getServerUnav();
114                         
115                 
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);
120                         }
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);
125                         }
126                         errRes = new ErrorResponse(errCode,dmaapErrCode,errMsg);
127                         
128                         LOGGER.info(errRes.toString());
129                         Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
130                                         errRes.getErrMapperStr()).build();
131                         
132                         return response;
133                         
134                 }
135                 /**
136                  * UnAuthorized 
137                  */
138                 if(ex instanceof NotAuthorizedException)
139                 {
140                         errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,DMaaPResponseCode.ACCESS_NOT_PERMITTED.
141                                         getResponseCode(),msgs.getAuthFailure());
142                         
143                         LOGGER.info(errRes.toString());
144                         Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
145                                         errRes.getErrMapperStr()).build();
146                         
147                         return response;
148                 }
149                 /**
150                  * Malformed request
151                  */
152                 if(ex instanceof BadRequestException)
153                 {
154                         errRes = new ErrorResponse(HttpStatus.SC_BAD_REQUEST,DMaaPResponseCode.INCORRECT_JSON.
155                                         getResponseCode(),msgs.getBadRequest());
156                         
157                         LOGGER.info(errRes.toString());
158                         Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
159                                         errRes.getErrMapperStr()).build();
160                         
161                         return response;
162                 }
163                 /**
164                  * HTTP Method not allowed
165                  */
166                 if(ex instanceof NotAllowedException)
167                 {
168                         errRes = new ErrorResponse(HttpStatus.SC_METHOD_NOT_ALLOWED,DMaaPResponseCode.METHOD_NOT_ALLOWED.
169                                         getResponseCode(),msgs.getMethodNotAllowed());
170                         
171                         LOGGER.info(errRes.toString());
172                         Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
173                                         errRes.getErrMapperStr()).build();
174                         
175                         return response;
176                 }
177                 
178                 /**
179                  * Server unavailable
180                  */
181                 if(ex instanceof ServiceUnavailableException)
182                 {
183                         errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,DMaaPResponseCode.SERVER_UNAVAILABLE.
184                                         getResponseCode(),msgs.getServerUnav());
185                         
186                         LOGGER.info(errRes.toString());
187                         Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
188                                         errRes.getErrMapperStr()).build();
189                         
190                         return response;
191                 }
192                 
193                 
194                 
195                 return Response.serverError().build();
196         }
197
198         
199
200         
201 }
202