Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM always returns 200 status code.

janellic4
Level 2
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
berliant
Correct answer by
Employee
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

0 Replies
janellic4
Level 2
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.

berliant
Correct answer by
Employee
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

janellic4
Level 2
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?

berliant
Employee
Employee

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

janellic4
Level 2
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.

antoniom5495929
Level 7
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

janellic4
Level 2
Level 2

Hi, antoniom54959291

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

antoniom5495929
Level 7
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

janellic4
Level 2
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.

janellic4
Level 2
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!

DariaSuleimanov
Level 1
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

janellic4
Level 2
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.

DariaSuleimanov
Level 1
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?

janellic4
Level 2
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() {

    }

}