Search in sources :

Example 16 with ConnectionRequest

use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.

the class CloudStorage method deleteCloudFile.

/**
 * Deletes a file from the cloud storage
 *
 * @param fileId the file id to delete
 * @return true if the operation was successful
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public boolean deleteCloudFile(String fileId) {
    if (CloudPersona.getCurrentPersona().getToken() == null) {
        CloudPersona.createAnonymous();
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/fileStoreDelete");
    req.addArgument("i", fileId);
    req.addArgument("t", CloudPersona.getCurrentPersona().getToken());
    NetworkManager.getInstance().addToQueueAndWait(req);
    if (req.getResponseCode() == 200) {
        return new String(req.getResponseData()).equals("OK");
    }
    return false;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 17 with ConnectionRequest

use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.

the class FacebookConnect method validateToken.

@Override
protected boolean validateToken(String token) {
    // make a call to the API if the return value is 40X the token is not
    // valid anymore
    final boolean[] retval = new boolean[1];
    retval[0] = true;
    ConnectionRequest req = new ConnectionRequest() {

        @Override
        protected void handleErrorResponseCode(int code, String message) {
            // access token not valid anymore
            if (code >= 400 && code <= 410) {
                retval[0] = false;
                return;
            }
            super.handleErrorResponseCode(code, message);
        }
    };
    req.setPost(false);
    req.setUrl("https://graph.facebook.com/v2.4/me");
    req.addArgumentNoEncoding("access_token", token);
    NetworkManager.getInstance().addToQueueAndWait(req);
    return retval[0];
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 18 with ConnectionRequest

use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.

the class GoogleConnect method validateToken.

@Override
protected boolean validateToken(String token) {
    // make a call to the API if the return value is 40X the token is not
    // valid anymore
    final boolean[] retval = new boolean[1];
    retval[0] = true;
    ConnectionRequest req = new ConnectionRequest() {

        @Override
        protected void handleErrorResponseCode(int code, String message) {
            // access token not valid anymore
            if (code >= 400 && code <= 410) {
                retval[0] = false;
                return;
            }
            super.handleErrorResponseCode(code, message);
        }
    };
    req.setPost(false);
    req.setUrl("https://www.googleapis.com/plus/v1/people/me");
    req.addRequestHeader("Authorization", "Bearer " + token);
    NetworkManager.getInstance().addToQueueAndWait(req);
    return retval[0];
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 19 with ConnectionRequest

use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.

the class Push method sendPushMessage.

/**
 * Sends a push message and returns true if server delivery succeeded, notice that the
 * push message isn't guaranteed to reach all devices.
 *
 * @param body the body of the message
 * @param deviceKey an optional parameter (can be null) when sending to a specific device
 * @param production whether pushing to production or test/sandbox environment
 * @param googleAuthKey authorization key from the google play store
 * @param iosCertificateURL a URL where you host the iOS certificate for this applications push capabilities.
 * @param iosCertificatePassword the password for the push certificate
 * @param bbUrl the URL to which the push should be submitted when sending a blackberry push for evaluation use https://pushapi.eval.blackberry.com
 * for production you will need to apply at https://cp310.pushapi.na.blackberry.com
 * @param bbApp the application id to authenticate on push for RIM devices
 * @param bbPass the application password credentials authenticate on push for RIM devices
 * @param bbPort the port of the blackberry push
 * @return true if the message reached the Codename One server successfully, this makes no guarantee
 * of delivery.
 * @deprecated this method sends a push using the old push servers which will be retired, you need to switch
 * to the equivalent method that accepts a push token
 */
public static boolean sendPushMessage(String body, String deviceKey, boolean production, String googleAuthKey, String iosCertificateURL, String iosCertificatePassword, String bbUrl, String bbApp, String bbPass, String bbPort) {
    ConnectionRequest cr = createPushMessage(body, deviceKey, production, googleAuthKey, iosCertificateURL, iosCertificatePassword, bbUrl, bbApp, bbPass, bbPort);
    NetworkManager.getInstance().addToQueueAndWait(cr);
    if (cr.getResposeCode() == 200) {
        return true;
    }
    return false;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 20 with ConnectionRequest

use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.

the class TestComponent method testCookiesInBrowserComponent.

private void testCookiesInBrowserComponent() throws IOException {
    Cookie.clearCookiesFromStorage();
    Form f = new Form("CookiesInBrowser");
    String formName = "CookiesInBrowser";
    f.setName(formName);
    f.setLayout(new BorderLayout());
    BrowserComponent bc = new BrowserComponent();
    f.add(BorderLayout.CENTER, bc);
    f.show();
    TestUtils.waitForFormName(formName, 2000);
    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";
    final BrowserStatus status = new BrowserStatus(bc);
    bc.setURL(clearCookiesUrl);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    Map<String, Object> res = status.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    status.reset();
    bc.setURL(setCookiesUrl);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    res = status.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    status.reset();
    bc.setURL(clearCookiesUrl);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    res = status.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    status.reset();
    bc.setURL(setCookiesUrlSession);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    res = status.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    // Now let's try to share cookies between the browser component and
    // a connection request.
    ConnectionRequest.setUseNativeCookieStore(true);
    Cookie.clearCookiesFromStorage();
    BrowserComponent bc2 = new BrowserComponent();
    bc.getParent().replace(bc, bc2, null);
    bc = bc2;
    f.revalidate();
    final BrowserStatus status2 = new BrowserStatus(bc);
    bc.setURL(clearCookiesUrl);
    status2.waitReady();
    status2.reset();
    // First verify that the cookie is not set in either browser or connection request
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    // Next let's set the cookie in the browser, and verify that it is set in both
    // browser and connection request.
    status2.reset();
    bc.setURL(setCookiesUrl);
    status2.waitReady();
    status2.reset();
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    // Now let's delete the cookie in the browser and verify that it is deleted in
    // both the browser and connection request.
    status2.reset();
    bc.setURL(clearCookiesUrl);
    status2.waitReady();
    status2.reset();
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    // Now let's set the cookie in the ConnectionRequest and verify that it is set in both
    // connection request and browser.
    ConnectionRequest.fetchJSON(setCookiesUrl);
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    status2.reset();
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout)

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