001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.core.io.marshallers.json; 021 022import static org.nuxeo.ecm.core.io.registry.MarshallingConstants.ENTITY_FIELD_NAME; 023 024import java.io.IOException; 025 026import org.codehaus.jackson.JsonNode; 027import org.nuxeo.ecm.core.io.registry.MarshallingException; 028 029/** 030 * Base class to read Nuxeo entity Json and convert it in Objects. This class checks the json is an object, the json 031 * property "entity-type" is present and as expected and delegate the body reading to an abstract method. 032 * 033 * @param <EntityType> The managed Java type. 034 * @since 7.2 035 */ 036public abstract class EntityJsonReader<EntityType> extends AbstractJsonReader<EntityType> { 037 038 /** 039 * The expected "entity-type" property in the json. 040 */ 041 private final String entityType; 042 043 /** 044 * @param entityType The expected "entity-type" property in the json. 045 */ 046 public EntityJsonReader(String entityType) { 047 super(); 048 this.entityType = entityType; 049 } 050 051 @Override 052 public final EntityType read(JsonNode jn) throws IOException { 053 if (!jn.isObject()) { 054 throw new MarshallingException("Json does not contain an object as expected"); 055 } 056 JsonNode entityNode = jn.get(ENTITY_FIELD_NAME); 057 if (entityNode == null || entityNode.isNull() || !entityNode.isTextual()) { 058 throw new MarshallingException("Json object does not contain an entity-type field as expected"); 059 } 060 String entityValue = entityNode.getTextValue(); 061 if (!entityType.equals(entityValue)) { 062 throw new MarshallingException("Json object entity-type is wrong. Expected is " + entityType + " but was " 063 + entityValue); 064 } 065 return readEntity(jn); 066 } 067 068 /** 069 * Implement this method to read the entity. 070 * 071 * @param jn A {@link JsonNode} pointing at the root of the json input. 072 * @return The parsed entity. 073 * @since 7.2 074 */ 075 protected abstract EntityType readEntity(JsonNode jn) throws IOException; 076 077}