Search in sources :

Example 6 with BadInputException

use of org.neo4j.server.rest.repr.BadInputException in project neo4j by neo4j.

the class RestfulGraphDatabase method addNodeLabel.

// Node Labels
@POST
@Path(PATH_NODE_LABELS)
public Response addNodeLabel(@PathParam("nodeId") long nodeId, String body) {
    try {
        Object rawInput = input.readValue(body);
        if (rawInput instanceof String) {
            ArrayList<String> s = new ArrayList<>();
            s.add((String) rawInput);
            actions.addLabelToNode(nodeId, s);
        } else if (rawInput instanceof Collection) {
            actions.addLabelToNode(nodeId, (Collection<String>) rawInput);
        } else {
            throw new InvalidArgumentsException(format("Label name must be a string. Got: '%s'", rawInput));
        }
    } catch (BadInputException e) {
        return output.badRequest(e);
    } catch (ArrayStoreException ase) {
        return generateBadRequestDueToMangledJsonResponse(body);
    } catch (NodeNotFoundException e) {
        return output.notFound(e);
    }
    return nothing();
}
Also used : BadInputException(org.neo4j.server.rest.repr.BadInputException) StartNodeNotFoundException(org.neo4j.server.rest.domain.StartNodeNotFoundException) EndNodeNotFoundException(org.neo4j.server.rest.domain.EndNodeNotFoundException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 7 with BadInputException

use of org.neo4j.server.rest.repr.BadInputException in project neo4j by neo4j.

the class UrlFormFormat method readMap.

@Override
public Map<String, Object> readMap(final String input, String... requiredKeys) throws BadInputException {
    HashMap<String, Object> result = new HashMap<String, Object>();
    if (input.isEmpty()) {
        return result;
    }
    for (String pair : input.split("\\&")) {
        String[] fields = pair.split("=");
        String key;
        String value;
        try {
            String charset = StandardCharsets.UTF_8.name();
            key = ensureThatKeyDoesNotHavePhPStyleParenthesesAtTheEnd(URLDecoder.decode(fields[0], charset));
            value = URLDecoder.decode(fields[1], charset);
        } catch (UnsupportedEncodingException e) {
            throw new BadInputException(e);
        }
        Object old = result.get(key);
        if (old == null) {
            result.put(key, value);
        } else {
            List<Object> list;
            if (old instanceof List<?>) {
                list = (List<Object>) old;
            } else {
                list = new ArrayList<Object>();
                result.put(key, list);
                list.add(old);
            }
            list.add(value);
        }
    }
    return DefaultFormat.validateKeys(result, requiredKeys);
}
Also used : BadInputException(org.neo4j.server.rest.repr.BadInputException) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BadInputException (org.neo4j.server.rest.repr.BadInputException)7 ArrayList (java.util.ArrayList)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)3 URI (java.net.URI)2 List (java.util.List)2 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)2 StartNodeNotFoundException (org.neo4j.server.rest.domain.StartNodeNotFoundException)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Principal (java.security.Principal)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 NotFoundException (org.neo4j.graphdb.NotFoundException)1 InvalidArgumentsException (org.neo4j.kernel.api.exceptions.InvalidArgumentsException)1 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)1