My team has 10 PDFs located on our DAM that we are wanting to have redirect to a different file. Does anyone know if this is possible?
Thanks,
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @garrettshue,
This is possible, you have a look on below options:
package com.example.filters; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.engine.EngineConstants; import org.osgi.framework.Constants; import org.osgi.service.component.annotations.Component; import javax.servlet.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Component(service = Filter.class, immediate = true, property = { EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST, EngineConstants.SLING_FILTER_METHODS + "=" + HttpConstants.METHOD_GET, Constants.SERVICE_RANKING + "=" + Integer.MAX_VALUE }) public class RedirectFilter implements Filter { private Map<String, String> redirectMap = new HashMap<String, String>(); @Override public void init(FilterConfig filterConfig) throws ServletException { redirectMap.put("/document/adobe.pdf", "/content/dam/wknd/en/adobefile.pdf"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest; SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) servletResponse; String path = slingRequest.getRequestPathInfo().getResourcePath(); if (redirectMap.containsKey(slingRequest.getRequestPathInfo().getResourcePath())) { slingResponse.sendRedirect(redirectMap.get(path)); return; } filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
Hi @garrettshue,
This is possible, you have a look on below options:
package com.example.filters; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.engine.EngineConstants; import org.osgi.framework.Constants; import org.osgi.service.component.annotations.Component; import javax.servlet.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Component(service = Filter.class, immediate = true, property = { EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST, EngineConstants.SLING_FILTER_METHODS + "=" + HttpConstants.METHOD_GET, Constants.SERVICE_RANKING + "=" + Integer.MAX_VALUE }) public class RedirectFilter implements Filter { private Map<String, String> redirectMap = new HashMap<String, String>(); @Override public void init(FilterConfig filterConfig) throws ServletException { redirectMap.put("/document/adobe.pdf", "/content/dam/wknd/en/adobefile.pdf"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest; SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) servletResponse; String path = slingRequest.getRequestPathInfo().getResourcePath(); if (redirectMap.containsKey(slingRequest.getRequestPathInfo().getResourcePath())) { slingResponse.sendRedirect(redirectMap.get(path)); return; } filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
Hi @garrettshue ,
Though we can write redirects but curious to know if we are talking about only 10 PDFs then why don't we replace them with correct files. Redirects can be seen on browser and redirecting one pdf URL to another one is not a good user experience.
Unless those URLs are bookmarked ones.
Thanks,
Ritesh Mittal