use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class AsyncDocumentRequestHandlerImpl method createConnectionRequest.
protected ConnectionRequest createConnectionRequest(final DocumentInfo docInfo, final IOCallback callback, final Object[] response) {
return new ConnectionRequest() {
protected void buildRequestBody(OutputStream os) throws IOException {
if (isPost()) {
if (docInfo.getParams() != null) {
OutputStreamWriter w = new OutputStreamWriter(os, docInfo.getEncoding());
w.write(docInfo.getParams());
}
}
}
protected void handleIOException(IOException err) {
if (callback == null) {
response[0] = err;
}
super.handleIOException(err);
}
protected boolean shouldAutoCloseResponse() {
return callback != null;
}
protected void readResponse(InputStream input) throws IOException {
if (callback != null) {
callback.streamReady(input, docInfo);
} else {
response[0] = input;
synchronized (LOCK) {
LOCK.notify();
}
}
}
};
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class TestComponent method testCookies.
private void testCookies() throws IOException {
Cookie.clearCookiesFromStorage();
String baseUrl = "http://solutions.weblite.ca/cn1tests/cookie";
String clearCookiesUrl = baseUrl + "/reset.php";
String setCookiesUrl = baseUrl + "/set.php";
String checkCookiesUrl = baseUrl + "/check.php";
String setCookiesUrlSession = baseUrl + "/set_session.php";
// Try without native cookie store
ConnectionRequest.setUseNativeCookieStore(false);
ConnectionRequest.fetchJSON(clearCookiesUrl);
Map<String, Object> res = ConnectionRequest.fetchJSON(checkCookiesUrl);
System.out.println(res);
TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
ConnectionRequest.fetchJSON(setCookiesUrl);
res = ConnectionRequest.fetchJSON(checkCookiesUrl);
TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
// Now check that session cookies (no explicit expiry) are set correctly
ConnectionRequest.fetchJSON(clearCookiesUrl);
res = ConnectionRequest.fetchJSON(checkCookiesUrl);
TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
ConnectionRequest.fetchJSON(setCookiesUrlSession);
res = ConnectionRequest.fetchJSON(checkCookiesUrl);
TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
// Try with native cookie store
ConnectionRequest.setUseNativeCookieStore(true);
ConnectionRequest.fetchJSON(clearCookiesUrl);
res = ConnectionRequest.fetchJSON(checkCookiesUrl);
TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
ConnectionRequest.fetchJSON(setCookiesUrl);
res = ConnectionRequest.fetchJSON(checkCookiesUrl);
TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
// Now check that session cookies (no explicit expiry) are set correctly
ConnectionRequest.fetchJSON(clearCookiesUrl);
res = ConnectionRequest.fetchJSON(checkCookiesUrl);
TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
ConnectionRequest.fetchJSON(setCookiesUrlSession);
res = ConnectionRequest.fetchJSON(checkCookiesUrl);
TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
Throwable[] t = new Throwable[1];
// Now test a different cookie date format.
ConnectionRequest req = new ConnectionRequest() {
@Override
protected void handleException(Exception err) {
Log.p("handling exception " + err);
t[0] = err;
}
@Override
protected void handleRuntimeException(RuntimeException err) {
Log.p("handling runtime exception " + err);
t[0] = err;
}
@Override
protected void handleErrorResponseCode(int code, String message) {
Log.p("Error response " + code + ", " + message);
}
};
String oldProp = (String) Display.getInstance().getProperty("com.codename1.io.ConnectionRequest.throwExceptionOnFailedCookieParse", null);
Display.getInstance().setProperty("com.codename1.io.ConnectionRequest.throwExceptionOnFailedCookieParse", "true");
req.setUrl(baseUrl + "/test_rfc822cookie.php");
req.setFollowRedirects(true);
req.setPost(false);
req.setDuplicateSupported(true);
// req.setFailSilently(true);
try {
NetworkManager.getInstance().addToQueueAndWait(req);
} finally {
// NetworkManager.getInstance().removeErrorListener(errorListener);
Display.getInstance().setProperty("com.codename1.io.ConnectionRequest.throwExceptionOnFailedCookieParse", oldProp);
}
TestUtils.assertTrue(req.getResponseCode() == 200, "Unexpected response code. Expected 200 but found " + req.getResponseCode());
TestUtils.assertTrue(t[0] == null, t[0] != null ? ("Exception was thrown getting URL " + t[0].getMessage()) : "");
}
Aggregations