updates for 0.9.4
[debian/openrocket] / src / net / sf / openrocket / communication / BugReporter.java
diff --git a/src/net/sf/openrocket/communication/BugReporter.java b/src/net/sf/openrocket/communication/BugReporter.java
new file mode 100644 (file)
index 0000000..048701a
--- /dev/null
@@ -0,0 +1,58 @@
+package net.sf.openrocket.communication;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+
+import net.sf.openrocket.util.Prefs;
+
+public class BugReporter extends Communicator {
+       
+       // Inhibit instantiation
+       private BugReporter() {
+       }
+
+       
+       /**
+        * Send the provided report to the OpenRocket bug report URL.  If the connection
+        * fails or the server does not respond with the correct response code, an
+        * exception is thrown.
+        * 
+        * @param report                the report to send.
+        * @throws IOException  if an error occurs while connecting to the server or
+        *                                              the server responds with a wrong response code.
+        */
+       public static void sendBugReport(String report) throws IOException {
+               
+               HttpURLConnection connection = connectionSource.getConnection(BUG_REPORT_URL);
+               
+               connection.setConnectTimeout(CONNECTION_TIMEOUT);
+               connection.setInstanceFollowRedirects(true);
+               connection.setRequestMethod("POST");
+               connection.setUseCaches(false);
+               connection.setRequestProperty("X-OpenRocket-Version", encode(Prefs.getVersion()));
+               
+               String post;
+               post = (VERSION_PARAM + "=" + encode(Prefs.getVersion())
+                               + "&" + BUG_REPORT_PARAM + "=" + encode(report));
+               
+               OutputStreamWriter wr = null;
+               try {
+                       // Send post information
+                       connection.setDoOutput(true);
+                       wr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
+                       wr.write(post);
+                       wr.flush();
+                       
+                       if (connection.getResponseCode() != BUG_REPORT_RESPONSE_CODE) {
+                               throw new IOException("Server responded with code " + 
+                                               connection.getResponseCode() + ", expecting " + BUG_REPORT_RESPONSE_CODE);
+                       }
+               } finally {
+                       if (wr != null)
+                               wr.close();
+                       connection.disconnect();
+               }
+       }
+       
+}