updates for 0.9.4
[debian/openrocket] / src / net / sf / openrocket / communication / Communicator.java
1 package net.sf.openrocket.communication;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.HttpURLConnection;
5 import java.net.URLEncoder;
6
7 public abstract class Communicator {
8
9         protected static final String BUG_REPORT_URL;
10
11         protected static final String UPDATE_INFO_URL;
12         
13         static {
14                 String url;
15                 url = System.getProperty("openrocket.debug.bugurl");
16                 if (url == null)
17                         url = "http://openrocket.sourceforge.net/actions/reportbug";
18                 BUG_REPORT_URL = url;
19                 
20                 url = System.getProperty("openrocket.debug.updateurl");
21                 if (url == null)
22                         url = "http://openrocket.sourceforge.net/actions/updates";
23                 UPDATE_INFO_URL = url;
24         }
25         
26
27         protected static final String VERSION_PARAM = "version";
28         
29
30         protected static final String BUG_REPORT_PARAM = "content";
31         protected static final int BUG_REPORT_RESPONSE_CODE = HttpURLConnection.HTTP_ACCEPTED;
32         protected static final int CONNECTION_TIMEOUT = 10000;  // in milliseconds
33
34         protected static final int UPDATE_INFO_UPDATE_AVAILABLE = HttpURLConnection.HTTP_OK;
35         protected static final int UPDATE_INFO_NO_UPDATE_CODE = HttpURLConnection.HTTP_NO_CONTENT;
36         protected static final String UPDATE_INFO_CONTENT_TYPE = "text/plain";
37
38         // Limit the number of bytes that can be read from the server
39         protected static final int MAX_INPUT_BYTES = 20000;
40
41         
42         protected static ConnectionSource connectionSource = new DefaultConnectionSource();
43         
44         
45         /**
46          * Set the source of the network connections.  This can be used for unit testing.
47          * By default the source is a DefaultConnectionSource.
48          * 
49          * @param source        the source of the connections.
50          */
51         public static void setConnectionSource(ConnectionSource source) {
52                 connectionSource = source;
53         }
54         
55
56         /**
57          * URL-encode the specified string in UTF-8 encoding.
58          * 
59          * @param str   the string to encode (null ok)
60          * @return              the encoded string or "null"
61          */
62         public static String encode(String str) {
63                 if (str == null)
64                         return "null";
65                 try {
66                         return URLEncoder.encode(str, "UTF-8");
67                 } catch (UnsupportedEncodingException e) {
68                         throw new RuntimeException("Unsupported encoding UTF-8", e);
69                 }
70         }
71         
72 }