package mikesaelim.arxivoaiharvester;

import com.google.common.annotations.VisibleForTesting;
import mikesaelim.arxivoaiharvester.io.ArxivRequest;
import mikesaelim.arxivoaiharvester.io.ArxivResponse;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;

/**
 * TODO: big-ass description, javadoc on methods
 *
 * Created by donerkebab on 1/3/15.
 */
public class ArxivOAIHarvester {

    private final ArxivRequest arxivRequest;
    // TODO: other fields for flow control

    public ArxivOAIHarvester(ArxivRequest arxivRequest) {
        this.arxivRequest = arxivRequest;
    }

    public ArxivResponse getNextBatch() throws IOException, ClientProtocolException, Exception {
        // TODO: the Exception in the throws clause is temporary, only used until we handle response codes and flow control properly
        // TODO: persistent HttpClient instance?  Closeable objects?  HttpClient passed in, leading to unit tests?
        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpGet httpRequest = new HttpGet(arxivRequest.getUri());
        httpRequest.addHeader("User-Agent", arxivRequest.getUserAgentHeader());
        httpRequest.addHeader("From", arxivRequest.getFromHeader());

        HttpResponse httpResponse = httpClient.execute(httpRequest);
        int httpStatusCode = httpResponse.getStatusLine().getStatusCode();

        // TODO: handle other response codes
        if (httpStatusCode != 200) {
            throw new Exception("Returned status code " + httpStatusCode + ": " +
                    httpResponse.getStatusLine().getReasonPhrase() + ": " +
                    EntityUtils.toString(httpResponse.getEntity()));
        }

        // TODO: implement resumption token storage and other flow control stuff

        return parseXMLStream(httpResponse.getEntity().getContent());
    }

    public boolean hasNextBatch() {
        // TODO: implement
        return false;
    }

    public ArxivRequest getArxivRequest() {
        return arxivRequest;
    }

    @VisibleForTesting ArxivResponse parseXMLStream(InputStream inputStream)
            throws ParserConfigurationException, SAXException, IOException {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        ArxivResponse.ArxivResponseBuilder responseBuilder = ArxivResponse.builder().arxivRequest(arxivRequest);

        parser.parse(inputStream, new XMLHandler(responseBuilder));

        return responseBuilder.build();
    }

}

@@ -218,28 +218,46 @@
 he.http.
-HttpResponse
+client.ClientProtocolException
 ;%0Aimport
@@ -284,73 +284,37 @@
 ent.
-ClientProtocolException;%0Aimport org.apache.http.client.HttpClient
+methods.CloseableHttpResponse
 ;%0Aim
@@ -393,16 +393,25 @@
 .client.
+Closeable
 HttpClie
@@ -412,23 +412,64 @@
 tpClient
-Builder
+;%0Aimport org.apache.http.impl.client.HttpClients
 ;%0Aimport
@@ -1336,28 +1336,8 @@
 nce?
-  Closeable objects?
   Ht
@@ -1383,24 +1383,76 @@
 ts?%0A        
+ArxivResponse arxivResponse;%0A%0A        try (Closeable
 HttpClient h
@@ -1477,35 +1477,33 @@
 ient
-Builder.create().build();%0A%0A
+s.createDefault()) %7B%0A    
     
@@ -1564,32 +1564,36 @@
 Uri());%0A        
+  
+  
 httpRequest.addH
@@ -1648,32 +1648,36 @@
 der());%0A        
+    
 httpRequest.addH
@@ -1726,24 +1726,42 @@
 );%0A%0A        
+   
+ try (Closeable
 HttpResponse
@@ -1807,26 +1807,34 @@
 Request)
-;%0A
+) %7B%0A      
         
 int http
@@ -1825,16 +1825,18 @@
         
+  
 int http
@@ -1891,24 +1891,32 @@
 tusCode();%0A%0A
+        
         // T
@@ -1956,16 +1956,24 @@
         
+       
+ 
 if (http
@@ -1989,24 +1989,32 @@
 e != 200) %7B%0A
+        
             
@@ -2075,32 +2075,40 @@
 usCode + %22: %22 +%0A
+        
                 
@@ -2187,16 +2187,24 @@
         
+        
 EntityUt
@@ -2240,24 +2240,32 @@
 Entity()));%0A
+        
         %7D%0A%0A 
@@ -2263,32 +2263,40 @@
      %7D%0A%0A        
+ 
+       
 // TODO: impleme
@@ -2361,22 +2361,39 @@
         
-return
+        arxivResponse =
  parseXM
@@ -2436,24 +2436,79 @@
 Content());%0A
+            %7D%0A        %7D%0A%0A        return arxivResponse;%0A
     %7D%0A%0A    p

change to CloseableHttpClient/CloseableHttpResponse