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