Downloads and recursively unpacks archives


git-svn-id: https://svn.apache.org/repos/asf/openejb/trunk/sandbox/legal@1176657 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/openejb/tools/legal/Main.java b/src/main/java/org/apache/openejb/tools/legal/Main.java
index 923d8ce..858b52f 100644
--- a/src/main/java/org/apache/openejb/tools/legal/Main.java
+++ b/src/main/java/org/apache/openejb/tools/legal/Main.java
@@ -19,48 +19,151 @@
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
 import org.codehaus.swizzle.stream.StreamLexer;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.Set;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
 /**
  * @version $Rev$ $Date$
  */
 public class Main {
 
-    private DefaultHttpClient client;
+    static {
+        Logger root = Logger.getRootLogger();
 
-    public Main() {
+        root.addAppender(new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
+        root.setLevel(Level.INFO);
+    }
+
+    private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Main.class);
+
+
+    private final DefaultHttpClient client;
+    private final File local;
+    private final URI repo;
+    private final File localRepo;
+
+    public Main(String... args) throws Exception {
         client = new DefaultHttpClient();
+        local = File.createTempFile("repository-check", "local");
+        assert local.delete();
+        assert local.mkdirs();
+
+        localRepo = new File(local, "repo");
+        assert localRepo.mkdirs();
+
+        repo = new URI(args[0]);
     }
 
     public static void main(String[] args) throws Exception {
-        new Main()._main(args);
+        new Main("https://repository.apache.org/content/repositories/orgapacheopenejb-094").main();
     }
 
-    private void _main(String... args) throws Exception {
+    private void main() throws Exception {
         // https://repository.apache.org/content/repositories/orgapacheopenejb-094
 
         final URI index = new URI("https://repository.apache.org/content/repositories/orgapacheopenejb-094");
 
         final Set<URI> resources = crawl(index);
 
+        final Set<File> files = new HashSet<File>();
+
         for (URI uri : resources) {
-            System.out.println(uri);
+            files.add(download(uri));
         }
 
+        for (File file : files) {
+            unpack(file);
+        }
+    }
+
+    private void unpack(File archive) throws IOException {
+        log.info("Unpack " + archive);
+
+        try {
+            final ZipInputStream zip = IOUtil.unzip(archive);
+
+            final File contents = new File(archive.getAbsolutePath() + ".contents");
+            assert contents.mkdir();
+
+            try {
+                ZipEntry entry = null;
+
+                while ((entry = zip.getNextEntry()) != null) {
+                    final String path = entry.getName();
+
+                    final File fileEntry = new File(contents, path);
+
+                    mkdirs(fileEntry);
+
+                    // Open the output file
+
+                    IOUtil.copy(zip, fileEntry);
+
+                    if (fileEntry.getName().endsWith(".jar")) {
+                        unpack(fileEntry);
+                    }
+                }
+            } finally {
+                IOUtil.close(zip);
+            }
+        } catch (IOException e) {
+            log.error("Not a zip " + archive);
+        }
+    }
+
+    private File download(URI uri) throws IOException {
+        log.info("Download " + uri);
+
+        final HttpResponse response = get(uri);
+
+        final InputStream content = response.getEntity().getContent();
+        final String name = uri.toString().replace(repo.toString(), "").replaceFirst("^/", "");
+
+        final File file = new File(localRepo, name);
+
+        mkdirs(file);
+
+        IOUtil.copy(content, file);
+
+        return file;
+    }
+
+    private void mkdirs(File file) {
+
+        final File parent = file.getParentFile();
+
+        if (!parent.exists()) {
+            assert parent.mkdirs() : "mkdirs " + parent;
+            return;
+        }
+
+        assert parent.isDirectory() : "not a directory" + parent;
+    }
+
+    private HttpResponse get(URI uri) throws IOException {
+        final HttpGet request = new HttpGet(uri);
+        request.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
+        return client.execute(request);
     }
 
     private Set<URI> crawl(URI index) throws IOException {
+        log.info("Crawl " + index);
         final Set<URI> resources = new LinkedHashSet<URI>();
 
-        final HttpGet request = new HttpGet(index);
-        request.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
-        final HttpResponse response = client.execute(request);
+        final HttpResponse response = get(index);
 
         final InputStream content = response.getEntity().getContent();
         final StreamLexer lexer = new StreamLexer(content);
@@ -84,7 +187,7 @@
                     continue;
                 }
 
-                if (!uri.getPath().matches(".*(jar|zip|tar.gz)")) continue;
+                if (!uri.getPath().matches(".*(jar|zip|war|tar.gz)")) continue;
 
                 resources.add(uri);