private JSONObject setCookie(Cookie[] cookies, HttpServletResponse response, String last, List<String> prevEntityIDs, int maxAge) {
        Map<String, String> previousIdps = new HashMap<String, String>();
        boolean setLast = (last != null);
        boolean setPrevious = (prevEntityIDs.size() > 0);
        last = (setLast) ? last : "";
        StringBuilder b = new StringBuilder();

        if (setLast) {
            Cookie lastCookie = getCookie(cookies, "lastIdp", response);
            lastCookie.setMaxAge(maxAge);
            lastCookie.setPath("/");
            lastCookie.setValue(last);
            response.addCookie(lastCookie);
        }

        if (setPrevious) {
            Cookie prevCookie = getCookie(cookies, "previousIdPs", response);
            prevCookie.setMaxAge(maxAge);
            prevCookie.setPath("/");
            for (int i = 0; i < prevEntityIDs.size(); i++) {
                previousIdps.put(getId(i), prevEntityIDs.get(i));
                b.append(getId(i)).append("=").append(prevEntityIDs.get(i));
                if ((i + 1) < prevEntityIDs.size()) {
                    b.append(";");
                }
            }
            String encode = Base64Coder.encodeString(b.toString());
            prevCookie.setValue(encode);
            response.addCookie(prevCookie);
        }

        return getResponseJson(last, previousIdps, setLast, setPrevious);
    }