aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-ws
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2019-08-12 11:16:43 +0200
committerSonarTech <sonartech@sonarsource.com>2019-08-14 20:21:14 +0200
commite9ae396829a95e449d2e02934fc0567e7f09f833 (patch)
tree35d6ef00a01d5914a29985b3708311db30b8535b /server/sonar-webserver-ws
parent3a3948b47bcb86fe0bb7c427397303f54bfaa0ed (diff)
downloadsonarqube-e9ae396829a95e449d2e02934fc0567e7f09f833.tar.gz
sonarqube-e9ae396829a95e449d2e02934fc0567e7f09f833.zip
move some WSUtils methods to NotFoundException and ServerException
Diffstat (limited to 'server/sonar-webserver-ws')
-rw-r--r--server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/BadRequestException.java13
-rw-r--r--server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/NotFoundException.java36
-rw-r--r--server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java2
-rw-r--r--server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WsUtils.java55
-rw-r--r--server/sonar-webserver-ws/src/test/java/org/sonar/server/ws/WsUtilsTest.java5
5 files changed, 52 insertions, 59 deletions
diff --git a/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/BadRequestException.java b/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/BadRequestException.java
index 7a2fdf7166d..3113d202d99 100644
--- a/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/BadRequestException.java
+++ b/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/BadRequestException.java
@@ -23,6 +23,7 @@ import com.google.common.base.MoreObjects;
import java.util.List;
import static com.google.common.base.Preconditions.checkArgument;
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.util.Arrays.asList;
@@ -38,6 +39,18 @@ public class BadRequestException extends ServerException {
this.errors = errors;
}
+ public static void checkRequest(boolean expression, String message, Object... messageArguments) {
+ if (!expression) {
+ throw create(format(message, messageArguments));
+ }
+ }
+
+ public static void checkRequest(boolean expression, List<String> messages) {
+ if (!expression) {
+ throw create(messages);
+ }
+ }
+
public static BadRequestException create(List<String> errorMessages) {
checkArgument(!errorMessages.isEmpty(), "At least one error message is required");
checkArgument(errorMessages.stream().noneMatch(message -> message == null || message.isEmpty()), "Message cannot be empty");
diff --git a/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/NotFoundException.java b/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/NotFoundException.java
index ff5fb2f13c4..f21a98b5157 100644
--- a/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/NotFoundException.java
+++ b/server/sonar-webserver-ws/src/main/java/org/sonar/server/exceptions/NotFoundException.java
@@ -19,6 +19,10 @@
*/
package org.sonar.server.exceptions;
+import com.google.common.base.Optional;
+import javax.annotation.Nullable;
+
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
public class NotFoundException extends ServerException {
@@ -26,4 +30,36 @@ public class NotFoundException extends ServerException {
public NotFoundException(String message) {
super(HTTP_NOT_FOUND, message);
}
+
+ /**
+ * @throws NotFoundException if the value if null
+ * @return the value
+ */
+ public static <T> T checkFound(@Nullable T value, String message, Object... messageArguments) {
+ if (value == null) {
+ throw new NotFoundException(format(message, messageArguments));
+ }
+
+ return value;
+ }
+
+ /**
+ * @throws NotFoundException if the value is not present
+ * @return the value
+ */
+ public static <T> T checkFoundWithOptional(Optional<T> value, String message, Object... messageArguments) {
+ if (!value.isPresent()) {
+ throw new NotFoundException(format(message, messageArguments));
+ }
+
+ return value.get();
+ }
+
+ public static <T> T checkFoundWithOptional(java.util.Optional<T> value, String message, Object... messageArguments) {
+ if (!value.isPresent()) {
+ throw new NotFoundException(format(message, messageArguments));
+ }
+
+ return value.get();
+ }
}
diff --git a/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java b/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java
index cbbd7a0389b..4ce8e051e00 100644
--- a/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java
+++ b/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WebServiceEngine.java
@@ -50,7 +50,7 @@ import static org.apache.commons.lang.StringUtils.substringAfterLast;
import static org.apache.commons.lang.StringUtils.substringBeforeLast;
import static org.sonar.server.ws.RequestVerifier.verifyRequest;
import static org.sonar.server.ws.ServletRequest.SUPPORTED_MEDIA_TYPES_BY_URL_SUFFIX;
-import static org.sonar.server.ws.WsUtils.checkFound;
+import static org.sonar.server.exceptions.NotFoundException.checkFound;
/**
* @since 4.2
diff --git a/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WsUtils.java b/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WsUtils.java
index 2f936764f7d..82ea6a6949d 100644
--- a/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WsUtils.java
+++ b/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WsUtils.java
@@ -19,22 +19,14 @@
*/
package org.sonar.server.ws;
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Message;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import java.util.List;
-import java.util.Set;
-import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
-import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.core.util.ProtobufJsonFormat;
-import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.NotFoundException;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -66,53 +58,6 @@ public class WsUtils {
}
}
- /**
- * @throws BadRequestException
- */
- public static void checkRequest(boolean expression, String message, Object... messageArguments) {
- if (!expression) {
- throw BadRequestException.create(format(message, messageArguments));
- }
- }
-
- public static void checkRequest(boolean expression, List<String> messages) {
- if (!expression) {
- throw BadRequestException.create(messages);
- }
- }
-
- /**
- * @throws NotFoundException if the value if null
- * @return the value
- */
- public static <T> T checkFound(@Nullable T value, String message, Object... messageArguments) {
- if (value == null) {
- throw new NotFoundException(format(message, messageArguments));
- }
-
- return value;
- }
-
- /**
- * @throws NotFoundException if the value is not present
- * @return the value
- */
- public static <T> T checkFoundWithOptional(Optional<T> value, String message, Object... messageArguments) {
- if (!value.isPresent()) {
- throw new NotFoundException(format(message, messageArguments));
- }
-
- return value.get();
- }
-
- public static <T> T checkFoundWithOptional(java.util.Optional<T> value, String message, Object... messageArguments) {
- if (!value.isPresent()) {
- throw new NotFoundException(format(message, messageArguments));
- }
-
- return value.get();
- }
-
public static <T> T checkStateWithOptional(java.util.Optional<T> value, String message, Object... messageArguments) {
if (!value.isPresent()) {
throw new IllegalStateException(format(message, messageArguments));
diff --git a/server/sonar-webserver-ws/src/test/java/org/sonar/server/ws/WsUtilsTest.java b/server/sonar-webserver-ws/src/test/java/org/sonar/server/ws/WsUtilsTest.java
index 5bbd1a9a891..3a48ff54dca 100644
--- a/server/sonar-webserver-ws/src/test/java/org/sonar/server/ws/WsUtilsTest.java
+++ b/server/sonar-webserver-ws/src/test/java/org/sonar/server/ws/WsUtilsTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.server.ws;
-import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -84,7 +83,7 @@ public class WsUtilsTest {
@Test
public void checkRequest_ok() {
- WsUtils.checkRequest(true, "Missing param: %s", "foo");
+ BadRequestException.checkRequest(true, "Missing param: %s", "foo");
// do not fail
}
@@ -93,7 +92,7 @@ public class WsUtilsTest {
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("Missing param: foo");
- WsUtils.checkRequest(false, "Missing param: %s", "foo");
+ BadRequestException.checkRequest(false, "Missing param: %s", "foo");
}
}