prototype feed reader
authorEugene Petrenko <eugene.petrenko@gmail.com>
Thu, 11 Aug 2011 15:13:48 +0000 (19:13 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Thu, 11 Aug 2011 15:13:48 +0000 (19:13 +0400)
nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/FeedClient.java
nuget-server/src/jetbrains/buildServer/nuget/server/feed/reader/NuGetFeedReader.java
nuget-tests/src/jetbrains/buildServer/nuget/tests/integration/FeedReaderTest.java

index 40331c48cddfabbda69bc42de4240891d14fdaf4..d10632afba13b202b5a7325785af75d1aa35dc3c 100644 (file)
 \r
 package jetbrains.buildServer.nuget.server.feed.reader;\r
 \r
+import jetbrains.buildServer.version.ServerVersionHolder;\r
 import org.apache.http.client.HttpClient;\r
 import org.apache.http.impl.client.DefaultHttpClient;\r
 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;\r
 import org.apache.http.params.HttpConnectionParams;\r
+import org.apache.http.params.HttpParams;\r
+import org.apache.http.params.HttpProtocolParams;\r
 import org.jetbrains.annotations.NotNull;\r
 \r
 /**\r
@@ -31,8 +34,13 @@ public class FeedClient {
 \r
   public FeedClient() {\r
     myClient = new DefaultHttpClient(new ThreadSafeClientConnManager());\r
-    HttpConnectionParams.setConnectionTimeout(myClient.getParams(), 10000);\r
-    HttpConnectionParams.setSoTimeout(myClient.getParams(), 10000);\r
+    final HttpParams params = myClient.getParams();\r
+\r
+    HttpConnectionParams.setConnectionTimeout(params, 10000);\r
+    HttpConnectionParams.setSoTimeout(params, 10000);\r
+\r
+    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();\r
+    HttpProtocolParams.setUserAgent(params, "JetBrains TeamCity " + serverVersion);\r
   }\r
 \r
   @NotNull\r
index f0e05c25304f3b080ba9d8e5ecfcf96b64fc0e62..f965bf2f381d0a689d96bc91cb80a8d70e7374f7 100644 (file)
 \r
 package jetbrains.buildServer.nuget.server.feed.reader;\r
 \r
+import com.intellij.openapi.diagnostic.Logger;\r
+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
 \r
 import java.io.IOException;\r
+import java.net.URISyntaxException;\r
 \r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
  * Date: 11.08.11 15:42\r
  */\r
 public class NuGetFeedReader {\r
+  private static final Logger LOG = Logger.getInstance(NuGetFeedReader.class.getName());\r
   private FeedClient myClient;\r
 \r
   public NuGetFeedReader(FeedClient client) {\r
     myClient = client;\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 {\r
-    HttpGet get = new HttpGet(feedUrl + "/Packages()");\r
-    get.getParams().setParameter("$filter", "Id eq '" + packageId + "'");\r
-    get.setHeader(HttpHeaders.ACCEPT_ENCODING, "application/xml");\r
+                           @NotNull String packageId) throws IOException, URISyntaxException {\r
+    LOG.debug("Connecting to NuGet feed url: " + feedUrl);\r
+    final Pair<String, HttpResponse> pair = resolvePath(feedUrl);\r
+    feedUrl = pair.first;\r
+    LOG.debug("Resolved NuGet feed URL to " + feedUrl);\r
+    final Element element = toDocument(pair.second);\r
+\r
+    LOG.debug("Recieved xml: " + XmlUtil.to_s(element));\r
+    HttpGet get = new HttpGet(feedUrl + "/Packages()?$filter=Id%20eq%20'" + packageId +"'");\r
+    get.setHeader(HttpHeaders.ACCEPT_ENCODING, "application/atom+xml");\r
     final HttpResponse execute = myClient.getClient().execute(get);\r
 \r
     System.out.println(execute);\r
     execute.getEntity().writeTo(System.out);\r
   }\r
+\r
+  private Element toDocument(HttpResponse response) throws IOException {\r
+    try {\r
+      return FileUtil.parseDocument(response.getEntity().getContent(), false);\r
+    } catch (final JDOMException e) {\r
+      throw new IOException("Failed to parse xml document. " + e.getMessage()) {{\r
+        initCause(e);\r
+      }};\r
+    }\r
+  }\r
 }\r
index bd26772ecd690ff1fade52ca22c6874bd23bcb9e..cb9dd79d807db3e51d3c1958448ec0518f818e43 100644 (file)
@@ -20,6 +20,10 @@ 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 org.apache.log4j.ConsoleAppender;\r
+import org.apache.log4j.Level;\r
+import org.apache.log4j.Logger;\r
+import org.apache.log4j.SimpleLayout;\r
 import org.testng.annotations.AfterMethod;\r
 import org.testng.annotations.BeforeMethod;\r
 import org.testng.annotations.Test;\r
@@ -51,6 +55,12 @@ public class FeedReaderTest extends BaseTestCase {
 \r
   @Test\r
   public void testRead() throws IOException {\r
+    enableDebug();\r
+\r
+    final Logger logger = Logger.getLogger("org.apache.commons");\r
+    logger.setLevel(Level.DEBUG);\r
+    logger.addAppender(new ConsoleAppender(new SimpleLayout()));\r
+\r
     myReader.queryPackage(FeedConstants.FEED_URL, "NuGet.CommandLine");\r
   }\r
 }\r