1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.adminui.endpoint;
23
24 import static org.apache.http.HttpStatus.SC_NOT_FOUND;
25 import static org.apache.http.HttpStatus.SC_OK;
26 import static org.opencastproject.util.RestUtil.getEndpointUrl;
27 import static org.opencastproject.util.doc.rest.RestParameter.Type.INTEGER;
28 import static org.opencastproject.util.doc.rest.RestParameter.Type.STRING;
29
30 import org.opencastproject.adminui.usersettings.UserSetting;
31 import org.opencastproject.adminui.usersettings.UserSettings;
32 import org.opencastproject.adminui.usersettings.UserSettingsService;
33 import org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException;
34 import org.opencastproject.util.NotFoundException;
35 import org.opencastproject.util.UrlSupport;
36 import org.opencastproject.util.data.Tuple;
37 import org.opencastproject.util.doc.rest.RestParameter;
38 import org.opencastproject.util.doc.rest.RestQuery;
39 import org.opencastproject.util.doc.rest.RestResponse;
40 import org.opencastproject.util.doc.rest.RestService;
41
42 import org.osgi.service.component.ComponentContext;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
46 import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import java.io.IOException;
51
52 import javax.servlet.http.HttpServletResponse;
53 import javax.ws.rs.DELETE;
54 import javax.ws.rs.FormParam;
55 import javax.ws.rs.GET;
56 import javax.ws.rs.POST;
57 import javax.ws.rs.PUT;
58 import javax.ws.rs.Path;
59 import javax.ws.rs.PathParam;
60 import javax.ws.rs.Produces;
61 import javax.ws.rs.QueryParam;
62 import javax.ws.rs.core.MediaType;
63 import javax.ws.rs.core.Response;
64
65 @Path("/admin-ng/user-settings")
66 @RestService(name = "usersettings", title = "User Settings service",
67 abstractText = "Provides operations for user settings",
68 notes = { "This service offers the default CRUD Operations for user settings for the admin UI.",
69 "<strong>Important:</strong> "
70 + "<em>This service is for exclusive use by the module admin-ui. Its API might change "
71 + "anytime without prior notice. Any dependencies other than the admin UI will be strictly ignored. "
72 + "DO NOT use this for integration of third-party applications.<em>"})
73 @Component(
74 immediate = true,
75 service = UserSettingsEndpoint.class,
76 property = {
77 "service.description=Admin UI - Users Settings facade Endpoint",
78 "opencast.service.type=org.opencastproject.adminui.endpoint.UserSettingsEndpoint",
79 "opencast.service.path=/admin-ng/user-settings"
80 }
81 )
82 @JaxrsResource
83 public class UserSettingsEndpoint {
84
85
86 private static final Logger logger = LoggerFactory.getLogger(ServerEndpoint.class);
87
88
89 private String endpointBaseUrl;
90
91 private UserSettingsService userSettingsService;
92
93
94
95
96 @Reference
97 public void setUserSettingsService(UserSettingsService userSettingsService) {
98 this.userSettingsService = userSettingsService;
99 }
100
101
102 @Activate
103 protected void activate(ComponentContext cc) {
104 logger.info("Activate the Admin ui - Users facade endpoint");
105 final Tuple<String, String> endpointUrl = getEndpointUrl(cc);
106 endpointBaseUrl = UrlSupport.concat(endpointUrl.getA(), endpointUrl.getB());
107 }
108
109 @GET
110 @Path("/settings.json")
111 @Produces(MediaType.APPLICATION_JSON)
112 @RestQuery(name = "getUserSettings", description = "Returns a list of the user settings for the current user", returnDescription = "Returns a JSON representation of the list of user settings", restParameters = {
113 @RestParameter(defaultValue = "100", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.STRING),
114 @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "The user settings.") })
115 public Response getUserSettings(@QueryParam("limit") int limit, @QueryParam("offset") int offset) throws IOException {
116 if (limit < 1) {
117 limit = 100;
118 }
119
120 UserSettings userSettings;
121 try {
122 userSettings = userSettingsService.findUserSettings(limit, offset);
123 } catch (UserSettingsServiceException e) {
124 logger.error("Unable to get user settings:", e);
125 return (Response.serverError().build());
126 }
127
128 return Response.ok(userSettings.toJson().toJson()).build();
129 }
130
131 @POST
132 @Path("/setting")
133 @Produces(MediaType.APPLICATION_JSON)
134 @RestQuery(name = "createUserSetting", description = "Create a new user setting", returnDescription = "Status ok", restParameters = {
135 @RestParameter(description = "The key used to represent this setting.", isRequired = true, name = "key", type = STRING),
136 @RestParameter(description = "The value representing this setting.", isRequired = true, name = "value", type = STRING) }, responses = { @RestResponse(responseCode = HttpServletResponse.SC_OK, description = "User setting has been created.") })
137 public Response createUserSetting(@FormParam("key") String key, @FormParam("value") String value)
138 throws NotFoundException {
139 try {
140 UserSetting newUserSetting = userSettingsService.addUserSetting(key, value);
141 return Response.ok(newUserSetting.toJson().toJson(), MediaType.APPLICATION_JSON).build();
142 } catch (UserSettingsServiceException e) {
143 return Response.serverError().build();
144 }
145 }
146
147 @PUT
148 @Path("/setting/{settingId}")
149 @Produces(MediaType.APPLICATION_JSON)
150 @RestQuery(name = "updateUserSetting", description = "Update a user setting", returnDescription = "The updated user setting as JSON", pathParameters = { @RestParameter(name = "settingId", description = "The setting's id", isRequired = true, type = RestParameter.Type.INTEGER) }, restParameters = {
151 @RestParameter(description = "The key used to represent this setting.", isRequired = true, name = "key", type = STRING),
152 @RestParameter(description = "The value representing this setting.", isRequired = true, name = "value", type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "User setting has been created.") })
153 public Response updateUserSetting(@PathParam("settingId") final int id, @FormParam("key") String key,
154 @FormParam("value") String value) throws NotFoundException {
155 try {
156 UserSetting updatedUserSetting = userSettingsService.updateUserSetting(id, key, value);
157 return Response.ok(updatedUserSetting.toJson().toJson(), MediaType.APPLICATION_JSON).build();
158 } catch (UserSettingsServiceException e) {
159 logger.error("Unable to update user setting", e);
160 return Response.serverError().build();
161 }
162 }
163
164 @DELETE
165 @Path("/setting/{settingId}")
166 @RestQuery(name = "deleteUserSetting", description = "Delete a user setting", returnDescription = "Status ok", pathParameters = @RestParameter(name = "settingId", type = INTEGER, isRequired = true, description = "The id of the user setting."), responses = {
167 @RestResponse(responseCode = SC_OK, description = "User setting has been deleted."),
168 @RestResponse(responseCode = SC_NOT_FOUND, description = "User setting not found.") })
169 public Response deleteUserSetting(@PathParam("settingId") long id) throws NotFoundException {
170 try {
171 userSettingsService.deleteUserSetting(id);
172 } catch (UserSettingsServiceException e) {
173 logger.error("Unable to remove user setting id: '{}'", id, e);
174 return Response.serverError().build();
175 }
176 logger.debug("User setting with id {} removed.", id);
177 return Response.status(SC_OK).build();
178 }
179 }