first commit
This commit is contained in:
parent
352e1653f8
commit
16a197e856
44 changed files with 5942 additions and 3 deletions
|
@ -0,0 +1,142 @@
|
|||
package net.vhati.modmanager.json;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
public class GrognakCatalogFetcher {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(GrognakCatalogFetcher.class);
|
||||
|
||||
public static final String CATALOG_URL = "https://raw.github.com/Grognak/Grognaks-Mod-Manager/master/backup/current_catalog.json";
|
||||
|
||||
|
||||
/**
|
||||
* Downloads the latest mod catalog.
|
||||
*
|
||||
* @return true if the catalog successfully downloaded, false otherwise
|
||||
*/
|
||||
public static boolean fetchCatalog( String catalogURL, File catalogFile, File eTagFile ) {
|
||||
String localETag = null;
|
||||
|
||||
log.debug( "Attempting to download a newer catalog..." );
|
||||
if ( eTagFile.exists() ) {
|
||||
// Load the old eTag.
|
||||
InputStream etagIn = null;
|
||||
try {
|
||||
etagIn = new FileInputStream( eTagFile );
|
||||
BufferedReader br = new BufferedReader( new InputStreamReader( etagIn, "UTF-8" ) );
|
||||
String line = br.readLine();
|
||||
if ( line.length() > 0 )
|
||||
localETag = line;
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
// Not serious enough to be a real error.
|
||||
log.debug( String.format( "Error reading catalog eTag from \"%s\".", eTagFile.getName() ), e );
|
||||
}
|
||||
finally {
|
||||
try {if ( etagIn != null ) etagIn.close();}
|
||||
catch ( IOException e ) {}
|
||||
}
|
||||
}
|
||||
|
||||
String remoteETag = null;
|
||||
InputStream urlIn = null;
|
||||
OutputStream catalogOut = null;
|
||||
try {
|
||||
URL url = new URL( catalogURL );
|
||||
URLConnection conn = url.openConnection();
|
||||
|
||||
if ( conn instanceof HttpURLConnection == false ) {
|
||||
log.error( String.format( "Non-Http(s) URL given for catalog fetching: %s", catalogURL ) );
|
||||
return false;
|
||||
}
|
||||
HttpURLConnection httpConn = (HttpURLConnection)conn;
|
||||
|
||||
httpConn.setReadTimeout( 10000 );
|
||||
if ( localETag != null )
|
||||
httpConn.setRequestProperty( "If-None-Match", localETag );
|
||||
httpConn.connect();
|
||||
|
||||
int responseCode = httpConn.getResponseCode();
|
||||
|
||||
if ( responseCode == HttpURLConnection.HTTP_NOT_MODIFIED ) {
|
||||
log.debug( "The server's catalog has not been modified since the previous check." );
|
||||
|
||||
// Update the catalog file's timestamp as if it had downloaded.
|
||||
catalogFile.setLastModified( new Date().getTime() );
|
||||
|
||||
return false;
|
||||
}
|
||||
else if ( responseCode == HttpURLConnection.HTTP_OK ) {
|
||||
Map<String, List<String>> headerMap = httpConn.getHeaderFields();
|
||||
List<String> eTagValues = headerMap.get( "ETag" );
|
||||
if ( eTagValues != null && eTagValues.size() > 0 )
|
||||
remoteETag = eTagValues.get( 0 );
|
||||
|
||||
urlIn = httpConn.getInputStream();
|
||||
catalogOut = new FileOutputStream( catalogFile );
|
||||
byte[] buf = new byte[4096];
|
||||
int len;
|
||||
while ( (len = urlIn.read(buf)) >= 0 ) {
|
||||
catalogOut.write( buf, 0, len );
|
||||
}
|
||||
}
|
||||
else {
|
||||
log.error( String.format( "Catalog 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 );
|
||||
}
|
||||
finally {
|
||||
try {if ( urlIn != null ) urlIn.close();}
|
||||
catch ( IOException e ) {}
|
||||
|
||||
try {if ( catalogOut != null ) catalogOut.close();}
|
||||
catch ( IOException e ) {}
|
||||
}
|
||||
|
||||
if ( remoteETag != null ) {
|
||||
// Save the new eTag.
|
||||
OutputStream etagOut = null;
|
||||
try {
|
||||
etagOut = new FileOutputStream( eTagFile );
|
||||
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( etagOut, "UTF-8" ) );
|
||||
bw.append( remoteETag );
|
||||
bw.flush();
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
log.error( String.format( "Error writing catalog eTag to \"%s\".", eTagFile.getName() ), e );
|
||||
}
|
||||
finally {
|
||||
try {if ( etagOut != null ) etagOut.close();}
|
||||
catch ( IOException e ) {}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package net.vhati.modmanager.json;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.vhati.modmanager.core.ModDB;
|
||||
import net.vhati.modmanager.core.ModInfo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
public class JacksonGrognakCatalogReader {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(JacksonGrognakCatalogReader.class);
|
||||
|
||||
|
||||
public static ModDB parse( File jsonFile ) {
|
||||
ModDB modDB = new ModDB();
|
||||
|
||||
Exception exception = null;
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
|
||||
|
||||
JsonNode rootNode = mapper.readTree(jsonFile);
|
||||
JsonNode catalogsNode = rootNode.get("catalog_versions");
|
||||
JsonNode catalogNode = catalogsNode.get("1");
|
||||
|
||||
for ( JsonNode infoNode : catalogNode ) {
|
||||
String threadURL = infoNode.get("url").textValue();
|
||||
String threadHash = infoNode.get("thread_hash").textValue();
|
||||
if ( !threadURL.equals("???") && !threadHash.equals("???") )
|
||||
modDB.putThreadHash( threadURL, threadHash );
|
||||
|
||||
JsonNode versionsNode = infoNode.get("versions");
|
||||
for ( JsonNode versionNode : versionsNode ) {
|
||||
ModInfo modInfo = new ModInfo();
|
||||
modInfo.setTitle( infoNode.get("title").textValue() );
|
||||
modInfo.setAuthor( infoNode.get("author").textValue() );
|
||||
modInfo.setURL( infoNode.get("url").textValue() );
|
||||
modInfo.setDescription( infoNode.get("desc").textValue() );
|
||||
modInfo.setFileHash( versionNode.get("hash").textValue() );
|
||||
modInfo.setVersion( versionNode.get("version").textValue() );
|
||||
modDB.addMod( modInfo );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch ( JsonProcessingException e ) {
|
||||
exception = e;
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
exception = e;
|
||||
}
|
||||
if ( exception != null ) {
|
||||
log.error( exception );
|
||||
return null;
|
||||
}
|
||||
|
||||
return modDB;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue