]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8416 move handling logic out from UnauthorizedException
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 29 Nov 2016 13:28:43 +0000 (14:28 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 14 Dec 2016 16:09:10 +0000 (17:09 +0100)
and to AuthenticationError

server/sonar-server/src/main/java/org/sonar/server/authentication/AuthenticationError.java
sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/UnauthorizedException.java

index 5376261766cded19d00942be4405e0639ca13e07..c17c7028e3fa50ca2c053da7616d6246b40072e8 100644 (file)
  */
 package org.sonar.server.authentication;
 
+import com.google.common.base.Charsets;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import javax.servlet.http.HttpServletResponse;
 import org.sonar.api.server.authentication.UnauthorizedException;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 
 import static java.lang.String.format;
-import static org.sonar.api.server.authentication.UnauthorizedException.UNAUTHORIZED_PATH;
+import static java.net.URLEncoder.encode;
 
 public class AuthenticationError {
 
+  private static final String UNAUTHORIZED_PATH = "/sessions/unauthorized";
+  private static final String UNAUTHORIZED_PATH_WITH_MESSAGE = UNAUTHORIZED_PATH + "?message=%s";
   private static final Logger LOGGER = Loggers.get(AuthenticationError.class);
 
   private AuthenticationError() {
@@ -47,7 +51,19 @@ public class AuthenticationError {
   }
 
   public static void handleUnauthorizedError(UnauthorizedException e, HttpServletResponse response) {
-    redirectTo(response, e.getPath());
+    redirectTo(response, getPath(e));
+  }
+
+  private static String getPath(UnauthorizedException e) {
+    return format(UNAUTHORIZED_PATH_WITH_MESSAGE, encodeMessage(e.getMessage()));
+  }
+
+  private static String encodeMessage(String message) {
+    try {
+      return encode(message, Charsets.UTF_8.name());
+    } catch (UnsupportedEncodingException e) {
+      throw new IllegalStateException(format("Fail to encode %s", message), e);
+    }
   }
 
   private static void redirectToUnauthorized(HttpServletResponse response) {
index a2713e5d87e572fa561906c773d4c70aebf68807..190e8afc497a533ecf7d19863eda361987020f95 100644 (file)
  */
 package org.sonar.api.server.authentication;
 
-import com.google.common.base.Charsets;
-import java.io.UnsupportedEncodingException;
-
-import static java.lang.String.format;
-import static java.net.URLEncoder.encode;
-
 /**
  * This exception should be used when a functional error is generated by an Identity Provider plugin.
  * The user will be redirected to an unauthorized page and the exception's message will be displayed in the UI.
@@ -33,9 +27,6 @@ import static java.net.URLEncoder.encode;
  */
 public class UnauthorizedException extends RuntimeException {
 
-  public static final String UNAUTHORIZED_PATH = "/sessions/unauthorized";
-  private static final String UNAUTHORIZED_PATH_WITH_MESSAGE = UNAUTHORIZED_PATH + "?message=%s";
-
   public UnauthorizedException(String message) {
     super(message);
   }
@@ -43,16 +34,4 @@ public class UnauthorizedException extends RuntimeException {
   public UnauthorizedException(String message, Throwable cause) {
     super(message, cause);
   }
-
-  public String getPath() {
-    return format(UNAUTHORIZED_PATH_WITH_MESSAGE, encodeMessage(getMessage()));
-  }
-
-  private static String encodeMessage(String message) {
-    try {
-      return encode(message, Charsets.UTF_8.name());
-    } catch (UnsupportedEncodingException unsupportedException) {
-      throw new IllegalStateException(format("Fail to encode %s", message), unsupportedException);
-    }
-  }
 }