Search in sources :

Example 1 with ByteString

use of org.eclipse.milo.opcua.stack.core.types.builtin.ByteString in project milo by eclipse.

the class DataTypeDictionaryReader method createDataTypeDictionary.

private CompletableFuture<DataTypeDictionary<?>> createDataTypeDictionary(NodeId dictionaryNodeId, ByteString bs) {
    ByteArrayInputStream is = new ByteArrayInputStream(bs.bytesOrEmpty());
    try {
        DictionaryDescription dictionaryDescription = bsdParser.parse(is);
        String namespaceUri = dictionaryDescription.getNamespaceUri();
        OpcUaBinaryDataTypeDictionary dictionary = new OpcUaBinaryDataTypeDictionary(namespaceUri);
        List<CodecDescription> enumCodecs = dictionaryDescription.getEnumCodecs();
        enumCodecs.forEach(cd -> dictionary.registerEnumCodec(cd.getCodec(), cd.getDescription()));
        logger.debug("enumCodecs.size()={}", enumCodecs.size());
        List<CodecDescription> structCodecs = dictionaryDescription.getStructCodecs();
        logger.debug("structCodecs.size()={}", structCodecs.size());
        if (Namespaces.OPC_UA.equals(namespaceUri)) {
            return registerBuiltinStructCodecs(dictionary, structCodecs);
        } else {
            return registerCustomStructCodecs(dictionaryNodeId, dictionary, structCodecs);
        }
    } catch (JAXBException e) {
        return failedFuture(e);
    }
}
Also used : CodecDescription(org.eclipse.milo.opcua.binaryschema.parser.CodecDescription) ByteArrayInputStream(java.io.ByteArrayInputStream) JAXBException(javax.xml.bind.JAXBException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) DictionaryDescription(org.eclipse.milo.opcua.binaryschema.parser.DictionaryDescription) OpcUaBinaryDataTypeDictionary(org.eclipse.milo.opcua.stack.core.types.OpcUaBinaryDataTypeDictionary)

Example 2 with ByteString

use of org.eclipse.milo.opcua.stack.core.types.builtin.ByteString in project milo by eclipse.

the class DataTypeDictionaryReader method readDataTypeDictionaryBytes.

CompletableFuture<ByteString> readDataTypeDictionaryBytes(NodeId nodeId, int fragmentSize) {
    if (Identifiers.OpcUa_BinarySchema.equals(nodeId)) {
        try (InputStream inputStream = DataTypeDictionaryReader.class.getResourceAsStream("/Opc.Ua.Types.bsd")) {
            assert inputStream != null;
            // noinspection UnstableApiUsage
            ByteString bs = ByteString.of(ByteStreams.toByteArray(inputStream));
            return completedFuture(bs);
        } catch (IOException e) {
            return failedFuture(e);
        }
    } else {
        CompositeByteBuf fragmentBuffer = Unpooled.compositeBuffer();
        CompletableFuture<ByteBuf> future = readFragments(nodeId, fragmentBuffer, fragmentSize, 0);
        return future.thenApply(buffer -> {
            // trim any junk at the end. some servers have a bug
            // that cause a null byte to be appended to the end,
            // which makes it invalid XML.
            int length = buffer.readableBytes();
            for (int i = buffer.writerIndex() - 1; i >= 0; i--) {
                byte lastByte = buffer.getByte(i);
                boolean empty = (lastByte == 0 || Character.isWhitespace(lastByte) || Character.isSpaceChar(lastByte));
                if (!empty)
                    break;
                else
                    length -= 1;
            }
            byte[] bs = new byte[length];
            buffer.readBytes(bs, 0, length);
            if (logger.isDebugEnabled()) {
                String xmlString = new String(bs);
                logger.debug("Dictionary XML: {}", xmlString);
            }
            return ByteString.of(bs);
        });
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) IOException(java.io.IOException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)

Example 3 with ByteString

use of org.eclipse.milo.opcua.stack.core.types.builtin.ByteString in project milo by eclipse.

the class NonceUtil method generateNonce.

/**
 * @param length the length of the nonce to generate.
 * @return a nonce of the given length.
 */
public static ByteString generateNonce(int length) {
    if (length == 0)
        return ByteString.NULL_VALUE;
    byte[] bs = new byte[length];
    Random random = null;
    if (SECURE_RANDOM_ENABLED) {
        random = SECURE_RANDOM.get();
    }
    if (random == null) {
        random = ThreadLocalRandom.current();
    }
    random.nextBytes(bs);
    return new ByteString(bs);
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)

Example 4 with ByteString

use of org.eclipse.milo.opcua.stack.core.types.builtin.ByteString in project milo by eclipse.

the class JsonStructureCodec method opcUaToMemberTypeScalar.

@Override
protected JsonElement opcUaToMemberTypeScalar(String name, Object value, String typeName) {
    if (value == null) {
        return JsonNull.INSTANCE;
    } else if (value instanceof Number) {
        if (value instanceof UByte) {
            return new JsonPrimitive(((UByte) value).shortValue());
        } else if (value instanceof UShort) {
            return new JsonPrimitive(((UShort) value).intValue());
        } else if (value instanceof UInteger) {
            return new JsonPrimitive(((UInteger) value).longValue());
        } else if (value instanceof ULong) {
            return new JsonPrimitive(((ULong) value).toBigInteger());
        } else {
            return new JsonPrimitive((Number) value);
        }
    } else if (value instanceof Boolean) {
        return new JsonPrimitive((Boolean) value);
    } else if (value instanceof String) {
        return new JsonPrimitive((String) value);
    } else if (value instanceof Character) {
        return new JsonPrimitive((Character) value);
    } else if (value instanceof JsonElement) {
        return (JsonElement) value;
    } else if (value instanceof DateTime) {
        return new JsonPrimitive(((DateTime) value).getUtcTime());
    } else if (value instanceof UUID) {
        return new JsonPrimitive(value.toString());
    } else if (value instanceof LocalizedText) {
        return gson.toJsonTree(value);
    } else if (value instanceof QualifiedName) {
        return gson.toJsonTree(value);
    } else if (value instanceof ByteString) {
        ByteString byteString = (ByteString) value;
        byte[] bs = byteString.bytesOrEmpty();
        JsonArray array = new JsonArray();
        for (Byte b : bs) {
            array.add(new JsonPrimitive(b));
        }
        return array;
    } else if (value instanceof XmlElement) {
        String fragment = ((XmlElement) value).getFragment();
        return fragment != null ? new JsonPrimitive(fragment) : JsonNull.INSTANCE;
    } else if (value instanceof NodeId) {
        String nodeId = ((NodeId) value).toParseableString();
        return new JsonPrimitive(nodeId);
    } else if (value instanceof ExpandedNodeId) {
        String xNodeId = ((ExpandedNodeId) value).toParseableString();
        return new JsonPrimitive(xNodeId);
    } else if (value instanceof StatusCode) {
        long code = ((StatusCode) value).getValue();
        return new JsonPrimitive(code);
    } else {
        throw new RuntimeException("could not create JsonElement for value: " + value);
    }
}
Also used : ULong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong) JsonPrimitive(com.google.gson.JsonPrimitive) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) Unsigned.ulong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ulong) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) JsonArray(com.google.gson.JsonArray) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) JsonElement(com.google.gson.JsonElement) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) XmlElement(org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement) UUID(java.util.UUID)

Example 5 with ByteString

use of org.eclipse.milo.opcua.stack.core.types.builtin.ByteString in project milo by eclipse.

the class AttributeWriter method validateDataType.

private static DataValue validateDataType(OpcUaServer server, NodeId dataType, DataValue value) throws UaException {
    Variant variant = value.getValue();
    if (variant == null)
        return value;
    Object o = variant.getValue();
    if (o == null)
        throw new UaException(StatusCodes.Bad_TypeMismatch);
    Class<?> valueClass = o.getClass().isArray() ? ArrayUtil.getType(o) : o.getClass();
    Class<?> expectedClass = getExpectedClass(server, dataType, valueClass);
    if (expectedClass != null) {
        LOGGER.debug("dataTypeId={}, valueClass={}, expectedClass={}", dataType, valueClass.getSimpleName(), expectedClass.getSimpleName());
        if (!expectedClass.isAssignableFrom(valueClass)) {
            // Writing a ByteString to a UByte[] is explicitly allowed by the spec.
            if (o instanceof ByteString && expectedClass == UByte.class) {
                ByteString byteString = (ByteString) o;
                return new DataValue(new Variant(byteString.uBytes()), value.getStatusCode(), value.getSourceTime(), value.getServerTime());
            } else if (expectedClass == Variant.class) {
                // Allow writing anything to a Variant
                return value;
            } else {
                throw new UaException(StatusCodes.Bad_TypeMismatch);
            }
        }
    } else {
        throw new UaException(StatusCodes.Bad_TypeMismatch);
    }
    return value;
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)

Aggregations

ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)59 UaException (org.eclipse.milo.opcua.stack.core.UaException)18 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)15 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)11 X509Certificate (java.security.cert.X509Certificate)9 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)9 Test (org.testng.annotations.Test)9 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)8 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)8 UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)7 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)7 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)7 ByteBuf (io.netty.buffer.ByteBuf)6 UUID (java.util.UUID)6 SignatureData (org.eclipse.milo.opcua.stack.core.types.structured.SignatureData)6 ByteBuffer (java.nio.ByteBuffer)5 KeyPair (java.security.KeyPair)5 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)5 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 List (java.util.List)4