use of org.bimserver.shared.meta.SService 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);
}
}
use of org.bimserver.shared.meta.SService in project BIMserver by opensourceBIM.
the class JsonHandler method processSingleRequest.
private void processSingleRequest(ObjectNode request, String jsonToken, String oAuthCode, HttpServletRequest httpRequest, JsonGenerator 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").asText();
if (!request.has("method")) {
throw new UserException("No \"method\" parameter found in request");
}
String methodName = request.get("method").asText();
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")) {
ObjectNode parametersJson = (ObjectNode) request.get("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");
}
ServiceMap serviceMap = getServiceMap(httpRequest, bimServer, methodName, jsonToken, oAuthCode);
PublicInterface service = getServiceInterface(httpRequest, bimServer, sService.getInterfaceClass(), methodName, jsonToken, serviceMap);
String oldThreadName = Thread.currentThread().getName();
Thread.currentThread().setName(interfaceName + "." + methodName);
try {
boolean isMonitor = serviceMap != null && serviceMap.getUser() != null && serviceMap.getUser().getUserType() == UserType.MONITOR;
Recording recording = null;
if (!isMonitor) {
recording = bimServer.getMetricsRegistry().startRecording(sService, method);
}
Object result = method.invoke(sService.getInterfaceClass(), service, parameters);
if (!isMonitor) {
recording.finish();
}
// can start streaming
if (writer != null) {
if (result == null) {
writer.writeStartObject();
writer.writeFieldName("result");
writer.writeStartObject();
writer.writeEndObject();
writer.writeEndObject();
} else {
writer.writeStartObject();
writer.writeFieldName("result");
JsonNode jsonResult = converter.toJson(result);
OBJECT_MAPPER.writeValue(writer, jsonResult);
writer.writeEndObject();
}
}
long e = System.nanoTime();
LOGGER.debug(interfaceName + "." + methodName + " " + ((e - s) / 1000000) + "ms");
} finally {
Thread.currentThread().setName(oldThreadName);
}
}
use of org.bimserver.shared.meta.SService in project BIMserver by opensourceBIM.
the class NotificationsManager method handleIncoming.
public void handleIncoming(ObjectNode request) throws UserException, ConvertException, IOException {
// TODO copied code from JsonHandler
String interfaceName = request.get("interface").asText();
String methodName = request.get("method").asText();
SService sService = servicesMap.getByName(interfaceName);
if (sService == null) {
sService = servicesMap.getBySimpleName(interfaceName);
}
if (sService == null) {
throw new UserException("No service found with name " + interfaceName);
}
SMethod method = sService.getSMethod(methodName);
if (method == null) {
SMethod alternative = servicesMap.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")) {
ObjectNode parametersJson = (ObjectNode) request.get("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 parameters: " + method.getName() + " -> " + parameter.getName());
}
}
} else {
throw new UserException("Missing 'parameters' field");
}
try {
method.invoke(sService.getInterfaceClass(), service, parameters);
} catch (ServiceException e) {
LOGGER.error("", e);
} catch (ReflectorException e) {
LOGGER.error("", e);
}
}
use of org.bimserver.shared.meta.SService in project BIMserver by opensourceBIM.
the class RealtimeReflectorFactoryBuilder method createCreateReflectorMethod2.
private void createCreateReflectorMethod2(String newClassPrefix, CtClass reflectorFactoryImpl) throws NotFoundException, CannotCompileException {
CtClass[] parameters = new CtClass[2];
parameters[0] = pool.get(Class.class.getName());
parameters[1] = pool.get(PublicInterface.class.getName());
CtMethod method = new CtMethod(pool.get(Reflector.class.getName()), "createReflector", parameters, reflectorFactoryImpl);
StringBuilder methodBuilder = new StringBuilder();
methodBuilder.append("{");
methodBuilder.append("if (1==0) {");
for (String name : servicesMap.keySetName()) {
SService sService = servicesMap.getByName(name);
methodBuilder.append("} else if ($1.getSimpleName().equals(\"" + sService.getSimpleName() + "\")) {");
methodBuilder.append("return new " + GENERATED_CLASSES_PACKAGE + "." + sService.getSimpleName() + "Reflector" + newClassPrefix + "((" + sService.getInterfaceClass().getName() + ")$2);");
}
methodBuilder.append("}");
methodBuilder.append("return null;");
methodBuilder.append("}");
method.setBody(methodBuilder.toString());
reflectorFactoryImpl.addMethod(method);
}
use of org.bimserver.shared.meta.SService in project BIMserver by opensourceBIM.
the class RealtimeReflectorFactoryBuilder method newReflectorFactory.
public ReflectorFactory newReflectorFactory() {
try {
pool = ClassPool.getDefault();
pool.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
pool.insertClassPath(new ClassClassPath(this.getClass()));
String newClassPrefix = getNewClassPrefix();
for (String name : servicesMap.keySetName()) {
SService sService = servicesMap.getByName(name);
build1(newClassPrefix, (Class<? extends PublicInterface>) sService.getInterfaceClass(), sService);
build2(newClassPrefix, (Class<? extends PublicInterface>) sService.getInterfaceClass(), sService);
}
CtClass reflectorFactoryImpl = pool.makeClass("org.bimserver.reflector.ReflectorFactoryImpl" + newClassPrefix);
reflectorFactoryImpl.addInterface(pool.get(ReflectorFactory.class.getName()));
createCreateReflectorMethod1(newClassPrefix, reflectorFactoryImpl);
createCreateReflectorMethod2(newClassPrefix, reflectorFactoryImpl);
Class<?> class1 = pool.toClass(reflectorFactoryImpl, NeighbourClass.class, getClass().getClassLoader(), getClass().getProtectionDomain());
return (ReflectorFactory) class1.newInstance();
} catch (Exception e) {
LOGGER.error("", e);
}
return null;
}
Aggregations