Search in sources :

Example 11 with RpcException

use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.

the class JsonDeserializer method readInt.

@Override
public int readInt(String name) throws RpcException {
    JsonNode node;
    if (name == null) {
        // The object is not under root, but directly in the response
        node = root;
        name = "root";
    } else {
        node = root.get(name);
    }
    if (node == null) {
        throw new RpcException(RpcException.Type.MISSING_VALUE, name);
    }
    if (!node.isInt()) {
        if (!node.isTextual()) {
            throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int");
        } else {
            try {
                return Integer.parseInt(node.getTextValue());
            } catch (NumberFormatException ex) {
                throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int", ex);
            }
        }
    }
    return node.getIntValue();
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonNode(org.codehaus.jackson.JsonNode)

Example 12 with RpcException

use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.

the class RpcCallerImpl method processRpcServerException.

protected void processRpcServerException(InputStream errorStream) throws RpcException, InternalErrorException, PerunException {
    JsonDeserializer errDes;
    try {
        errDes = new JsonDeserializer(errorStream);
    } catch (IOException e) {
        this.processIOException(e);
        return;
    }
    // Error occured, read the Exception if it is in response
    String errorId = errDes.readString("errorId");
    if (errorId != null) {
        String exceptionName = errDes.readString("name");
        if (!exceptionName.equals(RpcException.class.getSimpleName())) {
            String errorClass = errDes.readString("name");
            String errorInfo = errDes.readString("message");
            try {
                Class<?> exceptionClass = Class.forName("cz.metacentrum.perun.core.api.exceptions." + errorClass);
                Class<?>[] constructorParams = new Class[1];
                constructorParams[0] = String.class;
                Constructor<?> exceptionConstructor = exceptionClass.getConstructor(constructorParams);
                Object[] arglist = new Object[1];
                arglist[0] = errorInfo;
                PerunException exception = (PerunException) exceptionConstructor.newInstance(arglist);
                exception.setErrorId(errorId);
                throw exception;
            } catch (ClassNotFoundException e1) {
                throw new InternalErrorException(e1);
            } catch (InstantiationException e1) {
                throw new InternalErrorException(e1);
            } catch (IllegalAccessException e1) {
                throw new InternalErrorException(e1);
            } catch (IllegalArgumentException e1) {
                throw new InternalErrorException(e1);
            } catch (InvocationTargetException e1) {
                throw new InternalErrorException(e1);
            } catch (NoSuchMethodException e1) {
                throw new InternalErrorException(e1);
            }
        } else {
            // RPC Exception
            String errorClass = errDes.readString("type");
            String errorInfo = errDes.readString("errorInfo");
            throw new RpcException(errorClass, errorInfo);
        }
    }
}
Also used : PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException)

Example 13 with RpcException

use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.

the class JsonDeserializer method readArrayOfInts.

@Override
public int[] readArrayOfInts(String name) throws RpcException {
    JsonNode node;
    if (name == null) {
        // The object is not under root, but directly in the response
        node = root;
    } else {
        node = root.get(name);
    }
    if (node.isNull()) {
        return null;
    }
    if (!node.isArray()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int[] - not an array");
    }
    int[] array = new int[node.size()];
    for (int i = 0; i < node.size(); ++i) {
        JsonNode value = node.get(i);
        if (!value.isInt()) {
            throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int");
        }
        array[i] = node.get(i).getIntValue();
    }
    return array;
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonNode(org.codehaus.jackson.JsonNode)

Example 14 with RpcException

use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.

the class JsonDeserializer method readList.

@Override
public <T> List<T> readList(String name, Class<T> valueType) throws RpcException {
    JsonNode node;
    if (name == null) {
        // The object is not under root, but directly in the response
        node = root;
        name = "root";
    } else {
        node = root.get(name);
    }
    if (node == null) {
        throw new RpcException(RpcException.Type.MISSING_VALUE, name);
    }
    if (node.isNull()) {
        return null;
    }
    if (!node.isArray()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<" + valueType.getSimpleName() + "> - not an array");
    }
    try {
        List<T> list = new ArrayList<>(node.size());
        for (JsonNode e : node) {
            list.add(mapper.readValue(e, valueType));
        }
        return list;
    } catch (IOException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<" + valueType.getSimpleName() + ">", ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Example 15 with RpcException

use of cz.metacentrum.perun.core.api.exceptions.RpcException in project perun by CESNET.

the class JsonDeserializer method read.

@Override
public <T> T read(String name, Class<T> valueType) throws RpcException {
    JsonNode node;
    if (name == null) {
        // The object is not under root, but directly in the response
        node = root;
        name = "root";
    } else {
        node = root.get(name);
    }
    if (node == null) {
        throw new RpcException(RpcException.Type.MISSING_VALUE, name);
    }
    if (node.isNull()) {
        return null;
    }
    if (!node.isObject()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName());
    }
    try {
        return mapper.readValue(node, valueType);
    } catch (IOException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName(), ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Aggregations

RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)19 JsonNode (org.codehaus.jackson.JsonNode)8 IOException (java.io.IOException)6 JsonGenerator (org.codehaus.jackson.JsonGenerator)6 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)5 ArrayList (java.util.ArrayList)4 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 PerunException (cz.metacentrum.perun.core.api.exceptions.PerunException)2 PerunRequest (cz.metacentrum.perun.core.api.PerunRequest)1 User (cz.metacentrum.perun.core.api.User)1 ExtendMembershipException (cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)1 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 PerunRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.PerunRuntimeException)1 Deserializer (cz.metacentrum.perun.rpc.deserializer.Deserializer)1 JsonDeserializer (cz.metacentrum.perun.rpc.deserializer.JsonDeserializer)1 UrlDeserializer (cz.metacentrum.perun.rpc.deserializer.UrlDeserializer)1 JsonSerializer (cz.metacentrum.perun.rpc.serializer.JsonSerializer)1 JsonSerializerJSONP (cz.metacentrum.perun.rpc.serializer.JsonSerializerJSONP)1 Serializer (cz.metacentrum.perun.rpc.serializer.Serializer)1