Tagged: imageio

Producing ImageIO capabilities through JAX-RS, v3


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

    @XmlEnum
    public static enum Feature {
        FileSuffix() {
            @Override
            void probe(final Collection<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 Collection<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 Collection<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(Collection<ImageIoCapability> capabilities);
    }

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

        @Override
        public String toString() {
            return super.toString() + "{"
                   + "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;
        }

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

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

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

        private 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));
        final List<ImageIoCapability> entity = capabilities.stream()
                .filter(c -> features.isEmpty() || features.contains(c.feature))
                .sorted(comparing((ImageIoCapability c) -> c.feature)
                        .thenComparing(c -> c.value))
                .collect(toList());
        final CacheControl cacheControl = new CacheControl();
        cacheControl.setMaxAge((int) TimeUnit.DAYS.toSeconds(1));
        return Response.ok(new GenericEntity<List<ImageIoCapability>>(entity) {
        }).cacheControl(cacheControl).build();
    }
}
Advertisements

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();
    }
}

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
    }
]