Search in sources :

Example 6 with PutOperationContext

use of org.apache.geode.cache.operations.PutOperationContext in project geode by apache.

the class FilterPreAuthorization method authorizeOperation.

public boolean authorizeOperation(String regionName, OperationContext context) {
    assert !context.isPostOperation();
    OperationCode opCode = context.getOperationCode();
    if (opCode.isPut()) {
        PutOperationContext createContext = (PutOperationContext) context;
        // byte[] serializedValue = createContext.getSerializedValue();
        byte[] serializedValue = null;
        Object value = createContext.getValue();
        int valLength;
        byte lastByte;
        if (value == null) {
            // This means serializedValue too is null.
            valLength = 0;
            lastByte = 0;
        } else {
            if (value instanceof byte[]) {
                serializedValue = (byte[]) value;
                valLength = serializedValue.length;
                lastByte = serializedValue[valLength - 1];
            } else {
                ObjectWithAuthz authzObj = new ObjectWithAuthz(value, Integer.valueOf(value.hashCode()));
                createContext.setValue(authzObj, true);
                return true;
            }
        }
        HeapDataOutputStream hos = new HeapDataOutputStream(valLength + 32, Version.CURRENT);
        try {
            InternalDataSerializer.writeUserDataSerializableHeader(ObjectWithAuthz.CLASSID, hos);
            if (serializedValue != null) {
                hos.write(serializedValue);
            }
            // Some value that determines the Principals that can get this object.
            Integer allowedIndex = Integer.valueOf(lastByte);
            DataSerializer.writeObject(allowedIndex, hos);
        } catch (Exception ex) {
            return false;
        }
        createContext.setSerializedValue(hos.toByteArray(), true);
        if (this.logger.fineEnabled())
            this.logger.fine("FilterPreAuthorization: added authorization " + "info for key: " + createContext.getKey());
    } else if (opCode.isPutAll()) {
        PutAllOperationContext createContext = (PutAllOperationContext) context;
        Map map = createContext.getMap();
        Collection entries = map.entrySet();
        Iterator iterator = entries.iterator();
        Map.Entry mapEntry = null;
        while (iterator.hasNext()) {
            mapEntry = (Map.Entry) iterator.next();
            String currkey = (String) mapEntry.getKey();
            Object value = mapEntry.getValue();
            Integer authCode;
            if (value != null) {
                String valStr = value.toString();
                authCode = (int) valStr.charAt(valStr.length() - 1);
            } else {
                authCode = 0;
            }
            ObjectWithAuthz authzObj = new ObjectWithAuthz(value, authCode);
            mapEntry.setValue(authzObj);
            if (this.logger.fineEnabled())
                this.logger.fine("FilterPreAuthorization: putAll: added authorization " + "info for key: " + currkey);
        }
    // Now each of the map's values have become ObjectWithAuthz
    }
    return true;
}
Also used : OperationCode(org.apache.geode.cache.operations.OperationContext.OperationCode) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) PutAllOperationContext(org.apache.geode.cache.operations.PutAllOperationContext) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Iterator(java.util.Iterator) Collection(java.util.Collection) PutOperationContext(org.apache.geode.cache.operations.PutOperationContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PutOperationContext (org.apache.geode.cache.operations.PutOperationContext)6 IOException (java.io.IOException)4 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)4 EventID (org.apache.geode.internal.cache.EventID)4 EventIDHolder (org.apache.geode.internal.cache.EventIDHolder)4 LocalRegion (org.apache.geode.internal.cache.LocalRegion)4 CachedRegionHelper (org.apache.geode.internal.cache.tier.CachedRegionHelper)4 Part (org.apache.geode.internal.cache.tier.sockets.Part)4 AuthorizeRequest (org.apache.geode.internal.security.AuthorizeRequest)4 ByteBuffer (java.nio.ByteBuffer)3 ResourceException (org.apache.geode.cache.ResourceException)3 CacheServerStats (org.apache.geode.internal.cache.tier.sockets.CacheServerStats)3 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)3 InvalidDeltaException (org.apache.geode.InvalidDeltaException)2 DestroyOperationContext (org.apache.geode.cache.operations.DestroyOperationContext)2 StringId (org.apache.geode.i18n.StringId)2 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)2 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)2 DataOutput (java.io.DataOutput)1 DataOutputStream (java.io.DataOutputStream)1