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 getDeviceAsJson(final String deviceId, final IZWayCallback<String> callback) {
    if (checkLogin()) {
        try {/*from  w ww  . j  a  v  a2s . 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))
                    .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 {
                                getDeviceAsJson(deviceId, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(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 {
                        getDeviceAsJson(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 synchronized Device putDevice(Device device) {
    if (checkLogin()) {
        try {// w w w .j  a  v a  2  s  .c o m
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_DEVICES + "/" + device.getDeviceId())
                    .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(device)), "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.OK_200) {
                // Authentication error - retry login and operation
                if (statusCode == HttpStatus.UNAUTHORIZED_401) {
                    if (getLogin() == null) {
                        mCaller.authenticationError();
                    } else {
                        return putDevice(device);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return parsePutDevice(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 putDevice(device);
                    }
                }
            } else {
                handleException(e, "put 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 putDevice(final Device device, final IZWayCallback<Device> callback) {
    if (checkLogin()) {
        try {/*w ww  .  ja  v a2  s  .com*/
            startHttpClient();

            Request request = mHttpClient
                    .newRequest(getZAutomationTopLevelUrl() + "/" + PATH_INSTANCES + "/" + device.getDeviceId())
                    .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(device)), "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 {
                                putDevice(device, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(parsePutDevice(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 {
                        putDevice(device, callback);
                    }
                }
            } else {
                handleException(e, "put 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 synchronized String getDeviceCommand(DeviceCommand command) {
    if (checkLogin()) {
        try {/*from   w  ww .ja v  a  2 s . co m*/
            startHttpClient();

            String path = buildGetDeviceCommandPath(command);
            if (path == null) {
                return "Device command parameter invalid";
            }

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + path)
                    .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 getDeviceCommand(command);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return "Command: " + command.getCommand() + " successfully performed on device: "
                        + command.getDeviceId() + " (" + parseGetDeviceCommand(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 getDeviceCommand(command);
                    }
                }
            } else {
                handleException(e, "get device command");
            }
        } 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 getDeviceCommand(final DeviceCommand command, final IZWayCallback<String> callback) {
    if (checkLogin()) {
        try {// w ww . j a  va 2 s .c  o m
            startHttpClient();

            String path = buildGetDeviceCommandPath(command);
            if (path == null) {
                mCaller.apiError("Device command parameter invalid", false);
                return;
            }

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + path)
                    .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 {
                                getDeviceCommand(command, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess("Command: " + command.getCommand()
                                + " successfully performed on device: " + command.getDeviceId() + " ("
                                + parseGetDeviceCommand(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 {
                        getDeviceCommand(command, callback);
                    }
                }
            } else {
                handleException(e, "get device command");
            }
        } 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 IconList getIcons() {
    if (checkLogin()) {
        try {/*from w  ww.jav  a2s .  c  om*/
            startHttpClient();

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_ICONS)
                    .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 getIcons();
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return parseGetIcons(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 getIcons();
                    }
                }
            } else {
                handleException(e, "get icons");
            }
        } 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 getIcons(final IZWayCallback<IconList> callback) {
    if (checkLogin()) {
        try {//from  w  ww. jav  a2s  .co  m
            startHttpClient();

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_ICONS)
                    .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 {
                                getIcons(callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(parseGetIcons(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 {
                        getIcons(callback);
                    }
                }
            } else {
                handleException(e, "get icons");
            }
        } 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 postIcon(File image) {
    if (checkLogin()) {
        try {//from   w ww. j  ava2  s  .  co m
            startHttpClient();

            MultiPartContentProvider multiPart = new MultiPartContentProvider();
            multiPart.addFilePart("file", image.getName(), new PathContentProvider(Paths.get(image.getPath())),
                    null);
            multiPart.close();

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_ICONS + "/upload")
                    .method(HttpMethod.POST).header(HttpHeader.ACCEPT, "application/json").content(multiPart)
                    .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 postIcon(image);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return "Icon upload successfully performed";
            }
        } 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 postIcon(image);
                    }
                }
            } else {
                handleException(e, "get post icon");
            }
        } finally {
            stopHttpClient();
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method

    return null;
}

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

/**********************
 ****** ZWaveAPI ******/*from   w ww.  j  a  v a 2s  .  co m*/
 **********************/

/*
 * (non-Javadoc)
 *
 * @see de.fh_zwickau.informatik.sensor.ZWayApiBase#getZWaveDevice(int)
 */
@Override
public synchronized ZWaveDevice getZWaveDevice(int nodeId) {
    if (checkLogin()) {
        try {
            startHttpClient();

            String path = URLEncoder.encode(
                    StringUtils.replace(ZWAVE_PATH_DEVICES, "{nodeId}", String.valueOf(nodeId)), "UTF-8");

            Request request = mHttpClient.newRequest(getZWaveTopLevelUrl() + "/" + path).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 getZWaveDevice(nodeId);
                    }
                } else {
                    processResponseStatus(statusCode);
                }
            } else {
                return parseGetZWaveDevice(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 getZWaveDevice(nodeId);
                    }
                }
            } else {
                handleException(e, "get zwave 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 getZWaveDevice(final int nodeId, final IZWayCallback<ZWaveDevice> callback) {
    if (checkLogin()) {
        try {//from   w  w  w.j a v  a  2  s  . c o  m
            startHttpClient();

            String path = URLEncoder.encode(
                    StringUtils.replace(ZWAVE_PATH_DEVICES, "{nodeId}", String.valueOf(nodeId)), "UTF-8");

            Request request = mHttpClient.newRequest(getZWaveTopLevelUrl() + "/" + path).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 {
                                getZWaveDevice(nodeId, callback);
                            }
                        } else {
                            processResponseStatus(statusCode);
                        }
                    } else {
                        callback.onSuccess(parseGetZWaveDevice(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 {
                        getZWaveDevice(nodeId, callback);
                    }
                }
            } else {
                handleException(e, "get zwave device");
            }
        } finally {
            // do not stop http client for asynchronous call
        }
    } // no else ... checkLogin() method will invoke the appropriate callback method
}