[OLINGO-903] Check for in uri parser error cases

This commit is contained in:
Michael Bolz 2016-03-17 21:59:48 +01:00
parent 9c61e22375
commit 49b8599432
2 changed files with 99 additions and 3 deletions

View File

@ -18,6 +18,7 @@
*/
package org.apache.olingo.server.core;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -45,11 +46,17 @@ import org.apache.olingo.server.api.serializer.CustomContentTypeSupport;
import org.apache.olingo.server.api.serializer.RepresentationType;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.queryoption.FormatOption;
import org.apache.olingo.server.api.uri.queryoption.QueryOption;
import org.apache.olingo.server.api.uri.queryoption.SystemQueryOption;
import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind;
import org.apache.olingo.server.core.debug.ServerCoreDebugger;
import org.apache.olingo.server.core.uri.parser.Parser;
import org.apache.olingo.server.core.uri.parser.UriDecoder;
import org.apache.olingo.server.core.uri.parser.UriParserException;
import org.apache.olingo.server.core.uri.parser.UriParserSemanticException;
import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException;
import org.apache.olingo.server.core.uri.queryoption.FormatOptionImpl;
import org.apache.olingo.server.core.uri.validator.UriValidationException;
import org.apache.olingo.server.core.uri.validator.UriValidator;
@ -174,9 +181,9 @@ public class ODataHandlerImpl implements ODataHandler {
}
ContentType requestedContentType;
try {
requestedContentType = ContentNegotiator.doContentNegotiation(
uriInfo == null ? null : uriInfo.getFormatOption(), request, getCustomContentTypeSupport(),
RepresentationType.ERROR);
final FormatOption formatOption = getFormatOption(request, uriInfo);
requestedContentType = ContentNegotiator.doContentNegotiation(formatOption, request,
getCustomContentTypeSupport(), RepresentationType.ERROR);
} catch (final ContentNegotiatorException e) {
requestedContentType = ContentType.JSON;
}
@ -186,6 +193,34 @@ public class ODataHandlerImpl implements ODataHandler {
debugger.stopRuntimeMeasurement(measurementHandle);
}
/**
* Extract format option from either <code>uriInfo</code> (if not <code>NULL</code>)
* or query from <code>request</code> (if not <code>NULL</code>).
* If both options are <code>NULL</code>, <code>NULL</code> is returned.
*
* @param request request which is checked
* @param uriInfo uriInfo which is checked
* @return the evaluated format option or <code>NULL</code>.
*/
private FormatOption getFormatOption(final ODataRequest request, final UriInfo uriInfo) {
if(uriInfo == null) {
String query = request.getRawQueryPath();
if(query == null) {
return null;
}
final String formatOption = SystemQueryOptionKind.FORMAT.toString();
int index = query.indexOf(formatOption);
int endIndex = query.indexOf("&", index);
if(endIndex == -1) {
endIndex = query.length();
}
final String format = query.substring(index + formatOption.length(), endIndex);
return new FormatOptionImpl().setFormat(format);
}
return uriInfo.getFormatOption();
}
private void validateODataVersion(final ODataRequest request) throws ODataHandlerException {
final String maxVersion = request.getHeader(HttpHeader.ODATA_MAX_VERSION);
if (maxVersion != null && ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersion)) {

View File

@ -38,6 +38,7 @@ import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.edm.provider.CsdlAbstractEdmProvider;
import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet;
import org.apache.olingo.commons.api.ex.ODataException;
import org.apache.olingo.commons.api.format.AcceptType;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpMethod;
@ -207,6 +208,66 @@ public class ODataHandlerImplTest {
assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), response.getStatusCode());
}
@Test
public void uriParserExceptionWithFormatQueryJson() throws Exception {
final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json", "", "", null);
assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
assertEquals("application/json;odata.metadata=minimal",
response.getHeader(HttpHeader.CONTENT_TYPE));
}
@Test
public void uriParserExceptionWithFormatQueryJsonAndMore() throws Exception {
final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json&$top=3", "", "", null);
assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
assertEquals("application/json;odata.metadata=minimal",
response.getHeader(HttpHeader.CONTENT_TYPE));
}
@Test
public void uriParserExceptionWithFormatJsonAcceptAtom() throws Exception {
final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json",
HttpHeader.ACCEPT, ContentType.APPLICATION_ATOM_XML.toContentTypeString(), null);
assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
assertEquals("application/json;odata.metadata=minimal",
response.getHeader(HttpHeader.CONTENT_TYPE));
}
@Test
public void uriParserExceptionWithFormatQueryAtom() throws Exception {
final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=atom", "", "", null);
assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
assertEquals("application/json;odata.metadata=minimal",
response.getHeader(HttpHeader.CONTENT_TYPE));
}
@Test
public void uriParserExceptionWithFormatQueryAtomAndTop() throws Exception {
final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=atom&$top=19", "", "", null);
assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
assertEquals("application/json;odata.metadata=minimal",
response.getHeader(HttpHeader.CONTENT_TYPE));
}
@Test
public void uriParserExceptionWithFormatAtomAcceptJson() throws Exception {
final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=atom",
HttpHeader.ACCEPT, ContentType.APPLICATION_JSON.toContentTypeString(), null);
assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
assertEquals("application/json;odata.metadata=minimal",
response.getHeader(HttpHeader.CONTENT_TYPE));
}
@Test
public void uriParserExceptionWithFormatQueryInvali() throws Exception {
final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=somenotvalid", "", "", null);
assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), response.getStatusCode());
assertEquals("application/json;odata.metadata=minimal",
response.getHeader(HttpHeader.CONTENT_TYPE));
}
@Test
public void applicationExceptionInProcessor() throws Exception {
MetadataProcessor processor = mock(MetadataProcessor.class);