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