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

io.muserver.MediaTypeParser Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package io.muserver;
import io.muserver.rest.MuRuntimeDelegate;

import javax.ws.rs.core.MediaType;
import java.util.List;

import static io.muserver.Mutils.notNull;

/**
 * A utility class to parse Media Type or Content Type values such as text/plain and text/plain; charset=UTF-8 etc
 */
public class MediaTypeParser {
    static {
        MuRuntimeDelegate.ensureSet();
    }

    /**
     * Converts a string such as "text/plain" into a MediaType object.
     * @param value The value to parse
     * @return A MediaType object
     */
    public static MediaType fromString(String value) {
        if (value == null) {
            throw new NullPointerException("value");
        }
        List headerValues = ParameterizedHeaderWithValue.fromString(value);
        if (headerValues.isEmpty()) {
            throw new IllegalArgumentException("The value '" + value + "' did not contain a valid header value");
        }
        ParameterizedHeaderWithValue v = headerValues.get(0);
        String[] split = v.value().split("/");
        if (split.length != 2) {
            throw new IllegalArgumentException("Media types must be in the format 'type/subtype'; this is inavlid: '" + v.value() + "'");
        }
        return new MediaType(split[0], split[1], v.parameters());
    }

    /**
     * Converts a MediaType object into a string, suitable for printing in an HTTP header.
     * @param mediaType The type to print
     * @return A String, such as "image/jpeg"
     */
    public static String toString(MediaType mediaType) {
        notNull("mediaType", mediaType);
        return new ParameterizedHeaderWithValue(mediaType.getType() + "/" + mediaType.getSubtype(), mediaType.getParameters()).toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy