Search in sources :

Example 1 with OpcUaBinaryStreamDecoder

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);
}
Also used : OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) TestSerializationContext(org.eclipse.milo.opcua.stack.core.serialization.TestSerializationContext) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with OpcUaBinaryStreamDecoder

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);
}
Also used : OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) ByteBuf(io.netty.buffer.ByteBuf) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)

Example 3 with OpcUaBinaryStreamDecoder

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);
}
Also used : OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) ByteBuf(io.netty.buffer.ByteBuf) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)

Example 4 with OpcUaBinaryStreamDecoder

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);
    }
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) MessageSecurityMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode) KeyPair(java.security.KeyPair) OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) UaRequestMessage(org.eclipse.milo.opcua.stack.core.serialization.UaRequestMessage) UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) InetSocketAddress(java.net.InetSocketAddress) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ServiceRequest(org.eclipse.milo.opcua.stack.server.services.ServiceRequest) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)

Example 5 with OpcUaBinaryStreamDecoder

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());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) TestSerializationContext(org.eclipse.milo.opcua.stack.core.serialization.TestSerializationContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Aggregations

OpcUaBinaryStreamDecoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder)7 ByteBuf (io.netty.buffer.ByteBuf)5 OpcUaBinaryStreamEncoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)3 UaException (org.eclipse.milo.opcua.stack.core.UaException)2 TestSerializationContext (org.eclipse.milo.opcua.stack.core.serialization.TestSerializationContext)2 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 InetSocketAddress (java.net.InetSocketAddress)1 KeyPair (java.security.KeyPair)1 UaTransportRequest (org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest)1 UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)1 ServerSecureChannel (org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel)1 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)1 UaRequestMessage (org.eclipse.milo.opcua.stack.core.serialization.UaRequestMessage)1 UaResponseMessage (org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage)1 OpcUaBinaryDataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)1 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)1 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)1 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)1