colesico.framework.rpc.internal.RpcDataPortImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of colesico-rpc Show documentation
Show all versions of colesico-rpc Show documentation
Colesico framework Remote Procedure Call for Services
The newest version!
package colesico.framework.rpc.internal;
import colesico.framework.rpc.RpcError;
import colesico.framework.rpc.RpcException;
import colesico.framework.rpc.teleapi.*;
import colesico.framework.teleapi.TRWFactory;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.lang.reflect.Type;
public class RpcDataPortImpl implements RpcDataPort {
protected final TRWFactory trwFactory;
protected final RpcRequest request;
protected final RpcResponse response;
public RpcDataPortImpl(TRWFactory trwFactory, RpcRequest request, RpcResponse response) {
this.request = request;
this.response = response;
this.trwFactory = trwFactory;
}
@Override
public V read(Type valueType) {
return read(RpcTRContext.of(valueType));
}
@Override
public V read(RpcTRContext context) {
if (context == null) {
throw new RpcException("RPC value reading context required for reading value ");
}
// Try to get accurate reader
RpcTeleReader reader = trwFactory.findReader(RpcTeleReader.class, context.getValueType());
if (reader != null) {
// Ctx can be null for reading by type (Principal reading, etc.)
context.setRequest(request);
return reader.read(context);
}
// Common read
RpcTRContext.ValueGetter valueGetter = context.getValueGetter();
if (valueGetter == null) {
throw new RpcException("RPC value getter required reading type: " + context.getValueType().getTypeName());
}
return valueGetter.get(request);
}
@Override
public void write(V value, Type valueType) {
write(value, RpcTWContext.of(valueType));
}
@Override
public void write(V value, RpcTWContext context) {
// Try to get accurate writer
RpcTeleWriter writer = trwFactory.findWriter(RpcTeleWriter.class, context.getValueType());
if (writer != null) {
context.setResponse(response);
writer.write(value, context);
return;
}
// Common write
response.setResult(value);
}
@Override
public void writeError(T throwable) {
// Create default writing context
RpcTWContext context = RpcTWContext.of(throwable.getClass(), response);
RpcTeleWriter throwableWriter = trwFactory.findWriter(RpcTeleWriter.class, throwable.getClass());
if (throwableWriter != null) {
throwableWriter.write(throwable, context);
return;
}
// If no specific writer try to get root exception
// and determine writer for it
Throwable rootCause = ExceptionUtils.getRootCause(throwable);
if (rootCause != null) {
RpcTeleWriter rootCauseWriter = trwFactory.findWriter(RpcTeleWriter.class, rootCause.getClass());
if (rootCauseWriter != null) {
rootCauseWriter.write(rootCause, context);
return;
}
}
// No specific writer,
// Perform default writing
RpcError error = RpcError.of(throwable.getClass(), ExceptionUtils.getRootCauseMessage(throwable));
context.getResponse().setError(error);
}
}