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.security.api.SecurityService;
35 import org.opencastproject.util.NotFoundException;
36 import org.opencastproject.util.UrlSupport;
37 import org.opencastproject.util.data.Tuple;
38 import org.opencastproject.util.doc.rest.RestParameter;
39 import org.opencastproject.util.doc.rest.RestQuery;
40 import org.opencastproject.util.doc.rest.RestResponse;
41 import org.opencastproject.util.doc.rest.RestService;
42
43 import org.osgi.service.component.ComponentContext;
44 import org.osgi.service.component.annotations.Activate;
45 import org.osgi.service.component.annotations.Component;
46 import org.osgi.service.component.annotations.Reference;
47 import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import java.io.IOException;
52
53 import javax.servlet.http.HttpServletResponse;
54 import javax.ws.rs.DELETE;
55 import javax.ws.rs.FormParam;
56 import javax.ws.rs.GET;
57 import javax.ws.rs.POST;
58 import javax.ws.rs.PUT;
59 import javax.ws.rs.Path;
60 import javax.ws.rs.PathParam;
61 import javax.ws.rs.Produces;
62 import javax.ws.rs.QueryParam;
63 import javax.ws.rs.core.MediaType;
64 import javax.ws.rs.core.Response;
65
66 @Path("/admin-ng/user-settings")
67 @RestService(
68 name = "usersettings",
69 title = "User Settings service",
70 abstractText = "Provides operations for user settings",
71 notes = { "This service offers the default CRUD Operations for user settings for the admin UI.",
72 "<strong>Important:</strong> "
73 + "<em>This service is for exclusive use by the module admin-ui. Its API might change "
74 + "anytime without prior notice. Any dependencies other than the admin UI will be strictly ignored. "
75 + "DO NOT use this for integration of third-party applications.<em>"})
76 @Component(
77 immediate = true,
78 service = UserSettingsEndpoint.class,
79 property = {
80 "service.description=Admin UI - Users Settings facade Endpoint",
81 "opencast.service.type=org.opencastproject.adminui.endpoint.UserSettingsEndpoint",
82 "opencast.service.path=/admin-ng/user-settings"
83 }
84 )
85 @JaxrsResource
86 public class UserSettingsEndpoint {
87
88
89 private static final Logger logger = LoggerFactory.getLogger(ServerEndpoint.class);
90
91
92 private String endpointBaseUrl;
93
94 private UserSettingsService userSettingsService;
95
96 private SecurityService securityService;
97
98
99
100
101 @Reference
102 public void setUserSettingsService(UserSettingsService userSettingsService) {
103 this.userSettingsService = userSettingsService;
104 }
105
106
107
108
109 @Reference
110 public void setSecurityService(SecurityService securityService) {
111 this.securityService = securityService;
112 }
113
114
115 @Activate
116 protected void activate(ComponentContext cc) {
117 logger.info("Activate the Admin ui - Users facade endpoint");
118 final Tuple<String, String> endpointUrl = getEndpointUrl(cc);
119 endpointBaseUrl = UrlSupport.concat(endpointUrl.getA(), endpointUrl.getB());
120 }
121
122 @GET
123 @Path("/settings.json")
124 @Produces(MediaType.APPLICATION_JSON)
125 @RestQuery(
126 name = "getUserSettings",
127 description = "Returns a list of the user settings for the current user",
128 returnDescription = "Returns a JSON representation of the list of user settings",
129 restParameters = {
130 @RestParameter(defaultValue = "100", description = "The maximum number of items to return per page.",
131 isRequired = false, name = "limit", type = RestParameter.Type.STRING),
132 @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset",
133 type = RestParameter.Type.STRING)
134 },
135 responses = {
136 @RestResponse(responseCode = SC_OK, description = "The user settings.")
137 })
138 public Response getUserSettings(@QueryParam("limit") int limit, @QueryParam("offset") int offset) throws IOException {
139 if (limit < 1) {
140 limit = 100;
141 }
142
143 UserSettings userSettings;
144 try {
145 userSettings = userSettingsService.findUserSettings(limit, offset);
146 } catch (UserSettingsServiceException e) {
147 logger.error("Unable to get user settings:", e);
148 return (Response.serverError().build());
149 }
150
151 return Response.ok(userSettings.toJson().toJson()).build();
152 }
153
154 @POST
155 @Path("/setting")
156 @Produces(MediaType.APPLICATION_JSON)
157 @RestQuery(
158 name = "createUserSetting",
159 description = "Create a new user setting",
160 returnDescription = "Status ok",
161 restParameters = {
162 @RestParameter(description = "The key used to represent this setting.", isRequired = true, name = "key",
163 type = STRING),
164 @RestParameter(description = "The value representing this setting.", isRequired = true, name = "value",
165 type = STRING)
166 },
167 responses = {
168 @RestResponse(responseCode = HttpServletResponse.SC_OK, description = "User setting has been created.")
169 })
170 public Response createUserSetting(@FormParam("key") String key, @FormParam("value") String value)
171 throws NotFoundException {
172 String orgId = securityService.getOrganization().getId();
173 String username = securityService.getUser().getUsername();
174 try {
175 UserSetting newUserSetting = userSettingsService.addUserSetting(key, value);
176 return Response.ok(newUserSetting.toJson().toJson(), MediaType.APPLICATION_JSON).build();
177 } catch (UserSettingsServiceException e) {
178 logger.error("Could not add user setting username '{}' org: '{}' key: '{}' value: '{}'", username, orgId,
179 key, value);
180 return Response.serverError().build();
181 }
182 }
183
184 @PUT
185 @Path("/setting/{settingId}")
186 @Produces(MediaType.APPLICATION_JSON)
187 @RestQuery(
188 name = "updateUserSetting",
189 description = "Update a user setting",
190 returnDescription = "The updated user setting as JSON",
191 pathParameters = {
192 @RestParameter(name = "settingId", description = "The setting's id", isRequired = true,
193 type = RestParameter.Type.INTEGER)
194 },
195 restParameters = {
196 @RestParameter(description = "The key used to represent this setting.", isRequired = true,
197 name = "key", type = STRING),
198 @RestParameter(description = "The value representing this setting.", isRequired = true,
199 name = "value", type = STRING)
200 },
201 responses = {
202 @RestResponse(responseCode = SC_OK, description = "User setting has been created.")
203 })
204 public Response updateUserSetting(@PathParam("settingId") final int id, @FormParam("key") String key,
205 @FormParam("value") String value) throws NotFoundException {
206 try {
207 UserSetting updatedUserSetting = userSettingsService.updateUserSetting(id, key, value);
208 return Response.ok(updatedUserSetting.toJson().toJson(), MediaType.APPLICATION_JSON).build();
209 } catch (UserSettingsServiceException e) {
210 logger.error("Unable to update user setting", e);
211 return Response.serverError().build();
212 }
213 }
214
215 @DELETE
216 @Path("/setting/{settingId}")
217 @RestQuery(
218 name = "deleteUserSetting",
219 description = "Delete a user setting",
220 returnDescription = "Status ok",
221 pathParameters = @RestParameter(name = "settingId", type = INTEGER, isRequired = true,
222 description = "The id of the user setting."),
223 responses = {
224 @RestResponse(responseCode = SC_OK, description = "User setting has been deleted."),
225 @RestResponse(responseCode = SC_NOT_FOUND, description = "User setting not found.")
226 })
227 public Response deleteUserSetting(@PathParam("settingId") long id) throws NotFoundException {
228 try {
229 userSettingsService.deleteUserSetting(id);
230 } catch (UserSettingsServiceException e) {
231 logger.error("Unable to remove user setting id: '{}'", id, e);
232 return Response.serverError().build();
233 }
234 logger.debug("User setting with id {} removed.", id);
235 return Response.status(SC_OK).build();
236 }
237 }