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 com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
33 import org.openecomp.sdc.be.model.DataTypeDefinition;
34 import org.openecomp.sdc.be.model.User;
35 import org.openecomp.sdc.common.api.Constants;
36 import org.openecomp.sdc.common.datastructure.Wrapper;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.openecomp.sdc.exception.ResponseFormat;
39 import org.springframework.web.context.WebApplicationContext;
41 import javax.inject.Singleton;
42 import javax.servlet.ServletContext;
43 import javax.servlet.http.HttpServletRequest;
45 import javax.ws.rs.core.Context;
46 import javax.ws.rs.core.MediaType;
47 import javax.ws.rs.core.Response;
50 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
52 @Api(value = "Types Fetch Servlet", description = "Types Fetch Servlet")
54 public class TypesFetchServlet extends AbstractValidationsServlet {
56 private static final Logger log = Logger.getLogger(TypesFetchServlet.class);
60 @Consumes(MediaType.APPLICATION_JSON)
61 @Produces(MediaType.APPLICATION_JSON)
62 @ApiOperation(value = "Get data types", httpMethod = "GET", notes = "Returns data types", response = Response.class)
63 @ApiResponses(value = { @ApiResponse(code = 200, message = "datatypes"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
64 @ApiResponse(code = 404, message = "Data types not found") })
65 public Response getAllDataTypesServlet(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
67 Wrapper<Response> responseWrapper = new Wrapper<>();
68 Wrapper<User> userWrapper = new Wrapper<>();
69 ServletContext context = request.getSession().getServletContext();
73 validateUserExist(responseWrapper, userWrapper, userId);
75 if (responseWrapper.isEmpty()) {
76 String url = request.getMethod() + " " + request.getRequestURI();
77 log.debug("Start handle request of {} | modifier id is {}", url, userId);
79 PropertyBusinessLogic businessLogic = getPropertyBL(context);
80 Either<Map<String, DataTypeDefinition>, ResponseFormat> allDataTypes = businessLogic.getAllDataTypes();
82 if (allDataTypes.isRight()) {
83 log.info("Failed to get all dara types. Reason - {}", allDataTypes.right().value());
84 Response errorResponse = buildErrorResponse(allDataTypes.right().value());
85 responseWrapper.setInnerElement(errorResponse);
89 Map<String, DataTypeDefinition> dataTypes = allDataTypes.left().value();
90 String dataTypeJson = gson.toJson(dataTypes);
91 Response okResponse = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), dataTypeJson);
92 responseWrapper.setInnerElement(okResponse);
97 return responseWrapper.getInnerElement();
98 } catch (Exception e) {
99 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Property");
100 log.debug("get all data types failed with exception", e);
101 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
102 return buildErrorResponse(responseFormat);
106 private PropertyBusinessLogic getPropertyBL(ServletContext context) {
107 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
108 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
109 return webApplicationContext.getBean(PropertyBusinessLogic.class);