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

com.xlrit.gears.base.content.ContentRef Maven / Gradle / Ivy

There is a newer version: 1.17.5
Show newest version
package com.xlrit.gears.base.content;

import java.io.IOException;
import java.io.Serializable;
import java.util.Objects;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.Data;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

@JsonSerialize(using = ContentRef.JsonSerializer.class)
@JsonDeserialize(using = ContentRef.JsonDeserializer.class)
@Data
public class ContentRef /*implements Serializable*/ {
	public static final String VALUE_KIND = "content-ref";
	public static final String FIELD_KIND = "__kind";
	public static final String FIELD_ID   = "__id";
	public static final String FIELD_FILENAME = "filename";
	public static final String FIELD_CONTENTTYPE = "contentType";

	private final String id;
	private final String filename;
	private final String contentType;

	static class JsonSerializer extends StdSerializer {
		public JsonSerializer() { super(ContentRef.class); }

		@Override
		public void serialize(ContentRef contentRef, JsonGenerator jgen, SerializerProvider serializerProvider) throws IOException {
			jgen.writeStartObject();
			jgen.writeStringField(FIELD_KIND,        VALUE_KIND);
			jgen.writeStringField(FIELD_ID,          contentRef.getId());
			jgen.writeStringField(FIELD_FILENAME,    contentRef.getFilename());
			jgen.writeStringField(FIELD_CONTENTTYPE, contentRef.getContentType());
			jgen.writeEndObject();
		}
	}

	static class JsonDeserializer extends StdDeserializer {
		public JsonDeserializer() { super(ContentRef.class); }

		@Override
		public ContentRef deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
			JsonNode tn = p.readValueAsTree();

			String kind = asTextOr(tn.get(FIELD_KIND), null);
			checkState(Objects.equals(kind, VALUE_KIND), "Expected __kind to be '%s' but it's '%s'", VALUE_KIND, kind);

			String id = asTextOr(tn.get(FIELD_ID), null);
			checkNotNull(id, "id must not be null");

			String filename    = asTextOr(tn.get(FIELD_FILENAME), null);
			String contentType = asTextOr(tn.get(FIELD_CONTENTTYPE), null);
			return new ContentRef(id, filename, contentType);
		}

		private static String asTextOr(JsonNode jsonNode, String fallback) {
			return jsonNode == null ? fallback : jsonNode.asText();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy