content: /*
   * Copyright 2017-2025 Hyland Software, Inc. and its affiliates.
   *
   * 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 org.activiti.cloud.examples.connectors;
  
  import static org.activiti.cloud.examples.connectors.TestErrorConnector.TEST_ERROR_CONNECTOR_CONSUMER;
  
  import java.util.concurrent.CountDownLatch;
  import java.util.concurrent.TimeUnit;
  import org.activiti.cloud.api.process.model.IntegrationRequest;
  import org.activiti.cloud.api.process.model.IntegrationResult;
  import org.activiti.cloud.common.messaging.functional.ConnectorBinding;
  import org.activiti.cloud.common.messaging.functional.ConsumerConnector;
  import org.activiti.cloud.connectors.starter.channels.IntegrationResultSender;
  import org.activiti.cloud.connectors.starter.configuration.ConnectorProperties;
  import org.activiti.cloud.connectors.starter.model.IntegrationResultBuilder;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.messaging.Message;
  import org.springframework.stereotype.Component;
  
  @ConnectorBinding(
      input = ExampleConnectorChannels.EXAMPLE_CONNECTOR,
      condition = "",
      outputHeader = "",
      connectorType = "test-error-connector.throwError"
  )
  @Component(TEST_ERROR_CONNECTOR_CONSUMER + "Connector")
  public class TestErrorConnector implements ConsumerConnector<IntegrationRequest> {
  
      public static final String TEST_ERROR_CONNECTOR_CONSUMER = "testErrorConnectorInput";
  
      private static final Logger logger = LoggerFactory.getLogger(TestErrorConnector.class);
      private final IntegrationResultSender integrationResultSender;
      private final ConnectorProperties connectorProperties;
  
      private CountDownLatch countDownLatch;
  
      public TestErrorConnector(
          IntegrationResultSender integrationResultSender,
          ConnectorProperties connectorProperties
      ) {
          this.integrationResultSender = integrationResultSender;
          this.connectorProperties = connectorProperties;
      }
  
      @Override
      public void accept(IntegrationRequest integrationRequest) {
          String var = integrationRequest.getIntegrationContext().getInBoundVariable("var");
          if (!"replay".equals(var)) {
              throw new RuntimeException("TestErrorConnector");
          } else {
              countDownLatch = new CountDownLatch(1);
          }
  
          logger.info("Processing integration request: {}", integrationRequest);
  
          Message<IntegrationResult> message = IntegrationResultBuilder
              .resultFor(integrationRequest, connectorProperties)
              .buildMessage();
          try {
              countDownLatch.await(10, TimeUnit.SECONDS);
  
              logger.info("Sending integration result: {}", message.getPayload());
  
              integrationResultSender.send(message);
          } catch (InterruptedException e) {
              throw new IllegalStateException(e);
          }
      }
  }
  