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?
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
I use this app to upload JS/CSS files to a clientlibs folder. It works great.
Views
Replies
Total Likes
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")
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies