Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ConfigServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import org.openecomp.sdc.be.config.Configuration;
25 import org.openecomp.sdc.common.api.ConfigurationSource;
26 import org.openecomp.sdc.common.api.Constants;
27 import org.openecomp.sdc.common.servlets.BasicServlet;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import javax.servlet.ServletContext;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MediaType;
38
39 /**
40  * Root resource (exposed at "/" path)
41  */
42 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
43 @Path("/config")
44 public class ConfigServlet extends BasicServlet {
45
46     private static final Logger log = LoggerFactory.getLogger(ConfigServlet.class);
47
48     @GET
49     @Path("/get")
50     @Produces(MediaType.APPLICATION_JSON)
51     public String getConfig(@Context final HttpServletRequest request) {
52
53         String result = null;
54
55         ServletContext context = request.getSession().getServletContext();
56
57         ConfigurationSource configurationSource = (ConfigurationSource) context.getAttribute(Constants.CONFIGURATION_SOURCE_ATTR);
58         if (configurationSource != null) {
59             Configuration configuration = configurationSource.getAndWatchConfiguration(Configuration.class, null);
60
61             if (configuration == null) {
62                 log.warn("Configuration of type {} was not found", Configuration.class);
63             }
64             log.debug("{}", configuration);
65             log.info("Info level ENABLED...");
66             log.info("The value returned from getConfig is {}", configuration);
67
68             result = gson.toJson(configuration);
69
70         } else {
71             log.warn("Source Configuration object was not initialized in the context.");
72         }
73
74         return result;
75
76     }
77
78 }