Repository: incubator-slider
Updated Branches:
  refs/heads/develop a5c0d3b41 -> b72114850


SLIDER-593 handle cross-transport redirects


Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/b7211485
Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/b7211485
Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/b7211485

Branch: refs/heads/develop
Commit: b7211485046549e743325982f3806b5db0d467db
Parents: a5c0d3b
Author: Jon Maron <[email protected]>
Authored: Sun Nov 2 12:48:45 2014 -0500
Committer: Jon Maron <[email protected]>
Committed: Sun Nov 2 12:48:45 2014 -0500

----------------------------------------------------------------------
 .../registry/retrieve/RegistryRetriever.java    | 32 +++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/b7211485/slider-core/src/main/java/org/apache/slider/core/registry/retrieve/RegistryRetriever.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/main/java/org/apache/slider/core/registry/retrieve/RegistryRetriever.java
 
b/slider-core/src/main/java/org/apache/slider/core/registry/retrieve/RegistryRetriever.java
index a91f515..cdcf66e 100644
--- 
a/slider-core/src/main/java/org/apache/slider/core/registry/retrieve/RegistryRetriever.java
+++ 
b/slider-core/src/main/java/org/apache/slider/core/registry/retrieve/RegistryRetriever.java
@@ -25,6 +25,8 @@ import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
 import com.sun.jersey.api.json.JSONConfiguration;
+import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;
+import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;
 import org.apache.hadoop.registry.client.binding.RegistryTypeUtils;
 import org.apache.hadoop.registry.client.exceptions.RegistryIOException;
 import org.apache.hadoop.registry.client.types.Endpoint;
@@ -39,9 +41,13 @@ import 
org.apache.slider.core.registry.info.CustomRegistryConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.net.URL;
 import java.util.List;
 
 /**
@@ -63,7 +69,31 @@ public class RegistryRetriever {
     clientConfig.getFeatures().put(
         JSONConfiguration.FEATURE_POJO_MAPPING,
         Boolean.TRUE);
-    jerseyClient = Client.create(clientConfig);
+    clientConfig.getProperties().put(
+        
URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, 
true);
+    URLConnectionClientHandler handler =
+        new URLConnectionClientHandler(new HttpURLConnectionFactory() {
+      @Override
+      public HttpURLConnection getHttpURLConnection(URL url)
+          throws IOException {
+        HttpURLConnection connection = (HttpURLConnection) 
url.openConnection();
+        if (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) 
{
+          // is a redirect - are we changing schemes?
+          String redirectLocation = 
connection.getHeaderField(HttpHeaders.LOCATION);
+          String originalScheme = url.getProtocol();
+          String redirectScheme = URI.create(redirectLocation).getScheme();
+          if (!originalScheme.equals(redirectScheme)) {
+            // need to fake it out by doing redirect ourselves
+            log.info("Protocol change during redirect. Redirecting {} to URL 
{}",
+                     url, redirectLocation);
+            URL redirectURL = new URL(redirectLocation);
+            connection = (HttpURLConnection) redirectURL.openConnection();
+          }
+        }
+        return connection;
+      }
+    });
+    jerseyClient = new Client(handler, clientConfig);
     jerseyClient.setFollowRedirects(true);
   }
   

Reply via email to