Search in sources :

Example 11 with SService

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);
    }
}
Also used : KeyValuePair(org.bimserver.shared.reflector.KeyValuePair) JsonObject(com.google.gson.JsonObject) PublicInterface(org.bimserver.shared.interfaces.PublicInterface) SParameter(org.bimserver.shared.meta.SParameter) JsonObject(com.google.gson.JsonObject) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Example 12 with SService

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);
    }
}
Also used : KeyValuePair(org.bimserver.shared.reflector.KeyValuePair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ServiceMap(org.bimserver.webservices.ServiceMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) PublicInterface(org.bimserver.shared.interfaces.PublicInterface) SParameter(org.bimserver.shared.meta.SParameter) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Example 13 with SService

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);
    }
}
Also used : KeyValuePair(org.bimserver.shared.reflector.KeyValuePair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ServiceException(org.bimserver.shared.exceptions.ServiceException) SParameter(org.bimserver.shared.meta.SParameter) ReflectorException(org.bimserver.shared.reflector.ReflectorException) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Example 14 with SService

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);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) SService(org.bimserver.shared.meta.SService)

Example 15 with SService

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;
}
Also used : CtClass(javassist.CtClass) LoaderClassPath(javassist.LoaderClassPath) ClassClassPath(javassist.ClassClassPath) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) SService(org.bimserver.shared.meta.SService)

Aggregations

SService (org.bimserver.shared.meta.SService)16 SMethod (org.bimserver.shared.meta.SMethod)8 UserException (org.bimserver.shared.exceptions.UserException)7 CtClass (javassist.CtClass)5 SParameter (org.bimserver.shared.meta.SParameter)5 CtMethod (javassist.CtMethod)4 KeyValuePair (org.bimserver.shared.reflector.KeyValuePair)4 PublicInterface (org.bimserver.shared.interfaces.PublicInterface)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 JsonObject (com.google.gson.JsonObject)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ServiceException (org.bimserver.shared.exceptions.ServiceException)2 ReflectorException (org.bimserver.shared.reflector.ReflectorException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 Builder (com.google.protobuf.DynamicMessage.Builder)1 Message (com.google.protobuf.Message)1 EOFException (java.io.EOFException)1 File (java.io.File)1