|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package org.eclipse.hono.adapter.client.command.amqp; |
| 15 | + |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +import org.apache.qpid.proton.amqp.transport.ErrorCondition; |
| 19 | +import org.eclipse.hono.adapter.client.command.Command; |
| 20 | +import org.eclipse.hono.adapter.client.command.CommandContext; |
| 21 | +import org.eclipse.hono.util.Constants; |
| 22 | + |
| 23 | +import io.opentracing.Span; |
| 24 | +import io.opentracing.SpanContext; |
| 25 | +import io.vertx.proton.ProtonHelper; |
| 26 | + |
| 27 | +/** |
| 28 | + * A wrapper around a legacy {@link org.eclipse.hono.client.CommandContext}. |
| 29 | + */ |
| 30 | +public class ProtonBasedCommandContext implements CommandContext { |
| 31 | + |
| 32 | + private final org.eclipse.hono.client.CommandContext ctx; |
| 33 | + private final ProtonBasedCommand command; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a new command context. |
| 37 | + * |
| 38 | + * @param context The legacy command context to wrap. |
| 39 | + * @throws NullPointerException if context is {@code null}. |
| 40 | + */ |
| 41 | + public ProtonBasedCommandContext(final org.eclipse.hono.client.CommandContext context) { |
| 42 | + this.ctx = Objects.requireNonNull(context); |
| 43 | + this.command = new ProtonBasedCommand(context.getCommand()); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void logCommandToSpan(final Span span) { |
| 48 | + command.logToSpan(span); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Command getCommand() { |
| 53 | + return command; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void accept() { |
| 58 | + ctx.accept(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void release() { |
| 63 | + ctx.release(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void modify(final boolean deliveryFailed, final boolean undeliverableHere) { |
| 68 | + ctx.modify(deliveryFailed, undeliverableHere); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void reject(final String cause) { |
| 73 | + final ErrorCondition error = ProtonHelper.condition(Constants.AMQP_BAD_REQUEST, cause); |
| 74 | + ctx.reject(error); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public <T> T get(final String key) { |
| 79 | + return ctx.get(key); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public <T> T get(final String key, final T defaultValue) { |
| 84 | + return ctx.get(key, defaultValue); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void put(final String key, final Object value) { |
| 89 | + ctx.put(key, value); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public SpanContext getTracingContext() { |
| 94 | + return ctx.getTracingContext(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Span getTracingSpan() { |
| 99 | + return ctx.getTracingSpan(); |
| 100 | + } |
| 101 | +} |
0 commit comments