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

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

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

import java.util.List;
import java.util.Map;

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

import org.jboss.resteasy.reactive.common.jaxrs.LinkBuilderImpl;
import org.jboss.resteasy.reactive.common.util.QuarkusMultivaluedHashMap;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class LinkDelegate implements RuntimeDelegate.HeaderDelegate {
    public static final LinkDelegate INSTANCE = new LinkDelegate();

    private static class Parser {
        private int curr;
        private String value;
        private Link.Builder builder;

        Parser(final String value) {
            this.value = value;
            builder = new LinkBuilderImpl();
        }

        public Link getLink() {
            return builder.build();
        }

        public void parse() {
            String href = null;
            MultivaluedMap attributes = new QuarkusMultivaluedHashMap();
            while (curr < value.length()) {

                char c = value.charAt(curr);
                if (c == '<') {
                    if (href != null)
                        throw new IllegalArgumentException("Too many links");
                    href = parseLink();
                } else if (c == ';' || c == ' ') {
                    curr++;
                    continue;
                } else {
                    parseAttribute(attributes);
                }
            }
            populateLink(href, attributes);

        }

        protected void populateLink(String href, MultivaluedMap attributes) {
            builder.uri(href);
            for (String name : attributes.keySet()) {
                List values = attributes.get(name);
                if (name.equals("rel")) {
                    for (String val : values)
                        builder.rel(val);
                } else if (name.equals("title")) {
                    for (String val : values)
                        builder.title(val);

                } else if (name.equals("type")) {
                    for (String val : values)
                        builder.type(val);

                } else {
                    for (String val : values)
                        builder.param(name, val);

                }
            }
        }

        public String parseLink() {
            int end = value.indexOf('>', curr);
            if (end == -1)
                throw new IllegalArgumentException("No end to link");
            String href = value.substring(curr + 1, end);
            curr = end + 1;
            return href;
        }

        public void parseAttribute(MultivaluedMap attributes) {
            int end = value.indexOf('=', curr);
            if (end == -1 || end + 1 >= value.length())
                throw new IllegalArgumentException("No end to parameter");
            String name = value.substring(curr, end);
            name = name.trim();
            curr = end + 1;
            String val = null;
            if (curr >= value.length()) {
                val = "";
            } else {

                if (value.charAt(curr) == '"') {
                    if (curr + 1 >= value.length())
                        throw new IllegalArgumentException("No end to parameter");
                    curr++;
                    end = value.indexOf('"', curr);
                    if (end == -1)
                        throw new IllegalArgumentException("No end to parameter");
                    val = value.substring(curr, end);
                    curr = end + 1;
                } else {
                    StringBuilder buf = new StringBuilder();
                    while (curr < value.length()) {
                        char c = value.charAt(curr);
                        if (c == ',' || c == ';')
                            break;
                        buf.append(value.charAt(curr));
                        curr++;
                    }
                    val = buf.toString();
                }
            }
            attributes.add(name, val);

        }

    }

    @Override
    public Link fromString(String value) throws IllegalArgumentException {
        if (value == null)
            throw new IllegalArgumentException("value was null");
        Parser parser = new Parser(value);
        parser.parse();
        return parser.getLink();
    }

    @Override
    public String toString(Link value) throws IllegalArgumentException {
        if (value == null)
            throw new IllegalArgumentException("value was null");
        StringBuilder buf = new StringBuilder("<");
        buf.append(value.getUri().toString()).append(">");

        for (Map.Entry entry : value.getParams().entrySet()) {
            buf.append("; ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
        }

        return buf.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy