use of org.apache.thrift.protocol.TMessage in project providence by morimekta.
the class TProtocolSerializer method serialize.
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException {
CountingOutputStream wrapper = new CountingOutputStream(output);
TTransport transport = new TIOStreamTransport(wrapper);
try {
TProtocol protocol = protocolFactory.getProtocol(transport);
TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence());
protocol.writeMessageBegin(tm);
writeMessage(call.getMessage(), protocol);
protocol.writeMessageEnd();
transport.flush();
wrapper.flush();
return wrapper.getByteCount();
} catch (TException e) {
throw new SerializerException(e, e.getMessage());
}
}
use of org.apache.thrift.protocol.TMessage in project providence by morimekta.
the class TProtocolSerializer method deserialize.
@Nonnull
@Override
@SuppressWarnings("unchecked")
public <Message extends PMessage<Message, Field>, Field extends PField> PServiceCall<Message, Field> deserialize(@Nonnull InputStream input, @Nonnull PService service) throws SerializerException {
PServiceCallType type = null;
TMessage tm = null;
try {
TTransport transport = new TIOStreamTransport(input);
TProtocol protocol = protocolFactory.getProtocol(transport);
tm = protocol.readMessageBegin();
type = PServiceCallType.findById(tm.type);
if (type == null) {
throw new SerializerException("Unknown call type for id " + tm.type).setExceptionType(PApplicationExceptionType.INVALID_MESSAGE_TYPE);
} else if (type == PServiceCallType.EXCEPTION) {
PApplicationException exception = readMessage(protocol, PApplicationException.kDescriptor);
return new PServiceCall(tm.name, type, tm.seqid, exception);
}
PServiceMethod method = service.getMethod(tm.name);
if (method == null) {
throw new SerializerException("No such method " + tm.name + " on " + service.getQualifiedName()).setExceptionType(PApplicationExceptionType.UNKNOWN_METHOD);
}
@SuppressWarnings("unchecked") PMessageDescriptor<Message, Field> descriptor = isRequestCallType(type) ? method.getRequestType() : method.getResponseType();
Message message = readMessage(protocol, descriptor);
protocol.readMessageEnd();
return new PServiceCall<>(tm.name, type, tm.seqid, message);
} catch (TTransportException e) {
throw new SerializerException(e, e.getMessage()).setExceptionType(PApplicationExceptionType.findById(e.getType())).setCallType(type).setSequenceNo(tm != null ? tm.seqid : 0).setMethodName(tm != null ? tm.name : null);
} catch (TException e) {
throw new SerializerException(e, e.getMessage()).setExceptionType(PApplicationExceptionType.PROTOCOL_ERROR).setCallType(type).setSequenceNo(tm != null ? tm.seqid : 0).setMethodName(tm != null ? tm.name : null);
} catch (SerializerException e) {
e.setMethodName(tm.name).setSequenceNo(tm.seqid).setCallType(type);
throw e;
}
}
use of org.apache.thrift.protocol.TMessage in project providence by morimekta.
the class TTupleProtocolSerializer method deserialize.
@Nonnull
@Override
@SuppressWarnings("unchecked")
public <Message extends PMessage<Message, Field>, Field extends PField> PServiceCall<Message, Field> deserialize(@Nonnull InputStream input, @Nonnull PService service) throws SerializerException {
TMessage tm = null;
PServiceCallType type = null;
try {
TTransport transport = new TIOStreamTransport(input);
TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
tm = protocol.readMessageBegin();
type = PServiceCallType.findById(tm.type);
if (type == null) {
throw new SerializerException("Unknown call type for id " + tm.type);
} else if (type == PServiceCallType.EXCEPTION) {
PApplicationException exception = readMessage(protocol, PApplicationException.kDescriptor);
return new PServiceCall(tm.name, type, tm.seqid, exception);
}
PServiceMethod method = service.getMethod(tm.name);
if (method == null) {
throw new SerializerException("No such method " + tm.name + " on " + service.getQualifiedName());
}
PMessageDescriptor<Message, Field> descriptor = isRequestCallType(type) ? method.getRequestType() : method.getResponseType();
Message message = readMessage(protocol, descriptor);
protocol.readMessageEnd();
return new PServiceCall<>(tm.name, type, tm.seqid, message);
} catch (TTransportException e) {
throw new SerializerException(e, "Unable to serialize into transport protocol").setExceptionType(PApplicationExceptionType.findById(e.getType())).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
} catch (TException e) {
throw new SerializerException(e, "Transport exception in protocol").setExceptionType(PApplicationExceptionType.PROTOCOL_ERROR).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
}
}
use of org.apache.thrift.protocol.TMessage in project hive by apache.
the class TUGIBasedProcessor method handleSetUGI.
private void handleSetUGI(TUGIContainingTransport ugiTrans, ThriftHiveMetastore.Processor.set_ugi<Iface> fn, TMessage msg, TProtocol iprot, TProtocol oprot) throws TException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
UserGroupInformation clientUgi = ugiTrans.getClientUGI();
if (null != clientUgi) {
throw new TException(new IllegalStateException("UGI is already set. Resetting is not " + "allowed. Current ugi is: " + clientUgi.getUserName()));
}
set_ugi_args args = fn.getEmptyArgsInstance();
try {
args.read(iprot);
} catch (TProtocolException e) {
iprot.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());
oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
x.write(oprot);
oprot.writeMessageEnd();
oprot.getTransport().flush();
return;
}
iprot.readMessageEnd();
set_ugi_result result = fn.getResult(iface, args);
List<String> principals = result.getSuccess();
// Store the ugi in transport and then continue as usual.
ugiTrans.setClientUGI(UserGroupInformation.createRemoteUser(principals.remove(principals.size() - 1)));
oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.REPLY, msg.seqid));
result.write(oprot);
oprot.writeMessageEnd();
oprot.getTransport().flush();
}
use of org.apache.thrift.protocol.TMessage in project dubbo by alibaba.
the class ThriftCodec method encodeRequest.
private void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException {
RpcInvocation inv = (RpcInvocation) request.getData();
int seqId = nextSeqId();
String serviceName = inv.getAttachment(INTERFACE_KEY);
if (StringUtils.isEmpty(serviceName)) {
throw new IllegalArgumentException("Could not find service name in attachment with key " + INTERFACE_KEY);
}
TMessage message = new TMessage(inv.getMethodName(), TMessageType.CALL, seqId);
String methodArgs = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class).getExtension(channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME)).generateArgsClassName(serviceName, inv.getMethodName());
if (StringUtils.isEmpty(methodArgs)) {
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "Could not encode request, the specified interface may be incorrect.");
}
Class<?> clazz = CACHED_CLASS.get(methodArgs);
if (clazz == null) {
try {
clazz = ClassUtils.forNameWithThreadContextClassLoader(methodArgs);
CACHED_CLASS.putIfAbsent(methodArgs, clazz);
} catch (ClassNotFoundException e) {
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
}
}
TBase args;
try {
args = (TBase) clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
}
for (int i = 0; i < inv.getArguments().length; i++) {
Object obj = inv.getArguments()[i];
if (obj == null) {
continue;
}
TFieldIdEnum field = args.fieldForId(i + 1);
String setMethodName = ThriftUtils.generateSetMethodName(field.getFieldName());
Method method;
try {
method = clazz.getMethod(setMethodName, inv.getParameterTypes()[i]);
} catch (NoSuchMethodException e) {
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
}
try {
method.invoke(args, obj);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
}
}
RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024);
TIOStreamTransport transport = new TIOStreamTransport(bos);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
int headerLength, messageLength;
byte[] bytes = new byte[4];
try {
// magic
protocol.writeI16(MAGIC);
// message length placeholder
protocol.writeI32(Integer.MAX_VALUE);
// message header length placeholder
protocol.writeI16(Short.MAX_VALUE);
// version
protocol.writeByte(VERSION);
// service name
protocol.writeString(serviceName);
// path
protocol.writeString(inv.getAttachment(PATH_KEY));
// dubbo request id
protocol.writeI64(request.getId());
protocol.getTransport().flush();
// header size
headerLength = bos.size();
// message body
protocol.writeMessageBegin(message);
args.write(protocol);
protocol.writeMessageEnd();
protocol.getTransport().flush();
int oldIndex = messageLength = bos.size();
// fill in message length and header length
try {
TFramedTransport.encodeFrameSize(messageLength, bytes);
bos.setWriteIndex(MESSAGE_LENGTH_INDEX);
protocol.writeI32(messageLength);
bos.setWriteIndex(MESSAGE_HEADER_LENGTH_INDEX);
protocol.writeI16((short) (0xffff & headerLength));
} finally {
bos.setWriteIndex(oldIndex);
}
} catch (TException e) {
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
}
buffer.writeBytes(bytes);
buffer.writeBytes(bos.toByteArray());
}
Aggregations