HttpServletFilter does not send a custom response

Hello :slight_smile:

I’m currently writing an extension (with a confluence http filter module) for redirecting mobile users to another mobile site - for example, mobile users should simply get a response like <html>...<script src='...'></script></html>, nothing else. My current problem is, that my custom response is mixed somehow with the normal website… the script tag is inside the body tag of the normal page.

After detecting the mobile device in the doFilter-Method I do the following:

response.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.getWriter().write("" +
                            "<html>" +
                            "<head>" +
                            "</head>" +
                            "<body>" +
                            "<app></app>" +
                            "<script src='" +
                            "https://example.com/some.js" +
                            "'></script>"+
                            "</body>"+
                            "</html>");
httpServletResponse.getWriter().flush();
httpServletResponse.getWriter().close();

The script-tag is included within the normal website, how can I load only my few custom lines when someone is using a mobile phone?

Thanks - btw, the build-in Confluence Mobile Theme is disabled.

Also - the chain.doFilter(request,response);-Method is never called on mobile devices.

You can/should do it through a servlet-filter. Can you post the lines of your servlet-filter definition from the atlassian-plugin.xml ?My guess is that your weight/location isn’t far enough up in the chain so the mobile ui intercepts it.

Hi daniel,

thank you for your response! The error was that the location-attribute in the <servlet-filter> was not before-decoration.

I still have another problem… I’m trying to get a absolute link to some web resources (like this one Solved: Confluence Download Image Resource URL), so I can refer to (for example) a javascript with the generated url.

String iconURL =
        StaticAccessor.getWebResourceUrlProvider() ...

I can’t access StaticAccessor, do I need a dependency for that?

Also, I can’t include com.atlassian.confluence in my pom.xml:

<dependency>
  <groupId>com.atlassian.confluence</groupId>
  <artifactId>confluence</artifactId>
  <version>5.9.1</version>
  <scope>provided</scope>
</dependency>

The version does not work, but do I need such a dependency?

Thanks! :slight_smile:

I think the StaticAccessor.getWebResourceUrlProvider() not available for confluence.
Maybe only for jira, I not really sure.
You can just autowired WebResourceUrlProvider.

Thank you for your response Sahal :slight_smile:
After adding the spring dependency to my pom.xml-file I can access the @Autowired-Attribute:

<dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-core</artifactId>
            <version>1.1.3</version>
            <scope>provided</scope>
</dependency>

But it seems like the HttpServletFilter does is not in the Spring Scope, because

@Autowired
private WebResourceUrlProvider webResourceUrlProvider;

is always null. Is there an easy way to access the WebResourceUrlProvider with Spring in a Filter?

Thanks!

I use this for spring

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.6.RELEASE</version>
  <scope>provided</scope>
</dependency>

Maybe you can try Non-autowired way. Refer this docs.
https://developer.atlassian.com/confdev/development-resources/confluence-developer-faq/how-do-i-get-a-reference-to-a-component

Its good if you read this too .
https://developer.atlassian.com/docs/getting-started/plugin-modules/component-import-plugin-module