summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-04-06 09:57:19 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-04-07 12:12:32 +0200
commita2be8c1b0c660f10b2e86ac046af3cfaea61214d (patch)
treea21057b61d9fb834c5c751e6f88021632d7fbcc3 /server
parentf6bfd1d2f0373da2d2929d9217721a81fc3afe5d (diff)
downloadsonarqube-a2be8c1b0c660f10b2e86ac046af3cfaea61214d.tar.gz
sonarqube-a2be8c1b0c660f10b2e86ac046af3cfaea61214d.zip
SONAR-6948 Ability for Java server extensions to call web services
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ws/DefaultLocalResponse.java146
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ws/LocalRequestAdapter.java64
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java31
-rw-r--r--server/sonar-web/npm.tar.gzbin0 -> 1534303 bytes
4 files changed, 234 insertions, 7 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/DefaultLocalResponse.java b/server/sonar-server/src/main/java/org/sonar/server/ws/DefaultLocalResponse.java
new file mode 100644
index 00000000000..1b3d3f3aa36
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/ws/DefaultLocalResponse.java
@@ -0,0 +1,146 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.ws;
+
+import com.google.common.base.Throwables;
+import com.google.common.collect.Maps;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+import java.nio.charset.StandardCharsets;
+import java.util.Collection;
+import java.util.Map;
+import javax.annotation.CheckForNull;
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.server.ws.LocalConnector;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.api.utils.text.XmlWriter;
+import org.sonarqube.ws.MediaTypes;
+
+public class DefaultLocalResponse implements Response, LocalConnector.LocalResponse {
+
+ private final InMemoryStream stream = new InMemoryStream();
+ private final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ private final Map<String, String> headers = Maps.newHashMap();
+
+ @Override
+ public int getStatus() {
+ return stream().status();
+ }
+
+ @Override
+ public String getMediaType() {
+ return stream().mediaType();
+ }
+
+ @Override
+ public byte[] getBytes() {
+ return output.toByteArray();
+ }
+
+ public class InMemoryStream implements Response.Stream {
+ private String mediaType;
+
+ private int status = 200;
+
+ @CheckForNull
+ public String mediaType() {
+ return mediaType;
+ }
+
+ public int status() {
+ return status;
+ }
+
+ @Override
+ public Response.Stream setMediaType(String s) {
+ this.mediaType = s;
+ return this;
+ }
+
+ @Override
+ public Response.Stream setStatus(int i) {
+ this.status = i;
+ return this;
+ }
+
+ @Override
+ public OutputStream output() {
+ return output;
+ }
+
+ }
+
+ @Override
+ public JsonWriter newJsonWriter() {
+ stream.setMediaType(MediaTypes.JSON);
+ return JsonWriter.of(new OutputStreamWriter(output, StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public XmlWriter newXmlWriter() {
+ stream.setMediaType(MediaTypes.XML);
+ return XmlWriter.of(new OutputStreamWriter(output, StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public InMemoryStream stream() {
+ return stream;
+ }
+
+ @Override
+ public Response noContent() {
+ stream().setStatus(HttpURLConnection.HTTP_NO_CONTENT);
+ IOUtils.closeQuietly(output);
+ return this;
+ }
+
+ public String outputAsString() {
+ return new String(output.toByteArray(), StandardCharsets.UTF_8);
+ }
+
+ @Override
+ public Response setHeader(String name, String value) {
+ headers.put(name, value);
+ return this;
+ }
+
+ @Override
+ public Collection<String> getHeaderNames() {
+ return headers.keySet();
+ }
+
+ @Override
+ public String getHeader(String name) {
+ return headers.get(name);
+ }
+
+ public byte[] getFlushedOutput() {
+ try {
+ output.flush();
+ return output.toByteArray();
+ } catch (IOException e) {
+ throw Throwables.propagate(e);
+ }
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/LocalRequestAdapter.java b/server/sonar-server/src/main/java/org/sonar/server/ws/LocalRequestAdapter.java
new file mode 100644
index 00000000000..412b97df73e
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/ws/LocalRequestAdapter.java
@@ -0,0 +1,64 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.ws;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import org.sonar.api.server.ws.LocalConnector;
+import org.sonar.api.server.ws.internal.ValidatingRequest;
+
+public class LocalRequestAdapter extends ValidatingRequest {
+
+ private final LocalConnector.LocalRequest localRequest;
+
+ public LocalRequestAdapter(LocalConnector.LocalRequest localRequest) {
+ this.localRequest = localRequest;
+ }
+
+ @Override
+ protected String readParam(String key) {
+ return localRequest.getParam(key);
+ }
+
+ @Override
+ protected InputStream readInputStreamParam(String key) {
+ String value = readParam(key);
+ if (value == null) {
+ return null;
+ }
+ return new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public boolean hasParam(String key) {
+ return localRequest.hasParam(key);
+ }
+
+ @Override
+ public String method() {
+ return localRequest.getMethod();
+ }
+
+ @Override
+ public String getMediaType() {
+ return localRequest.getMediaType();
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
index 024065fd422..f0a5dad038f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
@@ -22,9 +22,13 @@ package org.sonar.server.ws;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
+import org.apache.commons.lang.StringUtils;
import org.picocontainer.Startable;
import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ServerSide;
+import org.sonar.api.server.ws.LocalConnector;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.internal.ValidatingRequest;
import org.sonar.api.utils.log.Loggers;
@@ -43,7 +47,7 @@ import static org.sonar.server.ws.RequestVerifier.verifyRequest;
* @since 4.2
*/
@ServerSide
-public class WebServiceEngine implements Startable {
+public class WebServiceEngine implements LocalConnector, Startable {
private final WebService.Context context;
private final I18n i18n;
@@ -76,11 +80,22 @@ public class WebServiceEngine implements Startable {
return context.controllers();
}
- public void execute(ValidatingRequest request, ServletResponse response,
- String controllerPath, String actionKey) {
+ @Override
+ public LocalResponse call(LocalRequest request) {
+ String controller = StringUtils.substringBeforeLast(request.getPath(), "/");
+ String action = StringUtils.substringAfterLast(request.getPath(), "/");
+ DefaultLocalResponse localResponse = new DefaultLocalResponse();
+ execute(new LocalRequestAdapter(request), localResponse, controller, action);
+ return localResponse;
+ }
+
+ public void execute(Request request, Response response, String controllerPath, String actionKey) {
try {
WebService.Action action = getAction(controllerPath, actionKey);
- request.setAction(action);
+ if (request instanceof ValidatingRequest) {
+ ((ValidatingRequest) request).setAction(action);
+ ((ValidatingRequest) request).setLocalConnector(this);
+ }
verifyRequest(action, request);
action.handler().handle(request, response);
@@ -112,9 +127,11 @@ public class WebServiceEngine implements Startable {
return action;
}
- private void sendErrors(ServletResponse response, int status, Errors errors) {
- ServletResponse.ServletStream stream = response.stream();
- stream.reset();
+ private void sendErrors(Response response, int status, Errors errors) {
+ Response.Stream stream = response.stream();
+ if (stream instanceof ServletResponse.ServletStream) {
+ ((ServletResponse.ServletStream) stream).reset();
+ }
stream.setStatus(status);
stream.setMediaType(MediaTypes.JSON);
JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output(), StandardCharsets.UTF_8));
diff --git a/server/sonar-web/npm.tar.gz b/server/sonar-web/npm.tar.gz
new file mode 100644
index 00000000000..fd6d660a1e0
--- /dev/null
+++ b/server/sonar-web/npm.tar.gz
Binary files differ