Search in sources :

Example 66 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class JSONUtils method formulateJsonForListKeys.

public static String formulateJsonForListKeys(Object[] keys, String fieldName) {
    HeapDataOutputStream outputStream = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator generator = enableDisableJSONGeneratorFeature(getObjectMapper().getFactory().createGenerator((OutputStream) outputStream, JsonEncoding.UTF8));
        generator.writeStartObject();
        generator.writeFieldName(fieldName);
        JsonWriter.writeObjectArrayAsJson(generator, keys, null);
        generator.writeEndObject();
        generator.close();
        return new String(outputStream.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        outputStream.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) OutputStream(java.io.OutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Example 67 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class JSONUtils method formulateJsonForExistingQuery.

public static String formulateJsonForExistingQuery(String queryId, String oql) {
    HeapDataOutputStream outputStream = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator generator = enableDisableJSONGeneratorFeature(getObjectMapper().getFactory().createGenerator((OutputStream) outputStream, JsonEncoding.UTF8));
        JsonWriter.writeQueryAsJson(generator, queryId, oql);
        generator.close();
        return new String(outputStream.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        outputStream.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) OutputStream(java.io.OutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Example 68 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class JSONUtils method convertCollectionToJson.

public static String convertCollectionToJson(Collection<Object> collection) throws JSONException {
    HeapDataOutputStream outputStream = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator generator = enableDisableJSONGeneratorFeature(getObjectMapper().getFactory().createGenerator((OutputStream) outputStream, JsonEncoding.UTF8));
        JsonWriter.writeCollectionAsJson(generator, collection);
        generator.close();
        return new String(outputStream.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        outputStream.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) OutputStream(java.io.OutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Example 69 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class AbstractRegionEntry method checkPdxEquals.

/**
   * This method fixes bug 43643
   */
private static boolean checkPdxEquals(PdxInstance pdx, Object obj) {
    if (!(obj instanceof PdxInstance)) {
        // if we are not readSerialized.
        if (obj instanceof CachedDeserializable) {
            CachedDeserializable cdObj = (CachedDeserializable) obj;
            if (!cdObj.isSerialized()) {
                // obj is actually a byte[] which will never be equal to a PdxInstance
                return false;
            }
            Object cdVal = cdObj.getValue();
            if (cdVal instanceof byte[]) {
                byte[] cdValBytes = (byte[]) cdVal;
                PdxInstance pi = InternalDataSerializer.readPdxInstance(cdValBytes, GemFireCacheImpl.getForPdx("Could not check value equality"));
                if (pi != null) {
                    return pi.equals(pdx);
                } else {
                    // since obj is serialized as something other than pdx it must not equal our pdx
                    return false;
                }
            } else {
                // remove the cd wrapper so that obj is the actual value we want to compare.
                obj = cdVal;
            }
        }
        if (obj != null && obj.getClass().getName().equals(pdx.getClassName())) {
            InternalCache internalCache = GemFireCacheImpl.getForPdx("Could not access Pdx registry");
            if (internalCache != null) {
                PdxSerializer pdxSerializer;
                if (obj instanceof PdxSerializable) {
                    pdxSerializer = null;
                } else {
                    pdxSerializer = internalCache.getPdxSerializer();
                }
                if (pdxSerializer != null || obj instanceof PdxSerializable) {
                    // try to convert obj to a PdxInstance
                    HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
                    try {
                        if (InternalDataSerializer.autoSerialized(obj, hdos) || InternalDataSerializer.writePdx(hdos, internalCache, obj, pdxSerializer)) {
                            PdxInstance pi = InternalDataSerializer.readPdxInstance(hdos.toByteArray(), internalCache);
                            if (pi != null) {
                                obj = pi;
                            }
                        }
                    } catch (IOException | PdxSerializationException ignore) {
                    // we are not able to convert it so just fall through
                    }
                }
            }
        }
    }
    return basicEquals(obj, pdx);
}
Also used : PdxSerializer(org.apache.geode.pdx.PdxSerializer) PdxInstance(org.apache.geode.pdx.PdxInstance) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) IOException(java.io.IOException) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) PdxSerializable(org.apache.geode.pdx.PdxSerializable)

Example 70 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class FilterPostAuthorization method checkObjectAuth.

private byte[] checkObjectAuth(byte[] serializedObj, boolean isObject) {
    if (!isObject) {
        return null;
    }
    ByteArrayInputStream bis = new ByteArrayInputStream(serializedObj);
    DataInputStream dis = new DataInputStream(bis);
    Object obj;
    try {
        obj = DataSerializer.readObject(dis);
        if (this.logger.finerEnabled()) {
            this.logger.finer("FilterPostAuthorization: successfully read object " + "from serialized object: " + obj);
        }
    } catch (Exception ex) {
        this.logger.severe(LocalizedStrings.FilterPostAuthorization_FILTERPOSTAUTHORIZATION_AN_EXCEPTION_WAS_THROWN_WHILE_TRYING_TO_DESERIALIZE, ex);
        return null;
    }
    obj = checkObjectAuth(obj);
    if (obj != null) {
        HeapDataOutputStream hos = new HeapDataOutputStream(serializedObj.length + 32, Version.CURRENT);
        try {
            DataSerializer.writeObject(obj, hos);
            return hos.toByteArray();
        } catch (Exception ex) {
            this.logger.severe(LocalizedStrings.FilterPostAuthorization_FILTERPOSTAUTHORIZATION_AN_EXCEPTION_WAS_THROWN_WHILE_TRYING_TO_SERIALIZE, ex);
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) DataInputStream(java.io.DataInputStream) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException)

Aggregations

HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)134 Test (org.junit.Test)55 IOException (java.io.IOException)40 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)36 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)33 DataInputStream (java.io.DataInputStream)29 ByteArrayInputStream (java.io.ByteArrayInputStream)23 UnitTest (org.apache.geode.test.junit.categories.UnitTest)15 DiskAccessException (org.apache.geode.cache.DiskAccessException)12 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)11 PdxSerializerObject (org.apache.geode.internal.PdxSerializerObject)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)8 Version (org.apache.geode.internal.Version)8 DataInput (java.io.DataInput)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7 OutputStream (java.io.OutputStream)6 Properties (java.util.Properties)6 ByteBuffer (java.nio.ByteBuffer)5 HashMap (java.util.HashMap)5 InternalGemFireException (org.apache.geode.InternalGemFireException)5