create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / communication / BugReporter.java
1 package net.sf.openrocket.communication;
2
3 import java.io.IOException;
4 import java.io.OutputStreamWriter;
5 import java.net.HttpURLConnection;
6
7 import net.sf.openrocket.util.BuildProperties;
8
9 public class BugReporter extends Communicator {
10         
11         // Inhibit instantiation
12         private BugReporter() {
13         }
14
15         
16         /**
17          * Send the provided report to the OpenRocket bug report URL.  If the connection
18          * fails or the server does not respond with the correct response code, an
19          * exception is thrown.
20          * 
21          * @param report                the report to send.
22          * @throws IOException  if an error occurs while connecting to the server or
23          *                                              the server responds with a wrong response code.
24          */
25         public static void sendBugReport(String report) throws IOException {
26                 
27                 HttpURLConnection connection = connectionSource.getConnection(BUG_REPORT_URL);
28                 
29                 connection.setConnectTimeout(CONNECTION_TIMEOUT);
30                 connection.setInstanceFollowRedirects(true);
31                 connection.setRequestMethod("POST");
32                 connection.setUseCaches(false);
33                 connection.setRequestProperty("X-OpenRocket-Version", encode(BuildProperties.getVersion()));
34                 
35                 String post;
36                 post = (VERSION_PARAM + "=" + encode(BuildProperties.getVersion())
37                                 + "&" + BUG_REPORT_PARAM + "=" + encode(report));
38                 
39                 OutputStreamWriter wr = null;
40                 try {
41                         // Send post information
42                         connection.setDoOutput(true);
43                         wr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
44                         wr.write(post);
45                         wr.flush();
46                         
47                         if (connection.getResponseCode() != BUG_REPORT_RESPONSE_CODE) {
48                                 throw new IOException("Server responded with code " + 
49                                                 connection.getResponseCode() + ", expecting " + BUG_REPORT_RESPONSE_CODE);
50                         }
51                 } finally {
52                         if (wr != null)
53                                 wr.close();
54                         connection.disconnect();
55                 }
56         }
57         
58 }