01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License. For additional information regarding
15: * copyright in this work, please see the NOTICE file in the top level
16: * directory of this distribution.
17: */
18: package org.apache.roller.ui.authoring.ajax;
19:
20: import java.io.IOException;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: import javax.servlet.ServletException;
25: import javax.servlet.http.HttpServlet;
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28:
29: import org.apache.roller.RollerException;
30: import org.apache.roller.business.Roller;
31: import org.apache.roller.business.RollerFactory;
32: import org.apache.roller.business.UserManager;
33: import org.apache.roller.pojos.UserData;
34:
35: /**
36: * Return list of users matching a startsWith strings. <br />
37: * Accepts request params (none required):<br />
38: * startsWith: string to be matched against username and email address<br />
39: * enabled: true include only enabled users (default: no restriction<br />
40: * offset: offset into results (for paging)<br />
41: * length: number of users to return (max is 50)<br /><br />
42: * List format:<br />
43: * username0, emailaddress0 <br/>
44: * username1, emailaddress1 <br/>
45: * username2, emailaddress2 <br/>
46: * usernameN, emailaddressN <br/>
47: *
48: * @web.servlet name="UserDataServlet"
49: * @web.servlet-mapping url-pattern="/roller-ui/authoring/userdata/*"
50: * @author David M Johnson
51: */
52: public class UserDataServlet extends HttpServlet {
53: private final int MAX_LENGTH = 50;
54:
55: public void doGet(HttpServletRequest request,
56: HttpServletResponse response) throws ServletException,
57: IOException {
58:
59: String startsWith = request.getParameter("startsWith");
60: Boolean enabledOnly = null;
61: int offset = 0;
62: int length = MAX_LENGTH;
63: if ("true".equals(request.getParameter("enabled")))
64: enabledOnly = Boolean.TRUE;
65: if ("false".equals(request.getParameter("enabled")))
66: enabledOnly = Boolean.FALSE;
67: try {
68: offset = Integer.parseInt(request.getParameter("offset"));
69: } catch (Throwable ignored) {
70: }
71: try {
72: length = Integer.parseInt(request.getParameter("length"));
73: } catch (Throwable ignored) {
74: }
75:
76: Roller roller = RollerFactory.getRoller();
77: try {
78: UserManager umgr = roller.getUserManager();
79: List users = umgr.getUsersStartingWith(startsWith,
80: enabledOnly, offset, length);
81: Iterator userIter = users.iterator();
82: while (userIter.hasNext()) {
83: UserData user = (UserData) userIter.next();
84: response.getWriter().print(user.getUserName());
85: response.getWriter().print(",");
86: response.getWriter().println(user.getEmailAddress());
87: }
88: response.flushBuffer();
89: } catch (RollerException e) {
90: throw new ServletException(e.getMessage());
91: }
92: }
93: }
|