Search in sources :

Example 11 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class LoginResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, User.class, propertySet);
    final PropertyKey<String> nameKey = StructrApp.key(User.class, "name");
    final PropertyKey<String> eMailKey = StructrApp.key(User.class, "eMail");
    final PropertyKey<String> pwdKey = StructrApp.key(User.class, "password");
    final String name = properties.get(nameKey);
    final String email = properties.get(eMailKey);
    final String password = properties.get(pwdKey);
    String emailOrUsername = StringUtils.isNotEmpty(email) ? email : name;
    if (StringUtils.isNotEmpty(emailOrUsername) && StringUtils.isNotEmpty(password)) {
        Principal user = securityContext.getAuthenticator().doLogin(securityContext.getRequest(), emailOrUsername, password);
        if (user != null) {
            logger.info("Login successful: {}", new Object[] { user });
            // make logged in user available to caller
            securityContext.setCachedUser(user);
            RestMethodResult methodResult = new RestMethodResult(200);
            methodResult.addContent(user);
            return methodResult;
        }
    }
    logger.info("Invalid credentials (name, email, password): {}, {}, {}", new Object[] { name, email, password });
    return new RestMethodResult(401);
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Principal(org.structr.core.entity.Principal) RestMethodResult(org.structr.rest.RestMethodResult)

Example 12 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class ResetPasswordResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    if (propertySet.containsKey("eMail")) {
        final String emailString = (String) propertySet.get("eMail");
        if (StringUtils.isEmpty(emailString)) {
            return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
        }
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final PropertyKey<String> confirmationKey = StructrApp.key(User.class, "confirmationKey");
        final PropertyKey<String> eMail = StructrApp.key(User.class, "eMail");
        final String localeString = (String) propertySet.get("locale");
        final String confKey = UUID.randomUUID().toString();
        final Principal user = StructrApp.getInstance().nodeQuery(User.class).and(eMail, emailString).getFirst();
        if (user != null) {
            // update confirmation key
            user.setProperties(SecurityContext.getSuperUserInstance(), new PropertyMap(confirmationKey, confKey));
            if (!sendResetPasswordLink(user, propertySet, localeString, confKey)) {
                // return 400 Bad request
                return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
            }
            // return 200 OK
            return new RestMethodResult(HttpServletResponse.SC_OK);
        } else {
            // so we're failing silently here
            return new RestMethodResult(HttpServletResponse.SC_OK);
        }
    } else {
        // return 400 Bad request
        return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) ConfigurationProvider(org.structr.schema.ConfigurationProvider) RestMethodResult(org.structr.rest.RestMethodResult) Principal(org.structr.core.entity.Principal)

Example 13 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class VideoFile method getMetadata.

static RestMethodResult getMetadata(final VideoFile thisVideo) throws FrameworkException {
    final SecurityContext securityContext = thisVideo.getSecurityContext();
    final Map<String, String> metadata = AVConv.newInstance(securityContext, thisVideo).getMetadata();
    final RestMethodResult result = new RestMethodResult(200);
    final GraphObjectMap map = new GraphObjectMap();
    for (final Entry<String, String> entry : metadata.entrySet()) {
        map.setProperty(new StringProperty(entry.getKey()), entry.getValue());
    }
    result.addContent(map);
    return result;
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) StringProperty(org.structr.core.property.StringProperty) RestMethodResult(org.structr.rest.RestMethodResult)

Example 14 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class KafkaClient method unsubscribeTopic.

static RestMethodResult unsubscribeTopic(KafkaClient thisClient, final String topic) throws FrameworkException {
    if (getConsumer(thisClient) == null && thisClient.getServers() != null && thisClient.getServers().length > 0) {
        setConsumer(thisClient, new KafkaConsumer<>(getConfiguration(thisClient, KafkaConsumer.class)));
    } else if (thisClient.getServers() == null || thisClient.getServers().length == 0) {
        logger.error("Could not initialize consumer. No servers configured.");
        return new RestMethodResult(422);
    }
    if (getConsumer(thisClient) != null) {
        Set<String> subs = getConsumer(thisClient).subscription();
        List<String> newSubs = new ArrayList<>();
        subs.forEach(s -> newSubs.add(s));
        getConsumer(thisClient).subscribe(newSubs);
        newSubs.remove(topic);
        getConsumer(thisClient).unsubscribe();
        getConsumer(thisClient).subscribe(newSubs);
    }
    return new RestMethodResult(200);
}
Also used : RestMethodResult(org.structr.rest.RestMethodResult)

Example 15 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class JsonRestServlet method doDelete.

// <editor-fold defaultstate="collapsed" desc="DELETE">
@Override
protected void doDelete(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    SecurityContext securityContext = null;
    Authenticator authenticator = null;
    RestMethodResult result = null;
    Resource resource = null;
    try {
        assertInitialized();
        // first thing to do!
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        // isolate request authentication in a transaction
        try (final Tx tx = StructrApp.getInstance().tx()) {
            authenticator = config.getAuthenticator();
            securityContext = authenticator.initializeAndExamineRequest(request, response);
            tx.success();
        }
        final App app = StructrApp.getInstance(securityContext);
        // isolate resource authentication
        try (final Tx tx = app.tx()) {
            resource = ResourceHelper.optimizeNestedResourceChain(securityContext, request, resourceMap, propertyView);
            authenticator.checkResourceAccess(securityContext, request, resource.getResourceSignature(), propertyView.get(securityContext));
            tx.success();
        }
        // isolate doDelete
        boolean retry = true;
        while (retry) {
            try {
                result = resource.doDelete();
                retry = false;
            } catch (RetryException ddex) {
                retry = true;
            }
        }
        // isolate write output
        try (final Tx tx = app.tx()) {
            result.commitResponse(gson.get(), response);
            tx.success();
        }
    } catch (FrameworkException frameworkException) {
        // set status & write JSON output
        response.setStatus(frameworkException.getStatus());
        gson.get().toJson(frameworkException, response.getWriter());
        response.getWriter().println();
    } catch (JsonSyntaxException jsex) {
        logger.warn("JsonSyntaxException in DELETE", jsex);
        int code = HttpServletResponse.SC_BAD_REQUEST;
        response.setStatus(code);
        response.getWriter().append(RestMethodResult.jsonError(code, "JsonSyntaxException in DELETE: " + jsex.getMessage()));
    } catch (JsonParseException jpex) {
        logger.warn("JsonParseException in DELETE", jpex);
        int code = HttpServletResponse.SC_BAD_REQUEST;
        response.setStatus(code);
        response.getWriter().append(RestMethodResult.jsonError(code, "JsonSyntaxException in DELETE: " + jpex.getMessage()));
    } catch (Throwable t) {
        logger.warn("Exception in DELETE", t);
        int code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        response.setStatus(code);
        response.getWriter().append(RestMethodResult.jsonError(code, "JsonSyntaxException in DELETE: " + t.getMessage()));
    } finally {
        try {
            // response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException t) {
            logger.warn("Unable to flush and close response: {}", t.getMessage());
        }
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Resource(org.structr.rest.resource.Resource) StaticRelationshipResource(org.structr.rest.resource.StaticRelationshipResource) IOException(java.io.IOException) RetryException(org.structr.api.RetryException) JsonParseException(com.google.gson.JsonParseException) JsonSyntaxException(com.google.gson.JsonSyntaxException) SecurityContext(org.structr.common.SecurityContext) Authenticator(org.structr.core.auth.Authenticator) RestMethodResult(org.structr.rest.RestMethodResult)

Aggregations

RestMethodResult (org.structr.rest.RestMethodResult)27 App (org.structr.core.app.App)13 StructrApp (org.structr.core.app.StructrApp)13 Tx (org.structr.core.graph.Tx)10 FrameworkException (org.structr.common.error.FrameworkException)9 GraphObject (org.structr.core.GraphObject)7 SecurityContext (org.structr.common.SecurityContext)6 Authenticator (org.structr.core.auth.Authenticator)6 Resource (org.structr.rest.resource.Resource)6 JsonParseException (com.google.gson.JsonParseException)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 RetryException (org.structr.api.RetryException)5 IllegalPathException (org.structr.rest.exception.IllegalPathException)5 PropertyMap (org.structr.core.property.PropertyMap)4 NotFoundException (org.structr.rest.exception.NotFoundException)4 StaticRelationshipResource (org.structr.rest.resource.StaticRelationshipResource)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Principal (org.structr.core.entity.Principal)3 LinkedHashMap (java.util.LinkedHashMap)2