Commit 6547af2d by nixonrodrigues Committed by Madhan Neethiraj

ATLAS-1985: fix incorrect URL-encoding while redirecting to active instance in HA

Change-Id: I919fcef66368c10b1f8a752043c422a333835508 Signed-off-by: 's avatarMadhan Neethiraj <madhan@apache.org>
parent 2e8f6013
...@@ -20,7 +20,6 @@ package org.apache.atlas.web.filters; ...@@ -20,7 +20,6 @@ package org.apache.atlas.web.filters;
import org.apache.atlas.web.service.ActiveInstanceState; import org.apache.atlas.web.service.ActiveInstanceState;
import org.apache.atlas.web.service.ServiceState; import org.apache.atlas.web.service.ServiceState;
import org.apache.hadoop.http.HtmlQuoting;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -37,6 +36,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -37,6 +36,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.HttpMethod; import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder;
/** /**
* A servlet {@link Filter} that redirects web requests from a passive Atlas server instance to an active one. * A servlet {@link Filter} that redirects web requests from a passive Atlas server instance to an active one.
...@@ -127,14 +127,19 @@ public class ActiveServerFilter implements Filter { ...@@ -127,14 +127,19 @@ public class ActiveServerFilter implements Filter {
String activeServerAddress) throws IOException { String activeServerAddress) throws IOException {
String requestURI = servletRequest.getRequestURI(); String requestURI = servletRequest.getRequestURI();
String queryString = servletRequest.getQueryString(); String queryString = servletRequest.getQueryString();
if (queryString != null && (!queryString.isEmpty())) {
queryString = URLEncoder.encode(queryString, "UTF-8");
}
if ((queryString != null) && (!queryString.isEmpty())) { if ((queryString != null) && (!queryString.isEmpty())) {
requestURI += "?" + queryString; requestURI += "?" + queryString;
} }
String quotedUri = HtmlQuoting.quoteHtmlChars(requestURI);
if (quotedUri == null) { if (requestURI == null) {
quotedUri = "/"; requestURI = "/";
} }
String redirectLocation = activeServerAddress + quotedUri; String redirectLocation = activeServerAddress + requestURI;
LOG.info("Not active. Redirecting to {}", redirectLocation); LOG.info("Not active. Redirecting to {}", redirectLocation);
// A POST/PUT/DELETE require special handling by sending HTTP 307 instead of the regular 301/302. // A POST/PUT/DELETE require special handling by sending HTTP 307 instead of the regular 301/302.
// Reference: http://stackoverflow.com/questions/2068418/whats-the-difference-between-a-302-and-a-307-redirect // Reference: http://stackoverflow.com/questions/2068418/whats-the-difference-between-a-302-and-a-307-redirect
......
...@@ -137,11 +137,30 @@ public class ActiveServerFilterTest { ...@@ -137,11 +137,30 @@ public class ActiveServerFilterTest {
activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain);
verify(servletResponse).sendRedirect(ACTIVE_SERVER_ADDRESS + "types?query=TRAIT"); verify(servletResponse).sendRedirect(ACTIVE_SERVER_ADDRESS + "types?query%3DTRAIT");
} }
@Test @Test
public void testRedirectedRequestShouldContainEncodeQueryParameters() throws IOException, ServletException {
when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE);
ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState);
when(activeInstanceState.getActiveServerAddress()).thenReturn(ACTIVE_SERVER_ADDRESS);
when(servletRequest.getMethod()).thenReturn(HttpMethod.GET);
when(servletRequest.getRequestURI()).thenReturn("api/atlas/v2/search/basic");
when(servletRequest.getQueryString()).thenReturn("limit=25&excludeDeletedEntities=true&classification=ETL&_=1500969656054");
activeServerFilter.doFilter(servletRequest, servletResponse, filterChain);
verify(servletResponse).sendRedirect(ACTIVE_SERVER_ADDRESS +
"api/atlas/v2/search/basic?limit%3D25%26excludeDeletedEntities%3Dtrue%26classification%3DETL%26_%3D1500969656054");
}
@Test
public void testShouldRedirectPOSTRequest() throws IOException, ServletException { public void testShouldRedirectPOSTRequest() throws IOException, ServletException {
when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE);
when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment