Search in sources :

Example 1 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class PluginLoadingForStartup method loadPlugin.

private static NamedPlugin loadPlugin(Environment environment, ConfiguredPluginFactory factory) {
    PluginFactory.Environment pluginEnvironment = new PluginFactory.Environment() {

        @Override
        public <T> T pluginConfig(Class<T> clazz) {
            return factory.pluginConfig(clazz);
        }

        @Override
        public Configuration configuration() {
            return environment.configuration();
        }

        @Override
        public MeterRegistry pluginMeterRegistry() {
            return environment.meterRegistry().scope("plugin.internal." + factory.name().toLowerCase());
        }

        @Override
        @Deprecated
        public MetricRegistry metricRegistry() {
            return environment.metricRegistry().scope("plugin.internal." + factory.name());
        }
    };
    Plugin plugin = factory.pluginFactory().create(pluginEnvironment);
    return namedPlugin(factory.name(), plugin);
}
Also used : Environment(com.hotels.styx.Environment) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) Plugin(com.hotels.styx.api.plugins.spi.Plugin) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin)

Example 2 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class StyxServerTest method invokesPluginLifecycleMethods.

@Test
public void invokesPluginLifecycleMethods() {
    Plugin pluginMock1 = mock(Plugin.class);
    Plugin pluginMock2 = mock(Plugin.class);
    StyxServer styxServer = styxServerWithPlugins(Map.of("mockplugin1", pluginMock1, "mockplugin2", pluginMock2));
    try {
        styxServer.startAsync().awaitRunning();
        verify(pluginMock1).styxStarting();
        verify(pluginMock2).styxStarting();
        styxServer.stopAsync().awaitTerminated();
        verify(pluginMock1).styxStopping();
        verify(pluginMock2).styxStopping();
    } finally {
        stopIfRunning(styxServer);
    }
}
Also used : NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Example 3 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class TestPlugin method intercept.

@Override
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {
    String header = xHcomPluginsHeader(request);
    LiveHttpRequest newRequest = request.newBuilder().header(X_HCOM_PLUGINS_HEADER, header).header("X-Hcom-Styx-Started", styxStarted).header("X-Hcom-Styx-Stopped", styxStopped).build();
    Function<ByteBuf, String> byteBufStringFunction = byteBuf -> byteBuf.toString(UTF_8);
    return chain.proceed(newRequest).flatMap(response -> response.aggregate(1 * 1024 * 1024)).map(response -> response.newBuilder().header(X_HCOM_PLUGINS_HEADER, header).header("X-Hcom-Styx-Started", styxStarted).header("X-Hcom-Styx-Stopped", styxStopped).addHeader("X-Plugin-Identifier", config.getId()).build()).map(HttpResponse::stream);
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) Plugin(com.hotels.styx.api.plugins.spi.Plugin) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Map(java.util.Map) Collections(java.util.Collections) Function(java.util.function.Function) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 4 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class StyxServerTest method executesPluginsWhenProxying.

@Test
public void executesPluginsWhenProxying() {
    Plugin responseDecorator = (request, chain) -> chain.proceed(request).map(response -> response.newBuilder().header("plugin-executed", "yes").build());
    PluginFactory pluginFactory = environment -> responseDecorator;
    styxServer = new StyxServer.Builder().addRoute("/", originServer1.port()).addPluginFactory("response-decorator", pluginFactory, null).start();
    HttpResponse response = await(client.sendRequest(get(format("http://localhost:%d/foo", styxServer.proxyHttpPort())).build()));
    assertThat(response.status(), is(OK));
    assertThat(response.header("origin"), isValue("first"));
    assertThat(response.header("plugin-executed"), isValue("yes"));
    configureFor(originServer1.port());
    WireMock.verify(getRequestedFor(urlPathEqualTo("/foo")));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpResponse(com.hotels.styx.api.HttpResponse) StyxFutures.await(com.hotels.styx.common.StyxFutures.await) Matchers.not(org.hamcrest.Matchers.not) StyxHttpClient(com.hotels.styx.client.StyxHttpClient) HttpClient(com.hotels.styx.client.HttpClient) WireMock(com.github.tomakehurst.wiremock.client.WireMock) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) WireMockConfiguration.wireMockConfig(com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig) HttpRequest.get(com.hotels.styx.api.HttpRequest.get) Map(java.util.Map) Origins.origin(com.hotels.styx.testapi.Origins.origin) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) WireMock.configureFor(com.github.tomakehurst.wiremock.client.WireMock.configureFor) WireMock.urlPathEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo) Collections.emptyMap(java.util.Collections.emptyMap) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Plugin(com.hotels.styx.api.plugins.spi.Plugin) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Matchers.allOf(org.hamcrest.Matchers.allOf) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) TlsSettings(com.hotels.styx.api.extension.service.TlsSettings) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) IsOptional.isValue(com.hotels.styx.support.matchers.IsOptional.isValue) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) OK(com.hotels.styx.api.HttpResponseStatus.OK) Optional(java.util.Optional) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.getRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Example 5 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class StyxServerTest method informsPluginOfStopping.

@Test
public void informsPluginOfStopping() {
    Plugin plugin = mock(Plugin.class);
    styxServer = new StyxServer.Builder().addRoute("/", originServer1.port()).addPlugin("plugin-with-shutdownBehaviour", plugin).start();
    styxServer.stop();
    verify(plugin).styxStopping();
}
Also used : Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Aggregations

Plugin (com.hotels.styx.api.plugins.spi.Plugin)16 Test (org.junit.jupiter.api.Test)13 NamedPlugin.namedPlugin (com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin)10 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)9 Eventual (com.hotels.styx.api.Eventual)8 OK (com.hotels.styx.api.HttpResponseStatus.OK)7 Optional (java.util.Optional)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Matchers.is (org.hamcrest.Matchers.is)7 Matchers.not (org.hamcrest.Matchers.not)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 Mockito.mock (org.mockito.Mockito.mock)7 Mockito.verify (org.mockito.Mockito.verify)7 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)6 Environment (com.hotels.styx.Environment)5 NamedPlugin (com.hotels.styx.proxy.plugin.NamedPlugin)5 Eventual.error (com.hotels.styx.api.Eventual.error)4 HttpHandler (com.hotels.styx.api.HttpHandler)4 Chain (com.hotels.styx.api.HttpInterceptor.Chain)4 HttpResponse (com.hotels.styx.api.HttpResponse)4