7ed2170c84cbf0a8ca150eb5f4634bc3c14d9b3d
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2017-2018 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the Apache License, Version 2.0
5  * and the Eclipse Public License v1.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 package org.onap.sdc.workflowdesigner.resources;
14
15 import java.io.IOException;
16
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.QueryParam;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24
25 import org.eclipse.jetty.http.HttpStatus;
26 import org.onap.sdc.workflowdesigner.resources.entity.ExtendActivity;
27 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
28 import org.onap.sdc.workflowdesigner.utils.RestUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.codahale.metrics.annotation.Timed;
33 import com.google.gson.Gson;
34
35 import io.swagger.annotations.Api;
36 import io.swagger.annotations.ApiOperation;
37 import io.swagger.annotations.ApiParam;
38 import io.swagger.annotations.ApiResponse;
39 import io.swagger.annotations.ApiResponses;
40
41 /**
42  * Extend Activity Resource.
43  * 
44  */
45 @Path("/ext-activities")
46 @Api(tags = {"Workflow Modeler"})
47 public class ExtendActivityResource {
48   /** */
49   private static final String EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME = "ext-activities-display-info.json";
50
51   private static final Logger LOGGER = LoggerFactory.getLogger(ExtendActivityResource.class);
52
53   private static final String EXT_ACTIVITIES_FILE_NAME = "..\\distribution\\src\\main\\assembly\\ext-activities.json";
54
55   /**
56    * test function.
57    * 
58    * @return Response
59    */
60   @Path("/")
61   @GET
62   @Consumes(MediaType.APPLICATION_JSON)
63   @Produces(MediaType.APPLICATION_JSON)
64   @ApiOperation(value = "Get Extend Activities.", response = ExtendActivity.class, responseContainer = "List")
65   @ApiResponses(value = {
66       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
67           response = String.class),
68       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
69           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
70       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
71           response = String.class)})
72   @Timed
73   public Response getExtActivities(@ApiParam(value = "sence") @QueryParam("sence") String sence) {
74
75     try {
76       ExtendActivity[] extActivities = retriveExtActivites(sence);
77       
78       return Response.status(Response.Status.OK).entity(extActivities).build();
79     } catch (IOException e) {
80       LOGGER.error("Get ExtActivities failed.", e);
81       throw RestUtils.newInternalServerErrorException(e);
82     }
83
84   }
85
86   /**
87    * @param sence 
88    * @return
89    * @throws IOException
90    */
91   private ExtendActivity[] retriveExtActivites(String sence) throws IOException {
92     String json = FileCommonUtils.readString(EXT_ACTIVITIES_FILE_NAME);
93     Gson gson = new Gson();
94     return gson.fromJson(json, ExtendActivity[].class);
95   }
96   
97
98   @Path("/displayInfo")
99   @GET
100   @Consumes(MediaType.APPLICATION_JSON)
101   @Produces(MediaType.APPLICATION_JSON)
102   @ApiOperation(value = "Get Extend Activities DisplayInfo", response = String.class)
103   @ApiResponses(value = {
104       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
105           response = String.class),
106       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
107           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
108       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
109           response = String.class)})
110   @Timed
111   public Response getDisplayInfo(@ApiParam(value = "sence") @QueryParam("sence") String sence) {
112     try {
113       String json = FileCommonUtils.readString(EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME);
114       return Response.status(Response.Status.OK).entity(json).build();
115     } catch (IOException e) {
116       LOGGER.error("Get Extend Activities DisplayInfo failed.", e);
117       throw RestUtils.newInternalServerErrorException(e);
118     }
119   }
120
121 }