Search in sources :

Example 1 with ReflectorException

use of org.bimserver.shared.reflector.ReflectorException in project BIMserver by opensourceBIM.

the class JsonReflector method callMethod.

@Override
public Object callMethod(String interfaceName, String methodName, Class<?> definedReturnType, KeyValuePair... args) throws ServerException, UserException, ReflectorException {
    try {
        JsonObject request = new JsonObject();
        request.add("interface", new JsonPrimitive(interfaceName));
        request.add("method", new JsonPrimitive(methodName));
        JsonObject parameters = new JsonObject();
        for (KeyValuePair arg : args) {
            parameters.add(arg.getFieldName(), converter.toJson(arg.getValue()));
        }
        request.add("parameters", parameters);
        JsonObject requestObject = new JsonObject();
        requestObject.add("request", request);
        JsonObject jsonResult = call(requestObject);
        if (!isOneWay()) {
            if (jsonResult == null) {
                return null;
            }
            JsonObject response = jsonResult.getAsJsonObject("response");
            if (response.has("exception")) {
                JsonObject exceptionJson = response.getAsJsonObject("exception");
                String exceptionType = exceptionJson.get("__type").getAsString();
                String message = exceptionJson.has("message") ? exceptionJson.get("message").getAsString() : "unknown";
                if (exceptionType.equals(UserException.class.getSimpleName())) {
                    if (exceptionJson.has("errorCode")) {
                        throw new UserException(message, ErrorCode.parse(exceptionJson.get("errorCode").getAsInt()));
                    } else {
                        throw new UserException(message);
                    }
                } else if (exceptionType.equals(ServerException.class.getSimpleName())) {
                    if (exceptionJson.has("errorCode")) {
                        throw new ServerException(message, ErrorCode.parse(exceptionJson.get("errorCode").getAsInt()));
                    } else {
                        throw new ServerException(message);
                    }
                } else {
                    if (exceptionJson.has("errorCode")) {
                        throw new ServerException(message, ErrorCode.parse(exceptionJson.get("errorCode").getAsInt()));
                    } else {
                        throw new ServerException(message);
                    }
                }
            } else if (response.has("result")) {
                Object result = response.get("result");
                SMethod method = servicesMap.getBySimpleName(interfaceName).getSMethod(methodName);
                return converter.fromJson(method.getReturnType(), method.getGenericReturnType(), result);
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (ReflectorException e) {
        throw e;
    } catch (UserException e) {
        throw e;
    } catch (ServerException e) {
        throw e;
    } catch (Exception e) {
        throw new ReflectorException(e);
    }
}
Also used : ServerException(org.bimserver.shared.exceptions.ServerException) KeyValuePair(org.bimserver.shared.reflector.KeyValuePair) JsonPrimitive(com.google.gson.JsonPrimitive) ReflectorException(org.bimserver.shared.reflector.ReflectorException) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) ReflectorException(org.bimserver.shared.reflector.ReflectorException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException)

Example 2 with ReflectorException

use of org.bimserver.shared.reflector.ReflectorException in project BIMserver by opensourceBIM.

the class JsonSocketReflector method call.

public JsonObject call(JsonObject request) throws ReflectorException {
    try {
        if (tokenHolder.getToken() != null) {
            request.addProperty("token", tokenHolder.getToken());
        }
        HttpPost httppost = new HttpPost(remoteAddress);
        httppost.setHeader("Content-Type", "application/json");
        httppost.setEntity(new StringEntity(request.toString(), Charsets.UTF_8));
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(request.toString());
        }
        HttpResponse response = httpclient.execute(httppost, context);
        try {
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resultEntity = response.getEntity();
                JsonParser parser = new JsonParser();
                if (LOGGER.isDebugEnabled()) {
                    InputStream inputStream = resultEntity.getContent();
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    IOUtils.copy(inputStream, byteArrayOutputStream);
                    LOGGER.debug(new String(byteArrayOutputStream.toByteArray(), Charsets.UTF_8));
                    JsonObject resultObject = (JsonObject) parser.parse(new InputStreamReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), Charsets.UTF_8));
                    return resultObject;
                } else {
                    return (JsonObject) parser.parse(new InputStreamReader(resultEntity.getContent(), Charsets.UTF_8));
                }
            } else {
                throw new ReflectorException("Call unsuccessful, status code: " + response.getStatusLine().getStatusCode() + " (" + response.getStatusLine().getReasonPhrase() + "), " + remoteAddress);
            }
        } finally {
            httppost.releaseConnection();
        }
    } catch (Exception e) {
        throw new ReflectorException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) JsonObject(com.google.gson.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReflectorException(org.bimserver.shared.reflector.ReflectorException) StringEntity(org.apache.http.entity.StringEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ReflectorException(org.bimserver.shared.reflector.ReflectorException) JsonParser(com.google.gson.JsonParser)

Example 3 with ReflectorException

use of org.bimserver.shared.reflector.ReflectorException in project BIMserver by opensourceBIM.

the class NotificationsManager method handleIncoming.

public void handleIncoming(JsonObject request) throws UserException, ConvertException, IOException {
    // TODO copied code from JsonHandler
    String interfaceName = request.get("interface").getAsString();
    String methodName = request.get("method").getAsString();
    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")) {
        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 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) ServiceException(org.bimserver.shared.exceptions.ServiceException) SParameter(org.bimserver.shared.meta.SParameter) ReflectorException(org.bimserver.shared.reflector.ReflectorException) JsonObject(com.google.gson.JsonObject) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Aggregations

JsonObject (com.google.gson.JsonObject)3 ReflectorException (org.bimserver.shared.reflector.ReflectorException)3 UserException (org.bimserver.shared.exceptions.UserException)2 SMethod (org.bimserver.shared.meta.SMethod)2 KeyValuePair (org.bimserver.shared.reflector.KeyValuePair)2 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 ServerException (org.bimserver.shared.exceptions.ServerException)1 ServiceException (org.bimserver.shared.exceptions.ServiceException)1 SParameter (org.bimserver.shared.meta.SParameter)1 SService (org.bimserver.shared.meta.SService)1