79a39fb33284339a07b8360c5d6bfc8c60a83655
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / service / UIRestServices.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 com.att.nsa.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 kafka.common.TopicExistsException;
34
35 import org.apache.http.HttpStatus;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Qualifier;
41 import org.springframework.stereotype.Component;
42
43 import com.att.nsa.cambria.beans.DMaaPContext;
44 import com.att.nsa.cambria.service.UIService;
45 import com.att.nsa.cambria.utils.ConfigurationReader;
46 import com.att.nsa.cambria.utils.DMaaPResponseBuilder;
47 import com.att.nsa.configs.ConfigDbException;
48
49 /**
50  * UI Rest Service
51  * @author author
52  *
53  */
54 @Component
55 public class UIRestServices {
56
57         /**
58          * Logger obj
59          */
60         //private static final Logger LOGGER = Logger.getLogger(UIRestServices.class);
61         
62         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UIRestServices.class);
63
64         @Autowired
65         private UIService uiService;
66         
67         /**
68          * Config Reader
69          */
70         @Autowired
71         @Qualifier("configurationReader")
72         private ConfigurationReader configReader;
73
74         /**
75          * HttpServletRequest obj
76          */
77         @Context
78         private HttpServletRequest request;
79
80         /**
81          * HttpServletResponse obj
82          */
83         @Context
84         private HttpServletResponse response;
85
86         /**
87          * getting the hello
88          */
89         @GET
90         @Path("/")
91         public void hello() {
92                 try {
93                         LOGGER.info("Calling hello page.");
94
95                         uiService.hello(getDmaapContext());
96
97                         LOGGER.info("Hello page is returned.");
98                 } catch (IOException excp) {
99                         LOGGER.error("Error while calling hello page: " + excp.getMessage(), excp);
100                         DMaaPResponseBuilder.respondWithError(getDmaapContext(), HttpStatus.SC_NOT_FOUND,
101                                         "Error while calling hello page: " + excp.getMessage());
102                 }
103         }
104
105         /**
106          * getApikeysTable
107          */
108         @GET
109         @Path("/ui/apikeys")
110         public void getApiKeysTable() {
111                 try {
112                         LOGGER.info("Fetching list of all api keys.");
113
114                         uiService.getApiKeysTable(getDmaapContext());
115
116                         LOGGER.info("Returning list of all api keys.");
117                 } catch (ConfigDbException | IOException excp) {
118                         LOGGER.error("Error while fetching list of all api keys: " + excp.getMessage(), excp);
119                         DMaaPResponseBuilder.respondWithError(getDmaapContext(), HttpStatus.SC_NOT_FOUND,
120                                         "Error while fetching list of all api keys: " + excp.getMessage());
121                 }
122         }
123
124         /**
125          * getApiKey
126          * 
127          * @param apiKey
128          * @exception Exception
129          */
130         @GET
131         @Path("/ui/apikeys/{apiKey}")
132         public void getApiKey(@PathParam("apiKey") String apiKey) {
133                 try {
134                         LOGGER.info("Fetching details of api key: " + apiKey);
135
136                         uiService.getApiKey(getDmaapContext(), apiKey);
137
138                         LOGGER.info("Returning details of api key: " + apiKey);
139                 } catch (Exception excp) {
140                         LOGGER.error("Error while fetching details of api key: " + apiKey, excp);
141                         DMaaPResponseBuilder.respondWithError(getDmaapContext(), HttpStatus.SC_NOT_FOUND,
142                                         "Error while fetching details of api key: " + apiKey);
143                 }
144         }
145
146         @GET
147         @Path("/ui/topics")
148         public void getTopicsTable() {
149                 try {
150                         LOGGER.info("Fetching list of all topics.");
151
152                         uiService.getTopicsTable(getDmaapContext());
153
154                         LOGGER.info("Returning list of all topics.");
155                 } catch (ConfigDbException | IOException excp) {
156                         LOGGER.error("Error while fetching list of all topics: " + excp, excp);
157                         DMaaPResponseBuilder.respondWithError(getDmaapContext(), HttpStatus.SC_NOT_FOUND,
158                                         "Error while fetching list of all topics: " + excp.getMessage());
159                 }
160         }
161
162         /**
163          * 
164          * @param topic
165          */
166         @GET
167         @Path("/ui/topics/{topic}")
168         public void getTopic(@PathParam("topic") String topic) {
169                 try {
170                         LOGGER.info("Fetching details of topic: " + topic);
171
172                         uiService.getTopic(getDmaapContext(), topic);
173
174                         LOGGER.info("Returning details of topic: " + topic);
175                 } catch (ConfigDbException | IOException | TopicExistsException excp) {
176                         LOGGER.error("Error while fetching details of topic: " + topic, excp);
177                         DMaaPResponseBuilder.respondWithError(getDmaapContext(), HttpStatus.SC_NOT_FOUND,
178                                         "Error while fetching details of topic: " + topic);
179                 }
180         }
181
182         /**
183          * This method is used for taking Configuration Object,HttpServletRequest
184          * Object,HttpServletRequest HttpServletResponse Object,HttpServletSession
185          * Object.
186          * 
187          * @return DMaaPContext object from where user can get Configuration
188          *         Object,HttpServlet Object
189          * 
190          */
191         private DMaaPContext getDmaapContext() {
192                 DMaaPContext dmaapContext = new DMaaPContext();
193                 dmaapContext.setConfigReader(configReader);
194                 dmaapContext.setRequest(request);
195                 dmaapContext.setResponse(response);
196                 return dmaapContext;
197         }
198 }