update the package name
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / service / TransactionRestService.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.service;
23
24 import java.io.IOException;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.core.Context;
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 import org.springframework.beans.factory.annotation.Qualifier;
38 import org.springframework.stereotype.Component;
39
40 import com.att.aft.dme2.internal.jettison.json.JSONException;
41 import  org.onap.dmaap.dmf.mr.CambriaApiException;
42 import  org.onap.dmaap.dmf.mr.beans.DMaaPContext;
43 import  org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
44 import  org.onap.dmaap.dmf.mr.exception.ErrorResponse;
45 import  org.onap.dmaap.dmf.mr.service.TransactionService;
46 import  org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
47 import com.att.nsa.configs.ConfigDbException;
48
49 /**
50  * This class is a CXF REST service 
51  * which acts as gateway for DMaaP
52  * Transaction Ids.
53  * @author rajashree.khare
54  *
55  */
56 @Component
57 @Path("/")
58 public class TransactionRestService {
59
60         /**
61          * Logger obj
62          */
63         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(TransactionRestService.class);
64
65         /**
66          * HttpServletRequest obj
67          */
68         @Context
69         private HttpServletRequest request;
70
71         /**
72          * HttpServletResponse obj
73          */
74         @Context
75         private HttpServletResponse response;
76
77         /**
78          * Config Reader
79          */
80         @Autowired
81         @Qualifier("configurationReader")
82         private ConfigurationReader configReader;
83
84         @Autowired
85         private TransactionService transactionService;
86
87         /**
88          * 
89          * Returns a list of all the existing Transaction Ids
90          * @throws CambriaApiException 
91          * 
92          * @throws IOException
93          * @exception ConfigDbException
94          * @exception IOException
95          * 
96          * 
97          */
98         @GET
99         public void getAllTransactionObjs() throws CambriaApiException {
100                 try {
101                         LOGGER.info("Retrieving list of all transactions.");
102
103                         transactionService.getAllTransactionObjs(getDmaapContext());
104
105                         LOGGER.info("Returning list of all transactions.");
106                 } catch (ConfigDbException | IOException e) {
107                         LOGGER.error("Error while retrieving list of all transactions: "
108                                         + e.getMessage(), e);
109                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_EXPECTATION_FAILED, 
110                                         DMaaPResponseCode.RETRIEVE_TRANSACTIONS.getResponseCode(), 
111                                         "Error while retrieving list of all transactions:"+e.getMessage());
112                         LOGGER.info(errRes.toString());
113                         throw new CambriaApiException(errRes);
114                 }
115         }
116
117         /**
118          * 
119          * Returns details of a particular transaction id whose <code>name</code> is
120          * passed as a parameter
121          * 
122          * @param transactionId
123          *            - id of transaction
124          * @throws CambriaApiException 
125          * @throws IOException
126          * @exception ConfigDbException
127          * @exception IOException
128          * @exception JSONException
129          * 
130          * 
131          */
132         @GET
133         @Path("/{transactionId}")
134         public void getTransactionObj(
135                         @PathParam("transactionId") String transactionId) throws CambriaApiException {
136
137                 LOGGER.info("Fetching details of Transaction ID : " + transactionId);
138
139                 try {
140                         transactionService.getTransactionObj(getDmaapContext(),
141                                         transactionId);
142                 } catch (ConfigDbException | JSONException | IOException e) {
143                         LOGGER.error("Error while retrieving transaction details for id: "
144                                         + transactionId, e);
145                 
146                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_EXPECTATION_FAILED, 
147                                         DMaaPResponseCode.RETRIEVE_TRANSACTIONS_DETAILS.getResponseCode(), 
148                                         "Error while retrieving transaction details for id: ["
149                                                         + transactionId + "]: " + e.getMessage());
150                         LOGGER.info(errRes.toString());
151                         throw new CambriaApiException(errRes);
152
153                 }
154
155                 LOGGER.info("Returning details of transaction " + transactionId);
156
157         }
158
159         /**
160          * This method is used for taking Configuration Object,HttpServletRequest
161          * Object,HttpServletRequest HttpServletResponse Object,HttpServletSession
162          * Object.
163          * 
164          * @return DMaaPContext object from where user can get Configuration
165          *         Object,HttpServlet Object
166          * 
167          */
168         private DMaaPContext getDmaapContext() {
169                 DMaaPContext dmaapContext = new DMaaPContext();
170                 dmaapContext.setConfigReader(configReader);
171                 dmaapContext.setRequest(request);
172                 dmaapContext.setResponse(response);
173                 return dmaapContext;
174         }
175
176 }