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