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