Search in sources :

Example 41 with ConnectionRequest

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

the class NetworkManager method handleException.

private boolean handleException(ConnectionRequest r, Exception o) {
    if (errorListeners != null) {
        ActionEvent ev = new NetworkEvent(r, o);
        errorListeners.fireActionEvent(ev);
        return ev.isConsumed();
    }
    return false;
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent)

Example 42 with ConnectionRequest

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

the class Oauth2 method handleURL.

private void handleURL(String url, WebBrowser web, final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {
    if ((url.startsWith(redirectURI))) {
        if (Display.getInstance().getCurrent() == progress) {
            progress.dispose();
        }
        web.stop();
        // remove the browser component.
        if (login != null) {
            login.removeAll();
            login.revalidate();
        }
        if (url.indexOf("code=") > -1) {
            Hashtable params = getParamsFromURL(url);
            ConnectionRequest req = new ConnectionRequest() {

                protected void readResponse(InputStream input) throws IOException {
                    byte[] tok = Util.readInputStream(input);
                    String t = new String(tok);
                    if (t.startsWith("{")) {
                        JSONParser p = new JSONParser();
                        Map map = p.parseJSON(new StringReader(t));
                        token = (String) map.get("access_token");
                        Object ex = map.get("expires_in");
                        if (ex == null) {
                            ex = map.get("expires");
                        }
                        if (ex != null) {
                            expires = ex.toString();
                        }
                    } else {
                        token = t.substring(t.indexOf("=") + 1, t.indexOf("&"));
                        int off = t.indexOf("expires=");
                        int start = 8;
                        if (off == -1) {
                            off = t.indexOf("expires_in=");
                            start = 11;
                        }
                        if (off > -1) {
                            int end = t.indexOf('&', off);
                            if (end < 0 || end < off) {
                                end = t.length();
                            }
                            expires = t.substring(off + start, end);
                        }
                    }
                    if (login != null) {
                        login.dispose();
                    }
                }

                protected void handleException(Exception err) {
                    if (backToForm != null) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        al.actionPerformed(new ActionEvent(err, ActionEvent.Type.Exception));
                    }
                }

                protected void postResponse() {
                    if (backToParent && backToForm != null) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
                    }
                }
            };
            req.setUrl(tokenRequestURL);
            req.setPost(true);
            req.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.addArgument("client_id", clientId);
            req.addArgument("redirect_uri", redirectURI);
            req.addArgument("client_secret", clientSecret);
            req.addArgument("code", (String) params.get("code"));
            req.addArgument("grant_type", "authorization_code");
            NetworkManager.getInstance().addToQueue(req);
        } else if (url.indexOf("error_reason=") > -1) {
            Hashtable table = getParamsFromURL(url);
            String error = (String) table.get("error_reason");
            if (login != null) {
                login.dispose();
            }
            if (backToForm != null) {
                backToForm.showBack();
            }
            if (al != null) {
                al.actionPerformed(new ActionEvent(new IOException(error), ActionEvent.Type.Exception));
            }
        } else {
            boolean success = url.indexOf("#") > -1;
            if (success) {
                String accessToken = url.substring(url.indexOf("#") + 1);
                if (accessToken.indexOf("&") > 0) {
                    token = accessToken.substring(accessToken.indexOf("=") + 1, accessToken.indexOf("&"));
                } else {
                    token = accessToken.substring(accessToken.indexOf("=") + 1);
                }
                if (login != null) {
                    login.dispose();
                }
                if (backToParent && backToForm != null) {
                    backToForm.showBack();
                }
                if (al != null) {
                    al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
                }
            }
        }
    } else {
        if (frm != null && Display.getInstance().getCurrent() != frm) {
            progress.dispose();
            frm.show();
        }
    }
}
Also used : Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) IOException(java.io.IOException) StringReader(com.codename1.util.regex.StringReader) Map(java.util.Map)

Example 43 with ConnectionRequest

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

the class Message method createMessage.

private ConnectionRequest createMessage(String sender, String recipient, String recipientName, String subject, String plainTextBody) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setUrl(Display.getInstance().getProperty("cloudServerURL", "https://codename-one.appspot.com/") + "sendEmailServlet");
    cr.setFailSilently(cloudMessageFailSilently);
    cr.setPost(true);
    cr.addArgument("d", Display.getInstance().getProperty("built_by_user", ""));
    cr.addArgument("from", sender);
    cr.addArgument("to", recipient);
    cr.addArgument("re", recipientName);
    cr.addArgument("subject", subject);
    if (mimeType.equals(MIME_TEXT)) {
        cr.addArgument("body", content);
    } else {
        cr.addArgument("body", plainTextBody);
        cr.addArgument("html", content);
    }
    return cr;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 44 with ConnectionRequest

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

the class Util method downloadUrlTo.

private static boolean downloadUrlTo(String url, String fileName, boolean showProgress, boolean background, boolean storage, ActionListener callback) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setPost(false);
    cr.setFailSilently(true);
    cr.setReadResponseForErrors(false);
    cr.setDuplicateSupported(true);
    cr.setUrl(url);
    if (callback != null) {
        cr.addResponseListener(callback);
    }
    if (storage) {
        cr.setDestinationStorage(fileName);
    } else {
        cr.setDestinationFile(fileName);
    }
    if (background) {
        NetworkManager.getInstance().addToQueue(cr);
        return true;
    }
    if (showProgress) {
        InfiniteProgress ip = new InfiniteProgress();
        Dialog d = ip.showInifiniteBlocking();
        NetworkManager.getInstance().addToQueueAndWait(cr);
        d.dispose();
    } else {
        NetworkManager.getInstance().addToQueueAndWait(cr);
    }
    int rc = cr.getResponseCode();
    return rc == 200 || rc == 201;
}
Also used : InfiniteProgress(com.codename1.components.InfiniteProgress) Dialog(com.codename1.ui.Dialog)

Example 45 with ConnectionRequest

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

the class AsyncDocumentRequestHandlerImpl method resourceRequested.

private InputStream resourceRequested(final DocumentInfo docInfo, final IOCallback callback) {
    try {
        if (docInfo.getUrl().startsWith("file://")) {
            String url = docInfo.getUrl();
            // trim anchors
            int hash = url.indexOf('#');
            if (hash != -1) {
                url = url.substring(0, hash);
            }
            callback.streamReady(FileSystemStorage.getInstance().openInputStream(url), docInfo);
            return null;
        }
    } catch (IOException ex) {
        Log.e(ex);
    }
    final Object[] response = new Object[1];
    ConnectionRequest reqest = createConnectionRequest(docInfo, callback, response);
    reqest.setPost(docInfo.isPostRequest());
    if (docInfo.isPostRequest()) {
        reqest.setUrl(docInfo.getUrl());
        reqest.setWriteRequest(true);
    } else {
        reqest.setUrl(docInfo.getFullUrl());
    }
    NetworkManager.getInstance().addToQueue(reqest);
    if (callback == null) {
        synchronized (LOCK) {
            while (response[0] == null) {
                try {
                    LOCK.wait(50);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            if (response[0] instanceof InputStream) {
                return (InputStream) response[0];
            }
            // we need a better way to handle this...
            if (response[0] instanceof Throwable) {
                ((Throwable) response[0]).printStackTrace();
            }
        }
    }
    return null;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStream(java.io.InputStream) 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