re base code
[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.log.wrappers.Logger;
28 import org.openecomp.sdc.common.servlets.BasicServlet;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
37
38 /**
39  * Root resource (exposed at "/" path)
40  */
41 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
42 @Path("/config")
43 public class ConfigServlet extends BasicServlet {
44
45     private static final Logger log = Logger.getLogger(ConfigServlet.class);
46
47     @GET
48     @Path("/get")
49     @Produces(MediaType.APPLICATION_JSON)
50     public String getConfig(@Context final HttpServletRequest request) {
51
52         String result = null;
53
54         ServletContext context = request.getSession().getServletContext();
55
56         ConfigurationSource configurationSource = (ConfigurationSource) context.getAttribute(Constants.CONFIGURATION_SOURCE_ATTR);
57         if (configurationSource != null) {
58             Configuration configuration = configurationSource.getAndWatchConfiguration(Configuration.class, null);
59
60             if (configuration == null) {
61                 log.warn("Configuration of type {} was not found", Configuration.class);
62             }
63             log.debug("{}", configuration);
64             log.info("Info level ENABLED...");
65             log.info("The value returned from getConfig is {}", configuration);
66
67             result = gson.toJson(configuration);
68
69         } else {
70             log.warn("Source Configuration object was not initialized in the context.");
71         }
72
73         return result;
74
75     }
76
77 }