Producing ImageIO capabilities through JAX-RS


@Path("/miscellaneous/imageIoCapabilities")
public class ImageIoCapabilitiesResource {

    @XmlEnum
    public static enum Feature {
        FileSuffix, FormatName, MIMEType
    }

    @XmlAccessorType(XmlAccessType.NONE)
    @XmlRootElement
    public static class ImageIoCapability {

        ImageIoCapability feature(final Feature feature) {
            this.feature = feature;
            return this;
        }

        ImageIoCapability readable(final boolean readable) {
            this.readable = readable;
            return this;
        }

        ImageIoCapability writable(final boolean writable) {
            this.writable = writable;
            return this;
        }

        ImageIoCapability value(final String value) {
            this.value = value;
            return this;
        }

        @XmlAttribute
        private Feature feature;

        @XmlAttribute
        private boolean readable;

        @XmlAttribute
        private boolean writable;

        @XmlValue
        private String value;
    }

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response read(@MatrixParam("feature") final Feature feature) {
        final Map<String, ImageIoCapability> map = new HashMap<>();
        stream(getReaderFileSuffixes()).forEach(
                value -> map.put(
                        Feature.FileSuffix.name() + value,
                        new ImageIoCapability().feature(Feature.FileSuffix)
                        .readable(true).value(value)));
        stream(getWriterFileSuffixes()).forEach(
                value -> ofNullable(map.putIfAbsent(
                        Feature.FileSuffix.name() + value,
                        new ImageIoCapability().feature(Feature.FileSuffix).
                        writable(true).value(value)))
                .ifPresent(previous -> previous.writable(true)));
        stream(getReaderFormatNames()).forEach(
                value -> map.put(
                        Feature.FormatName + value,
                        new ImageIoCapability().feature(Feature.FormatName)
                        .readable(true).value(value)));
        stream(getWriterFormatNames()).forEach(
                value -> ofNullable(map.putIfAbsent(
                        Feature.FormatName + value, new ImageIoCapability()
                        .feature(Feature.FormatName).writable(true)
                        .value(value)))
                .ifPresent(previous -> previous.writable(true)));
        stream(getReaderMIMETypes()).forEach(
                value -> map.putIfAbsent(
                        Feature.MIMEType + value, new ImageIoCapability()
                        .feature(Feature.MIMEType).readable(true)
                        .value(value)));
        stream(getWriterMIMETypes()).forEach(
                value -> ofNullable(map.putIfAbsent(
                        Feature.MIMEType + value,
                        new ImageIoCapability().feature(Feature.MIMEType)
                        .writable(true).value(value)))
                .ifPresent(previous -> previous.writable(true)));
        return Response.ok(
                new GenericEntity<List<ImageIoCapability>>(
                        map.values().stream()
                        .filter(c -> ofNullable(feature)
                                .map(f -> f.equals(c.feature)).orElse(true))
                        .sorted(comparing((ImageIoCapability c) -> c.feature)
                                .thenComparing(c -> c.value))
                        .collect(toList())) {
        }).build();
    }
}

$ http --json GET http://.../miscellaneous/imageIoCapabilities | python -m json.tool
[
    {
        "feature": "FileSuffix",
        "readable": true,
        "value": "wbmp",
        "writable": true
    },
    {
        "feature": "FileSuffix",
        "readable": true,
        "value": "jpeg",
        "writable": true
    },
    {
        "feature": "FileSuffix",
        "readable": true,
        "value": "bmp",
        "writable": true
    },
    {
        "feature": "FileSuffix",
        "readable": true,
        "value": "png",
        "writable": true
    },
    {
        "feature": "FileSuffix",
        "readable": true,
        "value": "jpg",
        "writable": true
    },
    {
        "feature": "FileSuffix",
        "readable": true,
        "value": "gif",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "WBMP",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "bmp",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "GIF",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "jpeg",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "BMP",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "gif",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "png",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "PNG",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "jpg",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "wbmp",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "JPG",
        "writable": true
    },
    {
        "feature": "FormatName",
        "readable": true,
        "value": "JPEG",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/x-png",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/vnd.wap.wbmp",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/gif",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/bmp",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/png",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/jpeg",
        "writable": true
    }
]

$ http --json GET "http://.../miscellaneous/imageIoCapabilities;feature=MIMEType" | python -m json.tool
[
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/x-png",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/vnd.wap.wbmp",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/gif",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/bmp",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/png",
        "writable": true
    },
    {
        "feature": "MIMEType",
        "readable": true,
        "value": "image/jpeg",
        "writable": true
    }
]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s