All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.envimate.httpmate.events.processors.DispatchEventProcessor Maven / Gradle / Ivy

There is a newer version: 1.0.26
Show newest version
/*
 * Copyright (c) 2019 envimate GmbH - https://envimate.com/.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.envimate.httpmate.events.processors;

import com.envimate.httpmate.chains.MetaData;
import com.envimate.httpmate.chains.Processor;
import com.envimate.messageMate.messageBus.MessageBus;
import com.envimate.messageMate.messageFunction.MessageFunction;
import com.envimate.messageMate.messageFunction.ResponseFuture;
import com.envimate.messageMate.processingContext.EventType;
import com.envimate.messageMate.processingContext.ProcessingContext;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.Map;
import java.util.concurrent.ExecutionException;

import static com.envimate.httpmate.HttpMateChainKeys.*;
import static com.envimate.httpmate.events.EventModule.*;
import static com.envimate.httpmate.events.processors.EventDispatchingException.eventDispatchingException;
import static com.envimate.messageMate.messageFunction.MessageFunctionBuilder.aMessageFunction;
import static java.lang.Thread.currentThread;
import static java.util.Optional.ofNullable;

@ToString
@EqualsAndHashCode
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class DispatchEventProcessor implements Processor {
    private final MessageFunction messageFunction;

    public static Processor dispatchEventProcessor(final MessageBus messageBus) {
        final MessageFunction messageFunction = aMessageFunction(messageBus);
        return new DispatchEventProcessor(messageFunction);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void apply(final MetaData metaData) {
        final EventType eventType = metaData.get(EVENT_TYPE);
        final Object event = metaData.get(EVENT);
        final ResponseFuture request = messageFunction.request(eventType, event);
        try {
            final ProcessingContext> raw = (ProcessingContext>) (Object) request.getRaw();
            if (raw.getErrorPayload() != null) {
                final Map errorPayload = (Map) raw.getErrorPayload();
                final Throwable exception = (Throwable) errorPayload.get("Exception");
                //metaData.set(EXCEPTION, exception);
                throw eventDispatchingException(exception);
            } else {
                final Map response = raw.getPayload();
                metaData.set(EVENT_RETURN_VALUE, ofNullable(response));
            }
        } catch (final InterruptedException e) {
            request.cancel(true);
            currentThread().interrupt();
        } catch (final ExecutionException e) {
            throw eventDispatchingException(e.getCause());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy