make UrlResolver reuse commons Http redirectory strategy
[teamcity/dotNetPackagesSupport.git] / nuget-server / src / jetbrains / buildServer / nuget / server / feed / reader / impl / UrlResolverImpl.java
index 4a583ab6191c9aa6a3dd6e2d07f83b47300ac341..256113c41ca0c25ec1a166c9c5a0a620af5956ec 100644 (file)
 \r
 package jetbrains.buildServer.nuget.server.feed.reader.impl;\r
 \r
+import com.intellij.openapi.diagnostic.Logger;\r
 import com.intellij.openapi.util.Pair;\r
-import org.apache.http.Header;\r
 import org.apache.http.HttpResponse;\r
 import org.apache.http.HttpStatus;\r
+import org.apache.http.ProtocolException;\r
+import org.apache.http.client.RedirectStrategy;\r
 import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.client.methods.HttpUriRequest;\r
 import org.apache.http.client.params.ClientPNames;\r
+import org.apache.http.impl.client.DefaultRedirectStrategy;\r
+import org.apache.http.protocol.BasicHttpContext;\r
+import org.apache.http.protocol.HttpContext;\r
 import org.jetbrains.annotations.NotNull;\r
+import org.jetbrains.annotations.Nullable;\r
 \r
 import java.io.IOException;\r
 \r
@@ -31,6 +38,8 @@ import java.io.IOException;
  * Date: 12.08.11 10:24\r
  */\r
 public class UrlResolverImpl implements UrlResolver {\r
+  private static final Logger LOG = Logger.getInstance(UrlResolverImpl.class.getName());\r
+\r
   private final FeedClient myClient;\r
   private final FeedGetMethodFactory myMethods;\r
 \r
@@ -56,15 +65,15 @@ public class UrlResolverImpl implements UrlResolver {
       ping.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);\r
 \r
       final HttpResponse execute = myClient.execute(ping);\r
-      final int statusCode = execute.getStatusLine().getStatusCode();\r
-      if (statusCode / 100 == 3) {\r
-        final Header location = execute.getFirstHeader("Location");\r
-        if (location != null) {\r
-          feedUrl = location.getValue();\r
-          continue;\r
-        }\r
+\r
+      String redirected = getRedirectedUrl(ping, execute);\r
+      if (redirected != null) {\r
+        LOG.debug("Redirected to " + redirected);\r
+        feedUrl = redirected;\r
+        continue;\r
       }\r
 \r
+      final int statusCode = execute.getStatusLine().getStatusCode();\r
       if (statusCode != HttpStatus.SC_OK) {\r
         throw new IOException("Failed to connect to " + feedUrl);\r
       }\r
@@ -73,4 +82,27 @@ public class UrlResolverImpl implements UrlResolver {
     throw new IOException("Failed to resolve redirects");\r
   }\r
 \r
+  private HttpContext createContext() {\r
+    return new BasicHttpContext();\r
+  }\r
+\r
+  @NotNull\r
+  public RedirectStrategy getRedirectStrategy() {\r
+    //TODO: could use http client for that\r
+    return new DefaultRedirectStrategy();\r
+  }\r
+\r
+  @Nullable\r
+  public String getRedirectedUrl(@NotNull HttpUriRequest request, @NotNull HttpResponse response) throws IOException {\r
+    try {\r
+      final RedirectStrategy redirectStrategy = getRedirectStrategy();\r
+      if (!redirectStrategy.isRedirected(request,response, createContext())) {\r
+        return null;\r
+      }\r
+\r
+      return redirectStrategy.getRedirect(request, response, createContext()).getURI().toString();\r
+    } catch (ProtocolException e) {\r
+      return null;\r
+    }\r
+  }\r
 }\r