[OLINGO-1201] Enhancements to run better with Netty

This commit is contained in:
ramya vasanth 2017-11-14 09:39:31 +05:30
parent 1ce51fd4ec
commit 5e21bb2ba5
5 changed files with 63 additions and 50 deletions

View File

@ -50,6 +50,11 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.16.Final</version>
</dependency>
</dependencies>
<build>

View File

@ -39,7 +39,7 @@ public interface ODataHttpHandler extends ODataHandler {
* @param response - HTTP OData response
*/
void process(HttpServletRequest request, HttpServletResponse response);
/**
* Sets the split parameter which is used for service resolution.
* @param split the number of path segments reserved for service resolution; default is 0

View File

@ -79,6 +79,11 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.16.Final</version>
</dependency>
</dependencies>
<build>

View File

@ -125,7 +125,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
environment.put("servletPath", request.getServletPath());
return environment;
}
private String getIntAsString(final int number) {
return number == 0 ? "unknown" : Integer.toString(number);
}
@ -164,7 +164,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
writeContent(odResponse, response);
}
}
static void writeContent(final ODataResponse odataResponse, final HttpServletResponse servletResponse) {
try {
ODataContent res = odataResponse.getODataContent();
@ -195,7 +195,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
closeStream(output);
}
}
private static void closeStream(final Channel closeable) {
if (closeable != null) {
try {
@ -205,7 +205,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
}
}
}
private ODataRequest fillODataRequest(final ODataRequest odRequest, final HttpServletRequest httpRequest,
final int split) throws ODataLibraryException {
final int requestHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest");
@ -228,43 +228,44 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
debugger.stopRuntimeMeasurement(requestHandle);
}
}
static HttpMethod extractMethod(final HttpServletRequest httpRequest) throws ODataLibraryException {
final HttpMethod httpRequestMethod;
try {
httpRequestMethod = HttpMethod.valueOf(httpRequest.getMethod());
} catch (IllegalArgumentException e) {
throw new ODataHandlerException("HTTP method not allowed" + httpRequest.getMethod(), e,
ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, httpRequest.getMethod());
}
try {
if (httpRequestMethod == HttpMethod.POST) {
String xHttpMethod = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD);
String xHttpMethodOverride = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD_OVERRIDE);
final HttpMethod httpRequestMethod;
try {
httpRequestMethod = HttpMethod.valueOf(httpRequest.getMethod());
} catch (IllegalArgumentException e) {
throw new ODataHandlerException("HTTP method not allowed" + httpRequest.getMethod(), e,
ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, httpRequest.getMethod());
}
try {
if (httpRequestMethod == HttpMethod.POST) {
String xHttpMethod = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD);
String xHttpMethodOverride = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD_OVERRIDE);
if (xHttpMethod == null && xHttpMethodOverride == null) {
return httpRequestMethod;
} else if (xHttpMethod == null) {
return HttpMethod.valueOf(xHttpMethodOverride);
} else if (xHttpMethodOverride == null) {
return HttpMethod.valueOf(xHttpMethod);
} else {
if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) {
throw new ODataHandlerException("Ambiguous X-HTTP-Methods",
ODataHandlerException.MessageKeys.AMBIGUOUS_XHTTP_METHOD, xHttpMethod, xHttpMethodOverride);
}
return HttpMethod.valueOf(xHttpMethod);
}
} else {
return httpRequestMethod;
}
} catch (IllegalArgumentException e) {
throw new ODataHandlerException("Invalid HTTP method" + httpRequest.getMethod(), e,
ODataHandlerException.MessageKeys.INVALID_HTTP_METHOD, httpRequest.getMethod());
}
}
static void fillUriInformation(final ODataRequest odRequest, final HttpServletRequest httpRequest, final int split) {
if (xHttpMethod == null && xHttpMethodOverride == null) {
return httpRequestMethod;
} else if (xHttpMethod == null) {
return HttpMethod.valueOf(xHttpMethodOverride);
} else if (xHttpMethodOverride == null) {
return HttpMethod.valueOf(xHttpMethod);
} else {
if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) {
throw new ODataHandlerException("Ambiguous X-HTTP-Methods",
ODataHandlerException.MessageKeys.AMBIGUOUS_XHTTP_METHOD, xHttpMethod, xHttpMethodOverride);
}
return HttpMethod.valueOf(xHttpMethod);
}
} else {
return httpRequestMethod;
}
} catch (IllegalArgumentException e) {
throw new ODataHandlerException("Invalid HTTP method" + httpRequest.getMethod(), e,
ODataHandlerException.MessageKeys.INVALID_HTTP_METHOD, httpRequest.getMethod());
}
}
static void fillUriInformation(final ODataRequest odRequest,
final HttpServletRequest httpRequest, final int split) {
String rawRequestUri = httpRequest.getRequestURL().toString();
String rawODataPath;
@ -274,10 +275,12 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
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();
int beginIndex = rawRequestUri.indexOf(httpRequest.getServletPath()) +
httpRequest.getServletPath().length();
rawODataPath = rawRequestUri.substring(beginIndex);
} else if (!"".equals(httpRequest.getContextPath())) {
int beginIndex = rawRequestUri.indexOf(httpRequest.getContextPath()) + httpRequest.getContextPath().length();
int beginIndex = rawRequestUri.indexOf(httpRequest.getContextPath()) +
httpRequest.getContextPath().length();
rawODataPath = rawRequestUri.substring(beginIndex);
} else {
rawODataPath = httpRequest.getRequestURI();
@ -310,13 +313,13 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
}
static void copyHeaders(ODataRequest odRequest, final HttpServletRequest req) {
for (final Enumeration<?> headerNames = req.getHeaderNames(); headerNames.hasMoreElements();) {
final String headerName = (String) headerNames.nextElement();
@SuppressWarnings("unchecked")
// getHeaders() says it returns an Enumeration of String.
final List<String> headerValues = Collections.list(req.getHeaders(headerName));
odRequest.addHeader(headerName, headerValues);
}
for (final Enumeration<?> headerNames = req.getHeaderNames(); headerNames.hasMoreElements();) {
final String headerName = (String) headerNames.nextElement();
@SuppressWarnings("unchecked")
// getHeaders() says it returns an Enumeration of String.
final List<String> headerValues = Collections.list(req.getHeaders(headerName));
odRequest.addHeader(headerName, headerValues);
}
}
@Override

View File

@ -62,7 +62,7 @@ public class ServerCoreDebugger {
}
}
}
public ODataResponse createDebugResponse(final ODataRequest request, final ODataResponse response,
final Exception exception, final UriInfo uriInfo, final Map<String, String> serverEnvironmentVariables) {
// Failsafe so we do not generate unauthorized debug messages