Catalog alignment
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / 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.fe.servlets;
22
23 import org.openecomp.sdc.common.api.Constants;
24 import org.openecomp.sdc.exception.NotFoundException;
25 import org.openecomp.sdc.fe.config.ConfigurationManager;
26 import org.openecomp.sdc.fe.config.FeEcompErrorManager;
27 import org.openecomp.sdc.fe.config.WorkspaceConfiguration;
28 import org.openecomp.sdc.fe.impl.PluginStatusBL;
29 import org.owasp.esapi.ESAPI;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.Response.Status;
43
44 /**
45  * Root resource (exposed at "/" path)
46  */
47 @Path("/config")
48 public class ConfigServlet extends LoggingServlet {
49
50     private static final Logger log = LoggerFactory.getLogger(ConfigServlet.class.getName());
51     public static final String UNEXPECTED_FE_RESPONSE_LOGGING_ERROR = "Unexpected FE response logging error :";
52     public static final String ERROR_FE_RESPONSE = "FE Response";
53
54     @GET
55     @Path("/ui/workspace")
56     @Produces(MediaType.APPLICATION_JSON)
57     public Response getUIWorkspaceConfiguration(@Context final HttpServletRequest request) {
58
59         try {
60             logFeRequest(request);
61
62             ServletContext context = request.getSession().getServletContext();
63
64             ConfigurationManager configurationManager = (ConfigurationManager) context
65                     .getAttribute(Constants.CONFIGURATION_MANAGER_ATTR);
66
67             WorkspaceConfiguration configuration = configurationManager.getWorkspaceConfiguration();
68             if (configuration == null) {
69                 throw new NotFoundException(WorkspaceConfiguration.class.getSimpleName());
70             }
71             log.info("The value returned from getConfig is {}", configuration);
72             String result = gson.toJson(configuration);
73             Response response = Response.status(Status.OK).entity(result).build();
74             logFeResponse(request, response);
75
76             return response;
77         } catch (Exception e) {
78             FeEcompErrorManager.getInstance().logFeHttpLoggingError(ERROR_FE_RESPONSE);
79             log.error(UNEXPECTED_FE_RESPONSE_LOGGING_ERROR, e);
80             return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{}").build();
81         }
82
83     }
84
85
86     @GET
87     @Path("/ui/plugins")
88     @Produces(MediaType.APPLICATION_JSON)
89     public Response getPluginsConfiguration(@Context final HttpServletRequest request) {
90
91         try {
92             logFeRequest(request);
93
94             ServletContext context = request.getSession().getServletContext();
95
96             PluginStatusBL pluginStatusBL = (PluginStatusBL) context.getAttribute(Constants.PLUGIN_BL_COMPONENT);
97
98             String result = pluginStatusBL.getPluginsList();
99
100             Response response = Response.status(Status.OK).entity(result).build();
101
102             logFeResponse(request, response);
103
104             return response;
105         } catch (Exception e) {
106             FeEcompErrorManager.getInstance().logFeHttpLoggingError( ERROR_FE_RESPONSE);
107             log.error(UNEXPECTED_FE_RESPONSE_LOGGING_ERROR, e);
108             return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{}").build();
109         }
110
111     }
112
113     @GET
114     @Path("/ui/plugins/{pluginId}/online")
115     @Produces(MediaType.APPLICATION_JSON)
116     public Response getPluginOnlineState(@PathParam("pluginId") String pluginId, @Context final HttpServletRequest request) {
117
118         try {
119             logFeRequest(request);
120             pluginId = ESAPI.encoder().encodeForHTML(pluginId);
121             ServletContext context = request.getSession().getServletContext();
122
123             PluginStatusBL pluginStatusBL = (PluginStatusBL) context.getAttribute(Constants.PLUGIN_BL_COMPONENT);
124
125             String result = pluginStatusBL.getPluginAvailability(pluginId);
126
127             if (result == null) {
128                 log.debug("Plugin with pluginId: {} was not found in the configuration", pluginId);
129                 return Response.status(Status.NOT_FOUND).entity("Plugin with pluginId:\"" + pluginId + "\" was not found in the configuration").build();
130             }
131
132             Response response = Response.status(Status.OK).entity(result).build();
133
134             logFeResponse(request, response);
135
136             return response;
137         } catch (Exception e) {
138             FeEcompErrorManager.getInstance().logFeHttpLoggingError(ERROR_FE_RESPONSE);
139             log.error(UNEXPECTED_FE_RESPONSE_LOGGING_ERROR, e);
140             return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{}").build();
141         }
142     }
143
144     protected void inHttpRequest(HttpServletRequest httpRequest) {
145         log.info("{} {} {}", httpRequest.getMethod(), httpRequest.getRequestURI(), httpRequest.getProtocol());
146     }
147
148     /**
149      * Extracted for purpose of clear method name, for logback %M parameter
150      *
151      * @param response http response
152      */
153     protected void outHttpResponse(Response response) {
154         log.info("SC=\"{}\"", response.getStatus());
155     }
156 }