Search in sources :

Example 16 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)

Example 17 with RpcException

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

the class UrlDeserializer method readList.

@Override
public <T> List<T> readList(String name, Class<T> valueType) throws RpcException {
    if (!contains(name))
        throw new RpcException(RpcException.Type.MISSING_VALUE, name);
    List<T> list = new ArrayList<T>();
    String[] stringParams = req.getParameterValues(name + "[]");
    if (stringParams == null) {
        // submitter probably forgot to add list decoration to param name ("[]").
        stringParams = req.getParameterValues(name);
    }
    for (String param : stringParams) {
        if (valueType.isAssignableFrom(String.class)) {
            list.add(valueType.cast(param));
        } else if (valueType.isAssignableFrom(Integer.class)) {
            list.add(valueType.cast(Integer.valueOf(param)));
        } else if (valueType.isAssignableFrom(Boolean.class)) {
            list.add(valueType.cast(Boolean.valueOf(param)));
        } else if (valueType.isAssignableFrom(Float.class)) {
            list.add(valueType.cast(Float.valueOf(param)));
        }
    }
    return list;
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) ArrayList(java.util.ArrayList)

Example 18 with RpcException

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

the class JsonSerializerGWT method write.

@Override
public void write(Object object) throws RpcException, IOException {
    JsonGenerator gen = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    if (object instanceof Throwable) {
        throw new IllegalArgumentException("Tried to serialize a throwable object using write()", (Throwable) object);
    }
    try {
        gen.writeRaw(callback + "(");
        gen.writeObject(object);
        gen.writeRaw(");");
        gen.flush();
        gen.close();
    } catch (JsonProcessingException ex) {
        throw new RpcException(RpcException.Type.CANNOT_SERIALIZE_VALUE, ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 19 with RpcException

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

the class JsonDeserializer method readListPerunBeans.

@Override
public List<PerunBean> readListPerunBeans(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.isNull()) {
        return null;
    }
    if (!node.isArray()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean> - not an array");
    }
    try {
        List<PerunBean> list = new ArrayList<>(node.size());
        for (JsonNode e : node) {
            String beanName = e.get("beanName").getTextValue();
            if (beanName == null) {
                throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean> - missing beanName info");
            }
            list.add((PerunBean) mapper.readValue(e, Class.forName("cz.metacentrum.perun.core.api." + beanName)));
        }
        return list;
    } catch (ClassNotFoundException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean> - class not found");
    } catch (IOException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean>", ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) ArrayList(java.util.ArrayList) 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