From cc270d577ac30ff93d3b6ba942f96a4cfd4c7c3d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 4 Sep 2010 15:12:17 +0100 Subject: [PATCH] proper error reporting of post requests incl. to yarrg ClientHttpRequest.java: * .post() returns a boolean saying whether the submission was successful; you have to call .resultstream() to get the stream. MarketUploader.java: * Factor out StringBuild and repeated readLine into new function readstreamstring. * Report http errors occurring in sendInitialData (part of PCTB upload) and have it return "null" in that case. * New helper function post_for_yarrg that does the post and puts up a dialogue box if it's not right. Use it in the two relevant places. --- .../tedpearson/ypp/market/MarketUploader.java | 54 ++++++++++++------- src/net/chiark/yarrg/ClientHttpRequest.java | 43 ++++++++------- 2 files changed, 60 insertions(+), 37 deletions(-) diff --git a/src/com/tedpearson/ypp/market/MarketUploader.java b/src/com/tedpearson/ypp/market/MarketUploader.java index 526fc25..666ad23 100644 --- a/src/com/tedpearson/ypp/market/MarketUploader.java +++ b/src/com/tedpearson/ypp/market/MarketUploader.java @@ -400,7 +400,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis ArrayList> data = getData(t); - if (uploadToYarrg) { + if (uploadToYarrg && yarrgts != null) { pm.setNote("Preparing data for Yarrg"); pm.setProgress(10); @@ -470,6 +470,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis writeBuySellOffers(buys,sells,offerCount,out); out.finish(); InputStream in = sendInitialData(new ByteArrayInputStream(outStream.toByteArray())); + if (in == null) return; pm.setProgress(80); if(pm.isCanceled()) { return; @@ -804,6 +805,16 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis writeOffers(out,sells); } + private String readstreamstring(InputStream in) throws IOException { + StringBuilder sb = new StringBuilder(); + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + String str; + while((str = br.readLine()) != null) { + sb.append(str+"\n"); + } + return sb.toString(); + } + /** * Sends the data to the server via multipart-formdata POST, * with the gzipped data as a file upload. @@ -813,7 +824,12 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis private InputStream sendInitialData(InputStream file) throws IOException { ClientHttpRequest http = new ClientHttpRequest(PCTB_HOST_URL + "upload.php"); http.setParameter("marketdata","marketdata.gz",file,"application/gzip"); - return http.post(); + if (!http.post()) { + String err = readstreamstring(http.resultstream()); + error("Error sending initial data:\n"+err); + return null; + } + return http.resultstream(); } /** @@ -837,13 +853,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis * @param in stream of data from the server to read */ private void finishUpload(InputStream in) throws IOException { - StringBuilder sb = new StringBuilder(); - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - String str; - while((str = br.readLine()) != null) { - sb.append(str+"\n"); - } - String html = sb.toString(); + String html = readstreamstring(in); //System.out.println(html); String topIsland = "0", ocean, islandNum, action, forceReload, filename; Matcher m; @@ -924,28 +934,33 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis filename = m.group(3); URL get = new URL(PCTB_HOST_URL + "upload.php?topisland=" + topIsland + "&ocean=" + ocean + "&island=" + islandNum + "&action=" + action + "&forcereload=" + forceReload + "&filename=" + filename); - // System.out.println(get); - BufferedReader br2 = new BufferedReader(new InputStreamReader(get.openStream())); - sb = new StringBuilder(); - while((str = br2.readLine()) != null) { - sb.append(str+"\n"); - } + String complete = readstreamstring(get.openStream()); Pattern done = Pattern.compile("Your data has been integrated into the database. Thank you!"); - m = done.matcher(sb.toString()); + m = done.matcher(complete); if(m.find()) { //System.out.println("FILE upload successful!!!"); } else { - error_html("Something was wrong with the final upload parameters!", html); + error_html("Something was wrong with the final upload parameters!", complete); } } + private InputStream post_for_yarrg(ClientHttpRequest http) throws IOException { + if (!http.post()) { + String err = readstreamstring(http.resultstream()); + error("

Error reported by YARRG server

\n" + err); + return null; + } + return http.resultstream(); + } + private String getYarrgTimestamp() throws IOException { ClientHttpRequest http = new ClientHttpRequest (YARRG_URL); http.setParameter("clientname", YARRG_CLIENTNAME); http.setParameter("clientversion", YARRG_CLIENTVERSION); http.setParameter("clientfixes", YARRG_CLIENTFIXES); http.setParameter("requesttimestamp", "y"); - InputStream in = http.post(); + InputStream in = post_for_yarrg(http); + if (in == null) return null; BufferedReader br = new BufferedReader(new InputStreamReader(in)); String tsresult = br.readLine(); return tsresult.substring(3, tsresult.length()-1); @@ -966,7 +981,8 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis http.setParameter("ocean", ocean); http.setParameter("island", island); http.setParameter("data", "deduped.tsv.gz", file, "application/octet-stream"); - InputStream in = http.post(); + InputStream in = post_for_yarrg(http); + if (in == null) return; BufferedReader br = new BufferedReader(new InputStreamReader(in)); String yarrgresult; while((yarrgresult = br.readLine()) != null) { diff --git a/src/net/chiark/yarrg/ClientHttpRequest.java b/src/net/chiark/yarrg/ClientHttpRequest.java index 9a3f877..ff4d942 100644 --- a/src/net/chiark/yarrg/ClientHttpRequest.java +++ b/src/net/chiark/yarrg/ClientHttpRequest.java @@ -31,6 +31,7 @@ import java.util.Iterator; public class ClientHttpRequest { URLConnection connection; OutputStream os = null; + InputStream tis = null; Map cookies = new HashMap(); protected void connect() throws IOException { @@ -277,16 +278,22 @@ public class ClientHttpRequest { * @return input stream with the server response * @throws IOException */ - public InputStream post() throws IOException { + public boolean post() throws IOException { boundary(); writeln("--"); os.close(); - InputStream tis; + boolean ok; try { tis = connection.getInputStream(); + ok = true; } catch (IOException e) { tis = ((java.net.HttpURLConnection) connection).getErrorStream(); + ok = false; } + return ok; + } + + public InputStream resultstream() { return tis; } @@ -297,7 +304,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameters */ - public InputStream post(Map parameters) throws IOException { + public boolean post(Map parameters) throws IOException { setParameters(parameters); return post(); } @@ -309,7 +316,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameters */ - public InputStream post(Object[] parameters) throws IOException { + public boolean post(Object[] parameters) throws IOException { setParameters(parameters); return post(); } @@ -323,7 +330,7 @@ public class ClientHttpRequest { * @see setParameters * @see setCookies */ - public InputStream post(Map cookies, Map parameters) throws IOException { + public boolean post(Map cookies, Map parameters) throws IOException { setCookies(cookies); setParameters(parameters); return post(); @@ -338,7 +345,7 @@ public class ClientHttpRequest { * @see setParameters * @see setCookies */ - public InputStream post(String[] cookies, Object[] parameters) throws IOException { + public boolean post(String[] cookies, Object[] parameters) throws IOException { setCookies(cookies); setParameters(parameters); return post(); @@ -352,7 +359,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public InputStream post(String name, Object value) throws IOException { + public boolean post(String name, Object value) throws IOException { setParameter(name, value); return post(); } @@ -367,7 +374,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException { + public boolean post(String name1, Object value1, String name2, Object value2) throws IOException { setParameter(name1, value1); return post(name2, value2); } @@ -384,7 +391,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException { + public boolean post(String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException { setParameter(name1, value1); return post(name2, value2, name3, value3); } @@ -403,7 +410,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException { + public boolean post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException { setParameter(name1, value1); return post(name2, value2, name3, value3, name4, value4); } @@ -415,7 +422,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameters */ - public static InputStream post(URL url, Map parameters) throws IOException { + public static boolean post(URL url, Map parameters) throws IOException { return new ClientHttpRequest(url).post(parameters); } @@ -426,7 +433,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameters */ - public static InputStream post(URL url, Object[] parameters) throws IOException { + public static boolean post(URL url, Object[] parameters) throws IOException { return new ClientHttpRequest(url).post(parameters); } @@ -439,7 +446,7 @@ public class ClientHttpRequest { * @see setCookies * @see setParameters */ - public static InputStream post(URL url, Map cookies, Map parameters) throws IOException { + public static boolean post(URL url, Map cookies, Map parameters) throws IOException { return new ClientHttpRequest(url).post(cookies, parameters); } @@ -452,7 +459,7 @@ public class ClientHttpRequest { * @see setCookies * @see setParameters */ - public static InputStream post(URL url, String[] cookies, Object[] parameters) throws IOException { + public static boolean post(URL url, String[] cookies, Object[] parameters) throws IOException { return new ClientHttpRequest(url).post(cookies, parameters); } @@ -464,7 +471,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public static InputStream post(URL url, String name1, Object value1) throws IOException { + public static boolean post(URL url, String name1, Object value1) throws IOException { return new ClientHttpRequest(url).post(name1, value1); } @@ -478,7 +485,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public static InputStream post(URL url, String name1, Object value1, String name2, Object value2) throws IOException { + public static boolean post(URL url, String name1, Object value1, String name2, Object value2) throws IOException { return new ClientHttpRequest(url).post(name1, value1, name2, value2); } @@ -494,7 +501,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException { + public static boolean post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException { return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3); } @@ -512,7 +519,7 @@ public class ClientHttpRequest { * @throws IOException * @see setParameter */ - public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException { + public static boolean post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException { return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3, name4, value4); } } -- 2.30.2