All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.webfolder.ui4j.api.interceptor.Response Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package io.webfolder.ui4j.api.interceptor;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class Response {

    private String url;

    private Map> headers;

    public Response(String url, Map> headers) {
        this.url = url;
        this.headers = headers;
    }

    public Map> getHeaders() {
        return headers;
    }

    public Optional getCookie(String name) {
        for (HttpCookie cookie : getCookies()) {
            if (cookie.getName().equals(name)) {
                return Optional.of(cookie);
            }
        }
        return Optional.empty();
    }

    public List getCookies() {
        List cookies = new ArrayList<>();
        if (headers != null) {
            List list = headers.get("Set-Cookie");
            if (list != null) {
                for (String next : list) {
                    List hc = HttpCookie.parse(next);
                    if (!hc.isEmpty()) {
                        cookies.add(hc.get(0));
                    }
                }
            }
        }
        return Collections.unmodifiableList(cookies);
    }

    public Optional getHeader(String name) {
        List values = headers.get(name);
        if (values != null && !values.isEmpty()) {
            return Optional.of(values.get(0));
        }
        return Optional.empty();
    }

    public String getUrl() {
        return url;
    }

    @Override
    public String toString() {
        return "Response [url=" + url + ", headers=" + headers + "]";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy