package zeta.handler.approve;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import javax.servlet.ServletException;
import zeta.ZetaServlet;
import zeta.handler.ApproveHandler;
import zeta.util.Base64;
import zeta.util.DatabaseUtils;
import zeta.util.StreamUtils;
import zeta.util.ThrowableHandler;
public abstract class ApproveBase {
public ApproveBase(ZetaServlet servlet, int userId, String user, String eMail) {
this.servlet = servlet;
this.userId = userId;
this.user = user;
this.eMail = eMail;
}
public abstract String getKey();
public String getValue(String text) {
String sub = getKey() + ':' + servlet.getServerId() + ',' + userId + ',' + user + ',' + eMail + ',';
if (text.startsWith(sub)) {
return text.substring(sub.length(), text.lastIndexOf(','));
}
return null;
}
public abstract String approve(Statement stmt, String value, long timeMillis) throws SQLException, ServletException;
public String generateAddressToApprove(byte[] data) throws SQLException, IOException, ServletException {
byte[] key = new byte[100];
for (int i = 0; i < data.length; i += 100) {
for (int j = 0; j < 100 && i+j < data.length; ++j) {
key[j] ^= data[i+j];
}
}
String code = Base64.encode(key);
String messageKey = getKey() + ':' + servlet.getServerId() + ',' + userId + ',' + user + ',' + eMail + ',' + code + ',' + System.currentTimeMillis();
Connection con = null;
PreparedStatement pStmt = null;
try {
con = servlet.getConnection();
pStmt = con.prepareStatement("SELECT data FROM zeta.approve WHERE server_id=? AND user_id=? AND key=?");
pStmt.setInt(1, servlet.getServerId());
pStmt.setInt(2, userId);
pStmt.setString(3, code);
ResultSet rs = pStmt.executeQuery();
if (rs.next()) {
byte[] data2 = rs.getBytes(1);
rs.close();
if (Arrays.equals(data, data2)) {
return ApproveHandler.generateAddressToApprove(servlet, messageKey);
} else {
pStmt = con.prepareStatement("UPDATE zeta.approve SET approved=requested WHERE server_id=? AND user_id=? AND key=?");
pStmt.setInt(1, servlet.getServerId());
pStmt.setInt(2, userId);
pStmt.setString(3, code);
pStmt.executeUpdate();
}
} else {
rs.close();
}
pStmt = con.prepareStatement("INSERT INTO zeta.approve (server_id,user_id,key,data) VALUES (?,?,?,?)");
pStmt.setInt(1, servlet.getServerId());
pStmt.setInt(2, userId);
pStmt.setString(3, code);
pStmt.setBytes(4, data);
pStmt.executeUpdate();
} finally {
DatabaseUtils.close(pStmt);
DatabaseUtils.close(con);
}
return ApproveHandler.generateAddressToApprove(servlet, messageKey);
}
public static Integer getServerId(String text) {
int idx = text.indexOf(':');
if (idx >= 0) {
int idx2 = text.indexOf(',', idx+1);
if (idx2 > idx) {
return new Integer(text.substring(idx+1, idx2));
}
}
return null;
}
public static Integer getUserId(String text) {
int idx = text.indexOf(':');
if (idx >= 0) {
int idx2 = text.indexOf(',', idx+1);
if (idx2 > idx) {
idx = idx2;
idx2 = text.indexOf(',', idx+1);
if (idx2 > idx) {
return new Integer(text.substring(idx+1, idx2));
}
}
}
return null;
}
public static long getTimeMillis(String text) {
int idx = text.lastIndexOf(',');
return (idx >= 0)? Long.parseLong(text.substring(idx+1)) : 0;
}
public static void main(String[] args) {
if (args.length < 1 || args[0].equals("?")) {
System.out.println("USAGE: <start timestamp>");
return;
}
Connection con = null;
PreparedStatement pStmt = null;
try {
DateFormat timeFormatter = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
con = zeta.tool.GetData.getConnection();
pStmt = con.prepareStatement("SELECT server_id,user_id,requested,approved,data FROM zeta.approve WHERE requested>?");
pStmt.setTimestamp(1, Timestamp.valueOf(args[0]));
ResultSet rs = pStmt.executeQuery();
while (rs.next()) {
FileOutputStream out = null;
try {
int serverId = rs.getInt(1);
int userId = rs.getInt(2);
Timestamp requested = rs.getTimestamp(3);
Timestamp approved = rs.getTimestamp(4);
String filename = (approved == null)? "requested_" + serverId + '_' + userId + '_' + timeFormatter.format(requested) + ".zip" : "approved_" + serverId + '_' + userId + '_' + timeFormatter.format(requested) + '_' + timeFormatter.format(requested) + ".zip";
out = new FileOutputStream(filename);
StreamUtils.writeData(new ByteArrayInputStream(rs.getBytes(5)), out, false, false);
System.out.println(filename);
} catch (IOException ioe) {
ThrowableHandler.handle(ioe);
} finally {
StreamUtils.close(out);
}
}
rs.close();
} catch (Exception e) {
ThrowableHandler.handle(e);
} finally {
DatabaseUtils.close(pStmt);
DatabaseUtils.close(con);
}
}
protected ZetaServlet servlet;
protected int userId;
protected String user;
protected String eMail;
}