Skip to content

Commit

Permalink
Enabled access to request meta-data after first operation in batch
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Sep 16, 2016
1 parent 93ef5fa commit f107748
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 38 deletions.
Expand Up @@ -40,6 +40,7 @@
import javax.ws.rs.core.UriInfo;

import org.neo4j.server.rest.web.InternalJettyServletRequest;
import org.neo4j.server.rest.web.InternalJettyServletRequest.RequestData;
import org.neo4j.server.rest.web.InternalJettyServletResponse;
import org.neo4j.server.web.WebServer;

Expand Down Expand Up @@ -152,6 +153,8 @@ protected void parseAndPerform( UriInfo uriInfo, HttpHeaders httpHeaders, HttpSe
{
JsonParser jp = jsonFactory.createJsonParser(body);
JsonToken token;
RequestData requestData = RequestData.from( req );

while ((token = jp.nextToken()) != null)
{
if (token == JsonToken.START_OBJECT)
Expand Down Expand Up @@ -182,7 +185,7 @@ protected void parseAndPerform( UriInfo uriInfo, HttpHeaders httpHeaders, HttpSe
}
// Read one job description. Execute it.
performRequest( uriInfo, jobMethod, jobPath, jobBody,
jobId, httpHeaders, locations, req );
jobId, httpHeaders, locations, requestData );
}
}
}
Expand All @@ -201,14 +204,14 @@ private String readBody( JsonParser jp ) throws IOException

protected void performRequest( UriInfo uriInfo, String method, String path, String body, Integer id,
HttpHeaders httpHeaders, Map<Integer, String> locations,
HttpServletRequest outerReq ) throws IOException, ServletException
RequestData requestData ) throws IOException, ServletException
{
path = replaceLocationPlaceholders(path, locations);
body = replaceLocationPlaceholders(body, locations);
URI targetUri = calculateTargetUri(uriInfo, path);

InternalJettyServletResponse res = new InternalJettyServletResponse();
InternalJettyServletRequest req = new InternalJettyServletRequest( method, targetUri.toString(), body, res, outerReq );
InternalJettyServletRequest req = new InternalJettyServletRequest( method, targetUri.toString(), body, res, requestData );
req.setScheme( targetUri.getScheme() );
addHeaders( req, httpHeaders );

Expand Down
Expand Up @@ -132,22 +132,18 @@ public boolean isAsync()
private final String method;
private final InternalJettyServletResponse response;

/** Optional, another HttpServletRequest to use to pull metadata, like remote address and port, out of. */
private HttpServletRequest outerRequest;
/** Contains metadata for the request, for example remote address and port. */
private final RequestData requestData;

public InternalJettyServletRequest( String method, String uri, String body, InternalJettyServletResponse res ) throws UnsupportedEncodingException
public InternalJettyServletRequest( String method, String uri, String body, InternalJettyServletResponse res,
RequestData requestData ) throws UnsupportedEncodingException
{
this( method, new HttpURI( uri ), body, new Cookie[] {}, MediaType.APPLICATION_JSON, StandardCharsets.UTF_8.name(), res );
this( method, new HttpURI( uri ), body, new Cookie[] {}, MediaType.APPLICATION_JSON,
StandardCharsets.UTF_8.name(), res, requestData );
}

public InternalJettyServletRequest( String method, String uri, String body, InternalJettyServletResponse res, HttpServletRequest outerReq ) throws UnsupportedEncodingException
{
this( method, new HttpURI( uri ), body, new Cookie[] {}, MediaType.APPLICATION_JSON, StandardCharsets.UTF_8.name(), res);
this.outerRequest = outerReq;
}

public InternalJettyServletRequest( String method, HttpURI uri, String body, Cookie[] cookies, String contentType, String encoding, InternalJettyServletResponse res )
throws UnsupportedEncodingException
public InternalJettyServletRequest( String method, HttpURI uri, String body, Cookie[] cookies, String contentType,
String encoding, InternalJettyServletResponse res, RequestData requestData ) throws UnsupportedEncodingException
{
super( null, null );

Expand All @@ -158,6 +154,7 @@ public InternalJettyServletRequest( String method, HttpURI uri, String body, Coo
this.cookies = cookies;
this.method = method;
this.response = res;
this.requestData = requestData;

this.headers = new HashMap<>();

Expand Down Expand Up @@ -213,49 +210,49 @@ public BufferedReader getReader() throws IOException
@Override
public String getRemoteAddr()
{
return outerRequest == null ? null : outerRequest.getRemoteAddr();
return requestData.remoteAddress;
}

@Override
public String getRemoteHost()
{
return outerRequest == null ? null : outerRequest.getRemoteHost();
return requestData.remoteHost;
}

@Override
public boolean isSecure()
{
return outerRequest != null && outerRequest.isSecure();
return requestData.isSecure;
}

@Override
public int getRemotePort()
{
return outerRequest == null ? -1 : outerRequest.getRemotePort();
return requestData.remotePort;
}

@Override
public String getLocalName()
{
return outerRequest == null ? null : outerRequest.getLocalName();
return requestData.localName;
}

@Override
public String getLocalAddr()
{
return outerRequest == null ? null : outerRequest.getLocalAddr();
return requestData.localAddress;
}

@Override
public int getLocalPort()
{
return outerRequest == null ? -1 : outerRequest.getLocalPort();
return requestData.localPort;
}

@Override
public String getAuthType()
{
return outerRequest == null ? null : outerRequest.getAuthType();
return requestData.authType;
}

@Override
Expand Down Expand Up @@ -361,4 +358,50 @@ public HttpChannelState getHttpChannelState()
{
return DUMMY_HTTP_CHANNEL_STATE;
}

public static class RequestData
{
public final String remoteAddress;
public final String remoteHost;
public final boolean isSecure;
public final int remotePort;
public final String localName;
public final String localAddress;
public final int localPort;
public final String authType;

public RequestData(
String remoteAddress,
String remoteHost,
boolean isSecure,
int remotePort,
String localName,
String localAddress,
int localPort,
String authType
) {
this.remoteAddress = remoteAddress;
this.remoteHost = remoteHost;
this.isSecure = isSecure;
this.remotePort = remotePort;
this.localName = localName;
this.localAddress = localAddress;
this.localPort = localPort;
this.authType = authType;
}

public static RequestData from( HttpServletRequest req )
{
return new RequestData(
req.getRemoteAddr(),
req.getRemoteHost(),
req.isSecure(),
req.getRemotePort(),
req.getLocalName(),
req.getLocalAddr(),
req.getLocalPort(),
req.getAuthType()
);
}
}
}
Expand Up @@ -26,16 +26,15 @@
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.junit.Test;

import org.neo4j.server.rest.web.InternalJettyServletRequest;
import org.neo4j.server.rest.web.InternalJettyServletRequest.RequestData;
import org.neo4j.server.rest.web.InternalJettyServletResponse;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BatchOperationsTest {

Expand All @@ -59,7 +58,9 @@ public void testReplaceLocations() throws Exception {
public void testSchemeInInternalJettyServletRequestForHttp() throws UnsupportedEncodingException
{
// when
InternalJettyServletRequest req = new InternalJettyServletRequest( "POST", "http://localhost:7473/db/data/node", "{'name':'node1'}", new InternalJettyServletResponse(), mock( HttpServletRequest.class ) );
InternalJettyServletRequest req = new InternalJettyServletRequest( "POST",
"http://localhost:7473/db/data/node", "{'name':'node1'}", new InternalJettyServletResponse(),
mock( RequestData.class ) );

// then
assertEquals("http",req.getScheme());
Expand All @@ -69,36 +70,35 @@ public void testSchemeInInternalJettyServletRequestForHttp() throws UnsupportedE
public void testSchemeInInternalJettyServletRequestForHttps() throws UnsupportedEncodingException
{
// when
InternalJettyServletRequest req = new InternalJettyServletRequest( "POST", "https://localhost:7473/db/data/node", "{'name':'node1'}", new InternalJettyServletResponse(), mock( HttpServletRequest.class ) );
InternalJettyServletRequest req = new InternalJettyServletRequest( "POST",
"https://localhost:7473/db/data/node", "{'name':'node1'}", new InternalJettyServletResponse(),
mock( RequestData.class ) );

// then
assertEquals("https",req.getScheme());
}

@Test
public void shouldForwardMetadataFromOuterRequest() throws Exception
public void shouldForwardMetadataFromRequestData() throws Exception
{
// Given
HttpServletRequest mock = mock( HttpServletRequest.class );

when(mock.getAuthType()).thenReturn( "authorization/auth" );
when(mock.getRemoteAddr()).thenReturn( "127.0.0.1" );
when(mock.getRemoteHost()).thenReturn( "localhost" );
when(mock.getRemotePort()).thenReturn( 1 );
when(mock.getLocalAddr()).thenReturn( "129.0.0.1" );
when(mock.getLocalPort()).thenReturn( 2 );
RequestData mock = new RequestData(
"127.0.0.1", "localhost", true, 1,
"TheLocalName", "129.0.0.1", 2, "authorization/auth" );

InternalJettyServletRequest req = new InternalJettyServletRequest( "POST",
"https://localhost:7473/db/data/node", "", new InternalJettyServletResponse(),
mock );

// When & then
assertEquals( "authorization/auth", req.getAuthType());
assertEquals( "127.0.0.1", req.getRemoteAddr());
assertEquals( "localhost", req.getRemoteHost());
assertEquals( true, req.isSecure() );
assertEquals( 1, req.getRemotePort());
assertEquals( 2, req.getLocalPort());
assertEquals( "TheLocalName", req.getLocalName());
assertEquals( "129.0.0.1", req.getLocalAddr());
assertEquals( 2, req.getLocalPort());
assertEquals( "authorization/auth", req.getAuthType());

}

Expand Down

0 comments on commit f107748

Please sign in to comment.