Highlighted

SlingServlet to handle GET requests from multiple extensions

singhprabhakar

27-09-2019

Hi All,

I'm using AEM6.4 and for a particular case I have a custom sling servlet must address get requests from multiple extensions.

Eg:

Hence, for any GET request from any of the above URLs('/content/rc/*/*.zip' ,etc).

Now, I have the below code snippet which works completely fine for '.pdf' extension but fails for any other extension.

I doubt that if it has to do anything with the way I have defined multiple extensions because as do notice below pdf is the second extension defined, i.e. its ignoring all other extensions.

1838017_pastedImage_0.png

How can I address this?

Any suggestions/help would be highly appreciated.

arunpatidar26smacdonald2008

Thanks & Regards,

Replies

Highlighted

Theo_Pendle

MVP

27-09-2019

Hi,

You say it works fine for .pdf extensions, but could you be more specific about how it is failing for the others?

Are you getting a 404 - Resource not found? Are you getting a 200 but an error during the execution of doGet()? Etc.

Could you also give us an XML sample of the node structure under /content/rc/abs/en/membership/interviews/AESSVID0250 please?

I think I have an idea but need more info

Highlighted

Arun_Patidar

MVP

27-09-2019

Hi,

There is a chance to having another servlet which is being resolved and has the highest ranking.

Can you check your request at http://localhost:4502/system/console/servletresolver

Check which servlet is getting called for zip,xls and ppt extensions.

Highlighted

singhprabhakar

27-09-2019

Hi Arun,

That's what I guessed, bcz when I updated the following property under '/system/console/configMgr' :

'Apache Sling GET Servlet => xml:pdf,zip,ppt '

The requests get handled by the default GET servlet and it downloads the asset(.zip,.ppt,etc) from the dam(what we want to achieve) but the control still never came to our custom servlet.

Thanks to you, now I do see which default servlet handles the request with extension .zip,.ppt, etc

1838120_pastedImage_3.png

BUT, how do I get my custom servlet to handle such request and possibly override any default servlet handlers?

Thanks & Regards,

Highlighted

singhprabhakar

01-10-2019

arunpatidar26 Yes, but that means the servlet is registered for one of the extensions but not for others(as .pdf requests are redirected to my custom servlet). I was not sure if that's possible

Also from the list of extensions defined for this servlet, it always only accepts pdf irrespective of its positioning.

eg: extension ={"pdf", "zip","ppt"}  or extensions = {"zip", "pdf", "ppt"}  or extensions = {"zip", "ppt", "pdf"}

Is there any place else where we need to define the list of extensions allowed by a servlet?

or

Is there something wrong with the way I have defined extensions because it does pick .pdf extension?

1838872_pastedImage_0.png

Regards,

Highlighted

Arun_Patidar

MVP

03-10-2019

Hi,

Can you try to set mime type based on the extension.

example:

package com.aem.community.core.servlets;

import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

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.ValueMap;

import org.apache.sling.api.servlets.HttpConstants;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

  "sling.servlet.methods=" + HttpConstants.METHOD_GET,

  "sling.servlet.resourceTypes=" + "AEM63App/components/structure/page", "sling.servlet.extensions=" + "txt",

  "sling.servlet.extensions=" + "pdf", "sling.servlet.extensions=" + "ppt", "sling.servlet.extensions=" + "zip",

  "sling.servlet.extensions=" + "xsl" })

public class SimpleServletMultiExtension extends SlingSafeMethodsServlet {

  private static final long serialVersionUID = 1L;

  @Override

  protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)

  throws ServletException, IOException {

  final Resource resource = req.getResource();

  if (req.getPathInfo().contains("txt")) {

  resp.setContentType("text/plain");

  resp.getWriter().write("TEXT Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));

  } else if (req.getPathInfo().contains("pdf")) {

  resp.setContentType("application/pdf");

  resp.getWriter().write("PDF Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));

  } else if (req.getPathInfo().contains("ppt")) {

  resp.setContentType("application/vnd.ms-powerpoint");

  resp.getWriter().write("PPT Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));

  } else if (req.getPathInfo().contains("zip")) {

  resp.setContentType("application/zip");

  resp.getWriter().write("Zip Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));

  } else if (req.getPathInfo().contains("xls")) {

  resp.setContentType("application/vnd.ms-excel");

  resp.getWriter().write("XLS Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));

  }

  }

}

Screenshot 2019-10-03 at 11.56.49 PM.png