avatar

923

Major fixups to use S3 as the transmission medium for logs and listing source, and Amazon SQS for sync-to-My JBidwatcher and reporting (in conjunction with S3!).

by mrs, 24 May, 2009 09:48 AM
893 923  
44 import com.jbidwatcher.util.config.ErrorHandler;
55 import com.jbidwatcher.util.Parameters;
66 import com.jbidwatcher.util.StringTools;
7 import com.jbidwatcher.util.Constants;
78 import com.jbidwatcher.util.html.JHTML;
89 import com.jbidwatcher.util.queue.MQFactory;
910 import com.jbidwatcher.util.queue.MessageQueue;
11 import com.jbidwatcher.util.queue.SuperQueue;
1012 import com.jbidwatcher.util.xml.XMLSerialize;
1113 import com.jbidwatcher.util.xml.XMLElement;
1214 import com.jbidwatcher.util.http.Http;
------
1719 import java.io.File;
1820 import java.io.InputStream;
1921 import java.io.IOException;
20 import java.net.URL;
2122 import java.net.URLEncoder;
23 import java.net.HttpURLConnection;
2224 
2325 /**
2426  * Created by IntelliJ IDEA.
------
3032  */
3133 public class MyJBidwatcher {
3234   private static MyJBidwatcher sInstance = null;
33   private static final String LOG_UPLOAD_URL = "http://my.jbidwatcher.com/upload/log";
35   private Http mNet = null;
36   private static final String LOG_UPLOAD_URL =  "http://my.jbidwatcher.com/upload/log";
37   private static final String ITEM_UPLOAD_URL = "http://my.jbidwatcher.com/upload/listing";
38   private String mSyncQueueURL = null;
39   private String mReportQueueURL = null;
3440 
35   public boolean sendLogFile(String email, String desc) {
41   private Http http() {
42     if(mNet == null) {
43       mNet = new Http();
44     }
45 
46     mNet.setAuthInfo(JConfig.queryConfiguration("my.jbidwatcher.id"), JConfig.queryConfiguration("my.jbidwatcher.key"));
47 
48     return mNet;
49   }
50 
51   public String sendLogFile(String email, String desc) {
3652     File fp = JConfig.log().closeLog();
37     return sendFile(fp, email, desc);
53     return sendFile(fp, LOG_UPLOAD_URL, email, desc);
3854   }
3955 
40   private String createFormSource(String email, String desc) {
41     String formSource = LOG_UPLOAD_URL;
56   private String createFormSource(String formBase, String email, String desc) {
4257     try {
4358       String parameters = "";
4459       if(email != null && email.length() != 0) {
------
4863         parameters += (parameters.length() == 0) ? '?' : '&';
4964         parameters += "description=" + URLEncoder.encode(desc, "UTF-8");
5065       }
51       formSource += parameters;
66       formBase += parameters;
5267     } catch(Exception e) {
53       formSource += "email=teh%40fail.com&description=Failed+to+encode+description";
68       formBase += "email=teh%40fail.com&description=Failed+to+encode+description";
5469     }
55     return formSource;
70     return formBase;
5671   }
5772 
58   public boolean sendFile(File fp, String email, String desc) {
59     String formSource = createFormSource(email, desc);
73   public String sendFile(File fp, String formBase, String email, String desc) {
74     String formSource = createFormSource(formBase, email, desc);
6075     if (fp != null) return uploadFile(fp, formSource);
61     return false;
76     return null;
6277   }
6378 
64   private boolean uploadFile(File f, String feedForm) {
79   private String uploadFile(File f, String feedForm) {
80     String result = null;
6581     try {
66       String sample = StringTools.cat(new URL(feedForm));
67       JHTML jh = new JHTML(new StringBuffer(sample));
68       JHTML.Form form = jh.getFormWithInput("AWSAccessKeyId");
69       if (form != null) {
70         form.delInput("upload");
71         String url = form.getAction();
72         ClientHttpRequest chr = new ClientHttpRequest(url);
73         chr.setParameters(form.getCGIMap());
74         chr.setParameter("file", f);
75         InputStream resp = chr.post();
76         String result = StringTools.cat(resp);
77         System.out.println(result);
78         resp.close();
82       StringBuffer sample = http().get(feedForm);
83       if(sample == null) {
84         JConfig.log().logDebug("Failed to get S3 upload form from " + feedForm);
85       } else {
86         JHTML jh = new JHTML(sample);
87         JHTML.Form form = jh.getFormWithInput("AWSAccessKeyId");
88         if (form != null) {
89           form.delInput("upload");
90           String url = form.getAction();
91           ClientHttpRequest chr = new ClientHttpRequest(url);
92           chr.setParameters(form.getCGIMap());
93           chr.setParameter("file", f);
94           HttpURLConnection huc = chr.post();
95           InputStream resp = http().getStream(huc);
96           result = StringTools.cat(resp);
97           JConfig.log().logDebug(result);
98           resp.close();
99         }
79100       }
80101     } catch (IOException e) {
81       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
82       return false;
102       JConfig.log().handleDebugException("Trying to upload a file to S3", e);
83103     }
84     return true;
104     return result;
85105   }
86106 
87107   public String recognizeBidpage(String identifier, StringBuffer page) {
------
90110     p.put("user", JConfig.queryConfiguration("my.jbidwatcher.id"));
91111     p.put("body", page);
92112     String url = "http://my.jbidwatcher.com/advanced/recognize";
93     return Http.postTo(url, p);
113     return http().postTo(url, p);
94114   }
95115 
96116   public String reportException(String sb) {
------
98118     p.put("user", JConfig.queryConfiguration("my.jbidwatcher.id"));
99119     p.put("body", sb);
100120     String url = "http://my.jbidwatcher.com/advanced/report";
101     return Http.postTo(url, p);
121     return http().postTo(url, p);
102122   }
103123 
104124   public static MyJBidwatcher getInstance() {
------
106126     return sInstance;
107127   }
108128 
109   public void postAuction(XMLSerialize ae) {
110     Parameters p = new Parameters();
111     postAuction(ae, "auctions/import", p);
129   private void getSQSURL() {
130     StringBuffer sb = http().get("http://my.jbidwatcher.com/services/syncq");
131     mSyncQueueURL = (sb == null) ? null : sb.toString();
132     sb = http().get("http://my.jbidwatcher.com/services/reportq");
133     mReportQueueURL = (sb == null) ? null : sb.toString();
112134   }
113135 
114   public void postAuction(XMLSerialize ae, String myPath, Parameters p) {
115     p.put("user", JConfig.queryConfiguration("my.jbidwatcher.id"));
116     p.put("auction_data", ae.toXML().toString());
117     Http.postTo("http://my.jbidwatcher.com/" + myPath, p);
136   public void postXML(String queue, XMLSerialize ae) {
137     XMLElement xmlWrapper = new XMLElement("message");
138     XMLElement user = new XMLElement("user");
139     XMLElement access_key = new XMLElement("key");
140     user.setContents(JConfig.queryConfiguration("my.jbidwatcher.id"));
141     access_key.setContents(JConfig.queryConfiguration("my.jbidwatcher.key"));
142     xmlWrapper.addChild(user);
143     xmlWrapper.addChild(access_key);
144     xmlWrapper.addChild(ae.toXML());
145     String aucXML = xmlWrapper.toString();
146 
147     if(queue != null) http().putTo(queue, aucXML);
118148   }
119149 
120150   private MyJBidwatcher() {
151     MQFactory.getConcrete("my").registerListener(new MessageQueue.Listener() {
152       public void messageAction(Object deQ) {
153         String cmd = (String)deQ;
154         if(JConfig.queryConfiguration("my.jbidwatcher.enabled", "false").equals("true")) {
155           if (cmd.equals("GETURLS")) getSQSURL();
156         }
157       }
158     });
159 
160     //  Get the URLs to POST stuff to, and get a new one every 12 hours.
161     SuperQueue.getInstance().preQueue("GETURLS", "my", System.currentTimeMillis(), 12 * Constants.ONE_HOUR);
162 
121163     MQFactory.getConcrete("upload").registerListener(new MessageQueue.Listener() {
122164       public void messageAction(Object deQ) {
123165         if(JConfig.queryConfiguration("my.jbidwatcher.id") != null) {
124           postAuction(EntryCorral.getInstance().takeForRead(deQ.toString()));
166           postXML(mSyncQueueURL, EntryCorral.getInstance().takeForRead(deQ.toString()));
125167         }
126168       }
127169     });
------
130172       MQFactory.getConcrete("report").registerListener(new MessageQueue.Listener() {
131173         public void messageAction(Object deQ) {
132174           AuctionEntry ae = EntryCorral.getInstance().takeForRead((String)deQ);
175           String s3Result = sendFile(ae.getContentFile(), ITEM_UPLOAD_URL, JConfig.queryConfiguration("my.jbidwatcher.id"), ae.getLastStatus());
133176           XMLElement root = new XMLElement("report");
134           XMLElement body = new XMLElement("body");
135           body.setContents(ae.getContent().toString());
177           XMLElement s3Key = new XMLElement("s3");
178           s3Key.setContents(s3Result);
136179           root.addChild(ae.toXML());
137           root.addChild(body);
138           Parameters p = new Parameters();
139           p.put("user_comment", ae.getLastStatus());
140           postAuction(root, "report/problem", p);
180           postXML(mReportQueueURL, root);
141181         }
142182       });
143183     }