use of org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder in project milo by eclipse.
the class BinarySerializationFixture method setUp.
@BeforeMethod
public void setUp() {
buffer = Unpooled.buffer();
writer = new OpcUaBinaryStreamEncoder(new TestSerializationContext()).setBuffer(buffer);
reader = new OpcUaBinaryStreamDecoder(new TestSerializationContext()).setBuffer(buffer);
}
use of org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder in project milo by eclipse.
the class BsdParserTest method assertRoundTrip.
protected void assertRoundTrip(String type, Object originalValue, OpcUaBinaryDataTypeCodec<Object> codec) {
System.out.printf("--- assertRoundTrip Type: %s ---\n", type);
System.out.println("originalValue:\t" + originalValue);
ByteBuf buffer = Unpooled.buffer();
codec.encode(context, new OpcUaBinaryStreamEncoder(context).setBuffer(buffer), originalValue);
ByteBuf encodedValue = buffer.copy();
System.out.println("encodedValue:\t" + ByteBufUtil.hexDump(encodedValue));
Object decodedValue = codec.decode(context, new OpcUaBinaryStreamDecoder(context).setBuffer(buffer));
assertEquals(decodedValue, originalValue);
System.out.println("decodedValue:\t" + decodedValue);
}
use of org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder in project milo by eclipse.
the class BsdParserTest method assertRoundTripUsingToString.
/**
* A weaker version of {@link #assertRoundTrip(String, Object, OpcUaBinaryDataTypeCodec)} for values that don't
* implement equals and hashcode or values that contain members not implementing equals and hashcode.
* <p>
* Relies on toString() values to be implemented at all levels instead... not great, but since the built-in structs
* don't implement equals/hashcode it's what we have.
*/
protected void assertRoundTripUsingToString(String type, Object originalValue, OpcUaBinaryDataTypeCodec<Object> codec) {
System.out.printf("--- assertRoundTrip Type: %s ---\n", type);
System.out.println("originalValue:\t" + originalValue);
ByteBuf buffer = Unpooled.buffer();
codec.encode(context, new OpcUaBinaryStreamEncoder(context).setBuffer(buffer), originalValue);
ByteBuf encodedValue = buffer.copy();
System.out.println("encodedValue:\t" + ByteBufUtil.hexDump(encodedValue));
Object decodedValue = codec.decode(context, new OpcUaBinaryStreamDecoder(context).setBuffer(buffer));
assertEquals(decodedValue.toString(), originalValue.toString());
System.out.println("decodedValue:\t" + decodedValue);
}
use of org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder in project milo by eclipse.
the class OpcServerHttpRequestHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest) throws Exception {
String host = httpRequest.headers().get(HttpHeaderNames.HOST);
String uri = httpRequest.uri();
String contentType = httpRequest.headers().get(HttpHeaderNames.CONTENT_TYPE);
String securityPolicyUri = httpRequest.headers().get("OPCUA-SecurityPolicy");
logger.debug("host={} uri={} contentType={} securityPolicy={}", host, uri, contentType, securityPolicyUri);
SecurityPolicy securityPolicy = securityPolicyUri != null ? SecurityPolicy.fromUri(securityPolicyUri) : SecurityPolicy.None;
MessageSecurityMode securityMode = securityPolicy == SecurityPolicy.None ? MessageSecurityMode.None : MessageSecurityMode.Sign;
EndpointDescription endpoint = stackServer.getEndpointDescriptions().stream().filter(e -> {
// TODO use contentType to determine which TransportProfile to match
boolean transportMatch = Objects.equals(e.getTransportProfileUri(), TransportProfile.HTTPS_UABINARY.getUri());
boolean pathMatch = Objects.equals(EndpointUtil.getPath(e.getEndpointUrl()), uri);
boolean securityPolicyMatch = Objects.equals(e.getSecurityPolicyUri(), securityPolicy.getUri());
boolean securityModeMatch = Objects.equals(e.getSecurityMode(), securityMode);
return transportMatch && pathMatch && securityPolicyMatch && securityModeMatch;
}).findFirst().orElseThrow(() -> new UaException(StatusCodes.Bad_TcpEndpointUrlInvalid, "unrecognized endpoint uri: " + uri));
ServerSecureChannel secureChannel = new ServerSecureChannel();
// TODO shared id per endpoint URL / path?
secureChannel.setChannelId(0L);
secureChannel.setSecurityPolicy(securityPolicy);
secureChannel.setMessageSecurityMode(securityMode);
ByteString thumbprint = ByteString.of(DigestUtil.sha1(endpoint.getServerCertificate().bytesOrEmpty()));
Optional<X509Certificate[]> certificateChain = stackServer.getConfig().getCertificateManager().getCertificateChain(thumbprint);
Optional<KeyPair> keyPair = stackServer.getConfig().getCertificateManager().getKeyPair(thumbprint);
certificateChain.ifPresent(chain -> {
secureChannel.setLocalCertificateChain(chain);
secureChannel.setLocalCertificate(chain[0]);
});
keyPair.ifPresent(secureChannel::setKeyPair);
OpcUaBinaryStreamDecoder decoder = new OpcUaBinaryStreamDecoder(stackServer.getSerializationContext());
decoder.setBuffer(httpRequest.content());
try {
UaRequestMessage request = (UaRequestMessage) decoder.readMessage(null);
UInteger requestHandle = request.getRequestHeader().getRequestHandle();
InetSocketAddress remoteSocketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
ServiceRequest serviceRequest = new ServiceRequest(stackServer, request, endpoint, secureChannel.getChannelId(), remoteSocketAddress.getAddress(), null);
serviceRequest.getFuture().whenComplete((response, fault) -> {
if (response != null) {
sendServiceResponse(ctx, request, response);
} else {
sendServiceFault(ctx, requestHandle, fault);
}
});
stackServer.onServiceRequest(uri, serviceRequest);
} catch (Throwable t) {
logger.error("Error decoding UaRequestMessage", t);
sendServiceFault(ctx, null, t);
}
}
use of org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder in project milo by eclipse.
the class VariantSerializationTest method testNullArrayEncodedWithNegativeArraySize.
@Test(description = "Test that a Variant containing a null array encoded with a negative array size to indicate a null value decodes properly.")
public void testNullArrayEncodedWithNegativeArraySize() {
ByteBuf buffer = Unpooled.buffer();
buffer.writeByte(BuiltinDataType.Int16.getTypeId() | (1 << 7));
buffer.writeIntLE(-1);
OpcUaBinaryStreamDecoder reader = new OpcUaBinaryStreamDecoder(new TestSerializationContext());
reader.setBuffer(buffer);
Variant v = reader.readVariant();
assertNotNull(v);
assertNull(v.getValue());
}
Aggregations