Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright [2017] [Andy Moncsek]
*
* 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.jacpfx.vertx.rest.response.basic;
import static java.util.Optional.ofNullable;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import org.jacpfx.common.VxmsShared;
import org.jacpfx.common.encoder.Encoder;
import org.jacpfx.common.throwable.ThrowableErrorConsumer;
import org.jacpfx.common.throwable.ThrowableFutureConsumer;
import org.jacpfx.vertx.rest.interfaces.basic.ExecuteEventbusObjectCall;
/**
* Created by Andy Moncsek on 12.01.16.
* This class is the end of the fluent API, all data collected to execute the chain.
*/
public class ExecuteRSBasicObject {
protected final String methodId;
protected final VxmsShared vxmsShared;
protected final Throwable failure;
protected final RoutingContext context;
protected final Map headers;
protected final Consumer errorHandler;
protected final Consumer errorMethodHandler;
protected final ThrowableFutureConsumer objectConsumer;
protected final ThrowableErrorConsumer onFailureRespond;
protected final ExecuteEventbusObjectCall excecuteEventBusAndReply;
protected final Encoder encoder;
protected final int httpStatusCode;
protected final int httpErrorCode;
protected final int retryCount;
protected final long timeout;
protected final long circuitBreakerTimeout;
/**
* The constructor to pass all needed members
*
* @param methodId the method identifier
* @param vxmsShared the vxmsShared instance, containing the Vertx instance and other shared
* objects per instance
* @param failure the failure thrown while task execution
* @param errorMethodHandler the error handler
* @param context the vertx routing context
* @param headers the headers to pass to the response
* @param objectConsumer the consumer that takes a Future to complete, producing the object
* response
* @param excecuteEventBusAndReply the response of an event-bus call which is passed to the fluent
* API
* @param encoder the encoder to encode your objects
* @param errorHandler the error handler
* @param onFailureRespond the consumer that takes a Future with the alternate response value in
* case of failure
* @param httpStatusCode the http status code to set for response
* @param httpErrorCode the http error code to set in case of failure handling
* @param retryCount the amount of retries before failure execution is triggered
* @param timeout the amount of time before the execution will be aborted
* @param circuitBreakerTimeout the amount of time before the circuit breaker closed again
*/
public ExecuteRSBasicObject(String methodId,
VxmsShared vxmsShared,
Throwable failure,
Consumer errorMethodHandler,
RoutingContext context,
Map headers,
ThrowableFutureConsumer objectConsumer,
ExecuteEventbusObjectCall excecuteEventBusAndReply,
Encoder encoder,
Consumer errorHandler,
ThrowableErrorConsumer onFailureRespond,
int httpStatusCode,
int httpErrorCode,
int retryCount,
long timeout,
long circuitBreakerTimeout) {
this.methodId = methodId;
this.vxmsShared = vxmsShared;
this.failure = failure;
this.errorMethodHandler = errorMethodHandler;
this.context = context;
this.headers = headers;
this.objectConsumer = objectConsumer;
this.encoder = encoder;
this.errorHandler = errorHandler;
this.onFailureRespond = onFailureRespond;
this.httpStatusCode = httpStatusCode;
this.httpErrorCode = httpErrorCode;
this.retryCount = retryCount;
this.excecuteEventBusAndReply = excecuteEventBusAndReply;
this.timeout = timeout;
this.circuitBreakerTimeout = circuitBreakerTimeout;
}
/**
* Execute the reply chain with given http status code
*
* @param status, the http status code
*/
public void execute(HttpResponseStatus status) {
Objects.requireNonNull(status);
new ExecuteRSBasicObject(methodId,
vxmsShared,
failure,
errorMethodHandler,
context,
headers,
objectConsumer,
excecuteEventBusAndReply,
encoder,
errorHandler,
onFailureRespond,
status.code(),
httpErrorCode,
retryCount,
timeout,
circuitBreakerTimeout).
execute();
}
/**
* Execute the reply chain with given http status code and content-type
*
* @param status, the http status code
* @param contentType , the html content-type
*/
public void execute(HttpResponseStatus status, String contentType) {
Objects.requireNonNull(status);
Objects.requireNonNull(contentType);
new ExecuteRSBasicObject(methodId,
vxmsShared,
failure,
errorMethodHandler,
context,
ResponseExecution.updateContentType(headers, contentType),
objectConsumer,
excecuteEventBusAndReply,
encoder,
errorHandler,
onFailureRespond,
status.code(),
httpErrorCode,
retryCount,
timeout,
circuitBreakerTimeout).
execute();
}
/**
* Executes the reply chain with given html content-type
*
* @param contentType, the html content-type
*/
public void execute(String contentType) {
Objects.requireNonNull(contentType);
new ExecuteRSBasicObject(methodId,
vxmsShared,
failure,
errorMethodHandler,
context,
ResponseExecution.updateContentType(headers, contentType),
objectConsumer,
excecuteEventBusAndReply,
encoder,
errorHandler,
onFailureRespond,
httpStatusCode,
httpErrorCode,
retryCount,
timeout,
circuitBreakerTimeout).
execute();
}
/**
* Execute the reply chain
*/
public void execute() {
final Vertx vertx = vxmsShared.getVertx();
vertx.runOnContext(action -> {
ofNullable(excecuteEventBusAndReply).ifPresent(evFunction -> {
try {
evFunction.execute(vxmsShared,
failure,
errorMethodHandler,
context,
headers,
encoder,
errorHandler,
onFailureRespond,
httpStatusCode,
httpErrorCode,
retryCount,
timeout,
circuitBreakerTimeout);
} catch (Exception e) {
e.printStackTrace();
}
});
ofNullable(objectConsumer).
ifPresent(userOperation -> {
int retry = retryCount;
ResponseExecution.createResponse(methodId,
userOperation,
errorHandler,
onFailureRespond,
errorMethodHandler,
vxmsShared,
failure,
value -> {
if (value.succeeded()) {
if (!value.handledError()) {
respond(value.getResult());
} else {
respond(value.getResult(), httpErrorCode);
}
} else {
respond(value.getCause().getMessage(),
HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
}
checkAndCloseResponse(retry);
}, retry, timeout, circuitBreakerTimeout);
}
);
});
}
protected void checkAndCloseResponse(int retry) {
final HttpServerResponse response = context.response();
if (retry == 0 && !response.ended()) {
response.end();
}
}
protected void respond(String result, int statuscode) {
final HttpServerResponse response = context.response();
if (!response.ended()) {
ResponseExecution.updateHeaderAndStatuscode(headers, statuscode, response);
if (result != null) {
response.end(result);
} else {
response.end();
}
}
}
protected void respond(Serializable result) {
final HttpServerResponse response = context.response();
if (!response.ended()) {
ResponseExecution.updateHeaderAndStatuscode(headers, httpStatusCode, response);
if (result != null) {
ResponseExecution.encode(result, encoder)
.ifPresent(value -> ResponseExecution.sendObjectResult(value, context.response()));
} else {
response.end();
}
}
}
protected void respond(Serializable result, int statuscode) {
final HttpServerResponse response = context.response();
if (!response.ended()) {
ResponseExecution.updateHeaderAndStatuscode(headers, statuscode, response);
if (result != null) {
ResponseExecution.encode(result, encoder)
.ifPresent(value -> ResponseExecution.sendObjectResult(value, context.response()));
} else {
response.end();
}
}
}
}