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