[ODATAJAVA-1134]Enhancement to run better with Spring

This commit is contained in:
Archana Rai 2017-06-08 16:37:27 +05:30
parent 32ff14fe78
commit 66ea810b0f
2 changed files with 67 additions and 1 deletions

View File

@ -57,6 +57,7 @@ import org.apache.olingo.server.core.debug.ServerCoreDebugger;
public class ODataHttpHandlerImpl implements ODataHttpHandler {
public static final int COPY_BUFFER_SIZE = 8192;
private static final String REQUESTMAPPING = "requestMapping";
private final ODataHandlerImpl handler;
private final ServerCoreDebugger debugger;
@ -267,7 +268,12 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
String rawRequestUri = httpRequest.getRequestURL().toString();
String rawODataPath;
if (!"".equals(httpRequest.getServletPath())) {
//Application need to set the request mapping attribute if the request is coming from a spring based application
if(httpRequest.getAttribute(REQUESTMAPPING)!=null){
String requestMapping = httpRequest.getAttribute(REQUESTMAPPING).toString();
int beginIndex = rawRequestUri.indexOf(requestMapping) + requestMapping.length();
rawODataPath = rawRequestUri.substring(beginIndex);
}else if(!"".equals(httpRequest.getServletPath())) {
int beginIndex = rawRequestUri.indexOf(httpRequest.getServletPath()) + httpRequest.getServletPath().length();
rawODataPath = rawRequestUri.substring(beginIndex);
} else if (!"".equals(httpRequest.getContextPath())) {

View File

@ -164,4 +164,64 @@ public class ODataHttpHandlerImplTest {
assertEquals(rawServiceResolutionUri, odr.getRawServiceResolutionUri());
}
}
@Test
public void extractUriForController() {
//@formatter:off (Eclipse formatter)
//CHECKSTYLE:OFF (Maven checkstyle)
String [][] uris = {
/* 0: host 1: cp 2: sp 3: sr 4: od 5: qp 6: spl */
{ "http://localhost", "", "/sp", "", "", "", "0"},
{ "http://localhost", "", "/sp", "", "/", "", "0"},
{ "http://localhost", "", "/sp", "", "/od", "", "0"},
{ "http://localhost", "/cp", "/sp", "", "", "", "0"},
{ "http://localhost", "/cp", "/sp", "", "/", "", "0"},
{ "http://localhost", "/cp", "/sp", "", "/od", "", "0"},
{ "http://localhost", "/cp", "/sp", "/sr", "", "", "1"},
{ "http://localhost", "/cp", "/sp", "/sr", "/", "", "1"},
{ "http://localhost", "/cp", "/sp", "/sr", "/od", "", "1"},
{ "http://localhost", "/cp", "/sp", "/sr", "/od", "qp", "1"},
{ "http://localhost:8080", "/c%20p", "/s%20p", "/s%20r", "/o%20d", "p+q", "1"},
};
//@formatter:on
// CHECKSTYLE:on
for (String[] p : uris) {
HttpServletRequest hr = mock(HttpServletRequest.class);
String requestUrl = p[0] + p[1] + p[2] + p[3] + p[4];
String requestUri = p[1] + p[2] + p[3] + p[4];
String queryString = p[5].isEmpty() ? null : p[5];
when(hr.getRequestURL()).thenReturn(new StringBuffer(requestUrl));
when(hr.getRequestURI()).thenReturn(requestUri);
when(hr.getQueryString()).thenReturn(queryString);
when(hr.getContextPath()).thenReturn(p[1]);
when(hr.getServletPath()).thenReturn(p[2]);
ODataRequest odr = new ODataRequest();
String rawBaseUri = p[0] + p[1] + p[2] + p[3];
String rawODataPath = p[4];
String rawQueryPath = "".equals(p[5]) ? null : p[5];
String rawRequestUri = requestUrl + (queryString == null ? "" : "?" + queryString);
String rawServiceResolutionUri = "".equals(p[3]) ? null : p[3];
when(hr.getAttribute("requestMapping")).thenReturn(p[2]);
ODataHttpHandlerImpl.fillUriInformation(odr, hr, Integer.parseInt(p[6]));
assertEquals(rawBaseUri, odr.getRawBaseUri());
assertEquals(rawODataPath, odr.getRawODataPath());
assertEquals(rawQueryPath, odr.getRawQueryPath());
assertEquals(rawRequestUri, odr.getRawRequestUri());
assertEquals(rawServiceResolutionUri, odr.getRawServiceResolutionUri());
}
}
}