[SDC-29] rebase continue work to align source
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / BeGenericServlet.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 java.util.EnumMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.function.Supplier;
27
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;
33
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;
66
67 import fj.data.Either;
68
69 public class BeGenericServlet extends BasicServlet {
70
71         @Context
72         protected HttpServletRequest servletRequest;
73
74         private static Logger log = LoggerFactory.getLogger(BeGenericServlet.class.getName());
75
76         /******************** New error response mechanism 
77          * @param additionalParams **************/
78         
79         protected Response buildErrorResponse(ResponseFormat requestErrorWrapper) {
80                 return Response.status(requestErrorWrapper.getStatus()).entity(gson.toJson(requestErrorWrapper.getRequestError())).build();             
81         }
82
83         protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity) {
84                 return buildOkResponse(errorResponseWrapper, entity, null);
85         }
86
87         protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity, Map<String, String> additionalHeaders) {
88                 int status = errorResponseWrapper.getStatus();
89                 ResponseBuilder responseBuilder = Response.status(status);
90                 if (entity != null) {
91                         if (log.isTraceEnabled())
92                                 log.trace("returned entity is {}", entity.toString());
93                         responseBuilder = responseBuilder.entity(entity);
94                 }
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);
102                         }
103                 }
104                 return responseBuilder.build();
105         }
106
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);
114
115                         getComponentsUtils().auditResource(errorResponse, user, null, "", "", AuditingActionEnum.CHECKOUT_RESOURCE, null);
116                         return Either.right(errorResponse);
117                 }
118                 return Either.left(eitherCreator.left().value());
119
120         }
121
122         protected UserBusinessLogic getUserAdminManager(ServletContext context) {
123                 return getClassFromWebAppContext(context, () -> UserBusinessLogic.class);
124         }
125
126         protected ResourceBusinessLogic getResourceBL(ServletContext context) {
127                 return getClassFromWebAppContext(context, () -> ResourceBusinessLogic.class);
128         }
129
130         protected ComponentsCleanBusinessLogic getComponentCleanerBL(ServletContext context) {
131                 return getClassFromWebAppContext(context, () -> ComponentsCleanBusinessLogic.class);
132         }
133
134         protected ServiceBusinessLogic getServiceBL(ServletContext context) {
135                 return getClassFromWebAppContext(context, () -> ServiceBusinessLogic.class);
136         }
137
138         protected ProductBusinessLogic getProductBL(ServletContext context) {
139                 return getClassFromWebAppContext(context, () -> ProductBusinessLogic.class);
140         }
141
142         protected ArtifactsBusinessLogic getArtifactBL(ServletContext context) {
143                 return getClassFromWebAppContext(context, () -> ArtifactsBusinessLogic.class);
144         }
145
146         protected ElementBusinessLogic getElementBL(ServletContext context) {
147                 return getClassFromWebAppContext(context, () -> ElementBusinessLogic.class);
148         }
149
150         protected MonitoringBusinessLogic getMonitoringBL(ServletContext context) {
151                 return getClassFromWebAppContext(context, () -> MonitoringBusinessLogic.class);
152         }
153
154         protected AssetMetadataConverter getAssetUtils(ServletContext context) {
155                 return getClassFromWebAppContext(context, () -> AssetMetadataConverter.class);
156         }
157         
158         protected LifecycleBusinessLogic getLifecycleBL(ServletContext context) {
159                 return getClassFromWebAppContext(context, () -> LifecycleBusinessLogic.class);
160         }
161         
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;
167         }
168
169         protected GroupBusinessLogic getGroupBL(ServletContext context) {
170
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;
175         }
176
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);
182                 }
183                 if (containerComponentType == ComponentTypeEnum.SERVICE) {
184                         return webApplicationContext.getBean(ServiceComponentInstanceBusinessLogic.class);
185                 }
186                 if (containerComponentType == ComponentTypeEnum.PRODUCT) {
187                         return webApplicationContext.getBean(ProductComponentInstanceBusinessLogic.class);
188                 }
189                 return null;
190         }
191
192         protected IElementDAO getElementDao(Class<? extends IElementDAO> clazz, ServletContext context) {
193                 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
194
195                 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
196
197                 return webApplicationContext.getBean(clazz);
198         }
199
200         protected ComponentsUtils getComponentsUtils() {
201                 ServletContext context = this.servletRequest.getSession().getServletContext();
202
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;
207         }
208
209         /**
210          * Used to support Unit Test.<br>
211          * Header Params are not supported in Unit Tests
212          * 
213          * @return
214          */
215         protected String initHeaderParam(String headerValue, HttpServletRequest request, String headerName) {
216                 String retValue;
217                 if (headerValue != null) {
218                         retValue = headerValue;
219                 } else {
220                         retValue = request.getHeader(headerName);
221                 }
222                 return retValue;
223         }
224
225         protected String getContentDispositionValue(String artifactFileName) {
226                 return new StringBuilder().append("attachment; filename=\"").append(artifactFileName).append("\"").toString();
227         }
228
229         protected ComponentBusinessLogic getComponentBL(ComponentTypeEnum componentTypeEnum, ServletContext context) {
230                 ComponentBusinessLogic businessLogic;
231                 switch (componentTypeEnum) {
232                 case RESOURCE: {
233                         businessLogic = getResourceBL(context);
234                         break;
235                 }
236                 case SERVICE: {
237                         businessLogic = getServiceBL(context);
238                         break;
239                 }
240                 case PRODUCT: {
241                         businessLogic = getProductBL(context);
242                         break;
243                 }
244                 case RESOURCE_INSTANCE: {
245                         businessLogic = getResourceBL(context);
246                         break;
247                 }
248                 default: {
249                         BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "getComponentBL");
250                         BeEcompErrorManager.getInstance().logBeSystemError("getComponentBL");
251                         throw new IllegalArgumentException("Illegal component type:" + componentTypeEnum.getValue());
252                 }
253                 }
254                 return businessLogic;
255         }
256 }