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;
24 import java.util.Map.Entry;
25 import java.util.function.Supplier;
27 import javax.servlet.ServletContext;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.Response.ResponseBuilder;
33 import org.openecomp.sdc.be.components.clean.ComponentsCleanBusinessLogic;
34 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
35 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
36 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.MonitoringBusinessLogic;
40 import org.openecomp.sdc.be.components.impl.ProductBusinessLogic;
41 import org.openecomp.sdc.be.components.impl.ProductComponentInstanceBusinessLogic;
42 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
44 import org.openecomp.sdc.be.components.impl.ServiceComponentInstanceBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.VFComponentInstanceBusinessLogic;
46 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
47 import org.openecomp.sdc.be.config.BeEcompErrorManager;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.dao.api.IElementDAO;
50 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
51 import org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter;
52 import org.openecomp.sdc.be.impl.ComponentsUtils;
53 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
54 import org.openecomp.sdc.be.model.User;
55 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
56 import org.openecomp.sdc.be.user.UserBusinessLogic;
57 import org.openecomp.sdc.common.api.Constants;
58 import org.openecomp.sdc.common.config.EcompErrorName;
59 import org.openecomp.sdc.common.servlets.BasicServlet;
60 import org.openecomp.sdc.exception.ResponseFormat;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.web.context.WebApplicationContext;
65 import fj.data.Either;
67 public class BeGenericServlet extends BasicServlet {
70 protected HttpServletRequest servletRequest;
72 private static Logger log = LoggerFactory.getLogger(BeGenericServlet.class.getName());
74 /******************** New error response mechanism
75 * @param additionalParams **************/
77 protected Response buildErrorResponse(ResponseFormat requestErrorWrapper) {
78 return Response.status(requestErrorWrapper.getStatus()).entity(gson.toJson(requestErrorWrapper.getRequestError())).build();
81 protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity) {
82 return buildOkResponse(errorResponseWrapper, entity, null);
85 protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity, Map<String, String> additionalHeaders) {
86 int status = errorResponseWrapper.getStatus();
87 ResponseBuilder responseBuilder = Response.status(status);
89 if (log.isTraceEnabled())
90 log.trace("returned entity is {}", entity.toString());
91 responseBuilder = responseBuilder.entity(entity);
93 if (additionalHeaders != null) {
94 for (Entry<String, String> additionalHeader : additionalHeaders.entrySet()) {
95 String headerName = additionalHeader.getKey();
96 String headerValue = additionalHeader.getValue();
97 if (log.isTraceEnabled())
98 log.trace("Adding header {} with value {} to the response", headerName, headerValue);
99 responseBuilder.header(headerName, headerValue);
102 return responseBuilder.build();
105 /*******************************************************************************************************/
106 protected Either<User, ResponseFormat> getUser(final HttpServletRequest request, String userId) {
107 Either<User, ActionStatus> eitherCreator = getUserAdminManager(request.getSession().getServletContext()).getUser(userId, false);
108 if (eitherCreator.isRight()) {
109 log.info("createResource method - user is not listed. userId= {}", userId);
110 ResponseFormat errorResponse = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_INFORMATION);
111 User user = new User("", "", userId, "", null, null);
113 getComponentsUtils().auditResource(errorResponse, user, null, "", "", AuditingActionEnum.CHECKOUT_RESOURCE, null);
114 return Either.right(errorResponse);
116 return Either.left(eitherCreator.left().value());
120 protected UserBusinessLogic getUserAdminManager(ServletContext context) {
121 return getClassFromWebAppContext(context, () -> UserBusinessLogic.class);
124 protected ResourceBusinessLogic getResourceBL(ServletContext context) {
125 return getClassFromWebAppContext(context, () -> ResourceBusinessLogic.class);
128 protected ComponentsCleanBusinessLogic getComponentCleanerBL(ServletContext context) {
129 return getClassFromWebAppContext(context, () -> ComponentsCleanBusinessLogic.class);
132 protected ServiceBusinessLogic getServiceBL(ServletContext context) {
133 return getClassFromWebAppContext(context, () -> ServiceBusinessLogic.class);
136 protected ProductBusinessLogic getProductBL(ServletContext context) {
137 return getClassFromWebAppContext(context, () -> ProductBusinessLogic.class);
140 protected ArtifactsBusinessLogic getArtifactBL(ServletContext context) {
141 return getClassFromWebAppContext(context, () -> ArtifactsBusinessLogic.class);
144 protected ElementBusinessLogic getElementBL(ServletContext context) {
145 return getClassFromWebAppContext(context, () -> ElementBusinessLogic.class);
148 protected MonitoringBusinessLogic getMonitoringBL(ServletContext context) {
149 return getClassFromWebAppContext(context, () -> MonitoringBusinessLogic.class);
152 protected AssetMetadataConverter getAssetUtils(ServletContext context) {
153 return getClassFromWebAppContext(context, () -> AssetMetadataConverter.class);
156 protected LifecycleBusinessLogic getLifecycleBL(ServletContext context) {
157 return getClassFromWebAppContext(context, () -> LifecycleBusinessLogic.class);
160 protected <SomeClass> SomeClass getClassFromWebAppContext(ServletContext context, Supplier<Class<SomeClass>> businessLogicClassGen) {
161 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
162 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
163 SomeClass monitoringBusinessLogic = webApplicationContext.getBean(businessLogicClassGen.get());
164 return monitoringBusinessLogic;
167 protected GroupBusinessLogic getGroupBL(ServletContext context) {
169 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
170 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
171 GroupBusinessLogic groupBusinessLogic = webApplicationContext.getBean(GroupBusinessLogic.class);
172 return groupBusinessLogic;
175 protected ComponentInstanceBusinessLogic getComponentInstanceBL(ServletContext context, ComponentTypeEnum containerComponentType) {
176 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
177 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
178 if (containerComponentType == ComponentTypeEnum.RESOURCE) {
179 return webApplicationContext.getBean(VFComponentInstanceBusinessLogic.class);
181 if (containerComponentType == ComponentTypeEnum.SERVICE) {
182 return webApplicationContext.getBean(ServiceComponentInstanceBusinessLogic.class);
184 if (containerComponentType == ComponentTypeEnum.PRODUCT) {
185 return webApplicationContext.getBean(ProductComponentInstanceBusinessLogic.class);
190 protected IElementDAO getElementDao(Class<? extends IElementDAO> clazz, ServletContext context) {
191 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
193 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
195 return webApplicationContext.getBean(clazz);
198 protected ComponentsUtils getComponentsUtils() {
199 ServletContext context = this.servletRequest.getSession().getServletContext();
201 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
202 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
203 ComponentsUtils componentsUtils = webApplicationContext.getBean(ComponentsUtils.class);
204 return componentsUtils;
208 * Used to support Unit Test.<br>
209 * Header Params are not supported in Unit Tests
213 protected String initHeaderParam(String headerValue, HttpServletRequest request, String headerName) {
215 if (headerValue != null) {
216 retValue = headerValue;
218 retValue = request.getHeader(headerName);
223 protected String getContentDispositionValue(String artifactFileName) {
224 return new StringBuilder().append("attachment; filename=\"").append(artifactFileName).append("\"").toString();
227 protected ComponentBusinessLogic getComponentBL(ComponentTypeEnum componentTypeEnum, ServletContext context) {
228 ComponentBusinessLogic businessLogic;
229 switch (componentTypeEnum) {
231 businessLogic = getResourceBL(context);
235 businessLogic = getServiceBL(context);
239 businessLogic = getProductBL(context);
242 case RESOURCE_INSTANCE: {
243 businessLogic = getResourceBL(context);
247 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "getComponentBL");
248 BeEcompErrorManager.getInstance().logBeSystemError("getComponentBL");
249 throw new IllegalArgumentException("Illegal component type:" + componentTypeEnum.getValue());
252 return businessLogic;