avatar

949

Support uploading auction HTML if the user's account allows for it. by mrs, 09 Jun, 2009 11:14 AM
Diff this changeset:
MyJBidwatcher.java
mrs 1   package com.jbidwatcher.my;
mrs 2   
mrs 3   import com.jbidwatcher.util.config.JConfig;
mrs 4   import com.jbidwatcher.util.config.ErrorHandler;
mrs 5   import com.jbidwatcher.util.Parameters;
mrs 6   import com.jbidwatcher.util.StringTools;
mrs 7   import com.jbidwatcher.util.Constants;
mrs 8   import com.jbidwatcher.util.ZoneDate;
mrs 9   import com.jbidwatcher.util.html.JHTML;
mrs 10  import com.jbidwatcher.util.queue.MQFactory;
mrs 11  import com.jbidwatcher.util.queue.MessageQueue;
mrs 12  import com.jbidwatcher.util.queue.SuperQueue;
mrs 13  import com.jbidwatcher.util.xml.XMLSerialize;
mrs 14  import com.jbidwatcher.util.xml.XMLElement;
mrs 15  import com.jbidwatcher.util.http.Http;
mrs 16  import com.jbidwatcher.util.http.ClientHttpRequest;
mrs 17  import com.jbidwatcher.auction.AuctionEntry;
mrs 18  import com.jbidwatcher.auction.EntryCorral;
mrs 19  
mrs 20  import java.io.File;
mrs 21  import java.io.InputStream;
mrs 22  import java.io.IOException;
mrs 23  import java.net.URLEncoder;
mrs 24  import java.net.HttpURLConnection;
mrs 25  import java.util.Date;
mrs 26  
mrs 27  /**
mrs 28   * Created by IntelliJ IDEA.
mrs 29   * User: mrs
mrs 30   * Date: Jun 16, 2008
mrs 31   * Time: 11:45:10 PM
mrs 32   *
mrs 33   * A set of methods to communicate with the 'my.jbidwatcher.com' site.
mrs 34   */
mrs 35  public class MyJBidwatcher {
mrs 36    private static MyJBidwatcher sInstance = null;
mrs 37    private Http mNet = null;
mrs 38    private static String LOG_UPLOAD_URL =  "my.jbidwatcher.com/upload/log";
mrs 39    private static String ITEM_UPLOAD_URL = "my.jbidwatcher.com/upload/listing";
mrs 40    private String mSyncQueueURL = null;
mrs 41    private String mReportQueueURL = null;
mrs 42    private boolean mUseSSL = false;
mrs 43    private boolean mUploadHTML = false;
mrs 44    private boolean mUseServerParser = false;
mrs 45    private boolean mGixen = false;
mrs 46    private boolean mReadSnipesFromServer = false;
mrs 47    private ZoneDate mExpiry;
mrs 48  
mrs 49    private String url(String url) {
mrs 50      if(mUseSSL) return "https://" + url;
mrs 51      return "http://" + url;
mrs 52    }
mrs 53  
mrs 54    private Http http() {
mrs 55      if(mNet == null) {
mrs 56        mNet = new Http();
mrs 57      }
mrs 58  
mrs 59      mNet.setAuthInfo(JConfig.queryConfiguration("my.jbidwatcher.id"), JConfig.queryConfiguration("my.jbidwatcher.key"));
mrs 60  
mrs 61      return mNet;
mrs 62    }
mrs 63  
mrs 64    public String sendLogFile(String email, String desc) {
mrs 65      File fp = JConfig.log().closeLog();
mrs 66      return sendFile(fp, url(LOG_UPLOAD_URL), email, desc);
mrs 67    }
mrs 68  
mrs 69    private String createFormSource(String formBase, String email, String desc) {
mrs 70      try {
mrs 71        String parameters = "";
mrs 72        if(email != null && email.length() != 0) {
mrs 73          parameters = "?email=" + URLEncoder.encode(email, "UTF-8");
mrs 74        }
mrs 75        if(desc != null && desc.length() != 0) {
mrs 76          parameters += (parameters.length() == 0) ? '?' : '&';
mrs 77          parameters += "description=" + URLEncoder.encode(desc, "UTF-8");
mrs 78        }
mrs 79        formBase += parameters;
mrs 80      } catch(Exception e) {
mrs 81        formBase += "email=teh%40fail.com&description=Failed+to+encode+description";
mrs 82      }
mrs 83      return formBase;
mrs 84    }
mrs 85  
mrs 86    public String sendFile(File fp, String formBase, String email, String desc) {
mrs 87      String formSource = createFormSource(formBase, email, desc);
mrs 88      if (fp != null) return uploadFile(fp, formSource);
mrs 89      return null;
mrs 90    }
mrs 91  
mrs 92    private String uploadFile(File f, String feedForm) {
mrs 93      String result = null;
mrs 94      try {
mrs 95        StringBuffer sample = http().get(feedForm);
mrs 96        if(sample == null) {
mrs 97          JConfig.log().logDebug("Failed to get S3 upload form from " + feedForm);
mrs 98        } else {
mrs 99          JHTML jh = new JHTML(sample);
mrs 100         JHTML.Form form = jh.getFormWithInput("AWSAccessKeyId");
mrs 101         if (form != null) {
mrs 102           form.delInput("upload");
mrs 103           String url = form.getAction();
mrs 104           ClientHttpRequest chr = new ClientHttpRequest(url);
mrs 105           chr.setParameters(form.getCGIMap());
mrs 106           chr.setParameter("file", f);
mrs 107           HttpURLConnection huc = chr.post();
mrs 108           InputStream resp = http().getStream(huc);
mrs 109           result = StringTools.cat(resp);
mrs 110           JConfig.log().logDebug(result);
mrs 111           resp.close();
mrs 112         }
mrs 113       }
mrs 114     } catch (IOException e) {
mrs 115       JConfig.log().handleDebugException("Trying to upload a file to S3", e);
mrs 116     }
mrs 117     return result;
mrs 118   }
mrs 119 
mrs 120   public String recognizeBidpage(String identifier, StringBuffer page) {
mrs 121     Parameters p = new Parameters();
mrs 122     if(identifier != null) p.put("item", identifier);
mrs 123     p.put("body", page);
mrs 124     String url = url("my.jbidwatcher.com/services/recognize");
mrs 125     return http().postTo(url, p);
mrs 126   }
mrs 127 
mrs 128   public String reportException(String sb) {
mrs 129     Parameters p = new Parameters();
mrs 130     p.put("body", sb);
mrs 131     String url = url("my.jbidwatcher.com/services/report_exception");
mrs 132     return http().postTo(url, p);
mrs 133   }
mrs 134 
mrs 135   public static MyJBidwatcher getInstance() {
mrs 136     if(sInstance == null) sInstance = new MyJBidwatcher();
mrs 137     return sInstance;
mrs 138   }
mrs 139 
mrs 140   private void getSQSURL() {
mrs 141     StringBuffer sb = http().get(url("my.jbidwatcher.com/services/syncq"));
mrs 142     mSyncQueueURL = (sb == null) ? null : sb.toString();
mrs 143     sb = http().get(url("my.jbidwatcher.com/services/reportq"));
mrs 144     mReportQueueURL = (sb == null) ? null : sb.toString();
mrs 145   }
mrs 146 
mrs 147   public void postXML(String queue, XMLSerialize ae) {
mrs 148     XMLElement xmlWrapper = new XMLElement("message");
mrs 149     XMLElement user = new XMLElement("user");
mrs 150     XMLElement access_key = new XMLElement("key");
mrs 151     user.setContents(JConfig.queryConfiguration("my.jbidwatcher.id"));
mrs 152     access_key.setContents(JConfig.queryConfiguration("my.jbidwatcher.key"));
mrs 153     xmlWrapper.addChild(user);
mrs 154     xmlWrapper.addChild(access_key);
mrs 155     xmlWrapper.addChild(ae.toXML());
mrs 156     String aucXML = xmlWrapper.toString();
mrs 157 
mrs 158     if(queue != null) http().putTo(queue, aucXML);
mrs 159   }
mrs 160 
mrs 161   private MyJBidwatcher() {
mrs 162     MQFactory.getConcrete("my").registerListener(new MessageQueue.Listener() {
mrs 163       public void messageAction(Object deQ) {
mrs 164         String cmd = (String)deQ;
mrs 165         if(JConfig.queryConfiguration("my.jbidwatcher.enabled", "false").equals("true")) {
mrs 166           if (cmd.equals("ACCOUNT")) getAccountInfo();
mrs 167         }
mrs 168       }
mrs 169     });
mrs 170 
mrs 171     //  Get the URLs to POST stuff to, and get a new one every 12 hours.
mrs 172     SuperQueue.getInstance().preQueue("ACCOUNT", "my", System.currentTimeMillis(), Constants.ONE_DAY);
mrs 173 
mrs 174     MQFactory.getConcrete("upload").registerListener(new MessageQueue.Listener() {
mrs 175       public void messageAction(Object deQ) {
mrs 176         if(JConfig.queryConfiguration("my.jbidwatcher.id") != null) {
mrs 177           AuctionEntry ae = EntryCorral.getInstance().takeForRead((String) deQ);
mrs 178           postXML(mSyncQueueURL, ae);
mrs 179           if(JConfig.queryConfiguration("my.jbidwatcher.uploadhtml", "false").equals("true")) {
mrs 180             uploadAuctionHTML(ae, "uploadhtml");
mrs 181           }
mrs 182         }
mrs 183       }
mrs 184     });
mrs 185 
mrs 186     if(JConfig.queryConfiguration("my.jbidwatcher.id") != null) {
mrs 187       MQFactory.getConcrete("report").registerListener(new MessageQueue.Listener() {
mrs 188         public void messageAction(Object deQ) {
mrs 189           AuctionEntry ae = EntryCorral.getInstance().takeForRead((String)deQ);
mrs 190           uploadAuctionHTML(ae, "report");
mrs 191         }
mrs 192       });
mrs 193     }
mrs 194 
mrs 195     JConfig.log().addHandler(new ErrorHandler() {
mrs 196       public void close() { /* ignored */ }
mrs 197       public void addLog(String s) { /* ignored */}
mrs 198 
mrs 199       public void exception(String log, String message, String trace) {
mrs 200         if(message == null) message = "(no message)";
mrs 201         if(JConfig.queryConfiguration("my.jbidwatcher.id") != null &&
mrs 202            JConfig.queryConfiguration("logging.remote", "false").equals("true")) {
mrs 203           reportException(log + "\n" + message + "\n" + trace);
mrs 204         }
mrs 205       }
mrs 206     });
mrs 207   }
mrs 208 
mrs 209   private void uploadAuctionHTML(AuctionEntry ae, String uploadType) {
mrs 210     String s3Result = sendFile(ae.getContentFile(), url(ITEM_UPLOAD_URL), JConfig.queryConfiguration("my.jbidwatcher.id"), ae.getLastStatus());
mrs 211     XMLElement root = new XMLElement(uploadType);
mrs 212     XMLElement s3Key = new XMLElement("s3");
mrs 213     s3Key.setContents(s3Result);
mrs 214     root.addChild(ae.toXML());
mrs 215     postXML(mReportQueueURL, root);
mrs 216   }
mrs 217 
mrs 218   public boolean getAccountInfo() {
mrs 219     StringBuffer sb = http().get("https://my.jbidwatcher.com/services/account");
mrs 220     if(sb == null) return false;
mrs 221     XMLElement xml = new XMLElement();
mrs 222     xml.parseString(sb.toString());
mrs 223     XMLElement sync = xml.getChild("syncq");
mrs 224     XMLElement expires = xml.getChild("expiry");
mrs 225     XMLElement listingsRemaining = xml.getChild("listings");
mrs 226     XMLElement categoriesRemaining = xml.getChild("categories");
mrs 227     XMLElement reporting = xml.getChild("reportq");
mrs 228     XMLElement snipesListen = xml.getChild("snipes");
mrs 229     XMLElement ssl = xml.getChild("ssl");
mrs 230     XMLElement uploadHTML = xml.getChild("uploadhtml");
mrs 231     XMLElement serverParser = xml.getChild("parser");
mrs 232     XMLElement gixen = xml.getChild("gixen");
mrs 233 
mrs 234     if(expires != null) {
mrs 235       String date = expires.getContents();
mrs 236       mExpiry = StringTools.figureDate(date, "yyyy-MM-dd'T'HH:mm:ssZ");
mrs 237       if(mExpiry.getDate().before(new Date())) {
mrs 238         JConfig.setConfiguration("my.jbidwatcher.enabled", "false");
mrs 239       } else {
mrs 240         JConfig.setConfiguration("my.jbidwatcher.enabled", "true");
mrs 241       }
mrs 242     }
mrs 243 
mrs 244     mSyncQueueURL = sync == null ? null : sync.getContents();
mrs 245     mReportQueueURL = reporting == null ? null : reporting.getContents();
mrs 246     mUseSSL = getBoolean(ssl);
mrs 247     mReadSnipesFromServer = getBoolean(snipesListen);
mrs 248     mUploadHTML = getBoolean(uploadHTML);
mrs 249     mUseServerParser = getBoolean(serverParser);
mrs 250     mGixen = getBoolean(gixen);
mrs 251 
mrs 252     return mSyncQueueURL != null && mReportQueueURL != null;
mrs 253   }
mrs 254 
mrs 255   private boolean getBoolean(XMLElement x) {
mrs 256     boolean rval = false;
mrs 257     if(x != null) {
mrs 258       String contents = x.getContents();
mrs 259       if(contents != null) {
mrs 260         rval = contents.equals("true");
mrs 261       }
mrs 262     }
mrs 263 
mrs 264     return rval;
mrs 265   }
mrs 266 
mrs 267   public boolean createAccount(String email, String password) {
mrs 268     //  TODO - GET http://my.jbidwatcher.com/users/new
mrs 269     //  POST http://my.jbidwatcher.com/users with user[email]={email}&user[password]={password}&user[password_confirmation]={password}
mrs 270     //  If 200 OK, the body contains the my.jbidwatcher.id
mrs 271     return false;
mrs 272   }
mrs 273 }

Check out the code: svn co jbidwatcher/trunk/src/com/jbidwatcher/my/MyJBidwatcher.java