Api Refactoring

Signed-off-by: Christian Amend <chrisam@apache.org>
This commit is contained in:
Christian Holzer 2014-11-11 16:50:03 +01:00 committed by Christian Amend
parent bc46b5352e
commit ad177ac11e
7 changed files with 75 additions and 80 deletions

View File

@ -18,14 +18,13 @@
*/package org.apache.olingo.server.api.batch; */package org.apache.olingo.server.api.batch;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.List; import java.util.List;
import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.ODataResponse;
public interface BatchOperation { public interface BatchOperation {
public List<BatchRequestPart> parseBatchRequest(InputStream in) throws BatchException; public List<BatchRequestPart> parseBatchRequest(ODataRequest request, boolean isStrict) throws BatchException;
public ODataResponse handleODataRequest(ODataRequest request, BatchRequestPart requestPart) throws BatchException; public ODataResponse handleODataRequest(ODataRequest request, BatchRequestPart requestPart) throws BatchException;

View File

@ -107,7 +107,7 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce
boolean continueOnError = shouldContinueOnError(request); boolean continueOnError = shouldContinueOnError(request);
try { try {
final List<BatchRequestPart> parts = operation.parseBatchRequest(request.getBody()); final List<BatchRequestPart> parts = operation.parseBatchRequest(request, true);
final List<ODataResponsePart> responseParts = new ArrayList<ODataResponsePart>(); final List<ODataResponsePart> responseParts = new ArrayList<ODataResponsePart>();
for (BatchRequestPart part : parts) { for (BatchRequestPart part : parts) {

View File

@ -19,7 +19,6 @@
package org.apache.olingo.server.core.batch.handler; package org.apache.olingo.server.core.batch.handler;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.List; import java.util.List;
import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpHeader;
@ -43,13 +42,13 @@ public class BatchOperationImpl implements BatchOperation {
final boolean isStrict) { final boolean isStrict) {
partHandler = new BatchPartHandler(oDataHandler, batchProcessor, this); partHandler = new BatchPartHandler(oDataHandler, batchProcessor, this);
writer = new BatchResponseWriter(); writer = new BatchResponseWriter();
parser = new BatchParser(getContentType(request), request.getRawBaseUri(), parser = new BatchParser();
request.getRawServiceResolutionUri(), isStrict);
} }
@Override @Override
public List<BatchRequestPart> parseBatchRequest(InputStream in) throws BatchException { public List<BatchRequestPart> parseBatchRequest(ODataRequest request, boolean isStrict) throws BatchException {
return parser.parseBatchRequest(in); return parser.parseBatchRequest(request.getBody(), getContentType(request), request.getRawBaseUri(),
request.getRawServiceResolutionUri(), isStrict);
} }
@Override @Override

View File

@ -18,9 +18,7 @@
*/ */
package org.apache.olingo.server.core.batch.handler; package org.apache.olingo.server.core.batch.handler;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.olingo.commons.api.ODataRuntimeException; import org.apache.olingo.commons.api.ODataRuntimeException;
@ -54,7 +52,7 @@ public class BatchPartHandler {
public ODataResponse handleODataRequest(ODataRequest request, BatchRequestPart requestPart) throws BatchException { public ODataResponse handleODataRequest(ODataRequest request, BatchRequestPart requestPart) throws BatchException {
final ODataResponse response; final ODataResponse response;
if(requestPart.isChangeSet()) { if (requestPart.isChangeSet()) {
final UriMapping mapping = replaceReference(request, requestPart); final UriMapping mapping = replaceReference(request, requestPart);
response = oDataHandler.process(request); response = oDataHandler.process(request);
@ -70,17 +68,17 @@ public class BatchPartHandler {
// Add content id to response // Add content id to response
final String contentId = request.getHeader(BatchParserCommon.HTTP_CONTENT_ID); final String contentId = request.getHeader(BatchParserCommon.HTTP_CONTENT_ID);
if(contentId != null) { if (contentId != null) {
response.setHeader(BatchParserCommon.HTTP_CONTENT_ID, contentId); response.setHeader(BatchParserCommon.HTTP_CONTENT_ID, contentId);
} }
return response; return response;
} }
private String getODataPath(ODataRequest request, ODataResponse response) throws BatchException { private String getODataPath(ODataRequest request, ODataResponse response) throws BatchException {
String resourceUri = null; String resourceUri = null;
if(request.getMethod() == HttpMethod.POST) { if (request.getMethod() == HttpMethod.POST) {
// Create entity // Create entity
// The URI of the new resource will be generated by the server and published in the location header // The URI of the new resource will be generated by the server and published in the location header
ODataURI uri = new ODataURI(response.getHeaders().get(HttpHeader.LOCATION), request.getRawBaseUri()); ODataURI uri = new ODataURI(response.getHeaders().get(HttpHeader.LOCATION), request.getRawBaseUri());
@ -98,10 +96,10 @@ public class BatchPartHandler {
final UriMapping mapping = getUriMappingOrDefault(requestPart); final UriMapping mapping = getUriMappingOrDefault(requestPart);
final String reference = BatchChangeSetSorter.getReferenceInURI(request); final String reference = BatchChangeSetSorter.getReferenceInURI(request);
if(reference != null) { if (reference != null) {
final String replacement = mapping.getUri(reference); final String replacement = mapping.getUri(reference);
if(replacement != null) { if (replacement != null) {
BatchChangeSetSorter.replaceContentIdReference(request, reference, replacement); BatchChangeSetSorter.replaceContentIdReference(request, reference, replacement);
} else { } else {
throw new ODataRuntimeException("Required Content-Id for reference \"" + reference + "\" not found."); throw new ODataRuntimeException("Required Content-Id for reference \"" + reference + "\" not found.");
@ -114,7 +112,7 @@ public class BatchPartHandler {
private UriMapping getUriMappingOrDefault(final BatchRequestPart requestPart) { private UriMapping getUriMappingOrDefault(final BatchRequestPart requestPart) {
UriMapping mapping = uriMapping.get(requestPart); UriMapping mapping = uriMapping.get(requestPart);
if(mapping == null) { if (mapping == null) {
mapping = new UriMapping(); mapping = new UriMapping();
} }
uriMapping.put(requestPart, mapping); uriMapping.put(requestPart, mapping);
@ -126,10 +124,9 @@ public class BatchPartHandler {
if (request.isChangeSet()) { if (request.isChangeSet()) {
return handleChangeSet(request); return handleChangeSet(request);
} else { } else {
final List<ODataResponse> responses = new ArrayList<ODataResponse>(); final ODataResponse response = handleODataRequest(request.getRequests().get(0), request);
responses.add(handleODataRequest(request.getRequests().get(0), request));
return new ODataResponsePart(responses, false); return new ODataResponsePart(response, false);
} }
} }

View File

@ -33,21 +33,18 @@ import org.apache.olingo.server.core.batch.transformator.BatchRequestTransformat
public class BatchParser { public class BatchParser {
private final String contentTypeMime; private String contentTypeMime;
private final String baseUri; private String rawServiceResolutionUri;
private final String rawServiceResolutionUri; private boolean isStrict;
private final boolean isStrict;
public BatchParser(final String contentType, final String baseUri, final String serviceResolutionUri,
final boolean isStrict) {
contentTypeMime = contentType;
this.baseUri = BatchParserCommon.removeEndingSlash(baseUri);
this.isStrict = isStrict;
this.rawServiceResolutionUri = serviceResolutionUri;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<BatchRequestPart> parseBatchRequest(final InputStream in) throws BatchException { public List<BatchRequestPart> parseBatchRequest(final InputStream in, final String contentType, final String baseUri,
final String serviceResolutionUri, final boolean isStrict) throws BatchException {
contentTypeMime = contentType;
this.isStrict = isStrict;
this.rawServiceResolutionUri = serviceResolutionUri;
return (List<BatchRequestPart>) parse(in, new BatchRequestTransformator(baseUri, rawServiceResolutionUri)); return (List<BatchRequestPart>) parse(in, new BatchRequestTransformator(baseUri, rawServiceResolutionUri));
} }

View File

@ -196,8 +196,9 @@ public class BatchRequestParserTest {
+ "--batch_1.2+34:2j)0?" + CRLF + "--batch_1.2+34:2j)0?" + CRLF
+ GET_REQUEST + GET_REQUEST
+ "--batch_1.2+34:2j)0?--"; + "--batch_1.2+34:2j)0?--";
final BatchParser parser = new BatchParser(contentType, SERVICE_ROOT, "", true); final BatchParser parser = new BatchParser();
final List<BatchRequestPart> batchRequestParts = parser.parseBatchRequest(StringUtil.toInputStream(batch)); final List<BatchRequestPart> batchRequestParts =
parser.parseBatchRequest(StringUtil.toInputStream(batch), contentType, SERVICE_ROOT, "", true);
assertNotNull(batchRequestParts); assertNotNull(batchRequestParts);
assertFalse(batchRequestParts.isEmpty()); assertFalse(batchRequestParts.isEmpty());
@ -210,10 +211,10 @@ public class BatchRequestParserTest {
+ "--batch_1740-bb84-2f7f" + CRLF + "--batch_1740-bb84-2f7f" + CRLF
+ GET_REQUEST + GET_REQUEST
+ "--batch_1740-bb84-2f7f--"; + "--batch_1740-bb84-2f7f--";
final BatchParser parser = new BatchParser(invalidContentType, SERVICE_ROOT, "", true); final BatchParser parser = new BatchParser();
try { try {
parser.parseBatchRequest(StringUtil.toInputStream(batch)); parser.parseBatchRequest(StringUtil.toInputStream(batch), invalidContentType, SERVICE_ROOT, "", true);
fail(); fail();
} catch (BatchException e) { } catch (BatchException e) {
assertMessageKey(e, BatchException.MessageKeys.INVALID_CONTENT_TYPE); assertMessageKey(e, BatchException.MessageKeys.INVALID_CONTENT_TYPE);
@ -224,11 +225,12 @@ public class BatchRequestParserTest {
public void testContentTypeCharset() throws BatchException { public void testContentTypeCharset() throws BatchException {
final String contentType = "multipart/mixed; charset=UTF-8;boundary=batch_14d1-b293-b99a"; final String contentType = "multipart/mixed; charset=UTF-8;boundary=batch_14d1-b293-b99a";
final String batch = "" final String batch = ""
+ "--batch_14d1-b293-b99a" + CRLF + "--batch_14d1-b293-b99a" + CRLF
+ GET_REQUEST + GET_REQUEST
+ "--batch_14d1-b293-b99a--"; + "--batch_14d1-b293-b99a--";
final BatchParser parser = new BatchParser(contentType, SERVICE_ROOT, "", true); final BatchParser parser = new BatchParser();
final List<BatchRequestPart> parts = parser.parseBatchRequest(StringUtil.toInputStream(batch)); final List<BatchRequestPart> parts =
parser.parseBatchRequest(StringUtil.toInputStream(batch), contentType, SERVICE_ROOT, "", true);
assertEquals(1, parts.size()); assertEquals(1, parts.size());
} }
@ -240,10 +242,10 @@ public class BatchRequestParserTest {
+ "--batch_1740-bb84-2f7f" + CRLF + "--batch_1740-bb84-2f7f" + CRLF
+ GET_REQUEST + GET_REQUEST
+ "--batch_1740-bb84-2f7f--"; + "--batch_1740-bb84-2f7f--";
final BatchParser parser = new BatchParser(invalidContentType, SERVICE_ROOT, "", true); final BatchParser parser = new BatchParser();
try { try {
parser.parseBatchRequest(StringUtil.toInputStream(batch)); parser.parseBatchRequest(StringUtil.toInputStream(batch), invalidContentType, SERVICE_ROOT, "", true);
fail(); fail();
} catch (BatchException e) { } catch (BatchException e) {
assertMessageKey(e, BatchException.MessageKeys.INVALID_CONTENT_TYPE); assertMessageKey(e, BatchException.MessageKeys.INVALID_CONTENT_TYPE);
@ -257,10 +259,10 @@ public class BatchRequestParserTest {
+ "--batch_1740-bb:84-2f7f" + CRLF + "--batch_1740-bb:84-2f7f" + CRLF
+ GET_REQUEST + GET_REQUEST
+ "--batch_1740-bb:84-2f7f--"; + "--batch_1740-bb:84-2f7f--";
final BatchParser parser = new BatchParser(invalidContentType, SERVICE_ROOT, "", true); final BatchParser parser = new BatchParser();
try { try {
parser.parseBatchRequest(StringUtil.toInputStream(batch)); parser.parseBatchRequest(StringUtil.toInputStream(batch), invalidContentType, SERVICE_ROOT, "", true);
fail(); fail();
} catch (BatchException e) { } catch (BatchException e) {
assertMessageKey(e, BatchException.MessageKeys.INVALID_BOUNDARY); assertMessageKey(e, BatchException.MessageKeys.INVALID_BOUNDARY);
@ -1272,8 +1274,9 @@ public class BatchRequestParserTest {
} }
private List<BatchRequestPart> parse(final InputStream in, final boolean isStrict) throws BatchException { private List<BatchRequestPart> parse(final InputStream in, final boolean isStrict) throws BatchException {
final BatchParser parser = new BatchParser(CONTENT_TYPE, SERVICE_ROOT, "", isStrict); final BatchParser parser = new BatchParser();
final List<BatchRequestPart> batchRequestParts = parser.parseBatchRequest(in); final List<BatchRequestPart> batchRequestParts =
parser.parseBatchRequest(in, CONTENT_TYPE, SERVICE_ROOT, "", isStrict);
assertNotNull(batchRequestParts); assertNotNull(batchRequestParts);
@ -1295,10 +1298,10 @@ public class BatchRequestParserTest {
private void parseInvalidBatchBody(final String batch, final MessageKeys key, final boolean isStrict) private void parseInvalidBatchBody(final String batch, final MessageKeys key, final boolean isStrict)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
final BatchParser parser = new BatchParser(CONTENT_TYPE, SERVICE_ROOT, "", isStrict); final BatchParser parser = new BatchParser();
try { try {
parser.parseBatchRequest(StringUtil.toInputStream(batch)); parser.parseBatchRequest(StringUtil.toInputStream(batch), CONTENT_TYPE, SERVICE_ROOT, "", isStrict);
fail("No exception thrown. Expect: " + key.toString()); fail("No exception thrown. Expect: " + key.toString());
} catch (BatchException e) { } catch (BatchException e) {
assertMessageKey(e, key); assertMessageKey(e, key);

View File

@ -593,7 +593,7 @@ public class MockedBatchHandlerTest {
@Override @Override
public void executeBatch(BatchOperation operation, ODataRequest request, ODataResponse response) { public void executeBatch(BatchOperation operation, ODataRequest request, ODataResponse response) {
try { try {
final List<BatchRequestPart> parts = operation.parseBatchRequest(request.getBody()); final List<BatchRequestPart> parts = operation.parseBatchRequest(request, true);
final List<ODataResponsePart> responseParts = new ArrayList<ODataResponsePart>(); final List<ODataResponsePart> responseParts = new ArrayList<ODataResponsePart>();
for (BatchRequestPart part : parts) { for (BatchRequestPart part : parts) {