/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.openejb.tools.legal;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.swizzle.stream.StreamLexer;

import java.io.IOException;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;

/**
 * @version $Rev$ $Date$
 */
public class Main {

    private DefaultHttpClient client;

    public Main() {
        client = new DefaultHttpClient();
    }

    public static void main(String[] args) throws Exception {
        new Main()._main(args);
    }

    private void _main(String... args) 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);

        for (URI uri : resources) {
            System.out.println(uri);
        }

    }

    private Set<URI> crawl(URI index) throws IOException {
        final Set<URI> resources = new HashSet<URI>();

        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");
        HttpResponse response = client.execute(request);
        StreamLexer lexer = new StreamLexer(response.getEntity().getContent());

        //<a href="https://repository.apache.org/content/repositories/orgapacheopenejb-094/archetype-catalog.xml">archetype-catalog.xml</a>
        while (lexer.readAndMark("<a ","/a>")) {
            final String link = lexer.peek("href=\"", "\"");
            final String name = lexer.peek(">", "<");

            final URI uri = index.resolve(link);

            if (name.endsWith("/")) {
                resources.addAll(crawl(uri));
            } else {
                resources.add(uri);
            }
        }
        return resources;
    }


}

@@ -1057,16 +1057,44 @@
 eption;%0A
+import java.io.InputStream;%0A
 import j
@@ -1123,16 +1123,22 @@
 va.util.
+Linked
 HashSet;
@@ -1916,16 +1916,22 @@
 s = new 
+Linked
 HashSet%3C
@@ -1943,24 +1943,30 @@
 );%0A%0A        
+final 
 HttpGet requ
@@ -2152,16 +2152,22 @@
         
+final 
 HttpResp
@@ -2207,24 +2207,25 @@
 quest);%0A
+%0A
         
 StreamLe
@@ -2220,77 +2220,186 @@
     
-StreamLexer lexer = new StreamLexer(response.getEntity().getContent()
+final InputStream content = response.getEntity().getContent();%0A        final StreamLexer lexer = new StreamLexer(content);%0A%0A        final Set%3CURI%3E crawl = new LinkedHashSet%3CURI%3E(
 );%0A%0A
@@ -2577,16 +2577,17 @@
 k(%22%3Ca %22,
+ 
 %22/a%3E%22)) 
@@ -2584,24 +2584,47 @@
 , %22/a%3E%22)) %7B%0A
+%0A            try %7B%0A    
             
@@ -2676,32 +2676,36 @@
 %22);%0A            
+    
 final String nam
@@ -2735,32 +2735,36 @@
 );%0A%0A            
+   
+ 
 final URI uri = 
@@ -2797,16 +2797,20 @@
         
+    
 if (name
@@ -2815,90 +2815,286 @@
 me.e
-ndsWith(%22/%22)) %7B%0A                resources.addAll(
+quals(%22../%22)) continue;%0A                if (link.equals(%22../%22)) continue;%0A%0A                if (name.endsWith(%22/%22)) %7B%0A                    
 crawl
+.add
 (uri)
-);%0A            %7D else %7B
+;%0A                    continue;%0A                %7D%0A%0A                if (!uri.getPath().matches(%22.*(jar%7Czip%7Ctar.gz)%22)) continue;%0A
 %0A   
@@ -3118,32 +3118,33 @@
 urces.add(uri);%0A
+%0A
             %7D%0A  
@@ -3132,32 +3132,199 @@
 ;%0A%0A            %7D
+ finally %7B%0A                lexer.unmark();%0A            %7D%0A        %7D%0A%0A        content.close();%0A%0A        for (URI uri : crawl) %7B%0A            resources.addAll(crawl(uri));
 %0A        %7D%0A     

collects a full list of binaries in sorted order