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);
}
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);
}
}
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;
}
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);
}
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());
}
}
}
Aggregations