Last active
December 28, 2015 19:18
-
-
Save topolik/7548995 to your computer and use it in GitHub Desktop.
filter replacing content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the filter: | |
@Override | |
public void doFilter( | |
ServletRequest servletRequest, ServletResponse servletResponse, | |
FilterChain filterChain) | |
throws IOException, ServletException { | |
HttpServletRequest request = (HttpServletRequest) servletRequest; | |
HttpServletResponse response = (HttpServletResponse) servletResponse; | |
/* | |
Apply only on the appropriate site (assuming you defined the virtual host to the site public pages) | |
*/ | |
String siteVirtualHost = "localhost"; | |
if(!request.getServerName().equals(siteVirtualHost)) { | |
filterChain.doFilter(servletRequest, servletResponse); | |
return; | |
} | |
/* | |
Cache response | |
*/ | |
GenericServletResponse bufferedResponse = new GenericServletResponse(response); | |
filterChain.doFilter(servletRequest, bufferedResponse); | |
byte[] text = bufferedResponse.getData(); | |
String xml = new String(text, "UTF-8"); | |
/* | |
Remove the lines from the XML | |
*/ | |
// just testing ... :) | |
xml = xml.replaceAll("localhost", "testtesttest"); | |
response.getWriter().write(xml); | |
} | |
// liferay-hook.xml mapping: | |
<servlet-filter-mapping> | |
<servlet-filter-name>Sample Filter</servlet-filter-name> | |
<after-filter>GZip Filter</after-filter> | |
<url-pattern>/c/layouts_admin/sitemap</url-pattern> | |
<dispatcher>REQUEST</dispatcher> | |
<dispatcher>FORWARD</dispatcher> | |
</servlet-filter-mapping> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment