io.apiman.gateway.platforms.vertx3.services.PolicyToIngestorServiceVertxProxyHandler.java Source code

Java tutorial

Introduction

Here is the source code for io.apiman.gateway.platforms.vertx3.services.PolicyToIngestorServiceVertxProxyHandler.java

Source

/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat 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 io.apiman.gateway.platforms.vertx3.services;

import io.vertx.core.AsyncResult;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.serviceproxy.ProxyHandler;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
  Generated Proxy code - DO NOT EDIT
  @author Roger the Robot
*/
public class PolicyToIngestorServiceVertxProxyHandler extends ProxyHandler {

    public static final long DEFAULT_CONNECTION_TIMEOUT = 5 * 60; // 5 minutes

    private final Vertx vertx;
    private final PolicyToIngestorService service;
    private final long timerID;
    private long lastAccessed;
    private final long timeoutSeconds;

    public PolicyToIngestorServiceVertxProxyHandler(Vertx vertx, PolicyToIngestorService service) {
        this(vertx, service, DEFAULT_CONNECTION_TIMEOUT);
    }

    public PolicyToIngestorServiceVertxProxyHandler(Vertx vertx, PolicyToIngestorService service,
            long timeoutInSecond) {
        this(vertx, service, true, timeoutInSecond);
    }

    public PolicyToIngestorServiceVertxProxyHandler(Vertx vertx, PolicyToIngestorService service, boolean topLevel,
            long timeoutSeconds) {
        this.vertx = vertx;
        this.service = service;
        this.timeoutSeconds = timeoutSeconds;
        if (timeoutSeconds != -1 && !topLevel) {
            long period = timeoutSeconds * 1000 / 2;
            if (period > 10000) {
                period = 10000;
            }
            this.timerID = vertx.setPeriodic(period, this::checkTimedOut);
        } else {
            this.timerID = -1;
        }
        accessed();
    }

    @Override
    public MessageConsumer<JsonObject> registerHandler(String address) {
        MessageConsumer<JsonObject> consumer = vertx.eventBus().<JsonObject>consumer(address).handler(this);
        this.setConsumer(consumer);
        return consumer;
    }

    private void checkTimedOut(long id) {
        long now = System.nanoTime();
        if (now - lastAccessed > timeoutSeconds * 1000000000) {
            service.end(done -> {
            });
            close();
        }
    }

    @Override
    public void close() {
        if (timerID != -1) {
            vertx.cancelTimer(timerID);
        }
        super.close();
    }

    private void accessed() {
        this.lastAccessed = System.nanoTime();
    }

    @Override
    public void handle(Message<JsonObject> msg) {
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "head": {
            service.head(json.getJsonObject("apiResponse") == null ? null
                    : new io.apiman.gateway.platforms.vertx3.io.VertxApiResponse(json.getJsonObject("apiResponse")),
                    createHandler(msg));
            break;
        }
        case "write": {
            service.write((java.lang.String) json.getValue("chunk"));
            break;
        }
        case "end": {
            service.end(createHandler(msg));
            close();
            break;
        }
        case "policyFailure": {
            service.policyFailure(json.getJsonObject("policyFailure") == null ? null
                    : new io.apiman.gateway.platforms.vertx3.io.VertxPolicyFailure(
                            json.getJsonObject("policyFailure")));
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    }

    private <T> Handler<AsyncResult<T>> createHandler(Message msg) {
        return res -> {
            if (res.failed()) {
                msg.fail(-1, res.cause().getMessage());
            } else {
                msg.reply(res.result());
            }
        };
    }

    private <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
        return res -> {
            if (res.failed()) {
                msg.fail(-1, res.cause().getMessage());
            } else {
                msg.reply(new JsonArray(res.result()));
            }
        };
    }

    private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
        return res -> {
            if (res.failed()) {
                msg.fail(-1, res.cause().getMessage());
            } else {
                msg.reply(new JsonArray(new ArrayList<>(res.result())));
            }
        };
    }

    private Handler<AsyncResult<List<Character>>> createListCharHandler(Message msg) {
        return res -> {
            if (res.failed()) {
                msg.fail(-1, res.cause().getMessage());
            } else {
                JsonArray arr = new JsonArray();
                for (Character chr : res.result()) {
                    arr.add((int) chr);
                }
                msg.reply(arr);
            }
        };
    }

    private Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) {
        return res -> {
            if (res.failed()) {
                msg.fail(-1, res.cause().getMessage());
            } else {
                JsonArray arr = new JsonArray();
                for (Character chr : res.result()) {
                    arr.add((int) chr);
                }
                msg.reply(arr);
            }
        };
    }

    private <T> Map<String, T> convertMap(Map map) {
        return map;
    }

    private <T> List<T> convertList(List list) {
        return list;
    }

    private <T> Set<T> convertSet(List list) {
        return new HashSet<T>(list);
    }
}