001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.wsclient.user.internal;
021
022 import org.json.simple.JSONValue;
023 import org.sonar.wsclient.internal.HttpRequestFactory;
024 import org.sonar.wsclient.user.User;
025 import org.sonar.wsclient.user.UserClient;
026 import org.sonar.wsclient.user.UserParameters;
027 import org.sonar.wsclient.user.UserQuery;
028
029 import java.util.ArrayList;
030 import java.util.HashMap;
031 import java.util.List;
032 import java.util.Map;
033
034 /**
035 * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#userClient()}.
036 */
037 public class DefaultUserClient implements UserClient {
038
039 private static final String BASE_URL = "/api/users/";
040 private static final String SEARCH_URL = BASE_URL + "search";
041 private static final String CREATE_URL = BASE_URL + "create";
042 private static final String UPDATE_URL = BASE_URL + "update";
043 private static final String DEACTIVATE_URL = BASE_URL + "deactivate";
044
045 private final HttpRequestFactory requestFactory;
046
047 /**
048 * For internal use. Use {@link org.sonar.wsclient.SonarClient} to get an instance.
049 */
050 public DefaultUserClient(HttpRequestFactory requestFactory) {
051 this.requestFactory = requestFactory;
052 }
053
054 @Override
055 public List<User> find(UserQuery query) {
056 String json = requestFactory.get(SEARCH_URL, query.urlParams());
057 List<User> result = new ArrayList<User>();
058 Map jsonRoot = (Map) JSONValue.parse(json);
059 List<Map> jsonUsers = (List<Map>) jsonRoot.get("users");
060 if (jsonUsers != null) {
061 for (Map jsonUser : jsonUsers) {
062 result.add(new User(jsonUser));
063 }
064 }
065 return result;
066 }
067
068 @Override
069 public User create(UserParameters userParameters) {
070 String json = requestFactory.post(CREATE_URL, userParameters.urlParams());
071 Map jsonRoot = (Map) JSONValue.parse(json);
072 Map jsonUser = (Map) jsonRoot.get("user");
073 return new User(jsonUser);
074 }
075
076 @Override
077 public User update(UserParameters userParameters) {
078 String json = requestFactory.post(UPDATE_URL, userParameters.urlParams());
079 Map jsonRoot = (Map) JSONValue.parse(json);
080 Map jsonUser = (Map) jsonRoot.get("user");
081 return new User(jsonUser);
082 }
083
084 @Override
085 public void deactivate(String login) {
086 Map<String, Object> params = new HashMap<String, Object>();
087 params.put("login", login);
088 requestFactory.post(DEACTIVATE_URL, params);
089 }
090 }