Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-6521 Add internal WS to list user details for batch
- Loading branch information
1 parent
cf4b511
commit cad73b7
Showing
12 changed files
with
292 additions
and
30 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
server/sonar-server/src/main/java/org/sonar/server/batch/BatchAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* SonarQube, open source software quality management tool. | ||
* Copyright (C) 2008-2014 SonarSource | ||
* mailto:contact AT sonarsource DOT com | ||
* | ||
* SonarQube is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* SonarQube is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.server.batch; | ||
|
||
import org.sonar.server.ws.WsAction; | ||
|
||
public interface BatchAction extends WsAction { | ||
|
||
// Marker interface | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
server/sonar-server/src/main/java/org/sonar/server/batch/UsersAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* SonarQube, open source software quality management tool. | ||
* Copyright (C) 2008-2014 SonarSource | ||
* mailto:contact AT sonarsource DOT com | ||
* | ||
* SonarQube is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* SonarQube is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
package org.sonar.server.batch; | ||
|
||
import org.sonar.api.server.ws.Request; | ||
import org.sonar.api.server.ws.Response; | ||
import org.sonar.api.server.ws.WebService; | ||
import org.sonar.batch.protocol.input.BatchInput; | ||
import org.sonar.core.permission.GlobalPermissions; | ||
import org.sonar.server.plugins.MimeTypes; | ||
import org.sonar.server.user.UserSession; | ||
import org.sonar.server.user.index.UserDoc; | ||
import org.sonar.server.user.index.UserIndex; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class UsersAction implements BatchAction { | ||
|
||
private static final String PARAM_LOGINS = "logins"; | ||
|
||
private final UserIndex userIndex; | ||
|
||
public UsersAction(UserIndex userIndex) { | ||
this.userIndex = userIndex; | ||
} | ||
|
||
@Override | ||
public void define(WebService.NewController controller) { | ||
WebService.NewAction action = controller.createAction("users") | ||
.setDescription("Return user details.") | ||
.setSince("5.2") | ||
.setInternal(true) | ||
.setHandler(this); | ||
|
||
action | ||
.createParam(PARAM_LOGINS) | ||
.setRequired(true) | ||
.setDescription("A comma separated list of user logins") | ||
.setExampleValue("ada.lovelace,grace.hopper"); | ||
} | ||
|
||
@Override | ||
public void handle(Request request, Response response) throws Exception { | ||
UserSession.get().checkGlobalPermission(GlobalPermissions.PREVIEW_EXECUTION); | ||
List<String> logins = request.mandatoryParamAsStrings(PARAM_LOGINS); | ||
|
||
response.stream().setMediaType(MimeTypes.PROTOBUF); | ||
BatchInput.User.Builder userBuilder = BatchInput.User.newBuilder(); | ||
OutputStream output = response.stream().output(); | ||
try { | ||
for (Iterator<UserDoc> userDocIterator = userIndex.selectUsersForBatch(logins); userDocIterator.hasNext();) { | ||
handleUser(userDocIterator.next(), userBuilder, output); | ||
} | ||
} finally { | ||
output.close(); | ||
} | ||
} | ||
|
||
private void handleUser(UserDoc user, BatchInput.User.Builder userBuilder, OutputStream out) { | ||
userBuilder.setLogin(user.login()) | ||
.setName(user.name()); | ||
try { | ||
userBuilder.build().writeDelimitedTo(out); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Unable to serialize user", e); | ||
} | ||
userBuilder.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.