use of org.bimserver.shared.interfaces.PublicInterface in project BIMserver by opensourceBIM.
the class CustomInvoker method getServiceObject.
@Override
public Object getServiceObject(Exchange exchange) {
Message inMessage = exchange.getInMessage();
if (inMessage instanceof SoapMessage) {
SoapMessage soapMessage = (SoapMessage) inMessage;
Header header = soapMessage.getHeader(new QName("uri:org.bimserver.shared", "token"));
String token = null;
if (header != null) {
token = ((Token) (header.getObject())).getToken();
}
if (token == null) {
token = (String) exchange.getSession().get("token");
}
if (token != null) {
try {
return serviceFactory.get(token, AccessMethod.SOAP).get(interfaceClass);
} catch (UserException e) {
LOGGER.error("", e);
return null;
}
} else {
try {
PublicInterface newService = serviceFactory.get(AccessMethod.SOAP).get(interfaceClass);
exchange.getSession().put("token", token);
return newService;
} catch (UserException e) {
LOGGER.error("", e);
}
}
} else {
try {
return serviceFactory.get(AccessMethod.SOAP).get(interfaceClass);
} catch (UserException e) {
LOGGER.error("", e);
}
}
return null;
}
use of org.bimserver.shared.interfaces.PublicInterface 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);
}
use of org.bimserver.shared.interfaces.PublicInterface in project BIMserver by opensourceBIM.
the class JsonHandler method processSingleRequest.
private void processSingleRequest(JsonObject request, String jsonToken, String oAuthCode, HttpServletRequest httpRequest, JsonWriter writer) throws Exception {
long s = System.nanoTime();
if (!request.has("interface")) {
throw new UserException("No \"interface\" parameter found in request");
}
String interfaceName = request.get("interface").getAsString();
if (!request.has("method")) {
throw new UserException("No \"method\" parameter found in request");
}
String methodName = request.get("method").getAsString();
SService sService = bimServer.getServicesMap().getByName(interfaceName);
if (sService == null) {
sService = bimServer.getServicesMap().getBySimpleName(interfaceName);
}
if (sService == null) {
throw new UserException("No service found with name " + interfaceName);
}
SMethod method = sService.getSMethod(methodName);
if (method == null) {
SMethod alternative = bimServer.getServicesMap().findMethod(methodName);
if (alternative == null) {
throw new UserException("Method " + methodName + " not found on " + interfaceName);
} else {
throw new UserException("Method " + methodName + " not found on " + interfaceName + " (suggestion: " + alternative.getService().getSimpleName() + ")");
}
}
KeyValuePair[] parameters = new KeyValuePair[method.getParameters().size()];
if (request.has("parameters")) {
JsonObject parametersJson = request.getAsJsonObject("parameters");
for (int i = 0; i < method.getParameters().size(); i++) {
SParameter parameter = method.getParameter(i);
if (parametersJson.has(parameter.getName())) {
parameters[i] = new KeyValuePair(parameter.getName(), converter.fromJson(parameter.getType(), parameter.getGenericType(), parametersJson.get(parameter.getName())));
} else {
LOGGER.error("Missing parameter: " + method.getName() + " -> " + parameter.getName());
throw new UserException("Missing parameter: " + method.getName() + " -> " + parameter.getName());
}
}
} else if (parameters.length > 0) {
throw new UserException("Missing 'parameters' field, expected " + parameters.length + " parameters");
}
PublicInterface service = getServiceInterface(httpRequest, bimServer, sService.getInterfaceClass(), methodName, jsonToken, oAuthCode);
String oldThreadName = Thread.currentThread().getName();
Thread.currentThread().setName(interfaceName + "." + methodName);
try {
Recording recording = bimServer.getMetricsRegistry().startRecording(sService, method);
Object result = method.invoke(sService.getInterfaceClass(), service, parameters);
recording.finish();
// can start streaming
if (writer != null) {
if (result == null) {
writer.beginObject();
writer.name("result");
writer.beginObject();
writer.endObject();
writer.endObject();
} else {
writer.beginObject();
writer.name("result");
converter.toJson(result, writer);
writer.endObject();
}
}
long e = System.nanoTime();
LOGGER.debug(interfaceName + "." + methodName + " " + ((e - s) / 1000000) + "ms");
} finally {
Thread.currentThread().setName(oldThreadName);
}
}
Aggregations