55 lines
1.6 KiB
Java
55 lines
1.6 KiB
Java
package de.nclazz.commons.io;
|
|
|
|
import lombok.NonNull;
|
|
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
* Simple implementation of a MimeType.
|
|
*/
|
|
public class MimeType {
|
|
|
|
public static final String MEDIA_APPLICATION = "application";
|
|
public static final String MEDIA_AUDIO = "audio";
|
|
public static final String MEDIA_EXAMPLE = "example";
|
|
public static final String MEDIA_IMAGE = "image";
|
|
public static final String MEDIA_MESSAGE = "message";
|
|
public static final String MEDIA_MODEL = "model";
|
|
public static final String MEDIA_MULTIPART = "multipart";
|
|
public static final String MEDIA_TEXT = "text";
|
|
public static final String MEDIA_VIDEO = "video";
|
|
|
|
private static final Pattern MIME_REGEX = Pattern.compile("^(?<media>\\p{Alnum}+)/(?<sub>\\p{Alnum}+)$");
|
|
|
|
private final String mediaType;
|
|
private final String subType;
|
|
|
|
public MimeType(String mediaType, String subType) {
|
|
this.mediaType = mediaType;
|
|
this.subType = subType;
|
|
}
|
|
|
|
public String getMediaType() {
|
|
return mediaType;
|
|
}
|
|
|
|
public String getSubType() {
|
|
return subType;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return String.format("%s/%s", this.mediaType, this.subType);
|
|
}
|
|
|
|
public static MimeType parse(@NonNull String mime) {
|
|
Matcher matcher = MIME_REGEX.matcher(mime);
|
|
if(!matcher.matches()) {
|
|
throw new IllegalArgumentException(String.format("Invalid MimeType '%s'!", mime));
|
|
}
|
|
return new MimeType(matcher.group("media"), matcher.group("sub"));
|
|
}
|
|
|
|
}
|