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);
}
}
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);
});
}
}
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);
}
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);
}
}
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;
}
Aggregations