summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/att/nsa/dmaap/service/ApiKeysRestService.java
blob: eb76a96ad1b6364e70725bac9013e73125d8236e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  org.onap.dmaap
 *  ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============LICENSE_END=========================================================
 *
 *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *  
 *******************************************************************************/
package com.att.nsa.dmaap.service;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.http.HttpStatus;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.att.nsa.cambria.CambriaApiException;
import com.att.nsa.cambria.beans.ApiKeyBean;
import com.att.nsa.cambria.beans.DMaaPContext;
import com.att.nsa.cambria.exception.DMaaPResponseCode;
import com.att.nsa.cambria.exception.ErrorResponse;
import com.att.nsa.cambria.service.ApiKeysService;
import com.att.nsa.cambria.utils.ConfigurationReader;
import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;

/**
 * This class is a CXF REST service 
 * which acts as gateway for Cambria Api
 * Keys.
 * @author author
 *
 */
@Component
@Path("/")
public class ApiKeysRestService {

	/**
	 * Logger obj
	 */
	//private Logger log = Logger.getLogger(ApiKeysRestService.class.toString());
	private static final EELFLogger log = EELFManager.getInstance().getLogger(ApiKeysRestService.class);
	/**
	 * HttpServletRequest obj
	 */
	@Context
	private HttpServletRequest request;

	/**
	 * HttpServletResponse obj
	 */
	@Context
	private HttpServletResponse response;

	/**
	 * Config Reader
	 */
	@Autowired
	@Qualifier("configurationReader")
	private ConfigurationReader configReader;

	/**
	 * ApiKeysService obj
	 */
	@Autowired
	private ApiKeysService apiKeyService;

	/**
	 * Returns a list of all the existing Api keys
	 * @throws CambriaApiException 
	 * 
	 * @throws IOException
	 * */
	@GET
	public void getAllApiKeys() throws CambriaApiException {

		log.info("Inside ApiKeysRestService.getAllApiKeys");

		try {
			apiKeyService.getAllApiKeys(ServiceUtil.getDMaaPContext(configReader, request, response));
			log.info("Fetching all API keys is Successful");
		} catch (ConfigDbException | IOException e) {
			log.error("Error while retrieving API keys: " + e);
			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
					DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
					"Error while retrieving API keys: "+ e.getMessage());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}

	}

	/**
	 * Returns details of a particular api key whose <code>name</code> is passed
	 * as a parameter
	 * 
	 * @param apiKeyName
	 *            - name of the api key
	 * @throws CambriaApiException 
	 * @throws IOException
	 * */
	@GET
	@Path("/{apiKey}")
	public void getApiKey(@PathParam("apiKey") String apiKeyName) throws CambriaApiException {
		log.info("Fetching details of api key: " + apiKeyName);

		try {
			apiKeyService.getApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), apiKeyName);
			log.info("Fetching specific API key is Successful");
		} catch (ConfigDbException | IOException e) {
			log.error("Error while retrieving API key details: " + e);
			
			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
					DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
					"Error while retrieving API key details: "+ e.getMessage());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}
	}
	
	

	/**
	 * Creates api key using the <code>email</code> and <code>description</code>
	 * 
	 * @param nsaApiKey
	 * @throws CambriaApiException 
	 * @throws JSONException 
	 * */
	@POST
	@Path("/create")
	@Consumes(MediaType.APPLICATION_JSON)
	public void createApiKey(ApiKeyBean nsaApiKey) throws CambriaApiException, JSONException {
		log.info("Creating Api Key.");

		try {
			apiKeyService.createApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), nsaApiKey);
			log.info("Creating API key is Successful");
		} catch (KeyExistsException | ConfigDbException | IOException e) {
			log.error("Error while Creating API key : " + e.getMessage(), e);
			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
					DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
					"Error while Creating API key : "+ e.getMessage());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}

	}

	/**
	 * Updates an existing apiKey using the key name passed a parameter and the
	 * details passed.
	 * 
	 * @param apiKeyName
	 *            - name of the api key to be updated
	 * @param nsaApiKey
	 * @throws CambriaApiException 
	 * @throws JSONException 
	 * @throws IOException
	 * @throws AccessDeniedException
	 * */
	@PUT
	@Path("/{apiKey}")
	public void updateApiKey(@PathParam("apiKey") String apiKeyName,
			ApiKeyBean nsaApiKey) throws CambriaApiException, JSONException {
		log.info("Updating Api Key.");

		try {
			
			apiKeyService
					.updateApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), apiKeyName, nsaApiKey);
			log.error("API key updated sucessfully");
		} catch (ConfigDbException | IOException | AccessDeniedException e) {
			log.error("Error while Updating API key : " + apiKeyName, e);
			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
					DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
					"Error while Updating API key : "+ e.getMessage());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
			
		}
	}

	/**
	 * Deletes an existing apiKey using the key name passed as a parameter.
	 * 
	 * @param apiKeyName
	 *            - name of the api key to be updated
	 * @throws CambriaApiException 
	 * @throws IOException
	 * @throws AccessDeniedException
	 * */
	@DELETE
	@Path("/{apiKey}")
	public void deleteApiKey(@PathParam("apiKey") String apiKeyName) throws CambriaApiException {
		log.info("Deleting Api Key: " + apiKeyName);
		try {
			apiKeyService.deleteApiKey(ServiceUtil.getDMaaPContext(configReader, request, response), apiKeyName);
			log.info("Api Key deleted successfully: " + apiKeyName);
		} catch (ConfigDbException | IOException | AccessDeniedException e) {
			log.error("Error while deleting API key : " + apiKeyName, e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
					DMaaPResponseCode.GENERIC_INTERNAL_ERROR.getResponseCode(), 
					"Error while deleting API key : "+ e.getMessage());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);

		}
	}


}