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(name = "usersettings", title = "User Settings service",
68 abstractText = "Provides operations for user settings",
69 notes = { "This service offers the default CRUD Operations for user settings for the admin UI.",
70 "<strong>Important:</strong> "
71 + "<em>This service is for exclusive use by the module admin-ui. Its API might change "
72 + "anytime without prior notice. Any dependencies other than the admin UI will be strictly ignored. "
73 + "DO NOT use this for integration of third-party applications.<em>"})
74 @Component(
75 immediate = true,
76 service = UserSettingsEndpoint.class,
77 property = {
78 "service.description=Admin UI - Users Settings facade Endpoint",
79 "opencast.service.type=org.opencastproject.adminui.endpoint.UserSettingsEndpoint",
80 "opencast.service.path=/admin-ng/user-settings"
81 }
82 )
83 @JaxrsResource
84 public class UserSettingsEndpoint {
85
86
87 private static final Logger logger = LoggerFactory.getLogger(ServerEndpoint.class);
88
89
90 private String endpointBaseUrl;
91
92 private UserSettingsService userSettingsService;
93
94 private SecurityService securityService;
95
96
97
98
99 @Reference
100 public void setUserSettingsService(UserSettingsService userSettingsService) {
101 this.userSettingsService = userSettingsService;
102 }
103
104
105
106
107 @Reference
108 public void setSecurityService(SecurityService securityService) {
109 this.securityService = securityService;
110 }
111
112
113 @Activate
114 protected void activate(ComponentContext cc) {
115 logger.info("Activate the Admin ui - Users facade endpoint");
116 final Tuple<String, String> endpointUrl = getEndpointUrl(cc);
117 endpointBaseUrl = UrlSupport.concat(endpointUrl.getA(), endpointUrl.getB());
118 }
119
120 @GET
121 @Path("/settings.json")
122 @Produces(MediaType.APPLICATION_JSON)
123 @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 = {
124 @RestParameter(defaultValue = "100", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.STRING),
125 @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "The user settings.") })
126 public Response getUserSettings(@QueryParam("limit") int limit, @QueryParam("offset") int offset) throws IOException {
127 if (limit < 1) {
128 limit = 100;
129 }
130
131 UserSettings userSettings;
132 try {
133 userSettings = userSettingsService.findUserSettings(limit, offset);
134 } catch (UserSettingsServiceException e) {
135 logger.error("Unable to get user settings:", e);
136 return (Response.serverError().build());
137 }
138
139 return Response.ok(userSettings.toJson().toJson()).build();
140 }
141
142 @POST
143 @Path("/setting")
144 @Produces(MediaType.APPLICATION_JSON)
145 @RestQuery(name = "createUserSetting", description = "Create a new user setting", returnDescription = "Status ok", restParameters = {
146 @RestParameter(description = "The key used to represent this setting.", isRequired = true, name = "key", type = STRING),
147 @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.") })
148 public Response createUserSetting(@FormParam("key") String key, @FormParam("value") String value)
149 throws NotFoundException {
150 String orgId = securityService.getOrganization().getId();
151 String username = securityService.getUser().getUsername();
152 try {
153 UserSetting newUserSetting = userSettingsService.addUserSetting(key, value);
154 return Response.ok(newUserSetting.toJson().toJson(), MediaType.APPLICATION_JSON).build();
155 } catch (UserSettingsServiceException e) {
156 logger.error("Could not add user setting username '{}' org: '{}' key: '{}' value: '{}'", username, orgId,
157 key, value);
158 return Response.serverError().build();
159 }
160 }
161
162 @PUT
163 @Path("/setting/{settingId}")
164 @Produces(MediaType.APPLICATION_JSON)
165 @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 = {
166 @RestParameter(description = "The key used to represent this setting.", isRequired = true, name = "key", type = STRING),
167 @RestParameter(description = "The value representing this setting.", isRequired = true, name = "value", type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "User setting has been created.") })
168 public Response updateUserSetting(@PathParam("settingId") final int id, @FormParam("key") String key,
169 @FormParam("value") String value) throws NotFoundException {
170 try {
171 UserSetting updatedUserSetting = userSettingsService.updateUserSetting(id, key, value);
172 return Response.ok(updatedUserSetting.toJson().toJson(), MediaType.APPLICATION_JSON).build();
173 } catch (UserSettingsServiceException e) {
174 logger.error("Unable to update user setting", e);
175 return Response.serverError().build();
176 }
177 }
178
179 @DELETE
180 @Path("/setting/{settingId}")
181 @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 = {
182 @RestResponse(responseCode = SC_OK, description = "User setting has been deleted."),
183 @RestResponse(responseCode = SC_NOT_FOUND, description = "User setting not found.") })
184 public Response deleteUserSetting(@PathParam("settingId") long id) throws NotFoundException {
185 try {
186 userSettingsService.deleteUserSetting(id);
187 } catch (UserSettingsServiceException e) {
188 logger.error("Unable to remove user setting id: '{}'", id, e);
189 return Response.serverError().build();
190 }
191 logger.debug("User setting with id {} removed.", id);
192 return Response.status(SC_OK).build();
193 }
194 }