GrognakCatalogFetcher became URLFetcher

This commit is contained in:
Vhati 2013-09-04 19:53:36 -04:00
parent cd040e6ecc
commit 11cd5cca61
2 changed files with 24 additions and 28 deletions

View file

@ -22,22 +22,20 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class GrognakCatalogFetcher {
public class URLFetcher {
private static final Logger log = LogManager.getLogger(GrognakCatalogFetcher.class);
public static final String CATALOG_URL = "https://raw.github.com/Vhati/Slipstream-Mod-Manager/master/skel_common/backup/current_catalog.json";
private static final Logger log = LogManager.getLogger(URLFetcher.class);
/**
* Downloads the latest mod catalog.
* Downloads content from a url into a file, if the remote content has changed.
*
* @return true if the catalog successfully downloaded, false otherwise
* @return true if successfully downloaded, false otherwise
*/
public static boolean fetchCatalog( String catalogURL, File catalogFile, File eTagFile ) {
public static boolean refetchURL( String url, File localFile, File eTagFile ) {
String localETag = null;
log.debug( "Attempting to download a newer catalog..." );
log.debug( String.format( "Attempting to download the latest \"%s\".", localFile.getName() ) );
if ( eTagFile.exists() ) {
// Load the old eTag.
InputStream etagIn = null;
@ -50,7 +48,7 @@ public class GrognakCatalogFetcher {
}
catch ( IOException e ) {
// Not serious enough to be a real error.
log.debug( String.format( "Error reading catalog eTag from \"%s\".", eTagFile.getName() ), e );
log.debug( String.format( "Error reading eTag from \"%s\".", eTagFile.getName() ), e );
}
finally {
try {if ( etagIn != null ) etagIn.close();}
@ -60,13 +58,12 @@ public class GrognakCatalogFetcher {
String remoteETag = null;
InputStream urlIn = null;
OutputStream catalogOut = null;
OutputStream localOut = null;
try {
URL url = new URL( catalogURL );
URLConnection conn = url.openConnection();
URLConnection conn = new URL( url ).openConnection();
if ( conn instanceof HttpURLConnection == false ) {
log.error( String.format( "Non-Http(s) URL given for catalog fetching: %s", catalogURL ) );
log.error( String.format( "Non-Http(s) URL given for fetching: %s", url ) );
return false;
}
HttpURLConnection httpConn = (HttpURLConnection)conn;
@ -79,10 +76,10 @@ public class GrognakCatalogFetcher {
int responseCode = httpConn.getResponseCode();
if ( responseCode == HttpURLConnection.HTTP_NOT_MODIFIED ) {
log.debug( "The server's catalog has not been modified since the previous check." );
log.debug( String.format( "The server's \"%s\" has not been modified since the previous check.", httpConn.getURL().getFile() ) );
// Update the catalog file's timestamp as if it had downloaded.
catalogFile.setLastModified( new Date().getTime() );
// Update the local file's timestamp as if it had downloaded.
localFile.setLastModified( new Date().getTime() );
return false;
}
@ -93,29 +90,26 @@ public class GrognakCatalogFetcher {
remoteETag = eTagValues.get( 0 );
urlIn = httpConn.getInputStream();
catalogOut = new FileOutputStream( catalogFile );
localOut = new FileOutputStream( localFile );
byte[] buf = new byte[4096];
int len;
while ( (len = urlIn.read(buf)) >= 0 ) {
catalogOut.write( buf, 0, len );
localOut.write( buf, 0, len );
}
}
else {
log.error( String.format( "Catalog download request failed: HTTP Code %d (%s).", responseCode, httpConn.getResponseMessage() ) );
log.error( String.format( "Download request failed: HTTP Code %d (%s).", responseCode, httpConn.getResponseMessage() ) );
return false;
}
}
catch ( MalformedURLException e ) {
log.error( "Error fetching latest catalog.", e );
}
catch ( IOException e ) {
log.error( "Error fetching latest catalog.", e );
log.error( String.format( "Error downloading the latest \"%s\".", localFile.getName() ), e );
}
finally {
try {if ( urlIn != null ) urlIn.close();}
catch ( IOException e ) {}
try {if ( catalogOut != null ) catalogOut.close();}
try {if ( localOut != null ) localOut.close();}
catch ( IOException e ) {}
}
@ -129,7 +123,7 @@ public class GrognakCatalogFetcher {
bw.flush();
}
catch ( IOException e ) {
log.error( String.format( "Error writing catalog eTag to \"%s\".", eTagFile.getName() ), e );
log.error( String.format( "Error writing eTag to \"%s\".", eTagFile.getName() ), e );
}
finally {
try {if ( etagOut != null ) etagOut.close();}

View file

@ -72,8 +72,8 @@ import net.vhati.modmanager.core.ModUtilities;
import net.vhati.modmanager.core.Report;
import net.vhati.modmanager.core.Report.ReportFormatter;
import net.vhati.modmanager.core.SlipstreamConfig;
import net.vhati.modmanager.json.GrognakCatalogFetcher;
import net.vhati.modmanager.json.JacksonGrognakCatalogReader;
import net.vhati.modmanager.json.URLFetcher;
import net.vhati.modmanager.ui.ChecklistTableModel;
import net.vhati.modmanager.ui.InertPanel;
import net.vhati.modmanager.ui.ModInfoArea;
@ -91,6 +91,9 @@ public class ManagerFrame extends JFrame implements ActionListener, HashObserver
private static final Logger log = LogManager.getLogger(ManagerFrame.class);
public static final String CATALOG_URL = "https://raw.github.com/Vhati/Slipstream-Mod-Manager/master/skel_common/backup/current_catalog.json";
public static final String AUTOUPDATE_URL = "https://raw.github.com/Vhati/Slipstream-Mod-Manager/master/auto-update.json";
private File backupDir = new File( "./backup/" );
private File modsDir = new File( "./mods/" );
@ -429,8 +432,7 @@ public class ManagerFrame extends JFrame implements ActionListener, HashObserver
Runnable fetchTask = new Runnable() {
@Override
public void run() {
String catalogURL = GrognakCatalogFetcher.CATALOG_URL;
boolean fetched = GrognakCatalogFetcher.fetchCatalog( catalogURL, catalogFile, catalogETagFile );
boolean fetched = URLFetcher.refetchURL( CATALOG_URL, catalogFile, catalogETagFile );
if ( fetched ) reloadCatalog();
}