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

com.blitline.image.BlitlinePostback Maven / Gradle / Ivy

package com.blitline.image;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonRootName("results")
@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
public class BlitlinePostback {

	public static final SimpleDateFormat BLITLINE_DATE_FORMAT = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.US);

	private OriginalMetadata originalMeta;

	private String jobId;

	private Collection errors = Collections.emptySet();

	private Collection failedImageIdentifiers = Collections.emptySet();

	private Collection images = Collections.emptySet();

	public OriginalMetadata getOriginalMeta() {
		return originalMeta;
	}

	public void setOriginalMeta(OriginalMetadata originalMeta) {
		this.originalMeta = originalMeta;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public Collection getErrors() {
		return errors;
	}

	public void setErrors(Collection errors) {
		this.errors = errors;
	}

	@JsonIgnore
	public boolean isSuccessful() {
		return errors.isEmpty();
	}

	public Collection getFailedImageIdentifiers() {
		return failedImageIdentifiers;
	}

	public void setFailedImageIdentifiers(Collection failedImageIdentifiers) {
		this.failedImageIdentifiers = failedImageIdentifiers;
	}

	public Collection getImages() {
		return images;
	}

	public void setImages(Collection images) {
		this.images = images;
	}

	@JsonIgnore
	public boolean isIdentifyOnly() {
		return isSuccessful() ?
			images.isEmpty() :
			(failedImageIdentifiers.size() == 1 &&
			failedImageIdentifiers.iterator().next().endsWith(BlitlineImageJob.IDENTIFY_ONLY_SUFFIX));
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("BlitlinePostback[jobId=");
		sb.append(jobId).append(", successful=").append(isSuccessful());
		if (isSuccessful())
			sb.append(", ").append(images.size()).append(" images processed");
		else
			sb.append(", errors=").append(errors);
		sb.append(']');
		return sb.toString();
	}

	@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
	public static class OriginalMetadata {
		private Integer width, height, filesize;
		private Date dateCreated;
		private Map allExif = Collections.emptyMap();

		public OriginalMetadata() {
		}

		public OriginalMetadata(Integer width, Integer height, Date dateCreated) {
			this.width = width;
			this.height = height;
			if (dateCreated != null)
				this.dateCreated = new Date(dateCreated.getTime());
		}

		public Integer getWidth() {
			return width;
		}

		public void setWidth(Integer width) {
			this.width = width;
		}

		public Integer getHeight() {
			return height;
		}

		public void setHeight(Integer height) {
			this.height = height;
		}

		public Integer getFilesize() {
			return filesize;
		}

		public void setFilesize(Integer filesize) {
			this.filesize = filesize;
		}

		public Date getDateCreated() {
			return dateCreated;
		}

		public void setDateCreated(Date dateCreated) {
			this.dateCreated = dateCreated;
		}

		@JsonDeserialize(using = ExifDeserializer.class)
		public void setAllExif(Map allExif) {
			this.allExif = allExif;
		}

		public Map getAllExif() {
			return allExif;
		}

		@Override
		public String toString() {
			return "size " + width + 'x' + height + ", created " + dateCreated;
		}
	}

	@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
	public static class Image {
		private String imageIdentifier;
		private String s3Url;
		private ImageMeta meta;

		public void setImageIdentifier(String imageIdentifier) {
			this.imageIdentifier = imageIdentifier;
		}

		public String getImageIdentifier() {
			return imageIdentifier;
		}

		public void setS3Url(String s3Url) {
			this.s3Url = s3Url;
		}

		public String getS3Url() {
			return s3Url;
		}

		public void setMeta(ImageMeta meta) {
			this.meta = meta;
		}

		public ImageMeta getMeta() {
			return meta;
		}

		@Override
		public String toString() {
			return new StringBuilder("image[identifier=").append(imageIdentifier)
				.append(", size=").append(meta)
				.append(", s3Url=").append(s3Url).append(']').toString();
		}

		@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
		public static class ImageMeta {
			private Integer width, height, filesize, depth, quality;
			private String density;

			public ImageMeta() {
			}

			public ImageMeta(Integer width, Integer height) {
				this.width = width;
				this.height = height;
			}

			public Integer getHeight() {
				return height;
			}

			public void setHeight(Integer height) {
				this.height = height;
			}

			public Integer getWidth() {
				return width;
			}

			public void setWidth(Integer width) {
				this.width = width;
			}

			public Integer getFilesize() {
				return filesize;
			}

			public void setFilesize(Integer filesize) {
				this.filesize = filesize;
			}

			public Integer getDepth() {
				return depth;
			}

			public void setDepth(Integer depth) {
				this.depth = depth;
			}

			public Integer getQuality() {
				return quality;
			}

			public void setQuality(Integer quality) {
				this.quality = quality;
			}

			public String getDensity() {
				return density;
			}

			public void setDensity(String density) {
				this.density = density;
			}

			@Override
			public String toString() {
				StringBuilder sb = new StringBuilder().append(width).append('x').append(height);
				if (filesize != null)
					sb.append(", ").append(filesize).append(" bytes");
				if (depth != null)
					sb.append(", depth=").append(depth);
				if (quality != null)
					sb.append(", quality=").append(quality);
				if (density != null)
					sb.append(", density=").append(density);
				return sb.toString();
			}
		}
	}

	public static class ExifDeserializer extends JsonDeserializer> {
		@Override
		public Map deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
			JsonProcessingException {

			Map map = new HashMap();

			JsonToken token;

			if ((token = jp.getCurrentToken()) != JsonToken.START_ARRAY)
				throw new JsonMappingException("expected start of array, but found " + token, jp.getCurrentLocation());

			while ((token = jp.nextToken()) == JsonToken.START_ARRAY) {
				map.put(jp.nextTextValue(), jp.nextTextValue());
				if ((token = jp.nextToken()) != JsonToken.END_ARRAY)
					throw new JsonMappingException("expected a 2-valued array, but next token was a " + token,
						jp.getCurrentLocation());
			}

			return map;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy