Repository: Unleash/unleash-client-java (java)

Issue:
Improve error messages
When connecting to the unleash-server over http instead of https and the client only receives 301 it will only log 301 and not the new location header. This can make it hard for the use to understand why the client cannot connect to the server. 

Pull request description:
add redirect location fix #84

Patch:
diff --git a/src/main/java/no/finn/unleash/repository/FeatureToggleResponse.java b/src/main/java/no/finn/unleash/repository/FeatureToggleResponse.java
index 3fd68d73..cf2d280e 100644
--- a/src/main/java/no/finn/unleash/repository/FeatureToggleResponse.java
+++ b/src/main/java/no/finn/unleash/repository/FeatureToggleResponse.java
@@ -15,6 +15,7 @@ public final class FeatureToggleResponse implements UnleashEvent {
     private final Status status;
     private final int httpStatusCode;
     private final ToggleCollection toggleCollection;
+    private String location;
 
     public FeatureToggleResponse(Status status, ToggleCollection toggleCollection) {
         this.status = status;
@@ -29,6 +30,11 @@ public final class FeatureToggleResponse implements UnleashEvent {
         this.toggleCollection = new ToggleCollection(emptyList);
     }
 
+    public FeatureToggleResponse(Status status, int httpStatusCode, String location) {
+        this(status, httpStatusCode);
+        this.location = location;
+    }
+
     public Status getStatus() {
         return status;
     }
@@ -41,18 +47,27 @@ public final class FeatureToggleResponse implements UnleashEvent {
         return httpStatusCode;
     }
 
+    public String getLocation() {
+        return location;
+    }
+
     @Override
     public String toString() {
         return "FeatureToggleResponse:"
                 + " status=" + status
                 + " httpStatus=" + httpStatusCode
+                + " location=" + location
                 ;
     }
 
     @Override
     public void publishTo(UnleashSubscriber unleashSubscriber) {
         if (status == FeatureToggleResponse.Status.UNAVAILABLE){
-            unleashSubscriber.onError(new UnleashException("Error fetching toggles from Unleash API - StatusCode: " + getHttpStatusCode(), null));
+            String msg = "Error fetching toggles from Unleash API - StatusCode: " + getHttpStatusCode();
+            if(location != null) {
+                msg += ", Location: " + location;
+            }
+            unleashSubscriber.onError(new UnleashException(msg, null));
         }
 
         unleashSubscriber.togglesFetched(this);
diff --git a/src/main/java/no/finn/unleash/repository/HttpToggleFetcher.java b/src/main/java/no/finn/unleash/repository/HttpToggleFetcher.java
index 7b6f0f30..c7bf3298 100644
--- a/src/main/java/no/finn/unleash/repository/HttpToggleFetcher.java
+++ b/src/main/java/no/finn/unleash/repository/HttpToggleFetcher.java
@@ -48,7 +48,7 @@ public final class HttpToggleFetcher implements ToggleFetcher {
             } else if (responseCode == 304) {
                 return new FeatureToggleResponse(FeatureToggleResponse.Status.NOT_CHANGED, responseCode);
             } else {
-                return new FeatureToggleResponse(FeatureToggleResponse.Status.UNAVAILABLE, responseCode);
+                return new FeatureToggleResponse(FeatureToggleResponse.Status.UNAVAILABLE, responseCode, getLocationHeader(connection));
             }
         } catch (IOException e) {
             throw new UnleashException("Could not fetch toggles", e);
@@ -71,4 +71,8 @@ public final class HttpToggleFetcher implements ToggleFetcher {
             return new FeatureToggleResponse(FeatureToggleResponse.Status.CHANGED, toggles);
         }
     }
+
+    private String getLocationHeader(HttpURLConnection connection) {
+        return connection.getHeaderField("Location");
+    }
 }
diff --git a/src/main/java/no/finn/unleash/repository/ToggleCollection.java b/src/main/java/no/finn/unleash/repository/ToggleCollection.java
index 51eda0c2..781116b9 100644
--- a/src/main/java/no/finn/unleash/repository/ToggleCollection.java
+++ b/src/main/java/no/finn/unleash/repository/ToggleCollection.java
@@ -25,11 +25,11 @@ public final class ToggleCollection {
         return features;
     }
 
-    Collection<FeatureToggle> getFeatures() {
+    public Collection<FeatureToggle> getFeatures() {
         return Collections.unmodifiableCollection(features);
     }
 
-    FeatureToggle getToggle(final String name) {
+    public FeatureToggle getToggle(final String name) {
         return cache.get(name);
     }
 }


Test patch:
diff --git a/src/test/java/no/finn/unleash/repository/HttpToggleFetcherTest.java b/src/test/java/no/finn/unleash/repository/HttpToggleFetcherTest.java
index 2d293925..c29d47db 100644
--- a/src/test/java/no/finn/unleash/repository/HttpToggleFetcherTest.java
+++ b/src/test/java/no/finn/unleash/repository/HttpToggleFetcherTest.java
@@ -247,4 +247,20 @@ public class HttpToggleFetcherTest {
 
     }
 
-}
\ No newline at end of file
+    @Test
+    public void should_notifiy_location_on_redirect() throws URISyntaxException {
+        stubFor(get(urlEqualTo("/api/client/features"))
+            .withHeader("Accept", equalTo("application/json"))
+            .willReturn(aResponse()
+                .withStatus(308)
+                .withHeader("Location", "https://unleash.com")));
+
+        URI uri = new URI("http://localhost:"+serverMock.port() + "/api/");
+        UnleashConfig config = UnleashConfig.builder().appName("test").unleashAPI(uri).build();
+        HttpToggleFetcher httpToggleFetcher = new HttpToggleFetcher(config);
+        FeatureToggleResponse response = httpToggleFetcher.fetchToggles();
+        assertThat(response.getLocation(), is("https://unleash.com"));
+
+    }
+
+}