From ad177ac11e9cf43087167a9146fa0bcff0f6dc23 Mon Sep 17 00:00:00 2001 From: Christian Holzer Date: Tue, 11 Nov 2014 16:50:03 +0100 Subject: [PATCH] Api Refactoring Signed-off-by: Christian Amend --- .../server/api/batch/BatchOperation.java | 3 +- .../api/processor/DefaultProcessor.java | 2 +- .../batch/handler/BatchOperationImpl.java | 9 ++- .../core/batch/handler/BatchPartHandler.java | 59 +++++++++---------- .../server/core/batch/parser/BatchParser.java | 27 ++++----- .../core/batch/BatchRequestParserTest.java | 53 +++++++++-------- .../batch/handler/MockedBatchHandlerTest.java | 2 +- 7 files changed, 75 insertions(+), 80 deletions(-) diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/batch/BatchOperation.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/batch/BatchOperation.java index 2d09a9444..a9edef0de 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/batch/BatchOperation.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/batch/BatchOperation.java @@ -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 parseBatchRequest(InputStream in) throws BatchException; + public List parseBatchRequest(ODataRequest request, boolean isStrict) throws BatchException; public ODataResponse handleODataRequest(ODataRequest request, BatchRequestPart requestPart) throws BatchException; diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java index 25778e382..c4493a14d 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java @@ -107,7 +107,7 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce boolean continueOnError = shouldContinueOnError(request); try { - final List parts = operation.parseBatchRequest(request.getBody()); + final List parts = operation.parseBatchRequest(request, true); final List responseParts = new ArrayList(); for (BatchRequestPart part : parts) { diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchOperationImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchOperationImpl.java index cf7ad7cc4..b5a4eb49d 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchOperationImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchOperationImpl.java @@ -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 parseBatchRequest(InputStream in) throws BatchException { - return parser.parseBatchRequest(in); + public List parseBatchRequest(ODataRequest request, boolean isStrict) throws BatchException { + return parser.parseBatchRequest(request.getBody(), getContentType(request), request.getRawBaseUri(), + request.getRawServiceResolutionUri(), isStrict); } @Override diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchPartHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchPartHandler.java index 23ba1066e..2147a2e3f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchPartHandler.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/handler/BatchPartHandler.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -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; @@ -43,7 +41,7 @@ public class BatchPartHandler { private BatchProcessor batchProcessor; private BatchOperation batchOperation; private Map uriMapping = new HashMap(); - + public BatchPartHandler(final ODataHandler oDataHandler, final BatchProcessor processor, final BatchOperation batchOperation) { this.oDataHandler = oDataHandler; @@ -53,12 +51,12 @@ 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); - + // Store resource URI final String resourceUri = getODataPath(request, response); final String contentId = request.getHeader(BatchParserCommon.HTTP_CONTENT_ID); @@ -67,20 +65,20 @@ public class BatchPartHandler { } else { response = oDataHandler.process(request); } - + // 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); } - - return response; + + return response; } 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()); @@ -90,62 +88,61 @@ public class BatchPartHandler { // These methods still addresses a given resource, so we use the URI given by the request resourceUri = request.getRawODataPath(); } - + return resourceUri; } private UriMapping replaceReference(ODataRequest request, BatchRequestPart requestPart) { 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."); } } - + return mapping; } - + private UriMapping getUriMappingOrDefault(final BatchRequestPart requestPart) { UriMapping mapping = uriMapping.get(requestPart); - - if(mapping == null) { + + if (mapping == null) { mapping = new UriMapping(); } uriMapping.put(requestPart, mapping); - + return mapping; } - + public ODataResponsePart handleBatchRequest(BatchRequestPart request) throws BatchException { if (request.isChangeSet()) { return handleChangeSet(request); } else { - final List responses = new ArrayList(); - 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); } } private ODataResponsePart handleChangeSet(BatchRequestPart request) throws BatchException { final BatchChangeSetSorter sorter = new BatchChangeSetSorter(request.getRequests()); - + return batchProcessor.executeChangeSet(batchOperation, sorter.getOrderdRequests(), request); } private static class UriMapping { private Map uriMapping = new HashMap(); - + public void addMapping(final String contentId, final String uri) { uriMapping.put(contentId, uri); } - + public String getUri(final String contentId) { return uriMapping.get(contentId); } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/parser/BatchParser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/parser/BatchParser.java index 75c008477..3a89440a4 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/parser/BatchParser.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/batch/parser/BatchParser.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -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 parseBatchRequest(final InputStream in) throws BatchException { + public List 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) parse(in, new BatchRequestTransformator(baseUri, rawServiceResolutionUri)); } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/BatchRequestParserTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/BatchRequestParserTest.java index 88fdf0871..cc249be2a 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/BatchRequestParserTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/BatchRequestParserTest.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -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 batchRequestParts = parser.parseBatchRequest(StringUtil.toInputStream(batch)); + final BatchParser parser = new BatchParser(); + final List 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); @@ -224,15 +225,16 @@ public class BatchRequestParserTest { public void testContentTypeCharset() throws BatchException { final String contentType = "multipart/mixed; charset=UTF-8;boundary=batch_14d1-b293-b99a"; final String batch = "" - + "--batch_14d1-b293-b99a" + CRLF - + GET_REQUEST - + "--batch_14d1-b293-b99a--"; - final BatchParser parser = new BatchParser(contentType, SERVICE_ROOT, "", true); - final List parts = parser.parseBatchRequest(StringUtil.toInputStream(batch)); - + + "--batch_14d1-b293-b99a" + CRLF + + GET_REQUEST + + "--batch_14d1-b293-b99a--"; + final BatchParser parser = new BatchParser(); + final List parts = + parser.parseBatchRequest(StringUtil.toInputStream(batch), contentType, SERVICE_ROOT, "", true); + assertEquals(1, parts.size()); } - + @Test public void testBatchWithoutBoundaryParameter() throws UnsupportedEncodingException { final String invalidContentType = "multipart/mixed"; @@ -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); @@ -453,16 +455,16 @@ public class BatchRequestParserTest { parseInvalidBatchBody(batch, BatchException.MessageKeys.MISSING_CLOSE_DELIMITER); } - + @Test public void testEmptyRequest() throws BatchException, UnsupportedEncodingException { final String batch = "" + "--batch_8194-cf13-1f56--"; - + final List parts = parse(batch); assertEquals(0, parts.size()); } - + @Test public void testBadRequest() throws UnsupportedEncodingException { final String batch = "This is a bad request. There is no syntax and also no semantic"; @@ -523,7 +525,7 @@ public class BatchRequestParserTest { final List parts = parse(batch); assertEquals(1, parts.size()); - + final BatchRequestPart part = parts.get(0); assertTrue(part.isChangeSet()); assertEquals(0, part.getRequests().size()); @@ -1272,8 +1274,9 @@ public class BatchRequestParserTest { } private List parse(final InputStream in, final boolean isStrict) throws BatchException { - final BatchParser parser = new BatchParser(CONTENT_TYPE, SERVICE_ROOT, "", isStrict); - final List batchRequestParts = parser.parseBatchRequest(in); + final BatchParser parser = new BatchParser(); + final List 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); diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/handler/MockedBatchHandlerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/handler/MockedBatchHandlerTest.java index b81edb8bb..c68919c04 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/handler/MockedBatchHandlerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/batch/handler/MockedBatchHandlerTest.java @@ -593,7 +593,7 @@ public class MockedBatchHandlerTest { @Override public void executeBatch(BatchOperation operation, ODataRequest request, ODataResponse response) { try { - final List parts = operation.parseBatchRequest(request.getBody()); + final List parts = operation.parseBatchRequest(request, true); final List responseParts = new ArrayList(); for (BatchRequestPart part : parts) {