info.freelibrary.vertx.s3.service.S3ClientServiceVertxProxyHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-s3-service Show documentation
Show all versions of vertx-s3-service Show documentation
An S3 client library for the Vert.x toolkit
/*
* 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 info.freelibrary.vertx.s3.service;
import info.freelibrary.vertx.s3.service.S3ClientService;
import io.vertx.core.Vertx;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.ReplyException;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import io.vertx.serviceproxy.ProxyHandler;
import io.vertx.serviceproxy.ServiceException;
import io.vertx.serviceproxy.ServiceExceptionMessageCodec;
import io.vertx.serviceproxy.HelperUtils;
import io.vertx.serviceproxy.ServiceBinder;
import info.freelibrary.vertx.s3.HttpHeaders;
import info.freelibrary.vertx.s3.S3Object;
import io.vertx.core.Future;
/*
Generated Proxy code - DO NOT EDIT
@author Roger the Robot
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class S3ClientServiceVertxProxyHandler extends ProxyHandler {
public static final long DEFAULT_CONNECTION_TIMEOUT = 5 * 60; // 5 minutes
private final Vertx vertx;
private final S3ClientService service;
private final long timerID;
private long lastAccessed;
private final long timeoutSeconds;
private final boolean includeDebugInfo;
public S3ClientServiceVertxProxyHandler(Vertx vertx, S3ClientService service){
this(vertx, service, DEFAULT_CONNECTION_TIMEOUT);
}
public S3ClientServiceVertxProxyHandler(Vertx vertx, S3ClientService service, long timeoutInSecond){
this(vertx, service, true, timeoutInSecond);
}
public S3ClientServiceVertxProxyHandler(Vertx vertx, S3ClientService service, boolean topLevel, long timeoutInSecond){
this(vertx, service, true, timeoutInSecond, false);
}
public S3ClientServiceVertxProxyHandler(Vertx vertx, S3ClientService service, boolean topLevel, long timeoutSeconds, boolean includeDebugInfo) {
this.vertx = vertx;
this.service = service;
this.includeDebugInfo = includeDebugInfo;
this.timeoutSeconds = timeoutSeconds;
try {
this.vertx.eventBus().registerDefaultCodec(ServiceException.class,
new ServiceExceptionMessageCodec());
} catch (IllegalStateException ex) {}
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();
}
private void checkTimedOut(long id) {
long now = System.nanoTime();
if (now - lastAccessed > timeoutSeconds * 1000000000) {
service.close();
close();
}
}
@Override
public void close() {
if (timerID != -1) {
vertx.cancelTimer(timerID);
}
super.close();
}
private void accessed() {
this.lastAccessed = System.nanoTime();
}
public void handle(Message msg) {
try{
JsonObject json = msg.body();
String action = msg.headers().get("action");
if (action == null) throw new IllegalStateException("action not specified");
accessed();
switch (action) {
case "put": {
service.put((java.lang.String)json.getValue("aBucket"),
json.getJsonObject("aS3Object") != null ? new info.freelibrary.vertx.s3.S3Object((JsonObject)json.getJsonObject("aS3Object")) : null).onComplete(res -> {
if (res.failed()) {
HelperUtils.manageFailure(msg, res.cause(), includeDebugInfo);
} else {
msg.reply(res.result() != null ? res.result().toJson() : null);
}
});
break;
}
case "get": {
service.get((java.lang.String)json.getValue("aBucket"),
(java.lang.String)json.getValue("aKey")).onComplete(res -> {
if (res.failed()) {
HelperUtils.manageFailure(msg, res.cause(), includeDebugInfo);
} else {
msg.reply(res.result() != null ? res.result().toJson() : null);
}
});
break;
}
case "close": {
service.close().onComplete(HelperUtils.createHandler(msg, includeDebugInfo));
close();
break;
}
default: throw new IllegalStateException("Invalid action: " + action);
}
} catch (Throwable t) {
if (includeDebugInfo) msg.reply(new ServiceException(500, t.getMessage(), HelperUtils.generateDebugInfo(t)));
else msg.reply(new ServiceException(500, t.getMessage()));
throw t;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy