Example usage for com.google.gwt.user.client Cookies setCookie

List of usage examples for com.google.gwt.user.client Cookies setCookie

Introduction

In this page you can find the example usage for com.google.gwt.user.client Cookies setCookie.

Prototype

public static void setCookie(String name, String value, Date expires, String domain, String path,
        boolean secure) 

Source Link

Document

Sets a cookie.

Usage

From source file:anzsoft.xmpp4gwt.client.Bosh2Connector.java

License:Open Source License

public Bosh2Connector(final User user) {
    this.setUser(user);
    standardHandler = new RequestCallback() {

        public void onError(Request request, Throwable exception) {
            final String lastSendedBody = activeRequests.remove(request);
            if (exception instanceof RequestTimeoutException) {
                GWT.log("Request too old. Trying again.", null);
                DeferredCommand.addCommand(new Command() {
                    public void execute() {
                        if (lastSendedBody != null && state == State.connected && sid != null
                                && sid.length() > 0)
                            send(lastSendedBody, standardHandler);
                        else
                            continuousConnection(null);
                    }//ww  w .ja  va2  s .c om
                });
            } else if (exception.getMessage().startsWith("Unable to read XmlHttpRequest.status;")) {
                GWT.log("Lost request. Ignored. Resend.", null);
                if (lastSendedBody != null) {
                    DeferredCommand.addCommand(new Command() {
                        public void execute() {
                            if (state == State.connected && sid != null && sid.length() > 0) {
                                send(lastSendedBody, standardHandler);
                            }
                        }
                    });
                }
            } else {
                state = State.disconnected;
                GWT.log("Connection error", exception);
                exception.printStackTrace();
                fireEventError(BoshErrorCondition.remote_connection_failed, null,
                        "Response error: " + exception.getMessage());
            }
        }

        public void onResponseReceived(Request request, Response response) {
            if (state == State.disconnected)
                return;

            final String httpResponse = response.getText();
            final int httpStatusCode = response.getStatusCode();
            final String lastSendedBody = activeRequests.remove(request);

            System.out.println(" IN (" + httpStatusCode + "): " + httpResponse);
            fireOnBodyReceive(response, httpResponse);

            final Packet body = parse2(response.getText().replaceAll(";", ";"));

            final String type = body == null ? null : body.getAtribute("type");
            final String receivedSid = body == null ? null : body.getAtribute("sid");
            String $tmp = body == null ? null : body.getAtribute("rid");
            //final Long rid = $tmp == null ? null : Long.valueOf($tmp);
            final String ack = body == null ? null : body.getAtribute("ack");
            $tmp = body == null ? null : body.getAtribute("condition");
            if ($tmp != null)
                $tmp = $tmp.replace("-", "_");
            final BoshErrorCondition boshCondition = $tmp == null ? null : BoshErrorCondition.valueOf($tmp);

            final String wait = body == null ? null : body.getAtribute("wait");
            final String inactivity = body == null ? null : body.getAtribute("inactivity");
            if (wait != null && inactivity != null) {
                try {
                    int w = Integer.parseInt(wait);
                    int i = Integer.parseInt(inactivity);
                    int t = (w + i / 2) * 1000;
                    builder.setTimeoutMillis(t);
                    GWT.log("New timeout: " + t + "ms", null);
                } catch (Exception e) {
                    GWT.log("Error in wait and inactivity attributes", e);
                }
            }

            if (httpStatusCode != 200 || body == null
                    || type != null && ("terminate".equals(type) || "error".equals(type))) {
                GWT.log("ERROR (" + httpStatusCode + "): " + httpResponse, null);
                ErrorCondition condition = body == null ? ErrorCondition.bad_request
                        : ErrorCondition.undefined_condition;
                String msg = null;
                Packet error = body == null ? null : body.getFirstChild("error");
                if (error != null) {
                    for (Packet c : error.getChildren()) {
                        String xmlns = c.getAtribute("xmlns");
                        if ("text".equals(c.getName())) {
                            msg = c.getCData();
                            break;
                        } else if (xmlns != null && "urn:ietf:params:xml:ns:xmpp-stanzas".equals(xmlns)) {
                            condition = getCondition(c.getName(), httpStatusCode);
                        }
                    }
                }

                if (condition == ErrorCondition.item_not_found) {
                    state = State.disconnected;
                    fireEventError(boshCondition, condition, msg);
                } else if (errorCounter < MAX_ERRORS) {
                    errorCounter++;
                    send(lastSendedBody, standardHandler);
                } else if (type != null && "terminate".equals(type)) {
                    GWT.log("Disconnected by server", null);
                    state = State.disconnected;
                    fireDisconnectByServer(boshCondition, condition, msg);
                } else {
                    state = State.disconnected;
                    if (msg == null) {
                        msg = "[" + httpStatusCode + "] " + condition.name().replace('_', '-');
                    }
                    fireEventError(boshCondition, condition, msg);
                }
            } else {
                errorCounter = 0;
                if (receivedSid != null && sid != null && !receivedSid.equals(sid)) {
                    state = State.disconnected;
                    fireEventError(BoshErrorCondition.policy_violation, ErrorCondition.unexpected_request,
                            "Unexpected session initialisation.");
                } else if (receivedSid != null && sid == null) {
                    sid = receivedSid;
                    Cookies.setCookie(user.getResource() + "sid", sid, null, null, "/", false);
                    state = State.connected;
                }

                final List<? extends Packet> children = body.getChildren();
                if (children.size() > 0) {
                    fireEventReceiveStanzas(children);
                }
                continuousConnection(ack);
            }
            System.out.println("............sid value is:" + sid);
        }
    };

    //added by zhongfanglin@antapp.com
    scriptHandler = new ScriptSyntaxRequestCallback() {
        public void onError(String callbackID) {
            state = State.disconnected;
            GWT.log("Connection error", null);
            fireEventError(BoshErrorCondition.remote_connection_failed, null,
                    "Response error: request timeout or 404!");
        }

        public void onResponseReceived(String callbackID, String responseText) {

            if (state == State.disconnected)
                return;

            final String httpResponse = responseText;
            final String lastSendedBody = activeScriptRequests.remove(callbackID);

            System.out.println(" IN:" + httpResponse);
            fireOnBodyReceive(null, httpResponse);

            final Packet body = parse2(responseText.replaceAll("&semi;", ";"));

            final String type = body == null ? null : body.getAtribute("type");
            final String receivedSid = body == null ? null : body.getAtribute("sid");
            String $tmp = body == null ? null : body.getAtribute("rid");
            //final Long rid = $tmp == null ? null : Long.valueOf($tmp);
            final String ack = body == null ? null : body.getAtribute("ack");
            $tmp = body == null ? null : body.getAtribute("condition");
            if ($tmp != null)
                $tmp = $tmp.replace("-", "_");
            final BoshErrorCondition boshCondition = $tmp == null ? null : BoshErrorCondition.valueOf($tmp);

            final String wait = body == null ? null : body.getAtribute("wait");
            final String inactivity = body == null ? null : body.getAtribute("inactivity");
            if (wait != null && inactivity != null) {
                try {
                    int w = Integer.parseInt(wait);
                    int i = Integer.parseInt(inactivity);
                    int t = (w + i / 2) * 1000;
                    scriptBuilder.setTimeoutMillis(t);
                    GWT.log("New timeout: " + t + "ms", null);
                } catch (Exception e) {
                    GWT.log("Error in wait and inactivity attributes", e);
                }
            }

            if (body == null || type != null && ("terminate".equals(type) || "error".equals(type))) {
                GWT.log("ERROR : " + httpResponse, null);
                ErrorCondition condition = body == null ? ErrorCondition.bad_request
                        : ErrorCondition.undefined_condition;
                String msg = null;
                Packet error = body == null ? null : body.getFirstChild("error");
                if (error != null) {
                    for (Packet c : error.getChildren()) {
                        String xmlns = c.getAtribute("xmlns");
                        if ("text".equals(c.getName())) {
                            msg = c.getCData();
                            break;
                        } else if (xmlns != null && "urn:ietf:params:xml:ns:xmpp-stanzas".equals(xmlns)) {
                            condition = getCondition(c.getName(), -1);
                        }
                    }
                }

                if (condition == ErrorCondition.item_not_found) {
                    state = State.disconnected;
                    fireEventError(boshCondition, condition, msg);
                } else if (errorCounter < MAX_ERRORS) {
                    errorCounter++;
                    send(lastSendedBody, scriptHandler);
                } else if (type != null && "terminate".equals(type)) {
                    GWT.log("Disconnected by server", null);
                    state = State.disconnected;
                    fireDisconnectByServer(boshCondition, condition, msg);
                } else {
                    state = State.disconnected;
                    if (msg == null) {
                        msg = condition.name().replace('_', '-');
                    }
                    fireEventError(boshCondition, condition, msg);
                }
            } else {
                errorCounter = 0;
                if (receivedSid != null && sid != null && !receivedSid.equals(sid)) {
                    state = State.disconnected;
                    fireEventError(BoshErrorCondition.policy_violation, ErrorCondition.unexpected_request,
                            "Unexpected session initialisation.");
                } else if (receivedSid != null && sid == null) {
                    sid = receivedSid;
                    Cookies.setCookie(user.getResource() + "sid", sid, null, null, "/", false);
                    state = State.connected;
                }

                List<? extends Packet> children = body.getChildren();
                if (children.size() > 0) {
                    fireEventReceiveStanzas(children);
                }
                continuousConnection(ack);
            }
        }

    };
    //end added 
}

From source file:anzsoft.xmpp4gwt.client.Bosh2Connector.java

License:Open Source License

private String getNextRid() {
    this.rid++;/*from  ww w  .j a va  2 s .co  m*/
    String tmp = String.valueOf(this.rid);
    final Date expire = new Date(39 * 1000 + (new Date()).getTime());
    Cookies.setCookie(user.getResource() + "rid", tmp, expire, null, "/", false);
    return tmp;
}

From source file:anzsoft.xmpp4gwt.client.User.java

License:Open Source License

public boolean suspend() {
    JSONObject jUser = new JSONObject();
    if (domainname == null || username == null)
        return false;

    jUser.put(STORAGE_DOMAIN, new JSONString(domainname));
    //jUser.put("password", new JSONString(password)); //It's uneeded, and not safe
    String strPriority = String.valueOf(priority);
    if (strPriority != null)
        jUser.put(STORAGE_PRIORITY, new JSONString(String.valueOf(priority)));
    jUser.put(STORAGE_RESOURCE, new JSONString(resource));
    jUser.put(STORAGE_USERNAME, new JSONString(username));

    Cookies.setCookie(COOKIENAME, jUser.toString(), null, null, "/", false);
    return true;//w  w  w  .  j  ava2s.  co  m
}

From source file:at.ait.dme.yuma.client.Application.java

License:EUPL

/**
 * returns the provided name of the user 
 * /*ww w.  j a va  2 s . c o m*/
 * @return user name
 */
public static String getUser() {
    String userName = authenticatedUser.getName();
    if (!userName.equalsIgnoreCase(Cookies.getCookie(LEMO_COOKIE_NAME))) {
        Cookies.setCookie(LEMO_COOKIE_NAME, userName, null, null, "/", false);
    }
    return userName;
}

From source file:at.ait.dme.yuma.client.Application.java

License:EUPL

/**
 * set the authenticated user and create a cookie
 * /*from w w w.j  av  a  2  s .c o  m*/
 * @param user
 */
public static void setAuthenticatedUser(User user) {
    Cookies.setCookie(LEMO_COOKIE_NAME, user.getName(), null, null, "/", false);
    authenticatedUser = user;
}

From source file:at.ait.dme.yuma.client.Application.java

License:EUPL

/**
 * authenticate admin user as user provided
 * //www . j  a  v a2 s.  c  om
 * @param user
 */
public static void authenticateAs(String user) {
    if (authenticatedUser.isAdmin()) {
        Cookies.setCookie(LEMO_COOKIE_NAME, user, null, null, "/", false);
    }
}

From source file:cc.kune.core.client.cookies.CookiesManagerImpl.java

License:GNU Affero Public License

@Override
public void removeAnonCookie() {
    Cookies.removeCookie(ANON);/*from  ww w  . j av a2  s.  c o m*/
    Cookies.setCookie(ANON, null, new Date(0), CookieUtils.getDomain(), "/", false);
}

From source file:cc.kune.core.client.cookies.CookiesManagerImpl.java

License:GNU Affero Public License

@Override
public void removeAuthCookie() {
    // FIXME: Remove cookie doesn't works in all browsers, know
    // issue://  w ww . ja v a 2 s  . c o m
    // http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/ded86778ee56690/515dc513c7d085eb?lnk=st&q=remove+cookie#515dc513c7d085eb
    // http://code.google.com/p/google-web-toolkit/issues/detail?id=1735&q=removeCookie
    Cookies.removeCookie(SessionConstants.USERHASH);
    Cookies.removeCookie(SessionConstants.JSESSIONID);
    // Workaround:
    Cookies.setCookie(SessionConstants.USERHASH, null, new Date(0), CookieUtils.getDomain(), "/",
            WindowUtils.isHttps());
    Cookies.setCookie(SessionConstants.JSESSIONID, null, new Date(0), CookieUtils.getDomain(), "/",
            WindowUtils.isHttps());
}

From source file:cc.kune.core.client.cookies.CookiesManagerImpl.java

License:GNU Affero Public License

@Override
public void setAnonCookie(final Boolean userRegister) {
    final Date expires = new Date(
            System.currentTimeMillis() + (userRegister ? SessionConstants.ANON_SESSION_DURATION_AFTER_REG
                    : SessionConstants.ANON_SESSION_DURATION));
    Cookies.setCookie(ANON, userRegister.toString(), expires, CookieUtils.getDomain(), "/", false);
}

From source file:cc.kune.core.client.cookies.CookiesManagerImpl.java

License:GNU Affero Public License

@Override
public void setAuthCookie(final String userHash) {
    // http://code.google.com/p/google-web-toolkit-incubator/wiki/LoginSecurityFAQ
    final Date expires = new Date(System.currentTimeMillis() + SessionConstants.SESSION_DURATION);
    Cookies.setCookie(SessionConstants.USERHASH, userHash, expires, CookieUtils.getDomain(), "/",
            WindowUtils.isHttps());//from  w w  w . j a v  a 2 s  .  c  o m
    Cookies.setCookie(SessionConstants.JSESSIONID, userHash, expires, CookieUtils.getDomain(), "/",
            WindowUtils.isHttps());
    Log.info("Received hash: " + userHash, null);
}