Sonar majior issues
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / service / ApiKeysRestService.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.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
37
38 import org.apache.http.HttpStatus;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import org.json.JSONException;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Qualifier;
44 import org.springframework.stereotype.Component;
45
46 import org.onap.dmaap.dmf.mr.CambriaApiException;
47 import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
48 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
49 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
50 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
51 import org.onap.dmaap.dmf.mr.service.ApiKeysService;
52 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
53 import com.att.nsa.configs.ConfigDbException;
54 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
55 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
56
57 /**
58  * This class is a CXF REST service 
59  * which acts as gateway for Cambria Api
60  * Keys.
61  * @author rajashree.khare
62  *
63  */
64 @Component
65 @Path("/")
66 public class ApiKeysRestService {
67
68         /**
69          * Logger obj
70          */
71
72         private static final EELFLogger log = EELFManager.getInstance().getLogger(ApiKeysRestService.class);
73         /**
74          * HttpServletRequest obj
75          */
76         @Context
77         private HttpServletRequest request;
78
79         /**
80          * HttpServletResponse obj
81          */
82         @Context
83         private HttpServletResponse response;
84
85         /**
86          * Config Reader
87          */
88         @Autowired
89         @Qualifier("configurationReader")
90         private ConfigurationReader configReader;
91
92         /**
93          * ApiKeysService obj
94          */
95         @Autowired
96         private ApiKeysService apiKeyService;
97
98         /**
99          * Returns a list of all the existing Api keys
100          * @throws CambriaApiException 
101          * 
102          * @throws IOException
103          * */
104         @GET
105         public void getAllApiKeys() throws CambriaApiException {
106
107                 log.info("Inside ApiKeysRestService.getAllApiKeys");
108
109                 try {
110                         apiKeyService.getAllApiKeys(getDmaapContext());
111                         log.info("Fetching all API keys is Successful");
112                 } catch (ConfigDbException | IOException e) {
113                         log.error("Error while retrieving API keys: " + e);
114                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
115                                         DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
116                                         "Error while retrieving API keys: "+ e.getMessage());
117                         log.info(errRes.toString());
118                         throw new CambriaApiException(errRes);
119                 }
120
121         }
122
123         /**
124          * Returns details of a particular api key whose <code>name</code> is passed
125          * as a parameter
126          * 
127          * @param apiKeyName
128          *            - name of the api key
129          * @throws CambriaApiException 
130          * @throws IOException
131          * */
132         @GET
133         @Path("/{apiKey}")
134         public void getApiKey(@PathParam("apiKey") String apiKeyName) throws CambriaApiException {
135                 log.info("Fetching details of api key: " + apiKeyName);
136
137                 try {
138                         apiKeyService.getApiKey(getDmaapContext(), apiKeyName);
139                         log.info("Fetching specific API key is Successful");
140                 } catch (ConfigDbException | IOException e) {
141                         log.error("Error while retrieving API key details: " + e);
142                         
143                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
144                                         DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
145                                         "Error while retrieving API key details: "+ e.getMessage());
146                         log.info(errRes.toString());
147                         throw new CambriaApiException(errRes);
148                 }
149         }
150         
151         
152
153         /**
154          * Creates api key using the <code>email</code> and <code>description</code>
155          * 
156          * @param nsaApiKey
157          * @throws CambriaApiException 
158          * @throws JSONException 
159          * */
160         @POST
161         @Path("/create")
162         @Consumes(MediaType.APPLICATION_JSON)
163         public void createApiKey(ApiKeyBean nsaApiKey) throws CambriaApiException, JSONException {
164                 log.info("Creating Api Key.");
165
166                 try {
167                         apiKeyService.createApiKey(getDmaapContext(), nsaApiKey);
168                         log.info("Creating API key is Successful");
169                 } catch (KeyExistsException | ConfigDbException | IOException e) {
170                         log.error("Error while Creating API key : " + e.getMessage(), e);
171                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
172                                         DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
173                                         "Error while Creating API key : "+ e.getMessage());
174                         log.info(errRes.toString());
175                         throw new CambriaApiException(errRes);
176                 }
177
178         }
179
180         /**
181          * Updates an existing apiKey using the key name passed a parameter and the
182          * details passed.
183          * 
184          * @param apiKeyName
185          *            - name of the api key to be updated
186          * @param nsaApiKey
187          * @throws CambriaApiException 
188          * @throws JSONException 
189          * @throws IOException
190          * @throws AccessDeniedException
191          * */
192         @PUT
193         @Path("/{apiKey}")
194         public void updateApiKey(@PathParam("apiKey") String apiKeyName,
195                         ApiKeyBean nsaApiKey) throws CambriaApiException, JSONException {
196                 log.info("Updating Api Key.");
197
198                 try {
199                         
200                         apiKeyService
201                                         .updateApiKey(getDmaapContext(), apiKeyName, nsaApiKey);
202                         log.error("API key updated sucessfully");
203                 } catch (ConfigDbException | IOException | AccessDeniedException e) {
204                         log.error("Error while Updating API key : " + apiKeyName, e);
205                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
206                                         DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
207                                         "Error while Updating API key : "+ e.getMessage());
208                         log.info(errRes.toString());
209                         throw new CambriaApiException(errRes);
210                         
211                 }
212         }
213
214         /**
215          * Deletes an existing apiKey using the key name passed as a parameter.
216          * 
217          * @param apiKeyName
218          *            - name of the api key to be updated
219          * @throws CambriaApiException 
220          * @throws IOException
221          * @throws AccessDeniedException
222          * */
223         @DELETE
224         @Path("/{apiKey}")
225         public void deleteApiKey(@PathParam("apiKey") String apiKeyName) throws CambriaApiException {
226                 log.info("Deleting Api Key: " + apiKeyName);
227                 try {
228                         apiKeyService.deleteApiKey(getDmaapContext(), apiKeyName);
229                         log.info("Api Key deleted successfully: " + apiKeyName);
230                 } catch (ConfigDbException | IOException | AccessDeniedException e) {
231                         log.error("Error while deleting API key : " + apiKeyName, e);
232
233                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
234                                         DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
235                                         "Error while deleting API key : "+ e.getMessage());
236                         log.info(errRes.toString());
237                         throw new CambriaApiException(errRes);
238
239                 }
240         }
241
242         /**
243          * Create a dmaap context
244          * @return DMaaPContext
245          */
246         private DMaaPContext getDmaapContext() {
247                 DMaaPContext dmaapContext = new DMaaPContext();
248                 dmaapContext.setConfigReader(configReader);
249                 dmaapContext.setRequest(request);
250                 dmaapContext.setResponse(response);
251                 return dmaapContext;
252         }
253
254 }