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