View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.userdirectory;
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.doc.rest.RestParameter.Type.STRING;
27  
28  import org.opencastproject.security.api.JaxbUser;
29  import org.opencastproject.security.api.JaxbUserList;
30  import org.opencastproject.security.api.User;
31  import org.opencastproject.security.api.UserDirectoryService;
32  import org.opencastproject.util.NotFoundException;
33  import org.opencastproject.util.doc.rest.RestParameter;
34  import org.opencastproject.util.doc.rest.RestQuery;
35  import org.opencastproject.util.doc.rest.RestResponse;
36  import org.opencastproject.util.doc.rest.RestService;
37  
38  import org.apache.commons.lang3.StringUtils;
39  import org.osgi.service.component.annotations.Component;
40  import org.osgi.service.component.annotations.Reference;
41  import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource;
42  
43  import java.io.IOException;
44  import java.util.Iterator;
45  
46  import javax.ws.rs.GET;
47  import javax.ws.rs.Path;
48  import javax.ws.rs.PathParam;
49  import javax.ws.rs.Produces;
50  import javax.ws.rs.QueryParam;
51  import javax.ws.rs.core.MediaType;
52  import javax.ws.rs.core.Response;
53  import javax.ws.rs.core.Response.Status;
54  
55  /**
56   * Provides a sorted set of known users
57   */
58  @Path("/users")
59  @RestService(
60      name = "users",
61      title = "User account manager",
62      notes = "This service offers the ability to manage the roles for internal accounts.",
63      abstractText = "Displays the users available in the current user's organization"
64  )
65  @Component(
66      property = {
67          "service.description=User listing REST endpoint",
68          "opencast.service.type=org.opencastproject.userdirectory.users",
69          "opencast.service.path=/users",
70          "opencast.service.jobproducer=false"
71      },
72      immediate = true,
73      service = { UserEndpoint.class }
74  )
75  @JaxrsResource
76  public class UserEndpoint {
77  
78    /** The role directory service */
79    protected UserDirectoryService userDirectoryService = null;
80  
81    /**
82     * Sets the user directory service
83     *
84     * @param userDirectoryService
85     *          the userDirectoryService to set
86     */
87    @Reference
88    public void setUserDirectoryService(UserDirectoryService userDirectoryService) {
89      this.userDirectoryService = userDirectoryService;
90    }
91  
92    @GET
93    @Path("users.xml")
94    @Produces(MediaType.APPLICATION_XML)
95    @RestQuery(
96        name = "allusersasxml",
97        description = "Returns a list of users",
98        returnDescription = "Returns a XML representation of the list of user accounts",
99        restParameters = {
100           @RestParameter(
101               name = "query",
102               description = "The search query, must be at lest 3 characters long.",
103               isRequired = false,
104               type = RestParameter.Type.STRING
105           ),
106           @RestParameter(
107               name = "limit",
108               defaultValue = "100",
109               description = "The maximum number of items to return per page.",
110               isRequired = false,
111               type = RestParameter.Type.INTEGER
112           ),
113           @RestParameter(
114               name = "offset",
115               defaultValue = "0",
116               description = "The page number.",
117               isRequired = false,
118               type = RestParameter.Type.INTEGER
119           ),
120       },
121       responses = { @RestResponse(responseCode = SC_OK, description = "The user accounts.") }
122   )
123   public Response getUsersAsXml(
124       @QueryParam("query") String queryString,
125       @QueryParam("limit") int limit,
126       @QueryParam("offset") int offset
127   ) throws IOException {
128     if (limit < 1) {
129       limit = 100;
130     }
131 
132     String query = "%";
133     if (StringUtils.isNotBlank(queryString)) {
134       if (queryString.trim().length() < 3) {
135         return Response.status(Status.BAD_REQUEST).build();
136       }
137       query = queryString;
138     }
139 
140     JaxbUserList userList = new JaxbUserList();
141     for (Iterator<User> i = userDirectoryService.findUsers(query, offset, limit); i.hasNext();) {
142       userList.add(i.next());
143     }
144     return Response.ok(userList).build();
145   }
146 
147   @GET
148   @Path("users.json")
149   @Produces(MediaType.APPLICATION_JSON)
150   @RestQuery(
151       name = "allusersasjson",
152       description = "Returns a list of users",
153       returnDescription = "Returns a JSON representation of the list of user accounts",
154       restParameters = {
155           @RestParameter(
156               name = "query",
157               description = "The search query, must be at lest 3 characters long.",
158               isRequired = false,
159               type = RestParameter.Type.STRING
160           ),
161           @RestParameter(
162               name = "limit",
163               defaultValue = "100",
164               description = "The maximum number of items to return per page.",
165               isRequired = false,
166               type = RestParameter.Type.INTEGER
167           ),
168           @RestParameter(
169               name = "offset",
170               defaultValue = "0",
171               description = "The page number.",
172               isRequired = false,
173               type = RestParameter.Type.INTEGER
174           ),
175       },
176       responses = { @RestResponse(responseCode = SC_OK, description = "The user accounts.") }
177   )
178   public Response getUsersAsJson(
179       @QueryParam("query") String queryString,
180       @QueryParam("limit") int limit,
181       @QueryParam("offset") int offset
182   ) throws IOException {
183     return getUsersAsXml(queryString, limit, offset);
184   }
185 
186   @GET
187   @Path("{username}.xml")
188   @Produces(MediaType.APPLICATION_XML)
189   @RestQuery(
190       name = "user",
191       description = "Returns a user",
192       returnDescription = "Returns a XML representation of a user",
193       pathParameters = {
194           @RestParameter(description = "The username.", isRequired = true, name = "username", type = STRING),
195       },
196       responses = {
197           @RestResponse(responseCode = SC_OK, description = "The user account."),
198           @RestResponse(responseCode = SC_NOT_FOUND, description = "User not found"),
199       }
200   )
201   public JaxbUser getUserAsXml(@PathParam("username") String username) throws NotFoundException {
202     User user = userDirectoryService.loadUser(username);
203     if (user == null) {
204       throw new NotFoundException();
205     }
206     return JaxbUser.fromUser(user);
207   }
208 
209   @GET
210   @Path("{username}.json")
211   @Produces(MediaType.APPLICATION_JSON)
212   @RestQuery(
213       name = "user",
214       description = "Returns a user",
215       returnDescription = "Returns a JSON representation of a user",
216       pathParameters = {
217           @RestParameter(description = "The username.", isRequired = true, name = "username", type = STRING),
218       },
219       responses = {
220           @RestResponse(responseCode = SC_OK, description = "The user account."),
221           @RestResponse(responseCode = SC_NOT_FOUND, description = "User not found"),
222       }
223   )
224   public JaxbUser getUserAsJson(@PathParam("username") String username) throws NotFoundException {
225     return getUserAsXml(username);
226   }
227 
228 }