Search in sources :

Example 46 with ConnectionRequest

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();
                }
            }
        }
    };
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 47 with ConnectionRequest

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()) : "");
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) IOException(java.io.IOException)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)41 IOException (java.io.IOException)11 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)7 InputStream (java.io.InputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Map (java.util.Map)6 JSONParser (com.codename1.io.JSONParser)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 InputStreamReader (java.io.InputStreamReader)5 InfiniteProgress (com.codename1.components.InfiniteProgress)4 NetworkEvent (com.codename1.io.NetworkEvent)4 Dialog (com.codename1.ui.Dialog)4 Form (com.codename1.ui.Form)4 ActionListener (com.codename1.ui.events.ActionListener)3 DataInputStream (java.io.DataInputStream)3 Hashtable (java.util.Hashtable)3 Component (com.codename1.ui.Component)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 Vector (java.util.Vector)2