| Start/ | End/ | |||
| True | False | - | Line | Source |
| 1 | package net.library.jiga; | |||
| 2 | ||||
| 3 | import java.io.BufferedInputStream; | |||
| 4 | import java.net.HttpURLConnection; | |||
| 5 | import java.net.URL; | |||
| 6 | ||||
| 7 | /** | |||
| 8 | * Provide simple layout to make an HTTP request to the server | |||
| 9 | */ | |||
| 10 | public class XmlRequest implements Runnable { | |||
| 11 | ||||
| 12 | /** POST Method */ | |||
| 13 | public final static String METHOD_POST = "POST"; | |||
| 14 | /** GET Method */ | |||
| 15 | public final static String METHOD_GET = "GET"; | |||
| 16 | ||||
| 17 | /** Successfully completed */ | |||
| 18 | public final static int STATE_OK = 0; | |||
| 19 | /** Not yet completed */ | |||
| 20 | public final static int STATE_INCOMPLETE = 1; | |||
| 21 | /** Request is invalid, can't execute */ | |||
| 22 | public final static int STATE_INVALID_REQUEST = 3; | |||
| 23 | /** Timeout */ | |||
| 24 | public final static int STATE_TIMEOUT = 4; | |||
| 25 | /** Completed with errors */ | |||
| 26 | public final static int STATE_ERROR = 5; | |||
| 27 | ||||
| 28 | /** Timeout */ | |||
| 29 | private final static int REQUEST_TIMEOUT = 30000; // 30 secondes | |||
| 30 | ||||
| 31 | /** CodeBase URL */ | |||
| 32 | private URL codeBase; | |||
| 33 | /** Method (POST / GET) */ | |||
| 34 | private String method; | |||
| 35 | /** URL to call */ | |||
| 36 | private String url; | |||
| 37 | /** Data */ | |||
| 38 | private String data; | |||
| 39 | /** Should this call be synchronous */ | |||
| 40 | private boolean async; | |||
| 41 | /** Complete state */ | |||
| 42 | private int state; | |||
| 43 | /** Answer */ | |||
| 44 | private String answer; | |||
| 45 | ||||
| 0 | 0 | - | 46 | public XmlRequest(GameApplet applet) { |
| 47 | this(applet.getCodeBase()); | |||
| 48 | } | |||
| 49 | ||||
| 0 | 0 | - | 50 | public XmlRequest(URL url) { |
| 51 | codeBase = url; | |||
| 52 | } | |||
| 53 | ||||
| 54 | /** | |||
| 55 | * Defines what to call | |||
| 56 | * @param method Must be POST or GET. Set to POST if not understood | |||
| 57 | * @param url url to call (Will be completed with the codebase value) | |||
| 58 | * @param async defines asynchronous call | |||
| 59 | */ | |||
| 0 | 0 | - | 60 | public void open(String method, String url, boolean async) { |
| 61 | ||||
| 0 | 0 | - | 62 | if (METHOD_POST.equals(method) || METHOD_GET.equals(method)) { |
| 0 | - | 62 | T || _ | |
| 0 | - | 62 | F || T | |
| 0 | - | 62 | F || F | |
| 63 | this.method = method; | |||
| 64 | } | |||
| 65 | else { | |||
| 66 | this.method = METHOD_POST; | |||
| 67 | } | |||
| 68 | ||||
| 0 | 0 | - | 69 | if (url == null) { |
| 70 | url = "/"; | |||
| 71 | } | |||
| 72 | else { | |||
| 73 | this.url = url; | |||
| 74 | } | |||
| 75 | this.async = async; | |||
| 76 | ||||
| 77 | this.state = STATE_INCOMPLETE; | |||
| 78 | } | |||
| 79 | ||||
| 80 | /** | |||
| 81 | * Calls server. | |||
| 82 | * @param data form parameters, concatened to url (GET) or added to body (POST) | |||
| 83 | */ | |||
| 0 | 0 | - | 84 | public void send(String data) { |
| 85 | ||||
| 86 | this.data = data; | |||
| 87 | ||||
| 0 | 0 | - | 88 | if (method == null) { |
| 89 | // Do nothing if open() was not called | |||
| 90 | state = STATE_INVALID_REQUEST; | |||
| 91 | ||||
| 0 | - | 92 | return; | |
| 93 | } | |||
| 94 | ||||
| 95 | state = STATE_INCOMPLETE; | |||
| 96 | ||||
| 0 | 0 | - | 97 | if (async) { |
| 98 | new Thread(this).start(); | |||
| 99 | } | |||
| 100 | else { | |||
| 101 | run(); | |||
| 102 | } | |||
| 103 | } | |||
| 104 | ||||
| 0 | 0 | - | 105 | public void run() { |
| 0 | 0 | - | 106 | if (method != null && "http".equalsIgnoreCase(codeBase.getProtocol())) { // Avoid direct invalid call |
| 0 | - | 106 | T && T | |
| 0 | - | 106 | T && F | |
| 0 | - | 106 | F && _ | |
| 0 | 0 | - | 107 | try { |
| 108 | state = STATE_INCOMPLETE; | |||
| 109 | answer = null; | |||
| 110 | ||||
| 111 | long timeLimit = System.currentTimeMillis() + REQUEST_TIMEOUT; | |||
| 112 | ||||
| 113 | // Build URL | |||
| 114 | String urlString = "http://"; | |||
| 115 | urlString += codeBase.getHost(); | |||
| 116 | ||||
| 0 | 0 | - | 117 | if (codeBase.getPort() != -1 && codeBase.getPort() != 80) { |
| 0 | - | 117 | T && T | |
| 0 | - | 117 | T && F | |
| 0 | - | 117 | F && _ | |
| 118 | urlString += ":" + String.valueOf(codeBase.getPort()); | |||
| 119 | } | |||
| 120 | ||||
| 0 | 0 | - | 121 | if (!url.startsWith("/")) { |
| 122 | urlString += "/"; | |||
| 123 | } | |||
| 124 | urlString += url; | |||
| 125 | ||||
| 126 | // Add GET data | |||
| 127 | if (METHOD_GET.equals(method) | |||
| 128 | && data != null | |||
| 0 | 0 | - | 129 | && data.trim().length() > 0) { |
| 0 | - | 129 | T && T && T | |
| 0 | - | 129 | T && T && F | |
| 0 | - | 129 | T && F && _ | |
| 0 | - | 129 | F && _ && _ | |
| 130 | // Add parameters | |||
| 0 | 0 | - | 131 | if (url.indexOf("?") != -1) { |
| 132 | urlString += "&"; | |||
| 133 | } | |||
| 134 | else { | |||
| 135 | urlString += "?"; | |||
| 136 | } | |||
| 137 | ||||
| 138 | urlString += data; | |||
| 139 | ||||
| 140 | // Reset data | |||
| 141 | data = new String(); | |||
| 142 | } | |||
| 143 | ||||
| 0 | 0 | - | 144 | if (data == null) { |
| 145 | data = new String(); | |||
| 146 | } | |||
| 147 | ||||
| 148 | // Init HttpURLConnection | |||
| 149 | HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection(); | |||
| 150 | ||||
| 151 | connection.setRequestMethod(method); | |||
| 152 | ||||
| 0 | 0 | - | 153 | if (METHOD_POST.equals(method)) { |
| 154 | connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |||
| 155 | } | |||
| 156 | connection.setRequestProperty("Content-Length", String.valueOf(data.length())); | |||
| 157 | ||||
| 158 | connection.setInstanceFollowRedirects(true); | |||
| 159 | connection.setUseCaches(false); | |||
| 160 | ||||
| 161 | connection.setDoOutput(true); | |||
| 162 | connection.setDoInput(true); | |||
| 163 | ||||
| 164 | // Connect | |||
| 165 | connection.connect(); | |||
| 166 | ||||
| 167 | // Send request | |||
| 168 | connection.getOutputStream().write(data.getBytes("ISO-8859-1")); | |||
| 169 | connection.getOutputStream().flush(); | |||
| 170 | ||||
| 171 | // Read answer | |||
| 172 | BufferedInputStream bis = new BufferedInputStream(connection.getInputStream()); | |||
| 173 | int available = bis.available(); | |||
| 174 | ||||
| 175 | boolean dataReceived = false; | |||
| 176 | String content = new String(); | |||
| 177 | int dataSize = connection.getContentLength(); | |||
| 178 | ||||
| 0 | 0 | - | 179 | while (!dataReceived) { |
| 0 | 0 | - | 180 | if (available != 0) { |
| 181 | ||||
| 182 | byte[] inData = new byte[available]; | |||
| 183 | bis.read(inData); | |||
| 184 | ||||
| 185 | content += new String(inData, "ISO-8859-1"); | |||
| 186 | ||||
| 187 | dataReceived = (content.length() >= dataSize); | |||
| 188 | } | |||
| 189 | ||||
| 0 | 0 | - | 190 | if (System.currentTimeMillis() > timeLimit && !dataReceived) { |
| 0 | - | 190 | T && T | |
| 0 | - | 190 | T && F | |
| 0 | - | 190 | F && _ | |
| 191 | state = STATE_TIMEOUT; | |||
| 192 | dataReceived = true; // Stop while | |||
| 193 | } | |||
| 194 | ||||
| 0 | 0 | - | 195 | if (!dataReceived) { |
| 0 | 0 | - | 196 | try { |
| 197 | Thread.sleep(500); | |||
| 198 | } | |||
| 0 | - | 199 | catch(Exception e) {} | |
| 200 | ||||
| 201 | available = bis.available(); | |||
| 202 | } | |||
| 203 | else { | |||
| 204 | state = STATE_OK; | |||
| 205 | answer = content; | |||
| 206 | } | |||
| 207 | } | |||
| 208 | } | |||
| 0 | - | 209 | catch (Exception e) { | |
| 210 | e.printStackTrace(); | |||
| 211 | state = STATE_ERROR; | |||
| 212 | } | |||
| 213 | } | |||
| 214 | } | |||
| 215 | ||||
| 0 | 0 | - | 216 | public int getState() { |
| 0 | - | 217 | return state; | |
| 218 | } | |||
| 219 | ||||
| 0 | 0 | - | 220 | public boolean completed() { |
| 0 | - | 221 | return state != STATE_INCOMPLETE; | |
| 222 | } | |||
| 223 | ||||
| 0 | 0 | - | 224 | public String getResponseText() { |
| 0 | - | 225 | return answer; | |
| 226 | } | |||
| 227 | ||||
| 0 | 0 | - | 228 | public MiniDOM getResponseXML() { |
| 229 | MiniDOM output = null; | |||
| 230 | ||||
| 0 | 0 | - | 231 | try { |
| 232 | output = MiniDOM.getTree(answer); | |||
| 233 | } | |||
| 0 | - | 234 | catch(Exception e) {} | |
| 235 | ||||
| 0 | - | 236 | return output; | |
| 237 | } | |||
| 238 | } | |||
| ***TER 0% (0/66) of SOURCE FILE XmlRequest.java | ||||