4b9324ea55ad43ba7433dcdd8275e601be02b62e
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / DMaaPCambriaExceptionMapper.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 org.onap.dmaap;
23
24
25 import javax.inject.Singleton;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.core.Context;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.ext.ExceptionMapper;
31 import javax.ws.rs.ext.Provider;
32
33 import org.apache.http.HttpStatus;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import org.springframework.beans.factory.annotation.Autowired;
37
38 import org.onap.dmaap.dmf.mr.CambriaApiException;
39 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
40 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
41 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
42
43 /**
44  * Exception Mapper class to handle
45  * CambriaApiException 
46  * @author rajashree.khare
47  *
48  */
49 @Provider
50 @Singleton
51 public class DMaaPCambriaExceptionMapper implements ExceptionMapper<CambriaApiException>{
52
53 /**
54  * Error response obj
55  */
56         private ErrorResponse errRes;
57
58 /**
59  * Logger obj
60  */
61         
62
63         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DMaaPCambriaExceptionMapper.class);
64         
65
66         /**
67          * Error msg obj
68          */
69         @Autowired
70         private DMaaPErrorMessages msgs;
71         
72         /**
73          * HttpServletRequest obj
74          */
75         @Context
76         private HttpServletRequest req;
77         
78         /**
79          * HttpServletResponse obj
80          */
81         @Context
82         private HttpServletResponse res;
83         
84         /**
85          * Contructor for DMaaPCambriaExceptionMapper
86          */
87         public DMaaPCambriaExceptionMapper() {
88                 super();
89                 LOGGER.info("Cambria Exception Mapper Created..");
90         }
91         
92         /**
93          * The toResponse method is called when 
94          * an exception of type CambriaApiException
95          * is thrown.This method will send a custom error
96          * response to the client.
97          */
98         @Override
99         public Response toResponse(CambriaApiException ex) {
100
101                 LOGGER.info("Reached Cambria Exception Mapper..");
102                 
103                 /**
104                  * Cambria Generic Exception
105                  */
106                 /*if(ex instanceof CambriaApiException)
107                 {*/
108                         
109                         errRes = ex.getErrRes();
110                         if(errRes!=null) {
111                                 
112                                 Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
113                                                 errRes.getErrMapperStr()).build();
114                                 
115                                 return response;
116                         }
117                         else
118                         {
119                                 
120                                 Response response = Response.status(ex.getStatus()).header("exception",
121                                                 ex.getMessage()).build();
122                                 
123                                 return response;
124                         }
125                         
126                         
127                 /*}
128                 else
129                 {
130                         errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
131                                         DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(), msgs.getServerUnav());
132                         
133                         Response response = Response.status(errRes.getHttpStatusCode()).header("exception", 
134                                         errRes.getErrMapperStr()).build();
135                         
136                         return response;
137                 }*/
138                 
139         }
140
141         
142         
143 }