Expand my Community achievements bar.

SOLVED

Servlet accessible on author instance

Avatar

Level 2

I made a servlet eg:

@SlingServlet(paths = { "/servlets/exampleServlet" }, methods = { "POST" }, extensions = { "json" }) public class ExampleServlet extends SlingAllMethodsServlet { @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { //return something } }

The thing is that my servlet is accessible when I access it on publish instance. But when I try to access it on admin instance it returns 401 http code.

Can I make it accessible on my author instance and how?

1 Accepted Solution

Avatar

Correct answer by
Level 10

If uou are invoking the servlet from outside cq, make sure you authenticate in the http request.  

Here is an article that posts files to cq from a java swing app

https://helpx.adobe.com/experience-manager/using/post_files.html

This example posts using this java code:

//Posts the selected file to AEM
    private static void postAEM(File file, String host, String path, String mimeType)
    {
        try {
 
            String aemPostUrl = host+"/bin/upfile";
             HttpPost post = new HttpPost(aemPostUrl);
            org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity ();
            byte[] b = new byte[(int)file.length()];
            org.apache.http.entity.mime.content.FileBody fileBody = new org.apache.http.entity.mime.content.FileBody(file, mimeType) ;
            org.apache.http.entity.mime.content.StringBody imageTitle = new org.apache.http.entity.mime.content.StringBody(path);
 
            entity.addPart("imageTitle", imageTitle);
            entity.addPart("image", fileBody);
            post.setEntity(entity);
 
            org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
            org.apache.http.HttpResponse response = null;
 
            response = client.execute(post);
            System.out.println("Done") ;
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }

 

This is an example of invoking a servlet from outside cq.

View solution in original post

8 Replies

Avatar

Level 2

Hi Marko,

As far as I can say, this behavior is expected. Every request made to an author instance must be authenticated.

Moreover, I noticed you're trying to bind a Servlet to a specific path. I wanted to let you know that it is recommended to bind your servlet to a resource type. This way you can easily rely on ACL to restrict access to your instance but also have multiple configurations stored at a node level.

Hope that helps,

Alex

Avatar

Level 2

Are you saying it cannot be accessed like this? I know this is default behavior, but I want to make it accessible.
I do not see how I would use the servlet bound to resource type (i do not have any resource here to bind it with - i just need only one servlet).
What I need is a third party accessing the API through servlet. It has some internal logic that based on parameters returns correct json.
 

Avatar

Level 10

Try using an ajax request to do this. 

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Invoking a servlet on author is not an issue. It works.

Avatar

Level 2

No. I do not need ajax request on server. I have loads of other servlets for ajax and post that work on author instance. Why do they work? They have a session being previously logged on. 

What I need is a servlet that will be accessible for a third party application, not any browser or javascript requests that already have cookies. 

Avatar

Correct answer by
Level 10

If uou are invoking the servlet from outside cq, make sure you authenticate in the http request.  

Here is an article that posts files to cq from a java swing app

https://helpx.adobe.com/experience-manager/using/post_files.html

This example posts using this java code:

//Posts the selected file to AEM
    private static void postAEM(File file, String host, String path, String mimeType)
    {
        try {
 
            String aemPostUrl = host+"/bin/upfile";
             HttpPost post = new HttpPost(aemPostUrl);
            org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity ();
            byte[] b = new byte[(int)file.length()];
            org.apache.http.entity.mime.content.FileBody fileBody = new org.apache.http.entity.mime.content.FileBody(file, mimeType) ;
            org.apache.http.entity.mime.content.StringBody imageTitle = new org.apache.http.entity.mime.content.StringBody(path);
 
            entity.addPart("imageTitle", imageTitle);
            entity.addPart("image", fileBody);
            post.setEntity(entity);
 
            org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
            org.apache.http.HttpResponse response = null;
 
            response = client.execute(post);
            System.out.println("Done") ;
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }

 

This is an example of invoking a servlet from outside cq.

Avatar

Level 10

I use this app to upload JS/CSS files to a clientlibs folder. It works great.

Avatar

Level 2

Tnx for the example.I just needed to add this line to servlet to be accessible on the author. No authorization needed afterwards.

@Property(name = "sling.auth.requirements", value = "-/servlets/exampleServlet")

Avatar

Level 10
When doing no authentication, keep URL secret. I use this for my own tool. For public use, use authentication.