]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6226 When url is not https, throw a MessageException
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 3 Feb 2016 08:28:54 +0000 (09:28 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 4 Feb 2016 09:09:53 +0000 (10:09 +0100)
server/sonar-server/src/main/java/org/sonar/server/authentication/OAuth2ContextFactory.java
server/sonar-server/src/test/java/org/sonar/server/authentication/OAuth2ContextFactoryTest.java

index ce1d920becd8fae6587f5f4a26d42c0a056f067c..b6bbffa6221127db309c4071021e716abe1cfb29 100644 (file)
@@ -25,8 +25,11 @@ import javax.servlet.http.HttpServletResponse;
 import org.sonar.api.platform.Server;
 import org.sonar.api.server.authentication.OAuth2IdentityProvider;
 import org.sonar.api.server.authentication.UserIdentity;
+import org.sonar.api.utils.MessageException;
 
+import static java.lang.String.format;
 import static org.sonar.api.CoreProperties.SERVER_BASE_URL;
+import static org.sonar.server.authentication.OAuth2CallbackFilter.CALLBACK_PATH;
 
 public class OAuth2ContextFactory {
 
@@ -64,9 +67,9 @@ public class OAuth2ContextFactory {
     public String getCallbackUrl() {
       String publicRootUrl = server.getPublicRootUrl();
       if (publicRootUrl.startsWith("http:") && !server.isDev()) {
-        throw new IllegalStateException(String.format("The server url should be configured in https, please update the property '%s'", SERVER_BASE_URL));
+        throw MessageException.of(format("The server url should be configured in https, please update the property '%s'", SERVER_BASE_URL));
       }
-      return publicRootUrl +  OAuth2CallbackFilter.CALLBACK_PATH + "/" + identityProvider.getKey();
+      return publicRootUrl +  CALLBACK_PATH + "/" + identityProvider.getKey();
     }
 
     @Override
@@ -89,7 +92,7 @@ public class OAuth2ContextFactory {
       try {
         response.sendRedirect(url);
       } catch (IOException e) {
-        throw new IllegalStateException(String.format("Fail to redirect to %s", url), e);
+        throw new IllegalStateException(format("Fail to redirect to %s", url), e);
       }
     }
 
index 4dfb61728fca4e3ef65ff2706439fa808ea23e50..f44a9e7722f6a1ef3e295fa4cfa96cd21b888ec7 100644 (file)
@@ -29,6 +29,7 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.platform.Server;
 import org.sonar.api.server.authentication.OAuth2IdentityProvider;
 import org.sonar.api.server.authentication.UserIdentity;
+import org.sonar.api.utils.MessageException;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
@@ -104,7 +105,7 @@ public class OAuth2ContextFactoryTest {
 
     OAuth2IdentityProvider.InitContext context = underTest.newContext(request, response, identityProvider);
 
-    thrown.expect(IllegalStateException.class);
+    thrown.expect(MessageException.class);
     thrown.expectMessage("The server url should be configured in https, please update the property 'sonar.core.serverBaseURL'");
     context.getCallbackUrl();
   }