Search in sources :

Example 6 with PublicInterface

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;
}
Also used : Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Header(org.apache.cxf.headers.Header) PublicInterface(org.bimserver.shared.interfaces.PublicInterface) QName(javax.xml.namespace.QName) UserException(org.bimserver.shared.exceptions.UserException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage)

Example 7 with PublicInterface

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);
}
Also used : SocketException(java.net.SocketException) MethodDescriptorContainer(org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) Builder(com.google.protobuf.DynamicMessage.Builder) ProtocolBuffersMetaData(org.bimserver.shared.pb.ProtocolBuffersMetaData) IOException(java.io.IOException) EOFException(java.io.EOFException) SocketException(java.net.SocketException) UserException(org.bimserver.shared.exceptions.UserException) ReflectiveRpcChannel(org.bimserver.shared.pb.ReflectiveRpcChannel) PublicInterface(org.bimserver.shared.interfaces.PublicInterface) EOFException(java.io.EOFException) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Example 8 with PublicInterface

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);
    }
}
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)

Aggregations

PublicInterface (org.bimserver.shared.interfaces.PublicInterface)8 UserException (org.bimserver.shared.exceptions.UserException)3 IOException (java.io.IOException)2 QName (javax.xml.namespace.QName)2 Header (org.apache.cxf.headers.Header)2 SMethod (org.bimserver.shared.meta.SMethod)2 SService (org.bimserver.shared.meta.SService)2 JsonObject (com.google.gson.JsonObject)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 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JAXBException (javax.xml.bind.JAXBException)1 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)1 JAXBDataBinding (org.apache.cxf.jaxb.JAXBDataBinding)1 JaxWsProxyFactoryBean (org.apache.cxf.jaxws.JaxWsProxyFactoryBean)1