[OLINGO-669] Fix: Dispatching of reference delete requests to collection-valued navigation properties

This commit is contained in:
Christian Holzer 2015-05-29 12:41:16 +02:00
parent f182fd20c1
commit 93031e4889
2 changed files with 45 additions and 38 deletions

View File

@ -273,38 +273,40 @@ public class ODataDispatcher {
throws ODataHandlerException, ContentNegotiatorException, ODataApplicationException,
SerializerException, DeserializerException {
final HttpMethod method = request.getMethod();
if (((UriResourcePartTyped) uriInfo.getUriResourceParts().get(lastPathSegmentIndex - 1)).isCollection()) {
if (method == HttpMethod.GET) {
final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(),
request, handler.getCustomContentTypeSupport(), RepresentationType.COLLECTION_REFERENCE);
handler.selectProcessor(ReferenceCollectionProcessor.class)
.readReferenceCollection(request, response, uriInfo, responseFormat);
} else if (method == HttpMethod.POST) {
final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE));
checkContentTypeSupport(requestFormat, RepresentationType.REFERENCE);
handler.selectProcessor(ReferenceProcessor.class)
.createReference(request, response, uriInfo, requestFormat);
} else {
throw new ODataHandlerException("HTTP method " + method + " is not allowed.",
ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString());
}
final boolean isCollection = ((UriResourcePartTyped) uriInfo.getUriResourceParts()
.get(lastPathSegmentIndex - 1))
.isCollection();
if(isCollection && method == HttpMethod.GET) {
final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(),
request, handler.getCustomContentTypeSupport(), RepresentationType.COLLECTION_REFERENCE);
handler.selectProcessor(ReferenceCollectionProcessor.class)
.readReferenceCollection(request, response, uriInfo, responseFormat);
} else if(isCollection && method == HttpMethod.POST ) {
final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE));
checkContentTypeSupport(requestFormat, RepresentationType.REFERENCE);
handler.selectProcessor(ReferenceProcessor.class)
.createReference(request, response, uriInfo, requestFormat);
} else if(!isCollection && method == HttpMethod.GET) {
final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(),
request, handler.getCustomContentTypeSupport(), RepresentationType.REFERENCE);
handler.selectProcessor(ReferenceProcessor.class).readReference(request, response, uriInfo, responseFormat);
} else if(!isCollection && (method == HttpMethod.PUT || method == HttpMethod.PATCH)) {
final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE));
checkContentTypeSupport(requestFormat, RepresentationType.REFERENCE);
handler.selectProcessor(ReferenceProcessor.class)
.updateReference(request, response, uriInfo, requestFormat);
} else if(method == HttpMethod.DELETE) {
handler.selectProcessor(ReferenceProcessor.class)
.deleteReference(request, response, uriInfo);
} else {
if (method == HttpMethod.GET) {
final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(),
request, handler.getCustomContentTypeSupport(), RepresentationType.REFERENCE);
handler.selectProcessor(ReferenceProcessor.class).readReference(request, response, uriInfo, responseFormat);
} else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) {
final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE));
checkContentTypeSupport(requestFormat, RepresentationType.REFERENCE);
handler.selectProcessor(ReferenceProcessor.class)
.updateReference(request, response, uriInfo, requestFormat);
} else if (method == HttpMethod.DELETE) {
handler.selectProcessor(ReferenceProcessor.class)
.deleteReference(request, response, uriInfo);
} else {
throw new ODataHandlerException("HTTP method " + method + " is not allowed.",
ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString());
}
throw new ODataHandlerException("HTTP method " + method + " is not allowed.",
ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString());
}
}

View File

@ -29,6 +29,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@ -649,6 +650,7 @@ public class ODataHandlerTest {
@Test
public void dispatchReference() throws Exception {
final String uri = "ESAllPrim(0)/NavPropertyETTwoPrimOne/$ref";
final String uriDeleteMany = "ESAllPrim(0)/NavPropertyETTwoPrimMany/$ref";
final ReferenceProcessor processor = mock(ReferenceProcessor.class);
dispatch(HttpMethod.GET, uri, processor);
@ -663,28 +665,31 @@ public class ODataHandlerTest {
verify(processor, times(2)).updateReference(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class),
any(ContentType.class));
dispatch(HttpMethod.DELETE, uri, processor);
verify(processor).deleteReference(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class));
dispatch(HttpMethod.POST, uri.replace("One", "Many"), processor);
verify(processor).createReference(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class),
any(ContentType.class));
dispatch(HttpMethod.DELETE, uriDeleteMany, "$id=ESTwoPrim(1)", null, Arrays.asList(new Processor[] { processor }));
verify(processor).deleteReference(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class));
dispatchMethodNotAllowed(HttpMethod.POST, uri, processor);
}
@Test
public void dispatchReferenceCollection() throws Exception {
final String uri = "ESAllPrim(0)/NavPropertyETTwoPrimMany/$ref";
final ReferenceCollectionProcessor processor = mock(ReferenceCollectionProcessor.class);
final ReferenceProcessor singleProcessor = mock(ReferenceProcessor.class);
dispatch(HttpMethod.GET, uri, processor);
verify(processor).readReferenceCollection(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class),
any(ContentType.class));
dispatch(HttpMethod.DELETE, uri, singleProcessor);
verify(singleProcessor).deleteReference(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class));
dispatchMethodNotAllowed(HttpMethod.PATCH, uri, processor);
dispatchMethodNotAllowed(HttpMethod.PUT, uri, processor);
dispatchMethodNotAllowed(HttpMethod.DELETE, uri, processor);
}
@Test