Search in sources :

Example 96 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class NumericRange method writeToValueAtRange.

public static Object writeToValueAtRange(Variant currentVariant, Variant updateVariant, NumericRange range) throws UaException {
    Object current = currentVariant.getValue();
    Object update = updateVariant.getValue();
    if (current == null || update == null) {
        throw new UaException(StatusCodes.Bad_IndexRangeNoData);
    }
    try {
        return writeToValueAtRange(current, update, range, 1);
    } catch (Throwable ex) {
        throw new UaException(StatusCodes.Bad_IndexRangeNoData, ex);
    }
}
Also used : UaException(org.eclipse.milo.opcua.stack.core.UaException)

Example 97 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class NumericRange method parse.

public static NumericRange parse(String range) throws UaException {
    try {
        String[] ss = range.split(",");
        Bounds[] bounds = new Bounds[ss.length];
        for (int i = 0; i < ss.length; i++) {
            String s = ss[i];
            String[] bs = s.split(":");
            if (bs.length == 1) {
                int index = Integer.parseInt(bs[0]);
                bounds[i] = new Bounds(index, index);
            } else if (bs.length == 2) {
                int low = Integer.parseInt(bs[0]);
                int high = Integer.parseInt(bs[1]);
                if (low == high)
                    throw new UaException(StatusCodes.Bad_IndexRangeInvalid);
                bounds[i] = new Bounds(low, high);
            } else {
                throw new UaException(StatusCodes.Bad_IndexRangeInvalid);
            }
        }
        return new NumericRange(range, bounds);
    } catch (Throwable ex) {
        throw new UaException(StatusCodes.Bad_IndexRangeInvalid, ex);
    }
}
Also used : UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)

Example 98 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class NumericRange method readFromValueAtRange.

private static Object readFromValueAtRange(Object array, NumericRange range, int dimension) throws UaException {
    int dimensionCount = range.getDimensionCount();
    Bounds bounds = range.getDimensionBounds(dimension);
    int low = bounds.getLow();
    int high = bounds.getHigh();
    if (dimension == dimensionCount) {
        if (array.getClass().isArray()) {
            int len = Math.min(high - low + 1, Array.getLength(array));
            Class<?> type = array.getClass().getComponentType();
            Object a = Array.newInstance(type, len);
            for (int i = 0; i < len; i++) {
                Object element = Array.get(array, low + i);
                Array.set(a, i, element);
            }
            return a;
        } else if (array instanceof String) {
            String s = (String) array;
            int to = Math.min(high + 1, s.length());
            return s.substring(low, to);
        } else if (array instanceof ByteString) {
            ByteString bs = (ByteString) array;
            int to = Math.min(high + 1, bs.length());
            byte[] copy = Arrays.copyOfRange(bs.bytesOrEmpty(), low, to);
            return new ByteString(copy);
        } else {
            throw new UaException(StatusCodes.Bad_IndexRangeNoData);
        }
    } else {
        int len = Math.min(high - low + 1, Array.getLength(array));
        Class<?> type = array.getClass().getComponentType();
        Object a = Array.newInstance(type, len);
        for (int i = 0; i < len; i++) {
            Object na = Array.get(array, low + i);
            Object element = readFromValueAtRange(na, range, dimension + 1);
            Array.set(a, i, element);
        }
        return a;
    }
}
Also used : ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)

Example 99 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class NumericRange method writeToValueAtRange.

private static Object writeToValueAtRange(Object current, Object update, NumericRange range, int dimension) throws UaException {
    int dimensionCount = range.getDimensionCount();
    Bounds bounds = range.getDimensionBounds(dimension);
    int low = bounds.getLow();
    int high = bounds.getHigh();
    if (dimension == dimensionCount) {
        if (current.getClass().isArray()) {
            Class<?> currentType = ArrayUtil.getType(current);
            Class<?> updateType = ArrayUtil.getType(update);
            if (currentType != updateType) {
                throw new UaException(StatusCodes.Bad_TypeMismatch, String.format("currentType=%s, updateType=%s", current, update));
            }
            int length = Array.getLength(current);
            Object copy = Array.newInstance(currentType, length);
            if (low >= length || high >= length) {
                throw new UaException(StatusCodes.Bad_IndexRangeNoData);
            }
            for (int i = 0; i < length; i++) {
                if (i < low || i > high) {
                    Object element = Array.get(current, i);
                    Array.set(copy, i, element);
                } else {
                    Object element = Array.get(update, i - low);
                    Array.set(copy, i, element);
                }
            }
            return copy;
        } else if (current instanceof String) {
            String cs = (String) current;
            String us = (String) update;
            int length = cs.length();
            StringBuilder copy = new StringBuilder();
            if (low >= length || high >= length) {
                throw new UaException(StatusCodes.Bad_IndexRangeNoData);
            }
            for (int i = 0; i < length; i++) {
                if (i < low || i > high) {
                    copy.append(cs.charAt(i));
                } else {
                    copy.append(us.charAt(i - low));
                }
            }
            return copy.toString();
        } else if (current instanceof ByteString) {
            ByteString bs = (ByteString) current;
            ByteString us = (ByteString) update;
            int length = bs.length();
            byte[] copy = new byte[length];
            if (low >= length || high >= length) {
                throw new UaException(StatusCodes.Bad_IndexRangeNoData);
            }
            for (int i = 0; i < length; i++) {
                if (i < low || i > high) {
                    copy[i] = bs.byteAt(i);
                } else {
                    copy[i] = us.byteAt(i - low);
                }
            }
            return new ByteString(copy);
        } else {
            throw new UaException(StatusCodes.Bad_IndexRangeNoData);
        }
    } else {
        Class<?> type = current.getClass().getComponentType();
        int length = Array.getLength(current);
        Object copy = Array.newInstance(type, length);
        if (low >= length || high >= length) {
            throw new UaException(StatusCodes.Bad_IndexRangeNoData);
        }
        for (int i = 0; i < length; i++) {
            if (i < low || i > high) {
                Object element = Array.get(current, i);
                Array.set(copy, i, element);
            } else {
                Object c = Array.get(current, i);
                Object u = Array.get(update, i - low);
                Object element = writeToValueAtRange(c, u, range, dimension + 1);
                Array.set(copy, i, element);
            }
        }
        return copy;
    }
}
Also used : UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)

Example 100 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class ChunkDecoder method validateSymmetricSecurityHeaders.

private static void validateSymmetricSecurityHeaders(SecureChannel secureChannel, List<ByteBuf> chunkBuffers) throws UaException {
    ChannelSecurity channelSecurity = secureChannel.getChannelSecurity();
    long currentTokenId = channelSecurity.getCurrentToken().getTokenId().longValue();
    long previousTokenId = channelSecurity.getPreviousToken().map(t -> t.getTokenId().longValue()).orElse(-1L);
    for (ByteBuf chunkBuffer : chunkBuffers) {
        // tokenId starts after messageType + chunkType + messageSize + secureChannelId
        long tokenId = chunkBuffer.getUnsignedIntLE(3 + 1 + 4 + 4);
        if (tokenId != currentTokenId && tokenId != previousTokenId) {
            String message = String.format("received unknown secure channel token: " + "tokenId=%s currentTokenId=%s previousTokenId=%s", tokenId, currentTokenId, previousTokenId);
            throw new UaException(StatusCodes.Bad_SecureChannelTokenUnknown, message);
        }
    }
}
Also used : ErrorMessage(org.eclipse.milo.opcua.stack.core.channel.messages.ErrorMessage) MessageDigest(java.security.MessageDigest) LoggerFactory(org.slf4j.LoggerFactory) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureMessageHeader(org.eclipse.milo.opcua.stack.core.channel.headers.SecureMessageHeader) Cipher(javax.crypto.Cipher) ByteBuffer(java.nio.ByteBuffer) GeneralSecurityException(java.security.GeneralSecurityException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) ByteBuf(io.netty.buffer.ByteBuf) SecurityAlgorithm(org.eclipse.milo.opcua.stack.core.security.SecurityAlgorithm) Buffer(java.nio.Buffer) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) SignatureUtil(org.eclipse.milo.opcua.stack.core.util.SignatureUtil) AsymmetricSecurityHeader(org.eclipse.milo.opcua.stack.core.channel.headers.AsymmetricSecurityHeader) Logger(org.slf4j.Logger) SignatureException(java.security.SignatureException) Signature(java.security.Signature) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) List(java.util.List) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) UaException(org.eclipse.milo.opcua.stack.core.UaException) SequenceHeader(org.eclipse.milo.opcua.stack.core.channel.headers.SequenceHeader) InvalidKeyException(java.security.InvalidKeyException) BufferUtil(org.eclipse.milo.opcua.stack.core.util.BufferUtil) SymmetricSecurityHeader(org.eclipse.milo.opcua.stack.core.channel.headers.SymmetricSecurityHeader) UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Aggregations

UaException (org.eclipse.milo.opcua.stack.core.UaException)213 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)99 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)94 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)53 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)49 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)37 List (java.util.List)28 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)27 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)27 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)27 ArrayList (java.util.ArrayList)26 X509Certificate (java.security.cert.X509Certificate)24 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)23 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)21 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)19 LocalizedText (org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)19 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)17 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)17 LoggerFactory (org.slf4j.LoggerFactory)17 Logger (org.slf4j.Logger)16