1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.nsa.dmaap.service;
24 import java.io.IOException;
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;
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;
46 import com.att.nsa.cambria.CambriaApiException;
47 import com.att.nsa.cambria.beans.ApiKeyBean;
48 import com.att.nsa.cambria.beans.DMaaPContext;
49 import com.att.nsa.cambria.exception.DMaaPResponseCode;
50 import com.att.nsa.cambria.exception.ErrorResponse;
51 import com.att.nsa.cambria.service.ApiKeysService;
52 import com.att.nsa.cambria.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;
58 * This class is a CXF REST service
59 * which acts as gateway for Cambria Api
66 public class ApiKeysRestService {
71 //private Logger log = Logger.getLogger(ApiKeysRestService.class.toString());
72 private static final EELFLogger log = EELFManager.getInstance().getLogger(ApiKeysRestService.class);
74 * HttpServletRequest obj
77 private HttpServletRequest request;
80 * HttpServletResponse obj
83 private HttpServletResponse response;
89 @Qualifier("configurationReader")
90 private ConfigurationReader configReader;
96 private ApiKeysService apiKeyService;
99 * Returns a list of all the existing Api keys
100 * @throws CambriaApiException
102 * @throws IOException
105 public void getAllApiKeys() throws CambriaApiException {
107 log.info("Inside ApiKeysRestService.getAllApiKeys");
110 apiKeyService.getAllApiKeys(ServiceUtil.getDMaaPContext(configReader, request, response));
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);
124 * Returns details of a particular api key whose <code>name</code> is passed
128 * - name of the api key
129 * @throws CambriaApiException
130 * @throws IOException
134 public void getApiKey(@PathParam("apiKey") String apiKeyName) throws CambriaApiException {
135 log.info("Fetching details of api key: " + apiKeyName);
138 apiKeyService.getApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), 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);
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);
154 * Creates api key using the <code>email</code> and <code>description</code>
157 * @throws CambriaApiException
158 * @throws JSONException
162 @Consumes(MediaType.APPLICATION_JSON)
163 public void createApiKey(ApiKeyBean nsaApiKey) throws CambriaApiException {
164 log.info("Creating Api Key.");
167 apiKeyService.createApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), 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);
181 * Updates an existing apiKey using the key name passed a parameter and the
185 * - name of the api key to be updated
187 * @throws CambriaApiException
188 * @throws JSONException
189 * @throws IOException
190 * @throws AccessDeniedException
194 public void updateApiKey(@PathParam("apiKey") String apiKeyName,
195 ApiKeyBean nsaApiKey) throws CambriaApiException, JSONException {
196 log.info("Updating Api Key.");
201 .updateApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), 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);
215 * Deletes an existing apiKey using the key name passed as a parameter.
218 * - name of the api key to be updated
219 * @throws CambriaApiException
220 * @throws IOException
221 * @throws AccessDeniedException
225 public void deleteApiKey(@PathParam("apiKey") String apiKeyName) throws CambriaApiException {
226 log.info("Deleting Api Key: " + apiKeyName);
228 apiKeyService.deleteApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), 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);
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);