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!).
| 893 | 923 | |
|---|---|---|
| 4 | 4 | import com.jbidwatcher.util.config.ErrorHandler; |
| 5 | 5 | import com.jbidwatcher.util.Parameters; |
| 6 | 6 | import com.jbidwatcher.util.StringTools; |
| 7 | import com.jbidwatcher.util.Constants; | |
| 7 | 8 | import com.jbidwatcher.util.html.JHTML; |
| 8 | 9 | import com.jbidwatcher.util.queue.MQFactory; |
| 9 | 10 | import com.jbidwatcher.util.queue.MessageQueue; |
| 11 | import com.jbidwatcher.util.queue.SuperQueue; | |
| 10 | 12 | import com.jbidwatcher.util.xml.XMLSerialize; |
| 11 | 13 | import com.jbidwatcher.util.xml.XMLElement; |
| 12 | 14 | import com.jbidwatcher.util.http.Http; |
| --- | --- | |
| 17 | 19 | import java.io.File; |
| 18 | 20 | import java.io.InputStream; |
| 19 | 21 | import java.io.IOException; |
| 20 | import java.net.URL; | |
| 21 | 22 | import java.net.URLEncoder; |
| 23 | import java.net.HttpURLConnection; | |
| 22 | 24 | |
| 23 | 25 | /** |
| 24 | 26 | * Created by IntelliJ IDEA. |
| --- | --- | |
| 30 | 32 | */ |
| 31 | 33 | public class MyJBidwatcher { |
| 32 | 34 | 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; | |
| 34 | 40 | |
| 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) { | |
| 36 | 52 | File fp = JConfig.log().closeLog(); |
| 37 | return sendFile(fp, email, desc); | |
| 53 | return sendFile(fp, LOG_UPLOAD_URL, email, desc); | |
| 38 | 54 | } |
| 39 | 55 | |
| 40 | private String createFormSource(String email, String desc) { | |
| 41 | String formSource = LOG_UPLOAD_URL; | |
| 56 | private String createFormSource(String formBase, String email, String desc) { | |
| 42 | 57 | try { |
| 43 | 58 | String parameters = ""; |
| 44 | 59 | if(email != null && email.length() != 0) { |
| --- | --- | |
| 48 | 63 | parameters += (parameters.length() == 0) ? '?' : '&'; |
| 49 | 64 | parameters += "description=" + URLEncoder.encode(desc, "UTF-8"); |
| 50 | 65 | } |
| 51 | formSource += parameters; | |
| 66 | formBase += parameters; | |
| 52 | 67 | } 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"; | |
| 54 | 69 | } |
| 55 | return formSource; | |
| 70 | return formBase; | |
| 56 | 71 | } |
| 57 | 72 | |
| 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); | |
| 60 | 75 | if (fp != null) return uploadFile(fp, formSource); |
| 61 | return false; | |
| 76 | return null; | |
| 62 | 77 | } |
| 63 | 78 | |
| 64 | private boolean uploadFile(File f, String feedForm) { | |
| 79 | private String uploadFile(File f, String feedForm) { | |
| 80 | String result = null; | |
| 65 | 81 | 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 | } | |
| 79 | 100 | } |
| 80 | 101 | } 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); | |
| 83 | 103 | } |
| 84 | return true; | |
| 104 | return result; | |
| 85 | 105 | } |
| 86 | 106 | |
| 87 | 107 | public String recognizeBidpage(String identifier, StringBuffer page) { |
| --- | --- | |
| 90 | 110 | p.put("user", JConfig.queryConfiguration("my.jbidwatcher.id")); |
| 91 | 111 | p.put("body", page); |
| 92 | 112 | String url = "http://my.jbidwatcher.com/advanced/recognize"; |
| 93 | return Http.postTo(url, p); | |
| 113 | return http().postTo(url, p); | |
| 94 | 114 | } |
| 95 | 115 | |
| 96 | 116 | public String reportException(String sb) { |
| --- | --- | |
| 98 | 118 | p.put("user", JConfig.queryConfiguration("my.jbidwatcher.id")); |
| 99 | 119 | p.put("body", sb); |
| 100 | 120 | String url = "http://my.jbidwatcher.com/advanced/report"; |
| 101 | return Http.postTo(url, p); | |
| 121 | return http().postTo(url, p); | |
| 102 | 122 | } |
| 103 | 123 | |
| 104 | 124 | public static MyJBidwatcher getInstance() { |
| --- | --- | |
| 106 | 126 | return sInstance; |
| 107 | 127 | } |
| 108 | 128 | |
| 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(); | |
| 112 | 134 | } |
| 113 | 135 | |
| 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); | |
| 118 | 148 | } |
| 119 | 149 | |
| 120 | 150 | 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 | ||
| 121 | 163 | MQFactory.getConcrete("upload").registerListener(new MessageQueue.Listener() { |
| 122 | 164 | public void messageAction(Object deQ) { |
| 123 | 165 | if(JConfig.queryConfiguration("my.jbidwatcher.id") != null) { |
| 124 | postAuction(EntryCorral.getInstance().takeForRead(deQ.toString())); | |
| 166 | postXML(mSyncQueueURL, EntryCorral.getInstance().takeForRead(deQ.toString())); | |
| 125 | 167 | } |
| 126 | 168 | } |
| 127 | 169 | }); |
| --- | --- | |
| 130 | 172 | MQFactory.getConcrete("report").registerListener(new MessageQueue.Listener() { |
| 131 | 173 | public void messageAction(Object deQ) { |
| 132 | 174 | AuctionEntry ae = EntryCorral.getInstance().takeForRead((String)deQ); |
| 175 | String s3Result = sendFile(ae.getContentFile(), ITEM_UPLOAD_URL, JConfig.queryConfiguration("my.jbidwatcher.id"), ae.getLastStatus()); | |
| 133 | 176 | 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); | |
| 136 | 179 | 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); | |
| 141 | 181 | } |
| 142 | 182 | }); |
| 143 | 183 | } |
