extract UrlResolver as standalone service to make it easy testable
authorEugene Petrenko <eugene.petrenko@gmail.com>
Fri, 12 Aug 2011 06:29:07 +0000 (10:29 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Fri, 12 Aug 2011 06:29:07 +0000 (10:29 +0400)
nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/NuGetFeedReader.java
nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/UrlResolver.java [new file with mode: 0644]
nuget-tests/src/jetbrains/buildServer/nuget/tests/integration/FeedReaderTest.java

index f965bf2f381d0a689d96bc91cb80a8d70e7374f7..ae7582b1fea29adcfb8a12ac28727408b53aa41f 100644 (file)
@@ -20,12 +20,9 @@ import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.util.Pair;\r
 import jetbrains.buildServer.util.FileUtil;\r
 import jetbrains.buildServer.util.XmlUtil;\r
-import org.apache.http.Header;\r
 import org.apache.http.HttpHeaders;\r
 import org.apache.http.HttpResponse;\r
-import org.apache.http.HttpStatus;\r
 import org.apache.http.client.methods.HttpGet;\r
-import org.apache.http.client.params.ClientPNames;\r
 import org.jdom.Element;\r
 import org.jdom.JDOMException;\r
 import org.jetbrains.annotations.NotNull;\r
@@ -39,38 +36,19 @@ import java.net.URISyntaxException;
  */\r
 public class NuGetFeedReader {\r
   private static final Logger LOG = Logger.getInstance(NuGetFeedReader.class.getName());\r
-  private FeedClient myClient;\r
+  private final FeedClient myClient;\r
+  private final UrlResolver myResolver;\r
 \r
-  public NuGetFeedReader(FeedClient client) {\r
+  public NuGetFeedReader(FeedClient client, UrlResolver resolver) {\r
     myClient = client;\r
+    myResolver = resolver;\r
   }\r
 \r
-  @NotNull\r
-  private Pair<String, HttpResponse> resolvePath(@NotNull String feedUrl) throws IOException {\r
-    for(int _ = 100; _-->0;) {\r
-      HttpGet ping = new HttpGet(feedUrl);\r
-      ping.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);\r
-      final HttpResponse execute = myClient.getClient().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
-      if (statusCode != HttpStatus.SC_OK) {\r
-        throw new IOException("Failed to connect to " + feedUrl);\r
-      }\r
-      return Pair.create(feedUrl, execute);\r
-    }\r
-    throw new IOException("Failed to resolve redirects");\r
-  }\r
 \r
   public void queryPackage(@NotNull String feedUrl,\r
-                           @NotNull String packageId) throws IOException, URISyntaxException {\r
+                           @NotNull String packageId) throws IOException {\r
     LOG.debug("Connecting to NuGet feed url: " + feedUrl);\r
-    final Pair<String, HttpResponse> pair = resolvePath(feedUrl);\r
+    final Pair<String, HttpResponse> pair = myResolver.resolvePath(feedUrl);\r
     feedUrl = pair.first;\r
     LOG.debug("Resolved NuGet feed URL to " + feedUrl);\r
     final Element element = toDocument(pair.second);\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/UrlResolver.java b/nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/UrlResolver.java
new file mode 100644 (file)
index 0000000..ffde939
--- /dev/null
@@ -0,0 +1,71 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.server.feed.reader;\r
+\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.client.methods.HttpGet;\r
+import org.apache.http.client.params.ClientPNames;\r
+import org.jetbrains.annotations.NotNull;\r
+\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 12.08.11 10:24\r
+ */\r
+public class UrlResolver {\r
+  private final FeedClient myClient;\r
+\r
+  public UrlResolver(FeedClient client) {\r
+    myClient = client;\r
+  }\r
+\r
+  /**\r
+   * Generates GET request to a given URL.\r
+   * If HTTP Status 3xx is returned, Location header is\r
+   * used to generate next request unless non 3xx status\r
+   * is returned\r
+   * @param feedUrl url to ping and resolve\r
+   * @return resolved URL and HttpResponse\r
+   * @throws IOException if failed to communicate or non 3xx or 200 status returned\r
+   */\r
+  @NotNull\r
+  public Pair<String, HttpResponse> resolvePath(@NotNull String feedUrl) throws IOException {\r
+    for(int _ = 100; _-->0;) {\r
+      HttpGet ping = new HttpGet(feedUrl);\r
+      ping.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);\r
+      final HttpResponse execute = myClient.getClient().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
+      if (statusCode != HttpStatus.SC_OK) {\r
+        throw new IOException("Failed to connect to " + feedUrl);\r
+      }\r
+      return Pair.create(feedUrl, execute);\r
+    }\r
+    throw new IOException("Failed to resolve redirects");\r
+  }\r
+\r
+}\r
index cb9dd79d807db3e51d3c1958448ec0518f818e43..5f2a1857c261bc8c3d47c2aa5ea543d48e395353 100644 (file)
@@ -20,6 +20,7 @@ import jetbrains.buildServer.BaseTestCase;
 import jetbrains.buildServer.nuget.server.feed.reader.FeedClient;\r
 import jetbrains.buildServer.nuget.server.feed.reader.FeedConstants;\r
 import jetbrains.buildServer.nuget.server.feed.reader.NuGetFeedReader;\r
+import jetbrains.buildServer.nuget.server.feed.reader.UrlResolver;\r
 import org.apache.log4j.ConsoleAppender;\r
 import org.apache.log4j.Level;\r
 import org.apache.log4j.Logger;\r
@@ -43,7 +44,7 @@ public class FeedReaderTest extends BaseTestCase {
   protected void setUp() throws Exception {\r
     super.setUp();\r
     myClient = new FeedClient();\r
-    myReader = new NuGetFeedReader(myClient);\r
+    myReader = new NuGetFeedReader(myClient, new UrlResolver(myClient));\r
   }\r
 \r
   @AfterMethod\r