@Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse httpResponse) throws ServletException,
IOException {
// prepare the response
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setReferer(request.getHeader("referer"));
// calculate the paths
String path = getItemPath(request);
htmlResponse.setPath(path);
// location
htmlResponse.setLocation(externalizePath(request, path));
// parent location
path = ResourceUtil.getParent(path);
if (path != null) {
htmlResponse.setParentLocation(externalizePath(request, path));
}
Session session = request.getResourceResolver().adaptTo(Session.class);
final List<Modification> changes = new ArrayList<Modification>();
try {
handleOperation(request, htmlResponse, changes);
//TODO: maybe handle SlingAuthorizablePostProcessor handlers here
// set changes on html response
for(Modification change : changes) {
switch ( change.getType() ) {
case MODIFY : htmlResponse.onModified(change.getSource()); break;
case DELETE : htmlResponse.onDeleted(change.getSource()); break;
case MOVE : htmlResponse.onMoved(change.getSource(), change.getDestination()); break;
case COPY : htmlResponse.onCopied(change.getSource(), change.getDestination()); break;
case CREATE : htmlResponse.onCreated(change.getSource()); break;
case ORDER : htmlResponse.onChange("ordered", change.getSource(), change.getDestination()); break;
}
}
if (session.hasPendingChanges()) {
session.save();
}
} catch (ResourceNotFoundException rnfe) {
htmlResponse.setStatus(HttpServletResponse.SC_NOT_FOUND,
rnfe.getMessage());
} catch (Throwable throwable) {
log.debug("Exception while handling POST "
+ request.getResource().getPath() + " with "
+ getClass().getName(), throwable);
htmlResponse.setError(throwable);
} finally {
try {
if (session.hasPendingChanges()) {
session.refresh(false);
}
} catch (RepositoryException e) {
log.warn("RepositoryException in finally block: {}",
e.getMessage(), e);
}
}
// check for redirect URL if processing succeeded
if (htmlResponse.isSuccessful()) {
String redirect = getRedirectUrl(request, htmlResponse);
if (redirect != null) {
httpResponse.sendRedirect(redirect);
return;
}
}
// create a html response and send if unsuccessful or no redirect
htmlResponse.send(httpResponse, isSetStatus(request));
}