Search in sources :

Example 1 with BadInputException

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

the class RestfulGraphDatabase method createPagedTraverser.

@POST
@Path(PATH_TO_CREATE_PAGED_TRAVERSERS)
public Response createPagedTraverser(@PathParam("nodeId") long startNode, @PathParam("returnType") TraverserReturnType returnType, @QueryParam("pageSize") @DefaultValue(FIFTY_ENTRIES) int pageSize, @QueryParam("leaseTime") @DefaultValue(SIXTY_SECONDS) int leaseTimeInSeconds, String body) {
    try {
        validatePageSize(pageSize);
        validateLeaseTime(leaseTimeInSeconds);
        String traverserId = actions.createPagedTraverser(startNode, input.readMap(body), pageSize, leaseTimeInSeconds);
        URI uri = new URI("node/" + startNode + "/paged/traverse/" + returnType + "/" + traverserId);
        return output.created(new ListEntityRepresentation(actions.pagedTraverse(traverserId, returnType), uri.normalize()));
    } catch (EvaluationException e) {
        return output.badRequest(e);
    } catch (BadInputException e) {
        return output.badRequest(e);
    } catch (NotFoundException e) {
        return output.notFound(e);
    } catch (URISyntaxException e) {
        return output.serverError(e);
    }
}
Also used : BadInputException(org.neo4j.server.rest.repr.BadInputException) StartNodeNotFoundException(org.neo4j.server.rest.domain.StartNodeNotFoundException) NotFoundException(org.neo4j.graphdb.NotFoundException) EndNodeNotFoundException(org.neo4j.server.rest.domain.EndNodeNotFoundException) EvaluationException(org.neo4j.server.rest.domain.EvaluationException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ListEntityRepresentation(org.neo4j.server.rest.repr.ListEntityRepresentation) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with BadInputException

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

the class URLTypeCaster method getList.

@Override
Object[] getList(GraphDatabaseAPI graphDb, ParameterList parameters, String name) throws BadInputException {
    URI[] uris = parameters.getUriList(name);
    URL[] urls = new URL[uris.length];
    try {
        for (int i = 0; i < urls.length; i++) {
            urls[i] = uris[i].toURL();
        }
    } catch (MalformedURLException e) {
        throw new BadInputException(e);
    }
    return urls;
}
Also used : MalformedURLException(java.net.MalformedURLException) BadInputException(org.neo4j.server.rest.repr.BadInputException) URI(java.net.URI) URL(java.net.URL)

Example 3 with BadInputException

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

the class ConsoleService method exec.

@POST
public Response exec(@Context InputFormat input, String data) {
    Map<String, Object> args;
    try {
        args = input.readMap(data);
    } catch (BadInputException e) {
        return output.badRequest(e);
    }
    if (!args.containsKey("command")) {
        return Response.status(Status.BAD_REQUEST).entity("Expected command argument not present.").build();
    }
    ScriptSession scriptSession;
    try {
        scriptSession = getSession(args);
    } catch (IllegalArgumentException e) {
        return output.badRequest(e);
    }
    log.debug(scriptSession.toString());
    try {
        Pair<String, String> result = scriptSession.evaluate((String) args.get("command"));
        List<Representation> list = new ArrayList<Representation>(asList(ValueRepresentation.string(result.first()), ValueRepresentation.string(result.other())));
        return output.ok(new ListRepresentation(RepresentationType.STRING, list));
    } catch (Exception e) {
        List<Representation> list = new ArrayList<Representation>(asList(ValueRepresentation.string(e.getClass() + " : " + e.getMessage() + "\n"), ValueRepresentation.string(null)));
        return output.ok(new ListRepresentation(RepresentationType.STRING, list));
    }
}
Also used : ArrayList(java.util.ArrayList) ValueRepresentation(org.neo4j.server.rest.repr.ValueRepresentation) ListRepresentation(org.neo4j.server.rest.repr.ListRepresentation) ConsoleServiceRepresentation(org.neo4j.server.rest.management.repr.ConsoleServiceRepresentation) Representation(org.neo4j.server.rest.repr.Representation) BadInputException(org.neo4j.server.rest.repr.BadInputException) BadInputException(org.neo4j.server.rest.repr.BadInputException) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ListRepresentation(org.neo4j.server.rest.repr.ListRepresentation) POST(javax.ws.rs.POST)

Example 4 with BadInputException

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

the class UserService method setPassword.

@POST
@Path("/{username}/password")
public Response setPassword(@PathParam("username") String username, @Context HttpServletRequest req, String payload) {
    Principal principal = req.getUserPrincipal();
    if (principal == null || !principal.getName().equals(username)) {
        return output.notFound();
    }
    final Map<String, Object> deserialized;
    try {
        deserialized = input.readMap(payload);
    } catch (BadInputException e) {
        return output.response(BAD_REQUEST, new ExceptionRepresentation(new Neo4jError(Status.Request.InvalidFormat, e.getMessage())));
    }
    Object o = deserialized.get(PASSWORD);
    if (o == null) {
        return output.response(UNPROCESSABLE, new ExceptionRepresentation(new Neo4jError(Status.Request.InvalidFormat, String.format("Required parameter '%s' is missing.", PASSWORD))));
    }
    if (!(o instanceof String)) {
        return output.response(UNPROCESSABLE, new ExceptionRepresentation(new Neo4jError(Status.Request.InvalidFormat, String.format("Expected '%s' to be a string.", PASSWORD))));
    }
    String newPassword = (String) o;
    try {
        SecurityContext securityContext = getSecurityContextFromUserPrincipal(principal);
        if (securityContext == null) {
            return output.notFound();
        } else {
            UserManager userManager = userManagerSupplier.getUserManager(securityContext);
            userManager.setUserPassword(username, newPassword, false);
        }
    } catch (IOException e) {
        return output.serverErrorWithoutLegacyStacktrace(e);
    } catch (InvalidArgumentsException e) {
        return output.response(UNPROCESSABLE, new ExceptionRepresentation(new Neo4jError(e.status(), e.getMessage())));
    }
    return output.ok();
}
Also used : Neo4jError(org.neo4j.server.rest.transactional.error.Neo4jError) ExceptionRepresentation(org.neo4j.server.rest.repr.ExceptionRepresentation) BadInputException(org.neo4j.server.rest.repr.BadInputException) UserManager(org.neo4j.kernel.api.security.UserManager) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) IOException(java.io.IOException) InvalidArgumentsException(org.neo4j.kernel.api.exceptions.InvalidArgumentsException) AuthorizedRequestWrapper.getSecurityContextFromUserPrincipal(org.neo4j.server.rest.dbms.AuthorizedRequestWrapper.getSecurityContextFromUserPrincipal) Principal(java.security.Principal) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 5 with BadInputException

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

the class ParameterList method getList.

private <T> T[] getList(String name, GraphDatabaseAPI graphDb, Converter<T> converter) throws BadInputException {
    Object value = data.get(name);
    if (value == null) {
        return null;
    }
    List<T> result = new ArrayList<>();
    if (value instanceof Object[]) {
        for (Object element : (Object[]) value) {
            result.add(converter.convert(graphDb, element));
        }
    } else if (value instanceof Iterable<?>) {
        for (Object element : (Iterable<?>) value) {
            result.add(converter.convert(graphDb, element));
        }
    } else {
        throw new BadInputException(name + " is not a list");
    }
    return result.toArray(converter.newArray(result.size()));
}
Also used : BadInputException(org.neo4j.server.rest.repr.BadInputException) ArrayList(java.util.ArrayList)

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