Producing ImageIO capabilities through JAX-RS, v2


import java.util.Arrays;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlEnum;
import static javax.imageio.ImageIO.getReaderFileSuffixes;
import static javax.imageio.ImageIO.getReaderFormatNames;
import static javax.imageio.ImageIO.getReaderMIMETypes;
import static javax.imageio.ImageIO.getWriterFileSuffixes;
import static javax.imageio.ImageIO.getWriterFormatNames;
import static javax.imageio.ImageIO.getWriterMIMETypes;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import javax.ws.rs.MatrixParam;
import static java.util.Arrays.stream;
import static java.util.Comparator.comparing;
import static java.util.logging.Logger.getLogger;
import kr.co.mediamonster.ippl.ws.rs.BaseResource;

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

    @XmlEnum
    public static enum Feature {
        FileSuffix() {
            @Override
            void probe(final Set<ImageIoCapability> capabilities) {
                stream(getReaderFileSuffixes()).forEach(
                        value -> capabilities.add(
                                new ImageIoCapability().feature(this)
                                .readable(true).value(value))
                );
                stream(getWriterFileSuffixes()).forEach(
                        value -> capabilities.add(
                                capabilities.stream()
                                .filter(e -> this == e.feature
                                             && value.equals(e.value))
                                .findFirst()
                                .orElseGet(() -> new ImageIoCapability()
                                        .feature(this).value(value)
                                ).writable(true)));
            }
        }, FormatName() {
            @Override
            void probe(final Set<ImageIoCapability> capabilities) {
                stream(getReaderFormatNames()).forEach(
                        value -> capabilities.add(
                                new ImageIoCapability().feature(this)
                                .readable(true).value(value))
                );
                stream(getWriterFormatNames()).forEach(
                        value -> capabilities.add(
                                capabilities.stream()
                                .filter(e -> this == e.feature
                                             && value.equals(e.value))
                                .findFirst()
                                .orElseGet(() -> new ImageIoCapability()
                                        .feature(this).value(value)
                                ).writable(true)));
            }
        }, MIMEType() {
            @Override
            void probe(final Set<ImageIoCapability> capabilities) {
                stream(getReaderMIMETypes()).forEach(value -> capabilities.add(
                        new ImageIoCapability().feature(this).readable(true)
                        .value(value))
                );
                stream(getWriterMIMETypes()).forEach(value -> capabilities.add(
                        capabilities.stream()
                        .filter(e -> this == e.feature && value.equals(e.value))
                        .findFirst()
                        .orElseGet(() -> new ImageIoCapability().feature(this)
                                .value(value)
                        ).writable(true)));
            }
        };

        abstract void probe(Set<ImageIoCapability> capabilities);
    }

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

        @Override
        public String toString() {
            return "ImageIoCapability{"
                   + "feature=" + feature
                   + ", readable=" + readable
                   + ", writable=" + writable
                   + ", value=" + value
                   + "}";
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 83 * hash + Objects.hashCode(this.feature);
            hash = 83 * hash + Objects.hashCode(this.value);
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final ImageIoCapability other = (ImageIoCapability) obj;
            if (!Objects.equals(this.value, other.value)) {
                return false;
            }
            if (this.feature != other.feature) {
                return false;
            }
            return true;
        }

        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 List<Feature> features) {
        final Set<ImageIoCapability> capabilities = new HashSet<>();
        Arrays.stream(Feature.values()).forEach(f -> f.probe(capabilities));
        return Response.ok(
                new GenericEntity<List<ImageIoCapability>>(
                        capabilities.stream()
                        .filter(c -> features.isEmpty()
                                     || features.contains(c.feature))
                        .sorted(comparing((ImageIoCapability c) -> c.feature)
                                .thenComparing(c -> c.value))
                        .collect(toList())) {
        }).build();
    }
}
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