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

com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot Maven / Gradle / Ivy

There is a newer version: 5.7.3
Show newest version
/* Copyright 2018 predic8 GmbH, www.predic8.com

   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 com.predic8.membrane.core.exchange.snapshots;

import com.google.common.collect.ImmutableMap;
import com.predic8.membrane.core.exchange.AbstractExchange;
import com.predic8.membrane.core.http.AbstractBody;
import com.predic8.membrane.core.http.BodyCollectingMessageObserver;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.interceptor.Interceptor;
import com.predic8.membrane.core.model.AbstractExchangeViewerListener;
import com.predic8.membrane.core.util.functionalInterfaces.Consumer;

import java.io.IOException;

public class DynamicAbstractExchangeSnapshot extends AbstractExchangeSnapshot{

    /**
     * @param exc the exchange to snapshot
     * @param flow what to copy from the exchange besides general properties
     * @param bodyCopiedCallback will be called once the {@code flow}'s body has been filled. if null, the body stream will be read
     *                 into memory immediately.
     * @param strategy how to handle body lengths exceeding the {@code limit}.
     * @param limit maximum length of the body.
     */
    public DynamicAbstractExchangeSnapshot(AbstractExchange exc, Interceptor.Flow flow, Consumer bodyCopiedCallback, BodyCollectingMessageObserver.Strategy strategy, long limit) throws IOException {
        super(exc, flow, bodyCopiedCallback, strategy, limit);
        addObservers(exc,this, bodyCopiedCallback, flow);
    }

    /**
     * called by JSON deserializer
     */
    public DynamicAbstractExchangeSnapshot() {
    }


    public static void addObservers(AbstractExchange exc, AbstractExchangeSnapshot excCopy, Consumer callback, Interceptor.Flow flow) {
        exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {
            @Override
            public void addResponse(Response response) {
                response.addObserver(new UpdateExchangeCopyObserver(callback, excCopy, exc, Interceptor.Flow.RESPONSE));
            }

            @Override
            public void setExchangeFinished() {
                update(callback, excCopy, exc, flow);
            }
        });

        ImmutableMap.of(Interceptor.Flow.REQUEST, exc.getRequest(),
                Interceptor.Flow.RESPONSE, exc.getResponse()).forEach((flow2, msg) -> {
            if(msg == null)
                return;
            if (!msg.getBody().getObservers().stream().anyMatch(obs -> obs instanceof UpdateExchangeCopyObserver)) {
                msg.addObserver(new UpdateExchangeCopyObserver(callback, excCopy, exc, flow2));
            }
        });

        update(callback,excCopy,exc,flow);
    }

    public static void update(Consumer callback, AbstractExchangeSnapshot excCopy, AbstractExchange exc, Interceptor.Flow flow) {
        try {
            excCopy = excCopy.updateFrom(exc, flow);
            if(callback != null)
                callback.call(excCopy);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class UpdateExchangeCopyObserver extends BodyCollectingMessageObserver {
        private final Consumer callback;
        private final AbstractExchangeSnapshot excCopy;
        private final AbstractExchange exc;
        private final Interceptor.Flow flow;

        public UpdateExchangeCopyObserver(Consumer callback, AbstractExchangeSnapshot excCopy, AbstractExchange exc, Interceptor.Flow flow) {
            super(Strategy.TRUNCATE, -1);
            this.callback = callback;
            this.excCopy = excCopy;
            this.exc = exc;
            this.flow = flow;
        }

        @Override
        public void bodyRequested(AbstractBody body) {

        }

        @Override
        public void bodyComplete(AbstractBody body) {
            // TODO: handle getBody(body)
            update(callback, excCopy, exc, flow);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy