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;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse;
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;

View File

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

View File

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

View File

@ -18,9 +18,7 @@
*/
package org.apache.olingo.server.core.batch.handler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.olingo.commons.api.ODataRuntimeException;
@ -54,7 +52,7 @@ public class BatchPartHandler {
public ODataResponse handleODataRequest(ODataRequest request, BatchRequestPart requestPart) throws BatchException {
final ODataResponse response;
if(requestPart.isChangeSet()) {
if (requestPart.isChangeSet()) {
final UriMapping mapping = replaceReference(request, requestPart);
response = oDataHandler.process(request);
@ -70,7 +68,7 @@ public class BatchPartHandler {
// Add content id to response
final String contentId = request.getHeader(BatchParserCommon.HTTP_CONTENT_ID);
if(contentId != null) {
if (contentId != null) {
response.setHeader(BatchParserCommon.HTTP_CONTENT_ID, contentId);
}
@ -80,7 +78,7 @@ public class BatchPartHandler {
private String getODataPath(ODataRequest request, ODataResponse response) throws BatchException {
String resourceUri = null;
if(request.getMethod() == HttpMethod.POST) {
if (request.getMethod() == HttpMethod.POST) {
// Create entity
// 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());
@ -98,10 +96,10 @@ public class BatchPartHandler {
final UriMapping mapping = getUriMappingOrDefault(requestPart);
final String reference = BatchChangeSetSorter.getReferenceInURI(request);
if(reference != null) {
if (reference != null) {
final String replacement = mapping.getUri(reference);
if(replacement != null) {
if (replacement != null) {
BatchChangeSetSorter.replaceContentIdReference(request, reference, replacement);
} else {
throw new ODataRuntimeException("Required Content-Id for reference \"" + reference + "\" not found.");
@ -114,7 +112,7 @@ public class BatchPartHandler {
private UriMapping getUriMappingOrDefault(final BatchRequestPart requestPart) {
UriMapping mapping = uriMapping.get(requestPart);
if(mapping == null) {
if (mapping == null) {
mapping = new UriMapping();
}
uriMapping.put(requestPart, mapping);
@ -126,10 +124,9 @@ public class BatchPartHandler {
if (request.isChangeSet()) {
return handleChangeSet(request);
} else {
final List<ODataResponse> responses = new ArrayList<ODataResponse>();
responses.add(handleODataRequest(request.getRequests().get(0), request));
final ODataResponse response = 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 {
private final String contentTypeMime;
private final String baseUri;
private final String rawServiceResolutionUri;
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;
}
private String contentTypeMime;
private String rawServiceResolutionUri;
private boolean isStrict;
@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));
}

View File

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

View File

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