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

com.artipie.http.headers.Accept Maven / Gradle / Ivy

There is a newer version: v1.17.16
Show newest version
/*
 * The MIT License (MIT) Copyright (c) 2020-2023 artipie.com
 * https://github.com/artipie/artipie/blob/master/LICENSE.txt
 */
package com.artipie.http.headers;

import com.artipie.http.Headers;
import com.artipie.http.rq.RqHeaders;
import wtf.g4s8.mime.MimeType;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Accept header, check
 * documentation
 * for more details.
 *
 * @since 0.19
 */
public final class Accept {

    /**
     * Header name.
     */
    public static final String NAME = "Accept";

    /**
     * Headers.
     */
    private final Headers headers;

    /**
     * Ctor.
     * @param headers Headers to extract `accept` header from
     */
    public Accept(Headers headers) {
        this.headers = headers;
    }

    /**
     * Parses `Accept` header values, sorts them according to weight and returns in
     * corresponding order.
     * @return Set or the values
     */
    public List values() {
        final RqHeaders rqh = new RqHeaders(this.headers, Accept.NAME);
        if (rqh.size() == 0) {
            return Collections.emptyList();
        }
        return MimeType.parse(
            rqh.stream().collect(Collectors.joining(","))
        ).stream()
            .map(mime -> String.format("%s/%s", mime.type(), mime.subtype()))
            .collect(Collectors.toList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy