2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import java.util.EnumMap;
25 import java.util.Map.Entry;
26 import java.util.function.Supplier;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.ws.rs.core.Context;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.Response.ResponseBuilder;
34 import org.openecomp.sdc.be.components.clean.ComponentsCleanBusinessLogic;
35 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
36 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
40 import org.openecomp.sdc.be.components.impl.MonitoringBusinessLogic;
41 import org.openecomp.sdc.be.components.impl.ProductBusinessLogic;
42 import org.openecomp.sdc.be.components.impl.ProductComponentInstanceBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
44 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.ServiceComponentInstanceBusinessLogic;
46 import org.openecomp.sdc.be.components.impl.VFComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.dao.api.IElementDAO;
51 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
52 import org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter;
53 import org.openecomp.sdc.be.impl.ComponentsUtils;
54 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
55 import org.openecomp.sdc.be.model.User;
56 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
57 import org.openecomp.sdc.be.user.UserBusinessLogic;
58 import org.openecomp.sdc.common.api.Constants;
59 import org.openecomp.sdc.common.config.EcompErrorName;
60 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
61 import org.openecomp.sdc.common.servlets.BasicServlet;
62 import org.openecomp.sdc.exception.ResponseFormat;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65 import org.springframework.web.context.WebApplicationContext;
67 import fj.data.Either;
69 public class BeGenericServlet extends BasicServlet {
72 protected HttpServletRequest servletRequest;
74 private static Logger log = LoggerFactory.getLogger(BeGenericServlet.class.getName());
76 /******************** New error response mechanism
77 * @param additionalParams **************/
79 protected Response buildErrorResponse(ResponseFormat requestErrorWrapper) {
80 return Response.status(requestErrorWrapper.getStatus()).entity(gson.toJson(requestErrorWrapper.getRequestError())).build();
83 protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity) {
84 return buildOkResponse(errorResponseWrapper, entity, null);
87 protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity, Map<String, String> additionalHeaders) {
88 int status = errorResponseWrapper.getStatus();
89 ResponseBuilder responseBuilder = Response.status(status);
91 if (log.isTraceEnabled())
92 log.trace("returned entity is {}", entity.toString());
93 responseBuilder = responseBuilder.entity(entity);
95 if (additionalHeaders != null) {
96 for (Entry<String, String> additionalHeader : additionalHeaders.entrySet()) {
97 String headerName = additionalHeader.getKey();
98 String headerValue = additionalHeader.getValue();
99 if (log.isTraceEnabled())
100 log.trace("Adding header {} with value {} to the response", headerName, headerValue);
101 responseBuilder.header(headerName, headerValue);
104 return responseBuilder.build();
107 /*******************************************************************************************************/
108 protected Either<User, ResponseFormat> getUser(final HttpServletRequest request, String userId) {
109 Either<User, ActionStatus> eitherCreator = getUserAdminManager(request.getSession().getServletContext()).getUser(userId, false);
110 if (eitherCreator.isRight()) {
111 log.info("createResource method - user is not listed. userId= {}", userId);
112 ResponseFormat errorResponse = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_INFORMATION);
113 User user = new User("", "", userId, "", null, null);
115 getComponentsUtils().auditResource(errorResponse, user, null, "", "", AuditingActionEnum.CHECKOUT_RESOURCE, null);
116 return Either.right(errorResponse);
118 return Either.left(eitherCreator.left().value());
122 protected UserBusinessLogic getUserAdminManager(ServletContext context) {
123 return getClassFromWebAppContext(context, () -> UserBusinessLogic.class);
126 protected ResourceBusinessLogic getResourceBL(ServletContext context) {
127 return getClassFromWebAppContext(context, () -> ResourceBusinessLogic.class);
130 protected ComponentsCleanBusinessLogic getComponentCleanerBL(ServletContext context) {
131 return getClassFromWebAppContext(context, () -> ComponentsCleanBusinessLogic.class);
134 protected ServiceBusinessLogic getServiceBL(ServletContext context) {
135 return getClassFromWebAppContext(context, () -> ServiceBusinessLogic.class);
138 protected ProductBusinessLogic getProductBL(ServletContext context) {
139 return getClassFromWebAppContext(context, () -> ProductBusinessLogic.class);
142 protected ArtifactsBusinessLogic getArtifactBL(ServletContext context) {
143 return getClassFromWebAppContext(context, () -> ArtifactsBusinessLogic.class);
146 protected ElementBusinessLogic getElementBL(ServletContext context) {
147 return getClassFromWebAppContext(context, () -> ElementBusinessLogic.class);
150 protected MonitoringBusinessLogic getMonitoringBL(ServletContext context) {
151 return getClassFromWebAppContext(context, () -> MonitoringBusinessLogic.class);
154 protected AssetMetadataConverter getAssetUtils(ServletContext context) {
155 return getClassFromWebAppContext(context, () -> AssetMetadataConverter.class);
158 protected LifecycleBusinessLogic getLifecycleBL(ServletContext context) {
159 return getClassFromWebAppContext(context, () -> LifecycleBusinessLogic.class);
162 protected <SomeClass> SomeClass getClassFromWebAppContext(ServletContext context, Supplier<Class<SomeClass>> businessLogicClassGen) {
163 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
164 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
165 SomeClass monitoringBusinessLogic = webApplicationContext.getBean(businessLogicClassGen.get());
166 return monitoringBusinessLogic;
169 protected GroupBusinessLogic getGroupBL(ServletContext context) {
171 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
172 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
173 GroupBusinessLogic groupBusinessLogic = webApplicationContext.getBean(GroupBusinessLogic.class);
174 return groupBusinessLogic;
177 protected ComponentInstanceBusinessLogic getComponentInstanceBL(ServletContext context, ComponentTypeEnum containerComponentType) {
178 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
179 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
180 if (containerComponentType == ComponentTypeEnum.RESOURCE) {
181 return webApplicationContext.getBean(VFComponentInstanceBusinessLogic.class);
183 if (containerComponentType == ComponentTypeEnum.SERVICE) {
184 return webApplicationContext.getBean(ServiceComponentInstanceBusinessLogic.class);
186 if (containerComponentType == ComponentTypeEnum.PRODUCT) {
187 return webApplicationContext.getBean(ProductComponentInstanceBusinessLogic.class);
192 protected IElementDAO getElementDao(Class<? extends IElementDAO> clazz, ServletContext context) {
193 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
195 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
197 return webApplicationContext.getBean(clazz);
200 protected ComponentsUtils getComponentsUtils() {
201 ServletContext context = this.servletRequest.getSession().getServletContext();
203 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
204 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
205 ComponentsUtils componentsUtils = webApplicationContext.getBean(ComponentsUtils.class);
206 return componentsUtils;
210 * Used to support Unit Test.<br>
211 * Header Params are not supported in Unit Tests
215 protected String initHeaderParam(String headerValue, HttpServletRequest request, String headerName) {
217 if (headerValue != null) {
218 retValue = headerValue;
220 retValue = request.getHeader(headerName);
225 protected String getContentDispositionValue(String artifactFileName) {
226 return new StringBuilder().append("attachment; filename=\"").append(artifactFileName).append("\"").toString();
229 protected ComponentBusinessLogic getComponentBL(ComponentTypeEnum componentTypeEnum, ServletContext context) {
230 ComponentBusinessLogic businessLogic;
231 switch (componentTypeEnum) {
233 businessLogic = getResourceBL(context);
237 businessLogic = getServiceBL(context);
241 businessLogic = getProductBL(context);
244 case RESOURCE_INSTANCE: {
245 businessLogic = getResourceBL(context);
249 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "getComponentBL");
250 BeEcompErrorManager.getInstance().logBeSystemError("getComponentBL");
251 throw new IllegalArgumentException("Illegal component type:" + componentTypeEnum.getValue());
254 return businessLogic;