Example usage for io.netty.channel AddressedEnvelope content

List of usage examples for io.netty.channel AddressedEnvelope content

Introduction

In this page you can find the example usage for io.netty.channel AddressedEnvelope content.

Prototype

M content();

Source Link

Document

Returns the message wrapped by this envelope message.

Usage

From source file:io.vertx.core.dns.impl.fix.DnsNameResolverContext.java

License:Apache License

void onResponse(final DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {
    try {/*ww w  .j  a  v  a 2 s .  c o  m*/
        final DnsResponse res = envelope.content();
        final DnsResponseCode code = res.code();
        if (code == DnsResponseCode.NOERROR) {
            final DnsRecordType type = question.type();
            if (type == DnsRecordType.A || type == DnsRecordType.AAAA) {
                onResponseAorAAAA(type, question, envelope);
            } else if (type == DnsRecordType.CNAME) {
                onResponseCNAME(question, envelope);
            }
            return;
        }

        if (traceEnabled) {
            addTrace(envelope.sender(), "response code: " + code + " with " + res.count(DnsSection.ANSWER)
                    + " answer(s) and " + res.count(DnsSection.AUTHORITY) + " authority resource(s)");
        }

        // Retry with the next server if the server did not tell us that the domain does not exist.
        if (code != DnsResponseCode.NXDOMAIN) {
            query(nameServerAddrs.next(), question);
        }
    } finally {
        ReferenceCountUtil.safeRelease(envelope);
    }
}

From source file:io.vertx.core.dns.impl.fix.DnsNameResolverContext.java

License:Apache License

private void onResponseAorAAAA(DnsRecordType qType, DnsQuestion question,
        AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {

    // We often get a bunch of CNAMES as well when we asked for A/AAAA.
    final DnsResponse response = envelope.content();
    final Map<String, String> cnames = buildAliasMap(response);
    final int answerCount = response.count(DnsSection.ANSWER);

    boolean found = false;
    for (int i = 0; i < answerCount; i++) {
        final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
        final DnsRecordType type = r.type();
        if (type != DnsRecordType.A && type != DnsRecordType.AAAA) {
            continue;
        }/*from  w  w w .j  a v  a 2  s .c  om*/

        final String qName = question.name().toLowerCase(Locale.US);
        final String rName = r.name().toLowerCase(Locale.US);

        // Make sure the record is for the questioned domain.
        if (!rName.equals(qName)) {
            // Even if the record's name is not exactly same, it might be an alias defined in the CNAME records.
            String resolved = qName;
            do {
                resolved = cnames.get(resolved);
                if (rName.equals(resolved)) {
                    break;
                }
            } while (resolved != null);

            if (resolved == null) {
                continue;
            }
        }

        if (!(r instanceof DnsRawRecord)) {
            continue;
        }

        final ByteBuf content = ((ByteBufHolder) r).content();
        final int contentLen = content.readableBytes();
        if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
            continue;
        }

        final byte[] addrBytes = new byte[contentLen];
        content.getBytes(content.readerIndex(), addrBytes);

        final InetAddress resolved;
        try {
            resolved = InetAddress.getByAddress(hostname, addrBytes);
        } catch (UnknownHostException e) {
            // Should never reach here.
            throw new Error(e);
        }

        if (resolvedEntries == null) {
            resolvedEntries = new ArrayList<DnsCacheEntry>(8);
        }

        final DnsCacheEntry e = new DnsCacheEntry(hostname, resolved);
        resolveCache.cache(hostname, resolved, r.timeToLive(), parent.ch.eventLoop());
        resolvedEntries.add(e);
        found = true;

        // Note that we do not break from the loop here, so we decode/cache all A/AAAA records.
    }

    if (found) {
        return;
    }

    if (traceEnabled) {
        addTrace(envelope.sender(), "no matching " + qType + " record found");
    }

    // We aked for A/AAAA but we got only CNAME.
    if (!cnames.isEmpty()) {
        onResponseCNAME(question, envelope, cnames, false);
    }
}

From source file:io.vertx.core.dns.impl.fix.DnsNameResolverContext.java

License:Apache License

private void onResponseCNAME(DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {
    onResponseCNAME(question, envelope, buildAliasMap(envelope.content()), true);
}

From source file:io.vertx.core.dns.impl.fix.DnsQueryContext.java

License:Apache License

void finish(AddressedEnvelope<? extends DnsResponse, InetSocketAddress> envelope) {
    final DnsResponse res = envelope.content();
    if (res.count(DnsSection.QUESTION) != 1) {
        logger.warn("Received a DNS response with invalid number of questions: {}", envelope);
        return;/*  w  w  w.  ja  v  a2 s. c  om*/
    }

    if (!question().equals(res.recordAt(DnsSection.QUESTION))) {
        logger.warn("Received a mismatching DNS response: {}", envelope);
        return;
    }

    setSuccess(envelope);
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketByteArrayDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        delegateDecoder.decode(ctx, (ByteBuf) msg.content(), out);
        byte[] content = (byte[]) out.remove(out.size() - 1);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                content, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/* www  .java 2s  .  c  o  m*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketByteArrayEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof byte[]) {
        delegateEncoder.encode(ctx, (byte[]) msg.content(), out);
        ByteBuf buf = (ByteBuf) out.remove(out.size() - 1);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                buf.retain(), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/*from  ww  w  .j  a  va 2 s . com*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketDelimiterDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        Object result = delegateDecoder.decode(ctx, payload);
        AddressedEnvelope<Object, InetSocketAddress> addressEvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                result, msg.recipient(), msg.sender());
        out.add(addressEvelop);// w  w w.  ja va  2  s.  c  om
    }

}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        // Just wrap the message as DatagramPacket, need to make sure the message content is ByteBuf
        DatagramPacket dp = new DatagramPacket(payload.retain(), msg.recipient());
        out.add(dp);//from w ww .ja v a  2s .c  o  m
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketObjectDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        Object result = delegateDecoder.decode(ctx, payload);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                result, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/*from w  ww .j a  va  2 s  .  c o  m*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketObjectEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof Serializable) {
        Serializable payload = (Serializable) msg.content();
        ByteBuf buf = ctx.alloc().heapBuffer();
        delegateObjectEncoder.encode(ctx, payload, buf);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                buf.retain(), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);//from   w  ww . j  a v  a2 s  . co m
    }

}