Search in sources :

Example 41 with Context

use of io.vertx.core.Context in project vertx-proton by vert-x3.

the class ProtonServerImpl method connectHandler.

@Override
public ProtonServerImpl connectHandler(Handler<ProtonConnection> handler) {
    this.handler = handler;
    server.connectHandler(netSocket -> {
        String hostname = null;
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
        // ignore
        }
        final ProtonConnectionImpl connection = new ProtonConnectionImpl(vertx, hostname);
        if (advertiseAnonymousRelayCapability) {
            connection.setOfferedCapabilities(new Symbol[] { ProtonConnectionImpl.ANONYMOUS_RELAY });
        }
        final ProtonSaslAuthenticator authenticator = authenticatorFactory.create();
        ProtonTransportOptions transportOptions = new ProtonTransportOptions();
        transportOptions.setHeartbeat(this.options.getHeartbeat());
        transportOptions.setMaxFrameSize(this.options.getMaxFrameSize());
        connection.bindServer(netSocket, new ProtonSaslAuthenticator() {

            @Override
            public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
                authenticator.init(socket, protonConnection, transport);
            }

            @Override
            public void process(Handler<Boolean> completionHandler) {
                final Context context = Vertx.currentContext();
                authenticator.process(complete -> {
                    final Context callbackContext = vertx.getOrCreateContext();
                    if (context != callbackContext) {
                        throw new IllegalStateException("Callback was not made on the original context");
                    }
                    if (complete) {
                        // The authenticator completed, now check success, do required post processing
                        if (succeeded()) {
                            handler.handle(connection);
                            connection.flush();
                        } else {
                            // auth failed, flush any pending data and disconnect client
                            connection.flush();
                            connection.disconnect();
                        }
                    }
                    completionHandler.handle(complete);
                });
            }

            @Override
            public boolean succeeded() {
                return authenticator.succeeded();
            }
        }, transportOptions);
    });
    return this;
}
Also used : NetSocket(io.vertx.core.net.NetSocket) Context(io.vertx.core.Context) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonServerOptions(io.vertx.proton.ProtonServerOptions) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) Context(io.vertx.core.Context) Future(io.vertx.core.Future) ProtonServer(io.vertx.proton.ProtonServer) ProtonTransportOptions(io.vertx.proton.ProtonTransportOptions) UnknownHostException(java.net.UnknownHostException) Transport(org.apache.qpid.proton.engine.Transport) InetAddress(java.net.InetAddress) NetServer(io.vertx.core.net.NetServer) Symbol(org.apache.qpid.proton.amqp.Symbol) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) UnknownHostException(java.net.UnknownHostException) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonTransportOptions(io.vertx.proton.ProtonTransportOptions) Transport(org.apache.qpid.proton.engine.Transport)

Example 42 with Context

use of io.vertx.core.Context in project vertx-proton by vert-x3.

the class VertxProtonExamples method example5.

public void example5(Vertx vertx, ProtonClient client) {
    Context myContext = vertx.getOrCreateContext();
    myContext.runOnContext(x -> {
        client.connect("hostname", 5672, connectResult -> {
        // In this case the context will be 'myContext' from earlier
        });
    });
}
Also used : Context(io.vertx.core.Context)

Example 43 with Context

use of io.vertx.core.Context in project hono by eclipse.

the class FileBasedCredentialsServiceTest method testGetCredentialsSucceedsForExistingCredentials.

/**
 * Verifies that the service returns existing credentials.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetCredentialsSucceedsForExistingCredentials(final TestContext ctx) {
    register(svc, "tenant", "device", "myId", "myType", new JsonArray(), ctx);
    final Async get = ctx.async();
    svc.get("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> {
        assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK));
        assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId"));
        assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType"));
        get.complete();
    }));
    get.await(2000);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 44 with Context

use of io.vertx.core.Context in project hono by eclipse.

the class FileBasedCredentialsServiceTest method testDoStartLoadsCredentials.

/**
 * Verifies that credentials are successfully loaded from file during startup.
 *
 * @param ctx The test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartLoadsCredentials(final TestContext ctx) {
    // GIVEN a service configured with a file name
    props.setFilename(FILE_NAME);
    when(fileSystem.existsBlocking(props.getFilename())).thenReturn(Boolean.TRUE);
    doAnswer(invocation -> {
        final Buffer data = DeviceRegistryTestUtils.readFile(FILE_NAME);
        Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture(data));
        return null;
    }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
    // WHEN the service is started
    Async startup = ctx.async();
    Future<Void> startFuture = Future.future();
    startFuture.setHandler(ctx.asyncAssertSuccess(s -> {
        startup.complete();
    }));
    svc.doStart(startFuture);
    // THEN the credentials from the file are loaded
    startup.await(2000);
    assertRegistered(svc, Constants.DEFAULT_TENANT, "sensor1", CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, ctx);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 45 with Context

use of io.vertx.core.Context in project hono by eclipse.

the class FileBasedCredentialsServiceTest method testDoStartIgnoresMalformedJson.

/**
 * Verifies that the credentials service successfully starts up even if
 * the file to read credentials from contains malformed JSON.
 *
 * @param ctx The vert.x context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartIgnoresMalformedJson(final TestContext ctx) {
    // GIVEN a registration service configured to read data from a file
    // that contains malformed JSON
    props.setFilename(FILE_NAME);
    when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.TRUE);
    doAnswer(invocation -> {
        final Buffer data = mock(Buffer.class);
        when(data.getBytes()).thenReturn("NO JSON".getBytes(StandardCharsets.UTF_8));
        Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture(data));
        return null;
    }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
    // WHEN starting the service
    Async startup = ctx.async();
    Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
        startup.complete();
    }));
    svc.doStart(startupTracker);
    // THEN startup succeeds
    startup.await(2000);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

Context (io.vertx.core.Context)125 Test (org.junit.Test)99 Handler (io.vertx.core.Handler)75 Vertx (io.vertx.core.Vertx)75 Before (org.junit.Before)58 Buffer (io.vertx.core.buffer.Buffer)57 TestContext (io.vertx.ext.unit.TestContext)57 Future (io.vertx.core.Future)53 Async (io.vertx.ext.unit.Async)53 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)53 RunWith (org.junit.runner.RunWith)53 JsonObject (io.vertx.core.json.JsonObject)49 HttpURLConnection (java.net.HttpURLConnection)48 StandardCharsets (java.nio.charset.StandardCharsets)48 Assert.assertThat (org.junit.Assert.assertThat)48 Mockito (org.mockito.Mockito)48 Rule (org.junit.Rule)39 CoreMatchers.is (org.hamcrest.CoreMatchers.is)38 ArgumentCaptor (org.mockito.ArgumentCaptor)38 Timeout (org.junit.rules.Timeout)36