Search in sources :

Example 1 with UaTransportRequest

use of org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest in project milo by eclipse.

the class UascClientMessageHandler method onSecureMessage.

private void onSecureMessage(ChannelHandlerContext ctx, ByteBuf buffer) throws UaException {
    // skip messageType, chunkType, messageSize
    buffer.skipBytes(3 + 1 + 4);
    long secureChannelId = buffer.readUnsignedIntLE();
    if (secureChannelId != secureChannel.getChannelId()) {
        throw new UaException(StatusCodes.Bad_SecureChannelIdInvalid, "invalid secure channel id: " + secureChannelId);
    }
    if (accumulateChunk(buffer)) {
        final List<ByteBuf> buffersToDecode = chunkBuffers;
        chunkBuffers = new ArrayList<>(maxChunkCount);
        serializationQueue.decode((binaryDecoder, chunkDecoder) -> {
            ByteBuf message;
            long requestId;
            try {
                ChunkDecoder.DecodedMessage decodedMessage = chunkDecoder.decodeSymmetric(secureChannel, buffersToDecode);
                message = decodedMessage.getMessage();
                requestId = decodedMessage.getRequestId();
            } catch (MessageAbortException e) {
                logger.warn("Received message abort chunk; error={}, reason={}", e.getStatusCode(), e.getMessage());
                UaTransportRequest request = pending.remove(e.getRequestId());
                if (request != null) {
                    request.getFuture().completeExceptionally(e);
                } else {
                    logger.warn("No pending request for requestId={}", e.getRequestId());
                }
                return;
            } catch (MessageDecodeException e) {
                logger.error("Error decoding symmetric message", e);
                ctx.close();
                return;
            }
            UaTransportRequest request = pending.remove(requestId);
            try {
                UaResponseMessage response = (UaResponseMessage) binaryDecoder.setBuffer(message).readMessage(null);
                if (request != null) {
                    request.getFuture().complete(response);
                } else {
                    logger.warn("No pending request with requestId={} for {}", requestId, response.getClass().getSimpleName());
                }
            } catch (Throwable t) {
                logger.error("Error decoding UaResponseMessage", t);
                if (request != null) {
                    request.getFuture().completeExceptionally(t);
                }
            } finally {
                message.release();
            }
        });
    }
}
Also used : UaTransportRequest(org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) UaException(org.eclipse.milo.opcua.stack.core.UaException) MessageAbortException(org.eclipse.milo.opcua.stack.core.channel.MessageAbortException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) MessageDecodeException(org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException)

Example 2 with UaTransportRequest

use of org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest in project milo by eclipse.

the class OpcClientHttpCodec method decode.

@Override
protected void decode(ChannelHandlerContext ctx, HttpResponse httpResponse, List<Object> out) throws Exception {
    logger.trace("channelRead0: " + httpResponse);
    UaTransportRequest transportRequest = ctx.channel().attr(KEY_PENDING_REQUEST).getAndSet(null);
    if (httpResponse instanceof FullHttpResponse) {
        String contentType = httpResponse.headers().get(HttpHeaderNames.CONTENT_TYPE);
        FullHttpResponse fullHttpResponse = (FullHttpResponse) httpResponse;
        ByteBuf content = fullHttpResponse.content();
        UaResponseMessage responseMessage;
        switch(transportProfile) {
            case HTTPS_UABINARY:
                {
                    if (!UABINARY_CONTENT_TYPE.equalsIgnoreCase(contentType)) {
                        throw new UaException(StatusCodes.Bad_DecodingError, "unexpected content-type: " + contentType);
                    }
                    OpcUaBinaryStreamDecoder decoder = new OpcUaBinaryStreamDecoder(client.getStaticSerializationContext());
                    decoder.setBuffer(content);
                    responseMessage = (UaResponseMessage) decoder.readMessage(null);
                    break;
                }
            case HTTPS_UAXML:
                {
                    // TODO extract document from SOAP message body
                    throw new UaException(StatusCodes.Bad_InternalError, "no decoder for transport: " + transportProfile);
                }
            default:
                throw new UaException(StatusCodes.Bad_InternalError, "no decoder for transport: " + transportProfile);
        }
        transportRequest.getFuture().complete(responseMessage);
    } else {
        HttpResponseStatus status = httpResponse.status();
        if (status.equals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE)) {
            transportRequest.getFuture().completeExceptionally(new UaException(StatusCodes.Bad_ResponseTooLarge));
        } else {
            transportRequest.getFuture().completeExceptionally(new UaException(StatusCodes.Bad_UnexpectedError, String.format("%s: %s", status.code(), status.reasonPhrase())));
        }
    }
}
Also used : UaTransportRequest(org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) UaException(org.eclipse.milo.opcua.stack.core.UaException) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 UaTransportRequest (org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest)2 UaException (org.eclipse.milo.opcua.stack.core.UaException)2 UaResponseMessage (org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 ChunkDecoder (org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder)1 MessageAbortException (org.eclipse.milo.opcua.stack.core.channel.MessageAbortException)1 MessageDecodeException (org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException)1 OpcUaBinaryStreamDecoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder)1