package net.i2p.router.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.lang.ClassLoader;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import net.i2p.crypto.SHA256Generator;
import net.i2p.data.DataHelper;
import net.i2p.util.FileUtil;
/**
* Dump info on jars and wars
*
* @since 0.8.13
*/
public class FileDumpHelper extends HelperBase {
public String getFileSummary() {
StringBuilder buf = new StringBuilder(16*1024);
buf.append("
").append(f.getAbsolutePath()).append(" | " +
"").append(f.length()).append(" | " +
"");
long mod = f.lastModified();
if (mod > 0)
buf.append((new Date(mod)).toString());
else
buf.append("Not found");
buf.append(" | ");
if (mod > 0 && !FileUtil.verifyZip(f))
buf.append("CORRUPT ");
byte[] hash = sha256(f);
if (hash != null) {
byte[] hh = new byte[16];
System.arraycopy(hash, 0, hh, 0, 16);
buf.append("");
String p1 = DataHelper.toHexString(hh);
for (int i = p1.length(); i < 32; i++) {
buf.append('0');
}
buf.append(p1).append(" ");
System.arraycopy(hash, 16, hh, 0, 16);
buf.append("").append(DataHelper.toHexString(hh)).append("");
}
Attributes att = attributes(f);
if (att == null)
att = new Attributes();
buf.append(" | ");
String iv = getAtt(att, "Implementation-Version");
if (iv != null)
buf.append("").append(iv).append("");
String s = getAtt(att, "Base-Revision");
if (s != null && s.length() > 20) {
if (iv != null)
buf.append(" ");
buf.append("" +
"").append(s.substring(0, 20)).append("" +
" " +
"").append(s.substring(20)).append("");
}
buf.append(" | ");
s = getAtt(att, "Created-By");
if (s != null)
buf.append(s);
buf.append(" | ");
s = getAtt(att, "Build-Date");
if (s != null)
buf.append(s);
buf.append(" | ");
s = getAtt(att, "Built-By");
if (s != null)
buf.append(s);
buf.append(" | ");
s = getAtt(att, "Workspace-Changes");
if (s != null)
buf.append(s.replace(",", " "));
buf.append(" |
\n");
}
private static byte[] sha256(File f) {
InputStream in = null;
try {
in = new FileInputStream(f);
MessageDigest md = SHA256Generator.getDigestInstance();
byte[] b = new byte[4096];
int cnt = 0;
while ((cnt = in.read(b)) >= 0) {
md.update(b, 0, cnt);
}
return md.digest();
} catch (IOException ioe) {
//ioe.printStackTrace();
return null;
} finally {
if (in != null) try { in.close(); } catch (IOException e) {}
}
}
private static Attributes attributes(File f) {
InputStream in = null;
try {
in = (new URL("jar:file:" + f.getAbsolutePath() + "!/META-INF/MANIFEST.MF")).openStream();
Manifest man = new Manifest(in);
return man.getMainAttributes();
} catch (IOException ioe) {
//ioe.printStackTrace();
return null;
} finally {
if (in != null) try { in.close(); } catch (IOException e) {}
}
}
private static String getAtt(Attributes atts, String s) {
String rv = atts.getValue(s);
if (rv != null)
rv = DataHelper.stripHTML(rv);
return rv;
}
}