Expand my Community achievements bar.

SOLVED

AEM always returns 200 status code.

Avatar

Level 2

Upon requesting non-existent pages, both aem author and publish instances return blank page with 200 status code. The default 404 error page is not showing.

1 Accepted Solution

Avatar

Correct answer by
Employee

Do you have any custom filters that can possibly massage the response? You might want to stop your custom bundles in OSGi and use /system/console/requests to track recent requests.

View solution in original post

34 Replies

Avatar

Level 2

Hi himanshusinghal,

I just tried to increase the size to different sizes. No matter how many size I increased, I still got the 200 status code and the blank page.

Avatar

Correct answer by
Employee

Do you have any custom filters that can possibly massage the response? You might want to stop your custom bundles in OSGi and use /system/console/requests to track recent requests.

Avatar

Level 2

Hi berliant,

Thank you for your reply. I have one custom filter that sets status code to 400 if the slinghttprequest does not satisfy certain condition. After I stopped the custom bundle, the 404 not found status code shows up correctly. However, this custom bundle is also needed in the project, is there any way to solve the issue instead of stopping the custom bundle?

Avatar

Employee

You might need to add configuration properties, as such you can define only those URLs that required a specific status.

Avatar

Level 2

Hi berliant,

In the custom filter, I actually add the logic to check the request URI, if it is not the path of the target servlet, the filter won't do anything (I use filterChain.doFilter(request, response) for this situation). Somehow I don't know why the response status code is always 200. If I put the filter logic inside the servlet and delete the filter, the 404 page works correctly.  But with this way, the servlet will have a very long code. I also implemented another filter to add header options to servlet response, however, this filter does not affect the status code.

Avatar

Level 7

Hi,

Have you already tryied to change the status into the ResponseStatus.java?

Just to check if a different status code is returned let's try to do this:

public void activate() throws Exception {

        getResponse().setStatus(407);

    }

Let us know.

Thanks,

Antonio

Avatar

Level 2

Hi, antoniom54959291

Thank you for your response. Do you mean that use this method in the Filter.java?

Avatar

Level 7

Hi,

I'm referring to your previous post:

6. Re: AEM always returns 200 status code.

yes, I created a 404.html and ResponseStatus.java under /apps/sling/servlet/errorhandler

In the 404.html, the code is as:

<sly data-sly-use.responseStatus="apps.sling.servlet.errorhandler.ResponseStatus">

    <sly data-sly-resource="/content/myproject/en/page-not-found.html"></sly>

</sly>

The code in ResponseStatus.java is:

package apps.sling.servlet.errorhandler;

import com.adobe.cq.sightly.WCMUsePojo;

public class ResponseStatus extends WCMUsePojo {

    @Override

    public void activate() throws Exception {

        getResponse().setStatus(404);

    }

}

If you have this implementation, you can try to change the setStatus in the ResponseStatus Pojo in order to check if you have an other different status code.

Let us know.

Thanks,

Antonio

Avatar

Level 2

Hi antoniom54959291

I tried changing the setStatus code in the errorhandler, the response still gets 200 for invalid url. I then stop the bundle that includes a custom filter (as suggested by @berliantberliantTherefore, it is the custom filter that causes the issue. I am still struggling on fixing the filter's problem.

Avatar

Level 2

I figure out the problem of the custom filter, the pattern that restricts the filter to the target servlet has a wrong setting. Now I change it to the correct setting, the 404 Page works correctly now. Thank you for all your help!

Avatar

Level 1

Hi, janellic4!

I got the problem exactly like yours.

Can you tell what was wrong in your custom filter and how you fix it? How did you find any useful information by "/system/console/requests"? I can't see any changes of status-code here.

Also can you change status of response in custom filter when slice-model throws an exception?

Thank you,

Daria

Avatar

Level 2

Hi DariaSuleimanova,

In my custom filter, there is a sling.filter.pattern property, but what I use is "pattern", so this filter cause the problem. After I changed it to "sling.filter.pattern", the 404 page shows correctly.

Avatar

Level 1

Thank you for your answer!

Can you please share your code?

I made Filter by @SlingFilter but didn't use sling.filter.pattern at all

Filter will not work without this property?

Avatar

Level 2

Hi DariaSuleimanova,

Filter will work without this property. For my situation, I want to restrict my custom filter to certain servlet, so I use the sling.filter.pattern property.

@Component(service = Filter.class,

property = {

                    Constants.SERVICE_DESCRIPTION + "=My Filter",

             EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,

                    EngineConstants.SLING_FILTER_PATTERN + "=" + "/my_path_pattern",

                   Constants.SERVICE_RANKING + ":Integer=-100"

           })

public class MyFilter implements Filter{

    private final Logger logger = LoggerFactory.getLogger(MyFilter.class);

    @Override

    public void doFilter(final ServletRequest request, final ServletResponse response,

final FilterChain filterChain) throws IOException, ServletException {

        final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;

        String requestURI = slingRequest.getRequestURI();

         //code to do something

filterChain.doFilter(request, response);

    }

    @Override

    public void init(FilterConfig filterConfig) {

    }

    @Override

    public void destroy() {

    }

}