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

org.jboss.resteasy.reactive.common.headers.LinkHeaders Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
package org.jboss.resteasy.reactive.common.headers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jakarta.ws.rs.core.Link;
import jakarta.ws.rs.core.MultivaluedMap;

public class LinkHeaders {
    private final Map linksByRelationship = new HashMap<>();
    private final List links = new ArrayList<>();

    public LinkHeaders(MultivaluedMap headers) {
        List values = headers.get("Link");
        if (values == null) {
            return;
        }

        for (Object val : values) {
            if (val instanceof Link) {
                addLink((Link) val);
            } else if (val instanceof String) {
                for (String link : ((String) val).split(",")) {
                    addLink(Link.valueOf(link));
                }
            } else {
                String str = HeaderUtil.headerToString(val);
                addLink(Link.valueOf(str));
            }
        }
    }

    private void addLink(final Link link) {
        links.add(link);
        for (String rel : link.getRels()) {
            linksByRelationship.put(rel, link);
        }
    }

    public Link getLinkByRelationship(String rel) {
        return linksByRelationship.get(rel);
    }

    public List getLinks() {
        return links;
    }

}