Example usage for java.net HttpCookie HttpCookie

List of usage examples for java.net HttpCookie HttpCookie

Introduction

In this page you can find the example usage for java.net HttpCookie HttpCookie.

Prototype

public HttpCookie(String name, String value) 

Source Link

Document

Constructs a cookie with a specified name and value.

Usage

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public void putInstance(final Instance instance, final IZWayCallback<Instance> callback) {
    if (checkLogin()) {
        try {/*from ww  w  . j a  v  a 2s . c  o m*/
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_INSTANCES + "/" + instance.getId())
                    .method(HttpMethod.PUT).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId))
                    .content(new StringContentProvider(new Gson().toJson(instance)), "application/json")
                    .onRequestFailure(new ZWayFailureListener());

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            request.send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    int statusCode = result.getResponse().getStatus();
                    if (statusCode != HttpStatus.OK_200) {
                        if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                            if (getLogin() == null) {
                                mCaller.authenticationError();
                            } else {
                                putInstance(instance, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(parsePutInstance(getContentAsString()));
                    }
                }
            });
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        putInstance(instance, callback);
                    }
                }
            } else {
                handleException(e, "put instance");
            }
        } finally {
            // do not stop http client for asynchronous call
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public synchronized Instance postInstance(Instance instance) {
    if (checkLogin()) {
        try {//from  ww  w  .ja  v a2s.  c o m
            startHttpClient();

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_INSTANCES)
                    .method(HttpMethod.POST).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId))
                    .content(new StringContentProvider(new Gson().toJson(instance)), "application/json");

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            ContentResponse response = request.send();

            // Check HTTP status code
            int statusCode = response.getStatus();
            if (statusCode != HttpStatus.CREATED_201) {
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return putInstance(instance);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return parsePostInstance(response.getContentAsString());
            }
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return postInstance(instance);
                    }
                }
            } else {
                handleException(e, "post instance");
            }
        } finally {
            stopHttpClient();
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method

    return null;
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public void postInstance(final Instance instance, final IZWayCallback<Instance> callback) {
    if (checkLogin()) {
        try {/* w w w.j a  v  a 2  s. c  o  m*/
            startHttpClient();

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_INSTANCES)
                    .method(HttpMethod.POST).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId))
                    .content(new StringContentProvider(new Gson().toJson(instance)), "application/json")
                    .onRequestFailure(new ZWayFailureListener());

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            request.send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    int statusCode = result.getResponse().getStatus();
                    if (statusCode != HttpStatus.CREATED_201) {
                        if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                            if (getLogin() == null) {
                                mCaller.authenticationError();
                            } else {
                                postInstance(instance, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(parsePostInstance(getContentAsString()));
                    }
                }
            });
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        postInstance(instance, callback);
                    }
                }
            } else {
                handleException(e, "post instance");
            }
        } finally {
            // do not stop http client for asynchronous call
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public synchronized Boolean deleteInstance(Integer id) {
    if (checkLogin()) {
        try {/*from  www  . j a v a 2s .c  om*/
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_INSTANCES + "/" + id)
                    .method(HttpMethod.DELETE).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId));

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            ContentResponse response = request.send();

            // Check HTTP status code
            int statusCode = response.getStatus();
            if (statusCode != HttpStatus.NO_CONTENT_204) {
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return deleteInstance(id);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return true;
            }
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return deleteInstance(id);
                    }
                }
            } else {
                handleException(e, "delete instance");
            }
        } finally {
            stopHttpClient();
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method

    return null;
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public void deleteInstance(final Integer id, final IZWayCallback<Boolean> callback) {
    if (checkLogin()) {
        try {//from w w w .  j a  va2 s.  com
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_INSTANCES + "/" + id)
                    .method(HttpMethod.DELETE).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId))
                    .onRequestFailure(new ZWayFailureListener());

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            request.send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    int statusCode = result.getResponse().getStatus();
                    if (statusCode != HttpStatus.NO_CONTENT_204) {
                        if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                            if (getLogin() == null) {
                                mCaller.authenticationError();
                            } else {
                                deleteInstance(id, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(true);
                    }
                }
            });
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        deleteInstance(id, callback);
                    }
                }
            } else {
                handleException(e, "delete instance");
            }
        } finally {
            // do not stop http client for asynchronous call
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public synchronized DeviceList getDevices() {
    if (checkLogin()) {
        try {/*from   w  w w . java2  s .  c o m*/
            startHttpClient();

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_DEVICES)
                    .method(HttpMethod.GET).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId));

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            ContentResponse response = request.send();

            // Check HTTP status code
            int statusCode = response.getStatus();
            if (statusCode != HttpStatus.OK_200) {
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return getDevices();
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return parseGetDevices(response.getContentAsString());
            }
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return getDevices();
                    }
                }
            } else {
                handleException(e, "get devices");
            }
        } finally {
            stopHttpClient();
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method

    return null;
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public void getDevices(final IZWayCallback<DeviceList> callback) {
    if (checkLogin()) {
        try {//ww w .  ja v  a 2  s.  c  om
            startHttpClient();

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_DEVICES)
                    .method(HttpMethod.GET).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId))
                    .onRequestFailure(new ZWayFailureListener());

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            request.send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    int statusCode = result.getResponse().getStatus();
                    if (statusCode != HttpStatus.OK_200) {
                        if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                            if (getLogin() == null) {
                                mCaller.authenticationError();
                            } else {
                                getDevices(callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(parseGetDevices(getContentAsString()));
                    }
                }
            });
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        getDevices(callback);
                    }
                }
            } else {
                handleException(e, "get devices");
            }
        } finally {
            // do not stop http client for asynchronous call
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public synchronized Device getDevice(String deviceId) {
    if (checkLogin()) {
        try {/*from w  w w.j  av a  2s  .c o  m*/
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_DEVICES + "/" + deviceId)
                    .method(HttpMethod.GET).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId));

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            ContentResponse response = request.send();

            // Check HTTP status code
            int statusCode = response.getStatus();
            if (statusCode != HttpStatus.OK_200) {
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return getDevice(deviceId);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return parseGetDevice(response.getContentAsString());
            }
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return getDevice(deviceId);
                    }
                }
            } else {
                handleException(e, "get device");
            }
        } finally {
            stopHttpClient();
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method

    return null;
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public void getDevice(final String deviceId, final IZWayCallback<Device> callback) {
    if (checkLogin()) {
        try {//  w w  w.  ja v  a2s. com
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_DEVICES + "/" + deviceId)
                    .method(HttpMethod.GET).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId))
                    .onRequestFailure(new ZWayFailureListener());

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            request.send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    int statusCode = result.getResponse().getStatus();
                    if (statusCode != HttpStatus.OK_200) {
                        if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                            if (getLogin() == null) {
                                mCaller.authenticationError();
                            } else {
                                getDevice(deviceId, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(parseGetDevice(getContentAsString()));
                    }
                }
            });
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        getDevice(deviceId, callback);
                    }
                }
            } else {
                handleException(e, "get device");
            }
        } finally {
            // do not stop http client for asynchronous call
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public String getDeviceAsJson(String deviceId) {
    if (checkLogin()) {
        try {//from   ww w  . j a v  a  2s .c  o  m
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_DEVICES + "/" + deviceId)
                    .method(HttpMethod.GET).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .cookie(new HttpCookie("ZWAYSession", mZWaySessionId));

            if (mUseRemoteService) {
                request.cookie(new HttpCookie("ZBW_SESSID", mZWayRemoteSessionId));
            }

            ContentResponse response = request.send();

            // Check HTTP status code
            int statusCode = response.getStatus();
            if (statusCode != HttpStatus.OK_200) {
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return getDeviceAsJson(deviceId);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return response.getContentAsString();
            }
        } catch (Exception e) {
            if (e.getCause() instanceof HttpResponseException) {
                int statusCode = ((HttpResponseException) e.getCause()).getResponse().getStatus();
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return getDeviceAsJson(deviceId);
                    }
                }
            } else {
                handleException(e, "get device (json)");
            }
        } finally {
            stopHttpClient();
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method

    return null;
}