Search in sources :

Example 1 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class MetacardImpl method writeObject.

/**
     * Serializes this {@link MetacardImpl} instance.
     *
     * @param stream the {@link ObjectOutputStream} that contains the object to be serialized
     * @throws IOException
     * @serialData First, all non-transient fields are written out by the default Java serialization
     * implementation ( {@link ObjectOutputStream#defaultWriteObject()}) . Next, the
     * {@link MetacardType} is written out as a {@link MetacardTypeImpl}. Then the
     * <i>number</i> of {@code Attribute} objects is written as an {@code int}. After
     * the number of objects, each {@code Attribute} object is written out.
     * <p>
     * <p>
     * The MetacardType object is written out as a {@link MetacardTypeImpl} object
     * because {@link MetacardTypeImpl} is a class that is part of the DDF API and is
     * guaranteed to be on the classpath when this object is deserialized. Secondly, the
     * {@link MetacardTypeImpl} has a trusted serialization implementation where the
     * object's logical representation is serialized.
     * </p>
     */
private void writeObject(ObjectOutputStream stream) throws IOException {
    /*
         * defaultWriteObject() is invoked for greater flexibility and compatibility. See the
         * *Serialization Note* in MetacardImpl's class Javadoc.
         */
    stream.defaultWriteObject();
    /*
         * Cannot allow unknown implementations of MetacardType to be serialized. Must convert them
         * to our implementation to guarantee it is serializing the logical representation and not
         * the physical representation.
         */
    if (type instanceof MetacardTypeImpl) {
        stream.writeObject(type);
    } else {
        MetacardTypeImpl mt = new MetacardTypeImpl(type.getName(), type.getAttributeDescriptors());
        stream.writeObject(mt);
    }
    if (map != null) {
        stream.writeInt(map.size());
        for (Attribute attribute : this.map.values()) {
            stream.writeObject(attribute);
        }
    } else {
        if (wrappedMetacard != null && wrappedMetacard.getMetacardType() != null) {
            MetacardType metacardType = wrappedMetacard.getMetacardType();
            List<Attribute> attributes = new ArrayList<Attribute>();
            if (metacardType.getAttributeDescriptors() == null) {
                // no descriptors, means no attributes can be defined.
                // no attributes defined, means no attributes written to
                // disk
                stream.writeInt(0);
            } else {
                for (AttributeDescriptor ad : metacardType.getAttributeDescriptors()) {
                    Attribute attribute = wrappedMetacard.getAttribute(ad.getName());
                    if (attribute != null) {
                        attributes.add(attribute);
                    }
                }
                // Must loop again because the size of the attributes list
                // is not known until list has been fully populated.
                stream.writeInt(attributes.size());
                for (Attribute attribute : attributes) {
                    stream.writeObject(attribute);
                }
            }
        }
    }
}
Also used : Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType)

Example 2 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class MetacardImpl method requestData.

/**
     * The brains of the operation -- does the interaction with the map or the wrapped metacard.
     *
     * @param <T>           the type of the Attribute value expected
     * @param attributeName the name of the {@link Attribute} to retrieve
     * @param returnType    the class that the value of the {@link ddf.catalog.data.AttributeType} is expected to be bound to
     * @return the value of the requested {@link Attribute} name
     */
protected <T> T requestData(String attributeName, Class<T> returnType) {
    Attribute attribute = getAttribute(attributeName);
    if (attribute == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Attribute {} was not found, returning null", attributeName);
        }
        return null;
    }
    Serializable data = attribute.getValue();
    if (data == null) {
        return null;
    }
    if (returnType.isAssignableFrom(data.getClass())) {
        return returnType.cast(data);
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(data.getClass().toString() + " can not be assigned to " + returnType.toString());
        }
    }
    return null;
}
Also used : Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute)

Example 3 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class MetacardImpl method readObject.

/**
     * Deserializes this {@link MetacardImpl}'s instance.
     *
     * @param stream the {@link ObjectInputStream} that contains the bytes of the object
     * @throws IOException
     * @throws ClassNotFoundException
     */
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    /*
         * defaultReadObject() is invoked for greater flexibility and compatibility. See the
         * *Serialization Note* in MetacardImpl's class Javadoc.
         */
    stream.defaultReadObject();
    map = new HashMap<String, Attribute>();
    wrappedMetacard = null;
    type = (MetacardType) stream.readObject();
    if (type == null) {
        throw new InvalidObjectException(MetacardType.class.getName() + " instance cannot be null.");
    }
    int numElements = stream.readInt();
    for (int i = 0; i < numElements; i++) {
        Attribute attribute = (Attribute) stream.readObject();
        if (attribute != null) {
            AttributeDescriptor attributeDescriptor = getMetacardType().getAttributeDescriptor(attribute.getName());
            if (attributeDescriptor != null && attribute.getValue() != null) {
                attributeDescriptor.getType().getAttributeFormat();
                attributeDescriptor.getType().getClass();
            }
        }
        setAttribute(attribute);
    }
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) InvalidObjectException(java.io.InvalidObjectException)

Example 4 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class AttributeImplTest method testDeserializationCorruption.

/**
     * Tests what happens when someone tries to manually change the serialized object after it has
     * been serialized. The expected outcome is that it will be detected that the object is corrupt.
     * The original serialized object's name field was "id", it was manually changed, then saved
     * again.
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
@Test(expected = StreamCorruptedException.class)
public void testDeserializationCorruption() throws IOException, ClassNotFoundException {
    String fileLocation = "src/test/resources/tamperedAttributeImpl.ser";
    Serializer<Attribute> serializer = new Serializer<Attribute>();
    Attribute readAttribute1 = serializer.deserialize(fileLocation);
    readAttribute1.getName();
}
Also used : Attribute(ddf.catalog.data.Attribute) Test(org.junit.Test)

Example 5 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class AttributeImplTest method testSerializationMultiple.

@Test
public void testSerializationMultiple() throws IOException, ClassNotFoundException {
    toTest = new AttributeImpl("id", UUID.randomUUID().toString());
    toTest.addValue(UUID.randomUUID().toString());
    toTest.addValue(UUID.randomUUID().toString());
    toTest.addValue(UUID.randomUUID().toString());
    toTest.addValue(UUID.randomUUID().toString());
    Attribute read = serializationLoop(toTest);
    assertEquals(toTest.getName(), read.getName());
    assertEquals(toTest.getValue(), read.getValue());
    assertEquals(toTest.getValues(), read.getValues());
}
Also used : Attribute(ddf.catalog.data.Attribute) Test(org.junit.Test)

Aggregations

Attribute (ddf.catalog.data.Attribute)103 Metacard (ddf.catalog.data.Metacard)39 Test (org.junit.Test)37 ArrayList (java.util.ArrayList)30 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)29 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)26 Serializable (java.io.Serializable)23 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 HashMap (java.util.HashMap)15 Result (ddf.catalog.data.Result)14 List (java.util.List)14 MetacardType (ddf.catalog.data.MetacardType)11 QueryResponse (ddf.catalog.operation.QueryResponse)11 Date (java.util.Date)11 Map (java.util.Map)11 HashSet (java.util.HashSet)10 Optional (java.util.Optional)8 Set (java.util.Set)8 Filter (org.opengis.filter.Filter)8 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)7