Pages

Friday, 10 January 2014

Check Authentication in CQ

AuthCheckerServlet.java

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
@SlingServlet(paths = "/bin/permissioncheck/html", generateComponent = true, generateService = true)
public class AuthCheckerServlet extends SlingSafeMethodsServlet {
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {       
        String uri = request.getParameter("uri");       
        Resource resource = request.getResourceResolver().resolve(uri);       
        if(ResourceUtil.isNonExistingResource(resource)) {           
            response.setStatus(SlingHttpServletResponse.SC_FORBIDDEN);           
            } else {           
            response.setStatus(SlingHttpServletResponse.SC_OK);           
        }       
    }   
} 


No comments:

Post a Comment

Converting InputStream to String

    private String convertToString(InputStream inputStreamObj)             throws IOException {         if (inputStreamObj != null) {     ...