1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.opencastproject.external.endpoint;
22
23 import org.opencastproject.external.common.ApiMediaType;
24 import org.opencastproject.external.common.ApiResponseBuilder;
25 import org.opencastproject.list.api.ListProviderException;
26 import org.opencastproject.list.api.ListProvidersService;
27 import org.opencastproject.list.impl.ListProviderNotFoundException;
28 import org.opencastproject.list.impl.ResourceListQueryImpl;
29 import org.opencastproject.list.query.StringListFilter;
30 import org.opencastproject.util.doc.rest.RestParameter;
31 import org.opencastproject.util.doc.rest.RestQuery;
32 import org.opencastproject.util.doc.rest.RestResponse;
33 import org.opencastproject.util.doc.rest.RestService;
34
35 import com.google.gson.Gson;
36
37 import org.json.simple.JSONArray;
38 import org.osgi.service.component.ComponentContext;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import java.util.Map;
47
48 import javax.servlet.http.HttpServletResponse;
49 import javax.ws.rs.GET;
50 import javax.ws.rs.HeaderParam;
51 import javax.ws.rs.Path;
52 import javax.ws.rs.PathParam;
53 import javax.ws.rs.Produces;
54 import javax.ws.rs.QueryParam;
55 import javax.ws.rs.core.Response;
56
57 @Path("/api/listproviders")
58 @Produces({ ApiMediaType.JSON, ApiMediaType.VERSION_1_10_0, ApiMediaType.VERSION_1_11_0 })
59 @RestService(
60 name = "externalapilistproviders",
61 title = "External API List Providers Service",
62 notes = {},
63 abstractText = "Provides resources and operations related to configurable lists"
64 )
65 @Component(
66 immediate = true,
67 service = ListProviderEndpoint.class,
68 property = {
69 "service.description=External API - List Providers Endpoint",
70 "opencast.service.type=org.opencastproject.external.listproviders",
71 "opencast.service.path=/api/listproviders"
72 }
73 )
74 @JaxrsResource
75 public class ListProviderEndpoint {
76
77
78 private static final Logger logger = LoggerFactory.getLogger(ListProviderEndpoint.class);
79
80
81 private ListProvidersService listProvidersService;
82
83
84 @Reference
85 public void setListProvidersService(ListProvidersService listProvidersService) {
86 this.listProvidersService = listProvidersService;
87 }
88
89
90 @Activate
91 void activate(ComponentContext cc) {
92 logger.info("Activating External API - List Providers Endpoint");
93 }
94
95 @GET
96 @Path("providers.json")
97 @Produces({ ApiMediaType.JSON, ApiMediaType.VERSION_1_10_0 })
98 @RestQuery(name = "availableProviders", description = "Provides the list of the available list providers", responses = { @RestResponse(description = "Returns the availables list providers.", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "")
99 public Response getAvailableProviders(@HeaderParam("Accept") String acceptHeader) {
100 JSONArray list = new JSONArray();
101
102 list.add(listProvidersService.getAvailableProviders());
103
104 return ApiResponseBuilder.Json.ok(acceptHeader, list.toJSONString());
105 }
106
107 @GET
108 @Path("{source}.json")
109 @Produces({ ApiMediaType.JSON, ApiMediaType.VERSION_1_10_0 })
110 @RestQuery(name = "list", description = "Provides key-value list from the given source", pathParameters = { @RestParameter(name = "source", description = "The source for the key-value list", isRequired = true, type = RestParameter.Type.STRING) }, restParameters = {
111 @RestParameter(description = "The maximum number of items to return per page", isRequired = false, name = "limit", type = RestParameter.Type.INTEGER),
112 @RestParameter(description = "The offset", isRequired = false, name = "offset", type = RestParameter.Type.INTEGER),
113 @RestParameter(description = "Filters", isRequired = false, name = "filter", type = RestParameter.Type.STRING) }, responses = { @RestResponse(description = "Returns the key-value list for the given source.", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "")
114 public Response getList(@PathParam("source") final String source, @QueryParam("limit") final int limit,
115 @QueryParam("filter") final String filter, @QueryParam("offset") final int offset,
116 @HeaderParam("Accept") String acceptHeader) {
117
118 ResourceListQueryImpl query = new ResourceListQueryImpl();
119 query.setLimit(limit);
120 query.setOffset(offset);
121 addRequestFiltersToQuery(filter, query);
122 Map<String, String> autocompleteList;
123 try {
124 autocompleteList = listProvidersService.getList(source, query, false);
125 } catch (ListProviderNotFoundException e) {
126 logger.debug("No list found for {}", source, e);
127 return ApiResponseBuilder.notFound("");
128 } catch (ListProviderException e) {
129 logger.error("Server error when getting list from provider {}", source, e);
130 return ApiResponseBuilder.serverError("");
131 }
132
133 Gson gson = new Gson();
134 String jsonList = gson.toJson(autocompleteList);
135 return ApiResponseBuilder.Json.ok(acceptHeader, jsonList);
136 }
137
138
139
140
141
142
143
144
145
146 private void addRequestFiltersToQuery(final String filterString, ResourceListQueryImpl query) {
147 if (filterString != null) {
148 String[] filters = filterString.split(",");
149 for (String filter : filters) {
150 String[] splitFilter = filter.split(":", 2);
151 if (splitFilter != null && splitFilter.length == 2) {
152 String key = splitFilter[0].trim();
153 String value = splitFilter[1].trim();
154 query.addFilter(new StringListFilter(key, value));
155 }
156 }
157 }
158 }
159
160 }
161