import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.cxf.common.util.Base64Utility; public class PDF_Alchemy_Download { private static final String ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxxx"; private static final String SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; private static final String HMAC_SHA1="HMACSHA1"; // Downloads the next document from PDF Alchemy. // Returns the documentId on success, or null on failure. private static String downloadNext() { try { // Generate the HMAC-SHA1 signature. SimpleDateFormat dateFormat=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); String date=dateFormat.format(new Date()); String request='/'+ACCESS_KEY_ID+"/outqueue/next"; String clearText="GET\n"+ACCESS_KEY_ID+'\n'+date+'\n'+request; Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(new SecretKeySpec(SECRET_ACCESS_KEY.getBytes(),HMAC_SHA1)); String signature=Base64Utility.encode(mac.doFinal(clearText.getBytes())); // Request the next document from PDF Alchemy. URLConnection conn=(new URL("http://www.pdfalchemy.com"+request)).openConnection(); conn.setRequestProperty("date",date); conn.setRequestProperty("X-PDF-Alchemy-Signature",signature); // Receive the binary data, and write it to file. InputStream inStream=conn.getInputStream(); String filename=conn.getHeaderField("Content-Disposition"); if (filename.indexOf('"')>0) filename=filename.substring(filename.indexOf('"')+1); if (filename.indexOf('"')>0) filename=filename.substring(0,filename.indexOf('"')); FileOutputStream fileStream=new FileOutputStream(filename); byte buffer[]=new byte[10*1024]; int bytesRead; while ((bytesRead=inStream.read(buffer))>=0) fileStream.write(buffer,0,bytesRead); fileStream.close(); inStream.close(); System.out.println("Downloaded '"+filename+"'."); // Return the PDF Alchemy document ID. return conn.getHeaderField("X-PDF-Alchemy-ID"); } catch (Exception e) { System.out.println(e.toString()); } return null; } // Removes documentId from the PDF Alchemy outqueue. private static void deleteDocument(String documentId) { try { // Generate the HMAC-SHA1 signature. SimpleDateFormat dateFormat=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); String date=dateFormat.format(new Date()); String request='/'+ACCESS_KEY_ID+"/outqueue/"+documentId; String clearText="DELETE\n"+ACCESS_KEY_ID+'\n'+date+'\n'+request; Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(new SecretKeySpec(SECRET_ACCESS_KEY.getBytes(),HMAC_SHA1)); String signature=Base64Utility.encode(mac.doFinal(clearText.getBytes())); // Remove the document from PDF Alchemy. HttpURLConnection conn=(HttpURLConnection)((new URL("http://www.pdfalchemy.com"+request)).openConnection()); conn.setRequestMethod("DELETE"); conn.setRequestProperty("date",date); conn.setRequestProperty("X-PDF-Alchemy-Signature",signature); conn.getInputStream().close(); System.out.println("Removed '"+documentId+"' from PDF Alchemy."); } catch (Exception e) { System.out.println(e.toString()); } } public static void main(String[] args) { String documentId; while ((documentId=downloadNext())!=null) deleteDocument(documentId); } }