use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.
the class HtmlRendererServlet method doGet.
@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException {
final Resource r = req.getResource();
if (ResourceUtil.isNonExistingResource(r)) {
throw new ResourceNotFoundException("No data to render.");
}
resp.setContentType(req.getResponseContentType());
resp.setCharacterEncoding("UTF-8");
final PrintWriter pw = resp.getWriter();
final boolean isIncluded = req.getAttribute(SlingConstants.ATTR_REQUEST_SERVLET) != null;
@SuppressWarnings({ "rawtypes" }) final Map map = r.adaptTo(Map.class);
if (map != null) {
printProlog(pw, isIncluded);
printResourceInfo(pw, r);
render(pw, r, map);
printEpilog(pw, isIncluded);
} else if (r.adaptTo(String.class) != null) {
printProlog(pw, isIncluded);
printResourceInfo(pw, r);
render(pw, r, r.adaptTo(String.class));
printEpilog(pw, isIncluded);
} else if (r.adaptTo(String[].class) != null) {
printProlog(pw, isIncluded);
printResourceInfo(pw, r);
render(pw, r, r.adaptTo(String[].class));
printEpilog(pw, isIncluded);
} else {
if (!isIncluded) {
// NO Content
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}
}
use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.
the class PlainTextRendererServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws ServletException, IOException {
final Resource r = req.getResource();
if (ResourceUtil.isNonExistingResource(r)) {
throw new ResourceNotFoundException("No data to render.");
}
/*
* TODO if(srd != null) { renderSyntheticResource(req, resp, srd);
* return; }
*/
resp.setContentType(req.getResponseContentType());
resp.setCharacterEncoding("UTF-8");
final PrintWriter pw = resp.getWriter();
@SuppressWarnings("unchecked") final Map map = r.adaptTo(Map.class);
if (map != null) {
dump(pw, r, map);
} else if (r.adaptTo(String.class) != null) {
printPropertyValue(pw, ResourceUtil.getName(r), r.adaptTo(String.class), false);
} else if (r.adaptTo(String[].class) != null) {
printPropertyValue(pw, ResourceUtil.getName(r), r.adaptTo(String[].class), false);
} else {
resp.sendError(HttpServletResponse.SC_NO_CONTENT);
}
}
use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.
the class StreamRendererServlet method processRequest.
private void processRequest(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
// whether this servlet is called as of a request include
final boolean included = request.getAttribute(SlingConstants.ATTR_REQUEST_SERVLET) != null;
// ensure no extension or "res"
String ext = request.getRequestPathInfo().getExtension();
if (ext != null && !ext.equals(EXT_RES)) {
request.getRequestProgressTracker().log("StreamRendererServlet does not support for extension " + ext);
if (included || response.isCommitted()) {
log.error("StreamRendererServlet does not support extension {}", ext);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
return;
}
Resource resource = request.getResource();
if (ResourceUtil.isNonExistingResource(resource)) {
throw new ResourceNotFoundException("No data to render.");
}
if ("/".equals(request.getRequestPathInfo().getSuffix())) {
renderDirectory(request, response, included);
return;
}
// check the last modification time and If-Modified-Since header
if (!included) {
ResourceMetadata meta = resource.getResourceMetadata();
long modifTime = meta.getModificationTime();
if (unmodified(request, modifTime)) {
response.setStatus(SC_NOT_MODIFIED);
return;
}
}
// fall back to plain text rendering if the resource has no stream
if (resource.getResourceType().equals(JcrConstants.NT_LINKEDFILE)) {
final ValueMap vm = resource.adaptTo(ValueMap.class);
final String actualResourcePath = vm.get(JcrConstants.JCR_CONTENT, String.class);
resource = request.getResourceResolver().getResource(actualResourcePath);
}
InputStream stream = resource.adaptTo(InputStream.class);
if (stream != null) {
if (isHeadRequest(request)) {
setContentLength(response, resource.getResourceMetadata().getContentLength());
setHeaders(resource, response);
return;
}
streamResource(resource, stream, included, request, response);
} else {
// the resource is the root, do not redirect, immediately index
if (isRootResourceRequest(resource)) {
renderDirectory(request, response, included);
} else if (included || response.isCommitted()) {
// request is included or committed, not redirecting
request.getRequestProgressTracker().log("StreamRendererServlet: Not redirecting with trailing slash, response is committed or request included");
log.warn("StreamRendererServlet: Not redirecting with trailing slash, response is committed or request included");
} else {
// redirect to this with trailing slash to render the index
String url = request.getResourceResolver().map(request, resource.getPath()) + "/";
response.sendRedirect(url);
}
}
}
use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.
the class AbstractPostServlet method doPost.
/*
* (non-Javadoc)
* @see
* org.apache.sling.api.servlets.SlingAllMethodsServlet#doPost(org.apache
* .sling.api.SlingHttpServletRequest,
* org.apache.sling.api.SlingHttpServletResponse)
*/
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse httpResponse) throws ServletException, IOException {
// prepare the response
AbstractPostResponse response = createHtmlResponse(request);
response.setReferer(request.getHeader("referer"));
// calculate the paths
String path = getItemPath(request);
response.setPath(path);
// location
response.setLocation(externalizePath(request, path));
// parent location
path = ResourceUtil.getParent(path);
if (path != null) {
response.setParentLocation(externalizePath(request, path));
}
Session session = request.getResourceResolver().adaptTo(Session.class);
final List<Modification> changes = new ArrayList<Modification>();
try {
handleOperation(request, response, changes);
// set changes on html response
for (Modification change : changes) {
switch(change.getType()) {
case MODIFY:
response.onModified(change.getSource());
break;
case DELETE:
response.onDeleted(change.getSource());
break;
case MOVE:
response.onMoved(change.getSource(), change.getDestination());
break;
case COPY:
response.onCopied(change.getSource(), change.getDestination());
break;
case CREATE:
response.onCreated(change.getSource());
break;
case ORDER:
response.onChange("ordered", change.getSource(), change.getDestination());
break;
}
}
if (session.hasPendingChanges()) {
session.save();
}
} catch (ResourceNotFoundException rnfe) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage());
} catch (Throwable throwable) {
log.debug("Exception while handling POST " + request.getResource().getPath() + " with " + getClass().getName(), throwable);
response.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 (response.isSuccessful()) {
String redirect = getRedirectUrl(request, response);
if (redirect != null) {
httpResponse.sendRedirect(redirect);
return;
}
}
// create a html response and send if unsuccessful or no redirect
response.send(httpResponse, isSetStatus(request));
}
use of org.apache.sling.api.resource.ResourceNotFoundException in project sling by apache.
the class ChangeUserPasswordServlet method changePassword.
/* (non-Javadoc)
* @see org.apache.sling.jackrabbit.usermanager.ChangeUserPassword#changePassword(javax.jcr.Session, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.List)
*/
public User changePassword(Session jcrSession, String name, String oldPassword, String newPassword, String newPasswordConfirm, List<Modification> changes) throws RepositoryException {
if ("anonymous".equals(name)) {
throw new RepositoryException("Can not change the password of the anonymous user.");
}
User user;
UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
Authorizable authorizable = userManager.getAuthorizable(name);
if (authorizable instanceof User) {
user = (User) authorizable;
} else {
throw new ResourceNotFoundException("User to update could not be determined");
}
//SLING-2069: if the current user is an administrator, then a missing oldPwd is ok,
// otherwise the oldPwd must be supplied.
boolean administrator = false;
// check that the submitted parameter values have valid values.
if (oldPassword == null || oldPassword.length() == 0) {
try {
UserManager um = AccessControlUtil.getUserManager(jcrSession);
User currentUser = (User) um.getAuthorizable(jcrSession.getUserID());
administrator = currentUser.isAdmin();
if (!administrator) {
//check if the user is a member of the 'User administrator' group
Authorizable userAdmin = um.getAuthorizable(this.userAdminGroupName);
if (userAdmin instanceof Group) {
boolean isMember = ((Group) userAdmin).isMember(currentUser);
if (isMember) {
administrator = true;
}
}
}
} catch (Exception ex) {
log.warn("Failed to determine if the user is an admin, assuming not. Cause: " + ex.getMessage());
administrator = false;
}
if (!administrator) {
throw new RepositoryException("Old Password was not submitted");
}
}
if (newPassword == null || newPassword.length() == 0) {
throw new RepositoryException("New Password was not submitted");
}
if (!newPassword.equals(newPasswordConfirm)) {
throw new RepositoryException("New Password does not match the confirmation password");
}
try {
if (oldPassword != null && oldPassword.length() > 0) {
// verify old password
user.changePassword(newPassword, oldPassword);
} else {
user.changePassword(newPassword);
}
final String passwordPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PREFIX + user.getID() + "/rep:password";
changes.add(Modification.onModified(passwordPath));
} catch (RepositoryException re) {
throw new RepositoryException("Failed to change user password.", re);
}
return user;
}
Aggregations