From 22713a886967755f61181ee2f79779a23ed95845 Mon Sep 17 00:00:00 2001 From: Andrea Tarocchi Date: Thu, 23 May 2019 22:18:17 +0200 Subject: [PATCH] Added servlet registration for api-provider and webhook. Fix #5221 #5222 --- app/server/controller/pom.xml | 6 ++ .../integration/camelk/CamelKSupport.java | 37 +++----- .../camelk/customizer/OpenApiCustomizer.java | 19 +++- .../camelk/customizer/WebhookCustomizer.java | 72 ++++++++++++++ .../customizer/OpenApiCustomizerTest.java | 10 ++ .../customizer/WebhookCustomizerTest.java | 94 +++++++++++++++++++ 6 files changed, 210 insertions(+), 28 deletions(-) create mode 100644 app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizer.java create mode 100644 app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizerTest.java diff --git a/app/server/controller/pom.xml b/app/server/controller/pom.xml index a74825093df..3fcf57f2d56 100644 --- a/app/server/controller/pom.xml +++ b/app/server/controller/pom.xml @@ -215,6 +215,12 @@ test + + io.syndesis.connector + connector-webhook + test + + diff --git a/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/CamelKSupport.java b/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/CamelKSupport.java index 4600d27264e..973f0b7e0f1 100644 --- a/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/CamelKSupport.java +++ b/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/CamelKSupport.java @@ -59,30 +59,28 @@ public final class CamelKSupport { // IntegrationPhaseWaitingForPlatform IntegrationPhase = "Waiting For Platform" // // IntegrationPhaseBuildingContext -- // IntegrationPhaseBuildingContext IntegrationPhase = "Building Context" - // // IntegrationPhaseBuildImageSubmitted -- - // IntegrationPhaseBuildImageSubmitted IntegrationPhase = "Build Image Submitted" - // // IntegrationPhaseBuildImageRunning -- - // IntegrationPhaseBuildImageRunning IntegrationPhase = "Build Image Running" + // // IntegrationPhaseResolvingContext -- + // IntegrationPhaseResolvingContext IntegrationPhase = "Resolving Context" // // IntegrationPhaseDeploying -- // IntegrationPhaseDeploying IntegrationPhase = "Deploying" // // IntegrationPhaseRunning -- // IntegrationPhaseRunning IntegrationPhase = "Running" // // IntegrationPhaseError -- // IntegrationPhaseError IntegrationPhase = "Error" - // // IntegrationPhaseBuildFailureRecovery -- - // IntegrationPhaseBuildFailureRecovery IntegrationPhase = "Building Failure Recovery" + // // IntegrationPhaseDeleting -- + // IntegrationPhaseDeleting IntegrationPhase = "Deleting" public static final ImmutableSet CAMEL_K_STARTED_STATES = ImmutableSet.of( "Waiting For Platform", "Building Context", - "Build Image Submitted", - "Build Image Running", + "Resolving Context", "Deploying"); public static final ImmutableSet CAMEL_K_FAILED_STATES = ImmutableSet.of( "Error", "Building Failure Recovery"); public static final ImmutableSet CAMEL_K_RUNNING_STATES = ImmutableSet.of( - "Running" ); + "Running", + "Deleting"); public static final String CAMEL_K_INTEGRATION_CRD_NAME = "integrations.camel.apache.org"; public static final String CAMEL_K_INTEGRATION_CRD_GROUP = "camel.apache.org"; @@ -182,21 +180,6 @@ public static Properties secretToProperties(Secret secret) { return properties; } -// @SuppressWarnings("unchecked") -// public static io.syndesis.server.controller.integration.camelk.crd.Integration getIntegrationCR( -// OpenShiftService openShiftService, -// CustomResourceDefinition integrationCRD, -// IntegrationDeployment integrationDeployment) { -// -// return openShiftService.getCR( -// integrationCRD, -// io.syndesis.server.controller.integration.camelk.crd.Integration.class, -// IntegrationList.class, -// DoneableIntegration.class, -// Names.sanitize(integrationDeployment.getIntegrationId().get()) -// ); -// } - @SuppressWarnings("unchecked") public static List getIntegrationCRbyLabels( OpenShiftService openShiftService, @@ -249,6 +232,10 @@ public static String integrationName(String integrationName) { return Names.sanitize("i-" + integrationName); } + public static boolean isWebhookPresent(io.syndesis.common.model.integration.Integration integration) { + return integration.getUsedConnectorIds().contains("webhook"); + } + public static EnumSet determineExposure(ControllersConfigurationProperties properties, IntegrationDeployment integrationDeployment) { boolean needsExposure = integrationDeployment.getSpec() .getFlows().stream() @@ -257,7 +244,7 @@ public static EnumSet determineExposure(ControllersConfigurationProper .flatMap(action -> action.getTags().stream()) .anyMatch("expose"::equals); - boolean webHook = integrationDeployment.getSpec().getUsedConnectorIds().contains("webhook"); + boolean webHook = CamelKSupport.isWebhookPresent(integrationDeployment.getSpec()); if (needsExposure) { if (properties.isExposeVia3scale() && !webHook) { diff --git a/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizer.java b/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizer.java index f2bf4bd74af..f92d4bb7398 100644 --- a/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizer.java +++ b/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizer.java @@ -32,6 +32,7 @@ import io.syndesis.server.controller.ControllersConfigurationProperties; import io.syndesis.server.controller.integration.camelk.CamelKPublishHandler; import io.syndesis.server.controller.integration.camelk.CamelKSupport; +import io.syndesis.server.controller.integration.camelk.crd.ConfigurationSpec; import io.syndesis.server.controller.integration.camelk.crd.DataSpec; import io.syndesis.server.controller.integration.camelk.crd.Integration; import io.syndesis.server.controller.integration.camelk.crd.IntegrationSpec; @@ -88,6 +89,18 @@ public Integration customize(IntegrationDeployment deployment, Integration integ spec.addResources(generateOpenAPIResource(res.get())); spec.addSources(generateOpenAPIRestDSL(res.get())); spec.addSources(generateOpenAPIRestEndpoint()); + spec.addConfiguration( + new ConfigurationSpec.Builder() + .type("property") + .value("customizer.servletregistration.enabled=true") + .build() + ); + spec.addConfiguration( + new ConfigurationSpec.Builder() + .type("property") + .value("customizer.servletregistration.path=/*") + .build() + ); } catch (Exception e) { throw new IllegalStateException(e); } @@ -99,15 +112,15 @@ public Integration customize(IntegrationDeployment deployment, Integration integ @SuppressWarnings("PMD.SignatureDeclareThrowsException") private ResourceSpec generateOpenAPIResource(OpenApi openApi) throws Exception { - //we always compress open api definition final byte[] openApiBytes = openApi.getDocument(); - final String compressedContent = CamelKSupport.compress(openApiBytes); +// final String content = configuration.getCamelk().isCompression() ? CamelKSupport.compress(openApiBytes) : new String(openApiBytes, UTF_8); return new ResourceSpec.Builder() .dataSpec(new DataSpec.Builder() + //we always compress openapi spec document .compression(true) .name("openapi.json") - .content(compressedContent) + .content(CamelKSupport.compress(openApiBytes)) .build()) .type("data") .build(); diff --git a/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizer.java b/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizer.java new file mode 100644 index 00000000000..7121e0871ab --- /dev/null +++ b/app/server/controller/src/main/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizer.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2016 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.syndesis.server.controller.integration.camelk.customizer; + +import io.syndesis.common.model.integration.IntegrationDeployment; +import io.syndesis.integration.api.IntegrationResourceManager; +import io.syndesis.server.controller.integration.camelk.CamelKSupport; +import io.syndesis.server.controller.integration.camelk.crd.ConfigurationSpec; +import io.syndesis.server.controller.integration.camelk.crd.Integration; +import io.syndesis.server.controller.integration.camelk.crd.IntegrationSpec; +import io.syndesis.server.openshift.Exposure; +import org.springframework.stereotype.Component; + +import java.util.EnumSet; + +/** + * Configure OpenApi + */ +@Component +public class WebhookCustomizer implements CamelKIntegrationCustomizer { + private final IntegrationResourceManager resourceManager; + + public WebhookCustomizer(IntegrationResourceManager resourceManager) { + this.resourceManager = resourceManager; + } + + @Override + public Integration customize(IntegrationDeployment deployment, Integration integration, EnumSet exposure) { + IntegrationSpec.Builder spec = new IntegrationSpec.Builder(); + if (integration.getSpec() != null) { + spec = spec.from(integration.getSpec()); + } + + if (!CamelKSupport.isWebhookPresent(deployment.getSpec())) { + return integration; + } + + try { + spec.addConfiguration( + new ConfigurationSpec.Builder() + .type("property") + .value("customizer.servletregistration.enabled=true") + .build() + ); + spec.addConfiguration( + new ConfigurationSpec.Builder() + .type("property") + .value("customizer.servletregistration.path=/webhook/*") + .build() + ); + } catch (Exception e) { + throw new IllegalStateException(e); + } + + integration.setSpec(spec.build()); + + return integration; + } +} diff --git a/app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizerTest.java b/app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizerTest.java index 0323662abe6..aef9ee440d6 100644 --- a/app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizerTest.java +++ b/app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/OpenApiCustomizerTest.java @@ -31,6 +31,7 @@ import io.syndesis.server.controller.ControllersConfigurationProperties; import io.syndesis.server.controller.integration.camelk.TestResourceManager; import io.syndesis.server.openshift.Exposure; +import org.assertj.core.api.Condition; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -68,6 +69,15 @@ public void testOpenApiCustomizer() throws Exception { EnumSet.of(Exposure.SERVICE) ); + assertThat(i.getSpec().getConfiguration()).hasSize(2); + assertThat(i.getSpec().getConfiguration()).anyMatch( + c -> Objects.equals("customizer.servletregistration.enabled=true", c.getValue()) + && Objects.equals("property", c.getType()) + ); + assertThat(i.getSpec().getConfiguration()).anyMatch( + c -> Objects.equals("customizer.servletregistration.path=/*", c.getValue()) + && Objects.equals("property", c.getType()) + ); assertThat(i.getSpec().getSources()).anyMatch( s -> Objects.equals("openapi-routes", s.getDataSpec().getName()) && Objects.equals("xml", s.getLanguage()) && !s.getDataSpec().getCompression().booleanValue() diff --git a/app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizerTest.java b/app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizerTest.java new file mode 100644 index 00000000000..9c2893ef02a --- /dev/null +++ b/app/server/controller/src/test/java/io/syndesis/server/controller/integration/camelk/customizer/WebhookCustomizerTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2016 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.syndesis.server.controller.integration.camelk.customizer; + +import io.syndesis.common.model.action.ConnectorAction; +import io.syndesis.common.model.action.ConnectorDescriptor; +import io.syndesis.common.model.connection.Connection; +import io.syndesis.common.model.connection.Connector; +import io.syndesis.common.model.integration.Integration; +import io.syndesis.common.model.integration.IntegrationDeployment; +import io.syndesis.common.model.integration.Step; +import io.syndesis.common.model.integration.StepKind; +import io.syndesis.common.util.Json; +import io.syndesis.server.controller.integration.camelk.TestResourceManager; +import io.syndesis.server.openshift.Exposure; +import org.junit.Test; + +import java.io.InputStream; +import java.util.EnumSet; +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; + +public class WebhookCustomizerTest { + @Test + public void testOpenApiCustomizer() throws Exception { +// URL location = getClass().getResource("/petstore.json"); +// byte[] content = Files.readAllBytes(Paths.get(location.toURI())); + + TestResourceManager manager = new TestResourceManager(); +// manager.put("petstore", new OpenApi.Builder().document(content).id("petstore").build()); + + Connector connector; + ConnectorAction webhookIncomingAction; + + try (InputStream is = WebhookCustomizerTest.class.getResourceAsStream("/META-INF/syndesis/connector/webhook.json")) { + connector = Json.readFromStream(is, Connector.class); + webhookIncomingAction = connector.getActions(ConnectorAction.class).stream() + .filter(a -> a.getId().get().equals("io.syndesis:webhook-incoming")) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException()); + } + + webhookIncomingAction = webhookIncomingAction.builder().descriptor(new ConnectorDescriptor.Builder().connectorId("webhook").build()).build(); + + Integration integration = manager.newIntegration( + new Step.Builder() + .stepKind(StepKind.endpoint) + .putConfiguredProperty("contextPath", "token") + .action(webhookIncomingAction) + .connection(new Connection.Builder() + .connector(connector) + .build()) + .build() + ); + + IntegrationDeployment deployment = new IntegrationDeployment.Builder() + .userId("user") + .id("idId") + .spec(integration) + .build(); + + CamelKIntegrationCustomizer customizer = new WebhookCustomizer(manager); + + io.syndesis.server.controller.integration.camelk.crd.Integration i = customizer.customize( + deployment, + new io.syndesis.server.controller.integration.camelk.crd.Integration(), + EnumSet.of(Exposure.SERVICE) + ); + + assertThat(i.getSpec().getConfiguration()).hasSize(2); + assertThat(i.getSpec().getConfiguration()).anyMatch( + c -> Objects.equals("customizer.servletregistration.enabled=true", c.getValue()) + && Objects.equals("property", c.getType()) + ); + assertThat(i.getSpec().getConfiguration()).anyMatch( + c -> Objects.equals("customizer.servletregistration.path=/webhook/*", c.getValue()) + && Objects.equals("property", c.getType()) + ); + } +}