use of com.google.protobuf.DynamicMessage.Builder in project BIMserver by opensourceBIM.
the class ProtocolBuffersReflector method callMethod.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Object callMethod(String interfaceName, String methodName, Class<?> definedReturnType, KeyValuePair... args) throws ServerException, UserException {
try {
MethodDescriptorContainer methodDescriptorContainer;
try {
methodDescriptorContainer = protocolBuffersMetaData.getMethod(interfaceName, methodName);
SMethod sMethod = servicesMap.getBySimpleName(interfaceName).getSMethod(methodName);
Descriptor inputDescriptor = methodDescriptorContainer.getInputDescriptor();
Builder builder = DynamicMessage.newBuilder(methodDescriptorContainer.getInputDescriptor());
int i = 0;
for (FieldDescriptor field : inputDescriptor.getFields()) {
Object arg = args[i].getValue();
if (field.getJavaType() == JavaType.ENUM) {
EnumDescriptor enumType = field.getEnumType();
builder.setField(field, enumType.findValueByName(arg.toString()));
} else {
if (arg instanceof SBase) {
builder.setField(field, convertSObjectToProtocolBuffersObject((SBase) arg, sMethod.getParameter(i).getType()));
} else if (arg instanceof DataHandler) {
DataHandler dataHandler = (DataHandler) arg;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
IOUtils.copy(dataHandler.getInputStream(), baos);
} catch (IOException e) {
LOGGER.error("", e);
}
builder.setField(field, ByteString.copyFrom(baos.toByteArray()));
} else {
if (arg != null) {
if (arg instanceof Collection) {
Collection col = (Collection) arg;
Iterator iterator = col.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
builder.addRepeatedField(field, o);
}
} else {
builder.setField(field, arg);
}
}
}
}
i++;
}
DynamicMessage message = builder.build();
Message result = channel.callBlockingMethod(methodDescriptorContainer, message);
FieldDescriptor errorMessageField = methodDescriptorContainer.getOutputField("errorMessage");
String errorMessage = (String) result.getField(errorMessageField);
if (errorMessage.equals("OKE")) {
if (result.getDescriptorForType().getName().equals("VoidResponse")) {
return null;
} else {
FieldDescriptor outputField = methodDescriptorContainer.getOutputField("value");
Object value = result.getField(outputField);
if (outputField.isRepeated()) {
if (value instanceof Collection) {
Collection collection = (Collection) value;
Collection x = null;
if (definedReturnType == List.class) {
x = new ArrayList();
} else if (definedReturnType == Set.class) {
x = new HashSet();
}
for (Object v : collection) {
if (v instanceof DynamicMessage) {
x.add(convertProtocolBuffersMessageToSObject((DynamicMessage) v, null, sMethod.getBestReturnType()));
} else {
x.add(v);
}
}
return x;
} else {
return new ArrayList();
}
} else if (outputField.getJavaType() == JavaType.ENUM) {
EnumDescriptor enumType = outputField.getEnumType();
return enumType.findValueByName(value.toString());
} else if (value instanceof DynamicMessage) {
return convertProtocolBuffersMessageToSObject((DynamicMessage) value, null, sMethod.getBestReturnType());
} else {
if (definedReturnType == Date.class) {
return new Date((Long) value);
}
return value;
}
}
} else {
throw new UserException(errorMessage);
}
} catch (ConvertException e) {
throw new ServerException(e);
} catch (ServiceException e) {
throw new ServerException(e.getMessage());
}
} catch (ServiceNotFoundException e) {
throw new ServerException(e);
} catch (ServiceMethodNotFoundException e) {
throw new ServerException(e);
}
}
use of com.google.protobuf.DynamicMessage.Builder in project BIMserver by opensourceBIM.
the class SocketProtocolBuffersChannel method callBlockingMethod.
public Message callBlockingMethod(MethodDescriptorContainer methodDescriptor, Message request) throws ServiceException {
try {
dataOutputStream.writeUTF(methodDescriptor.getServiceDescriptorContainer().getName());
dataOutputStream.writeUTF(methodDescriptor.getName());
dataOutputStream.writeUTF(tokenHolder.getToken() == null ? "" : tokenHolder.getToken());
request.writeDelimitedTo(dataOutputStream);
dataOutputStream.flush();
DynamicMessage response = DynamicMessage.getDefaultInstance(methodDescriptor.getOutputDescriptor());
Builder responseBuilder = response.newBuilderForType();
responseBuilder.mergeDelimitedFrom(inputStream);
return responseBuilder.build();
} catch (IOException e) {
LOGGER.error("", e);
}
return null;
}
use of com.google.protobuf.DynamicMessage.Builder in project BIMserver by opensourceBIM.
the class Handler method run.
@Override
public void run() {
running = true;
try {
DataInputStream dis = new DataInputStream(socket.getInputStream());
String token = dis.readUTF();
ReflectiveRpcChannel reflectiveRpcChannel = new ReflectiveRpcChannel(ServiceInterface.class, serviceFactory.get(token, AccessMethod.INTERNAL).get(ServiceInterface.class), protocolBuffersMetaData, servicesMap);
while (running) {
String serviceName = dis.readUTF();
String methodName = dis.readUTF();
MethodDescriptorContainer methodDescriptorContainer;
try {
methodDescriptorContainer = protocolBuffersMetaData.getMethod(serviceName, methodName);
Builder newBuilder = DynamicMessage.newBuilder(methodDescriptorContainer.getInputDescriptor());
newBuilder.mergeDelimitedFrom(dis);
DynamicMessage request = newBuilder.build();
Message response = reflectiveRpcChannel.callBlockingMethod(methodDescriptorContainer, request);
response.writeDelimitedTo(socket.getOutputStream());
} catch (ServiceNotFoundException e) {
// TODO should return a usable pb message for the user
LOGGER.error("", e);
} catch (ServiceMethodNotFoundException e) {
// TODO should return a usable pb message for the user
LOGGER.error("", e);
}
}
} catch (IOException e) {
LOGGER.error("", e);
} catch (UserException e) {
LOGGER.error("", e);
}
socketNotificationsClient.notifyDisconnect();
}
use of com.google.protobuf.DynamicMessage.Builder in project BIMserver by opensourceBIM.
the class ProtocolBuffersConnectionHandler method run.
@Override
public void run() {
try {
running = true;
while (running) {
String serviceName = dataInputStream.readUTF();
String methodName = dataInputStream.readUTF();
String token = dataInputStream.readUTF();
ProtocolBuffersMetaData protocolBuffersMetaData = protocolBuffersServer.getProtocolBuffersMetaData();
SService sService = servicesMap.getBySimpleName(serviceName);
if (sService == null) {
throw new UserException("No service found with name " + serviceName);
}
SMethod method = sService.getSMethod(methodName);
if (method == null) {
throw new UserException("Method " + methodName + " not found on " + serviceName);
}
Class<? extends PublicInterface> clazz = sService.getInterfaceClass();
PublicInterface service = null;
if (token.equals("")) {
service = serviceFactory.get(AccessMethod.PROTOCOL_BUFFERS).get(clazz);
} else {
service = serviceFactory.get(token, AccessMethod.PROTOCOL_BUFFERS).get(clazz);
}
ReflectiveRpcChannel reflectiveRpcChannel = new ReflectiveRpcChannel(clazz, service, protocolBuffersMetaData, servicesMap);
MethodDescriptorContainer pbMethod = protocolBuffersMetaData.getMethod(serviceName, methodName);
Builder requestBuilder = DynamicMessage.getDefaultInstance(pbMethod.getInputDescriptor()).newBuilderForType();
requestBuilder.mergeDelimitedFrom(dataInputStream);
Message response = reflectiveRpcChannel.callBlockingMethod(pbMethod, requestBuilder.build());
response.writeDelimitedTo(outputStream);
outputStream.flush();
}
} catch (SocketException e) {
} catch (EOFException e) {
} catch (Exception e) {
if (running) {
LOGGER.error("", e);
}
}
protocolBuffersServer.unregister(this);
}
Aggregations