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

com.artipie.http.headers.ContentDisposition 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 java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Content-Disposition header.
 *
 * @see 
 */
public final class ContentDisposition extends Header {

    /**
     * Header name.
     */
    public static final String NAME = "Content-Disposition";

    /**
     * Header directives pattern.
     */
    private static final Pattern DIRECTIVES = Pattern.compile(
        "(? \\w+ ) (?:= [\"] (? [^\"]+ ) [\"] )?[;]?",
        Pattern.COMMENTS
    );

    /**
     * Parsed directives.
     */
    private final Map directives;

    /**
     * Ctor.
     *
     * @param value Header value.
     */
    public ContentDisposition(final String value) {
        super(new Header(ContentDisposition.NAME, value));
        this.directives = this.parse();
    }

    /**
     * Ctor.
     *
     * @param headers Headers to extract header from.
     */
    public ContentDisposition(final Headers headers) {
        this(new RqHeaders.Single(headers, ContentDisposition.NAME).asString());
    }

    /**
     * The original name of the file transmitted.
     *
     * @return String.
     */
    public String fileName() {
        return this.directives.get("filename");
    }

    /**
     * The name of the HTML field in the form
     * that the content of this subpart refers to.
     *
     * @return String.
     */
    public String fieldName() {
        return this.directives.get("name");
    }

    /**
     * Inline.
     *
     * @return Boolean flag.
     */
    public Boolean isInline() {
        return this.directives.containsKey("inline");
    }

    /**
     * Inline.
     *
     * @return Boolean flag.
     */
    public Boolean isAttachment() {
        return this.directives.containsKey("attachment");
    }

    /**
     * Parse header value to a map.
     *
     * @return Map of keys and values.
     */
    private Map parse() {
        final Matcher matcher = ContentDisposition.DIRECTIVES.matcher(this.getValue());
        final Map values = new HashMap<>();
        while (matcher.find()) {
            values.put(matcher.group("key"), matcher.group("value"));
        }
        return values;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy