You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by sk...@apache.org on 2013/08/28 16:02:15 UTC
[27/41] delete aggregated-sources directory which was created by
accident
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/fece4008/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
----------------------------------------------------------------------
diff --git a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestParser.java b/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
deleted file mode 100644
index e37d572..0000000
--- a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
+++ /dev/null
@@ -1,573 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * 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
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
-package org.apache.olingo.odata2.core.batch;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Scanner;
-import java.util.Set;
-import java.util.regex.MatchResult;
-import java.util.regex.Pattern;
-
-import org.apache.olingo.odata2.api.batch.BatchException;
-import org.apache.olingo.odata2.api.batch.BatchRequestPart;
-import org.apache.olingo.odata2.api.commons.HttpContentType;
-import org.apache.olingo.odata2.api.commons.HttpHeaders;
-import org.apache.olingo.odata2.api.commons.ODataHttpMethod;
-import org.apache.olingo.odata2.api.ep.EntityProviderBatchProperties;
-import org.apache.olingo.odata2.api.processor.ODataRequest;
-import org.apache.olingo.odata2.api.processor.ODataRequest.ODataRequestBuilder;
-import org.apache.olingo.odata2.api.uri.PathInfo;
-import org.apache.olingo.odata2.api.uri.PathSegment;
-import org.apache.olingo.odata2.core.ODataPathSegmentImpl;
-import org.apache.olingo.odata2.core.PathInfoImpl;
-import org.apache.olingo.odata2.core.commons.Decoder;
-import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
-
-/**
- *
- */
-public class BatchRequestParser {
- private static final String LF = "\n";
- private static final String REG_EX_OPTIONAL_WHITESPACE = "\\s?";
- private static final String REG_EX_ZERO_OR_MORE_WHITESPACES = "\\s*";
- private static final String ANY_CHARACTERS = ".*";
-
- private static final Pattern REG_EX_BLANK_LINE = Pattern.compile("(|" + REG_EX_ZERO_OR_MORE_WHITESPACES + ")");
- private static final Pattern REG_EX_HEADER = Pattern.compile("([a-zA-Z\\-]+):" + REG_EX_OPTIONAL_WHITESPACE + "(.*)" + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_VERSION = Pattern.compile("(?:HTTP/[0-9]\\.[0-9])");
- private static final Pattern REG_EX_ANY_BOUNDARY_STRING = Pattern.compile("--" + ANY_CHARACTERS + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_REQUEST_LINE = Pattern.compile("(GET|POST|PUT|DELETE|MERGE|PATCH)\\s(.*)\\s?" + REG_EX_VERSION + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_BOUNDARY_PARAMETER = Pattern.compile(REG_EX_OPTIONAL_WHITESPACE + "boundary=(\".*\"|.*)" + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_CONTENT_TYPE = Pattern.compile(REG_EX_OPTIONAL_WHITESPACE + HttpContentType.MULTIPART_MIXED);
- private static final Pattern REG_EX_QUERY_PARAMETER = Pattern.compile("((?:\\$|)[^=]+)=([^=]+)");
-
- private static final String REG_EX_BOUNDARY = "([a-zA-Z0-9_\\-\\.'\\+]{1,70})|\"([a-zA-Z0-9_\\-\\.'\\+\\s\\(\\),/:=\\?]{1,69}[a-zA-Z0-9_\\-\\.'\\+\\(\\),/:=\\?])\""; // See RFC 2046
- private String baseUri;
- private PathInfo batchRequestPathInfo;
- private String contentTypeMime;
- private String boundary;
- private String currentMimeHeaderContentId;
- private int currentLineNumber = 0;
- private final static Set<String> HTTP_CHANGESET_METHODS;
- private final static Set<String> HTTP_BATCH_METHODS;
-
- static {
- HashSet<String> httpChangesetMethods = new HashSet<String>();
- httpChangesetMethods.add("POST");
- httpChangesetMethods.add("PUT");
- httpChangesetMethods.add("DELETE");
- httpChangesetMethods.add("MERGE");
- httpChangesetMethods.add("PATCH");
- HTTP_CHANGESET_METHODS = Collections.unmodifiableSet(httpChangesetMethods);
-
- HashSet<String> httpBatchMethods = new HashSet<String>();
- httpBatchMethods.add("GET");
- HTTP_BATCH_METHODS = Collections.unmodifiableSet(httpBatchMethods);
- }
-
- public BatchRequestParser(final String contentType, final EntityProviderBatchProperties properties) {
- contentTypeMime = contentType;
- batchRequestPathInfo = properties.getPathInfo();
- }
-
- public List<BatchRequestPart> parse(final InputStream in) throws BatchException {
- Scanner scanner = new Scanner(in, BatchHelper.DEFAULT_ENCODING).useDelimiter(LF);
- baseUri = getBaseUri();
- List<BatchRequestPart> requestList;
- try {
- requestList = parseBatchRequest(scanner);
- } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
- scanner.close();
- try {
- in.close();
- } catch (IOException e) {
- throw new ODataRuntimeException(e);
- }
- }
- return requestList;
- }
-
- private List<BatchRequestPart> parseBatchRequest(final Scanner scanner) throws BatchException {
- List<BatchRequestPart> requests = new LinkedList<BatchRequestPart>();
- if (contentTypeMime != null) {
- boundary = getBoundary(contentTypeMime);
- parsePreamble(scanner);
- final String closeDelimiter = "--" + boundary + "--" + REG_EX_ZERO_OR_MORE_WHITESPACES;
- while (scanner.hasNext() && !scanner.hasNext(closeDelimiter)) {
- requests.add(parseMultipart(scanner, boundary, false));
- parseNewLine(scanner);
- }
- if (scanner.hasNext(closeDelimiter)) {
- scanner.next(closeDelimiter);
- currentLineNumber++;
- } else {
- throw new BatchException(BatchException.MISSING_CLOSE_DELIMITER.addContent(currentLineNumber));
- }
- } else {
- throw new BatchException(BatchException.MISSING_CONTENT_TYPE);
- }
- return requests;
- }
-
- //The method parses additional information prior to the first boundary delimiter line
- private void parsePreamble(final Scanner scanner) {
- while (scanner.hasNext() && !scanner.hasNext(REG_EX_ANY_BOUNDARY_STRING)) {
- scanner.next();
- currentLineNumber++;
- }
- }
-
- private BatchRequestPart parseMultipart(final Scanner scanner, final String boundary, final boolean isChangeSet) throws BatchException {
- Map<String, String> mimeHeaders = new HashMap<String, String>();
- BatchRequestPart multipart = null;
- List<ODataRequest> requests = new ArrayList<ODataRequest>();
- if (scanner.hasNext("--" + boundary + REG_EX_ZERO_OR_MORE_WHITESPACES)) {
- scanner.next();
- currentLineNumber++;
- mimeHeaders = parseHeaders(scanner);
- currentMimeHeaderContentId = mimeHeaders.get(BatchHelper.HTTP_CONTENT_ID.toLowerCase(Locale.ENGLISH));
-
- String contentType = mimeHeaders.get(HttpHeaders.CONTENT_TYPE.toLowerCase(Locale.ENGLISH));
- if (contentType == null) {
- throw new BatchException(BatchException.MISSING_CONTENT_TYPE);
- }
- if (isChangeSet) {
- if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
- validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
- parseNewLine(scanner);// mandatory
-
- requests.add(parseRequest(scanner, isChangeSet));
- multipart = new BatchRequestPartImpl(false, requests);
- } else {
- throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.APPLICATION_HTTP));
- }
- } else {
- if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
- validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
- parseNewLine(scanner);// mandatory
- requests.add(parseRequest(scanner, isChangeSet));
- multipart = new BatchRequestPartImpl(false, requests);
- } else if (contentType.matches(REG_EX_OPTIONAL_WHITESPACE + HttpContentType.MULTIPART_MIXED + ANY_CHARACTERS)) {
- String changeSetBoundary = getBoundary(contentType);
- if (boundary.equals(changeSetBoundary)) {
- throw new BatchException(BatchException.INVALID_CHANGESET_BOUNDARY.addContent(currentLineNumber));
- }
- List<ODataRequest> changeSetRequests = new LinkedList<ODataRequest>();
- parseNewLine(scanner);// mandatory
- Pattern changeSetCloseDelimiter = Pattern.compile("--" + changeSetBoundary + "--" + REG_EX_ZERO_OR_MORE_WHITESPACES);
- while (!scanner.hasNext(changeSetCloseDelimiter)) {
- BatchRequestPart part = parseMultipart(scanner, changeSetBoundary, true);
- changeSetRequests.addAll(part.getRequests());
- }
- scanner.next(changeSetCloseDelimiter);
- currentLineNumber++;
- multipart = new BatchRequestPartImpl(true, changeSetRequests);
- } else {
- throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED + " or " + HttpContentType.APPLICATION_HTTP));
- }
- }
- } else if (scanner.hasNext(boundary + REG_EX_ZERO_OR_MORE_WHITESPACES)) {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_BOUNDARY_DELIMITER.addContent(currentLineNumber));
- } else if (scanner.hasNext(REG_EX_ANY_BOUNDARY_STRING)) {
- currentLineNumber++;
- throw new BatchException(BatchException.NO_MATCH_WITH_BOUNDARY_STRING.addContent(boundary).addContent(currentLineNumber));
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.MISSING_BOUNDARY_DELIMITER.addContent(currentLineNumber));
- }
- return multipart;
-
- }
-
- private ODataRequest parseRequest(final Scanner scanner, final boolean isChangeSet) throws BatchException {
- if (scanner.hasNext(REG_EX_REQUEST_LINE)) {
- scanner.next(REG_EX_REQUEST_LINE);
- currentLineNumber++;
- final String method;
- final String uri;
- MatchResult result = scanner.match();
- if (result.groupCount() == 2) {
- method = result.group(1);
- uri = result.group(2).trim();
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_REQUEST_LINE.addContent(scanner.next()).addContent(currentLineNumber));
- }
- PathInfo pathInfo = parseRequestUri(uri);
- Map<String, String> queryParameters = parseQueryParameters(uri);
- if (isChangeSet) {
- if (!HTTP_CHANGESET_METHODS.contains(method)) {
- throw new BatchException(BatchException.INVALID_CHANGESET_METHOD.addContent(currentLineNumber));
- }
- } else if (!HTTP_BATCH_METHODS.contains(method)) {
- throw new BatchException(BatchException.INVALID_QUERY_OPERATION_METHOD.addContent(currentLineNumber));
- }
- ODataHttpMethod httpMethod = ODataHttpMethod.valueOf(method);
- Map<String, List<String>> headers = parseRequestHeaders(scanner);
- if (currentMimeHeaderContentId != null) {
- List<String> headerList = new ArrayList<String>();
- headerList.add(currentMimeHeaderContentId);
- headers.put(BatchHelper.MIME_HEADER_CONTENT_ID.toLowerCase(Locale.ENGLISH), headerList);
- }
-
- String contentType = getContentTypeHeader(headers);
- List<String> acceptHeaders = getAcceptHeader(headers);
- List<Locale> acceptLanguages = getAcceptLanguageHeader(headers);
- parseNewLine(scanner);
- InputStream body = new ByteArrayInputStream(new byte[0]);
- if (isChangeSet) {
- body = parseBody(scanner);
- }
-
- ODataRequestBuilder requestBuilder = ODataRequest.method(httpMethod)
- .queryParameters(queryParameters)
- .requestHeaders(headers)
- .pathInfo(pathInfo)
- .acceptableLanguages(acceptLanguages)
- .body(body)
- .acceptHeaders(acceptHeaders);
-
- if (contentType != null) {
- requestBuilder = requestBuilder.contentType(contentType);
- }
- return requestBuilder.build();
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_REQUEST_LINE.addContent(scanner.next()).addContent(currentLineNumber));
- }
-
- }
-
- private Map<String, List<String>> parseRequestHeaders(final Scanner scanner) throws BatchException {
- Map<String, List<String>> headers = new HashMap<String, List<String>>();
- while (scanner.hasNext() && !scanner.hasNext(REG_EX_BLANK_LINE)) {
- if (scanner.hasNext(REG_EX_HEADER)) {
- scanner.next(REG_EX_HEADER);
- currentLineNumber++;
- MatchResult result = scanner.match();
- if (result.groupCount() == 2) {
- String headerName = result.group(1).trim().toLowerCase(Locale.ENGLISH);
- String headerValue = result.group(2).trim();
- if (HttpHeaders.ACCEPT.equalsIgnoreCase(headerName)) {
- List<String> acceptHeaders = parseAcceptHeaders(headerValue);
- headers.put(headerName, acceptHeaders);
- } else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(headerName)) {
- List<String> acceptLanguageHeaders = parseAcceptableLanguages(headerValue);
- headers.put(headerName, acceptLanguageHeaders);
- } else if (!BatchHelper.HTTP_CONTENT_ID.equalsIgnoreCase(headerName)) {
- if (headers.containsKey(headerName)) {
- headers.get(headerName).add(headerValue);
- } else {
- List<String> headerList = new ArrayList<String>();
- headerList.add(headerValue);
- headers.put(headerName, headerList);
- }
- } else {
- List<String> headerList = new ArrayList<String>();
- headerList.add(headerValue);
- headers.put(BatchHelper.REQUEST_HEADER_CONTENT_ID.toLowerCase(Locale.ENGLISH), headerList);
- }
- }
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_HEADER.addContent(scanner.next()).addContent(currentLineNumber));
- }
- }
- return headers;
- }
-
- private PathInfo parseRequestUri(final String uri) throws BatchException {
- PathInfoImpl pathInfo = new PathInfoImpl();
- pathInfo.setServiceRoot(batchRequestPathInfo.getServiceRoot());
- pathInfo.setPrecedingPathSegment(batchRequestPathInfo.getPrecedingSegments());
- final String odataPathSegmentsAsString;
- final String queryParametersAsString;
- try {
- Scanner uriScanner = new Scanner(uri).useDelimiter(LF);
- URI uriObject = new URI(uri);
- if (uriObject.isAbsolute()) {
- Pattern regexRequestUri = Pattern.compile(baseUri + "/([^/][^?]*)(\\?.*)?");
- if (uriScanner.hasNext(regexRequestUri)) {
- uriScanner.next(regexRequestUri);
- MatchResult result = uriScanner.match();
- if (result.groupCount() == 2) {
- odataPathSegmentsAsString = result.group(1);
- queryParametersAsString = result.group(2) != null ? result.group(2) : "";
- } else {
- uriScanner.close();
- throw new BatchException(BatchException.INVALID_URI.addContent(currentLineNumber));
- }
- } else {
- uriScanner.close();
- throw new BatchException(BatchException.INVALID_URI.addContent(currentLineNumber));
- }
- } else {
- Pattern regexRequestUri = Pattern.compile("([^/][^?]*)(\\?.*)?");
- if (uriScanner.hasNext(regexRequestUri)) {
- uriScanner.next(regexRequestUri);
- MatchResult result = uriScanner.match();
- if (result.groupCount() == 2) {
- odataPathSegmentsAsString = result.group(1);
- queryParametersAsString = result.group(2) != null ? result.group(2) : "";
- } else {
- uriScanner.close();
- throw new BatchException(BatchException.INVALID_URI.addContent(currentLineNumber));
- }
- } else if (uriScanner.hasNext("/(.*)")) {
- uriScanner.close();
- throw new BatchException(BatchException.UNSUPPORTED_ABSOLUTE_PATH.addContent(currentLineNumber));
- } else {
- uriScanner.close();
- throw new BatchException(BatchException.INVALID_URI.addContent(currentLineNumber));
- }
-
- }
- uriScanner.close();
- pathInfo.setODataPathSegment(parseODataPathSegments(odataPathSegmentsAsString));
- if (!odataPathSegmentsAsString.startsWith("$")) {
- String requestUri = baseUri + "/" + odataPathSegmentsAsString + queryParametersAsString;
- pathInfo.setRequestUri(new URI(requestUri));
- }
- return pathInfo;
- } catch (URISyntaxException e) {
- throw new BatchException(BatchException.INVALID_URI.addContent(currentLineNumber), e);
- }
-
- }
-
- private Map<String, String> parseQueryParameters(final String uri) throws BatchException {
- Scanner uriScanner = new Scanner(uri).useDelimiter("\n");
- Map<String, String> queryParametersMap = new HashMap<String, String>();
- Pattern regex = Pattern.compile("(?:" + baseUri + "/)?" + "[^?]+" + "\\?(.*)");
- if (uriScanner.hasNext(regex)) {
- uriScanner.next(regex);
- MatchResult uriResult = uriScanner.match();
- if (uriResult.groupCount() == 1) {
- String queryParams = uriResult.group(1);
- Scanner queryParamsScanner = new Scanner(queryParams).useDelimiter("&");
- while (queryParamsScanner.hasNext(REG_EX_QUERY_PARAMETER)) {
- queryParamsScanner.next(REG_EX_QUERY_PARAMETER);
- MatchResult result = queryParamsScanner.match();
- if (result.groupCount() == 2) {
- String systemQueryOption = result.group(1);
- String value = result.group(2);
- queryParametersMap.put(systemQueryOption, Decoder.decode(value));
- } else {
- queryParamsScanner.close();
- throw new BatchException(BatchException.INVALID_QUERY_PARAMETER);
- }
- }
- queryParamsScanner.close();
-
- } else {
- uriScanner.close();
- throw new BatchException(BatchException.INVALID_URI.addContent(currentLineNumber));
- }
- }
- uriScanner.close();
- return queryParametersMap;
- }
-
- private List<PathSegment> parseODataPathSegments(final String odataPathSegmentsAsString) {
- Scanner pathSegmentScanner = new Scanner(odataPathSegmentsAsString).useDelimiter("/");
- List<PathSegment> odataPathSegments = new ArrayList<PathSegment>();
- while (pathSegmentScanner.hasNext()) {
- odataPathSegments.add(new ODataPathSegmentImpl(pathSegmentScanner.next(), null));
- }
- pathSegmentScanner.close();
- return odataPathSegments;
- }
-
- private List<String> parseAcceptHeaders(final String headerValue) throws BatchException {
- return AcceptParser.parseAcceptHeaders(headerValue);
- }
-
- private List<String> parseAcceptableLanguages(final String headerValue) throws BatchException {
- return AcceptParser.parseAcceptableLanguages(headerValue);
- }
-
- private InputStream parseBody(final Scanner scanner) {
- StringBuilder body = null;
- final InputStream requestBody;
-
- while (scanner.hasNext() && !scanner.hasNext(REG_EX_ANY_BOUNDARY_STRING)) {
- if (!scanner.hasNext(REG_EX_ZERO_OR_MORE_WHITESPACES)) {
- if (body == null) {
- body = new StringBuilder(scanner.next());
- } else {
- body.append(LF).append(scanner.next());
- }
- } else {
- scanner.next();
- }
- currentLineNumber++;
- }
-
- if (body != null) {
- requestBody = new ByteArrayInputStream(BatchHelper.getBytes(body.toString()));
- } else {
- requestBody = new ByteArrayInputStream(new byte[0]);
- }
- return requestBody;
- }
-
- private String getBoundary(final String contentType) throws BatchException {
- Scanner contentTypeScanner = new Scanner(contentType).useDelimiter(";\\s?");
- if (contentTypeScanner.hasNext(REG_EX_CONTENT_TYPE)) {
- contentTypeScanner.next(REG_EX_CONTENT_TYPE);
- } else {
- contentTypeScanner.close();
- throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED));
- }
- if (contentTypeScanner.hasNext(REG_EX_BOUNDARY_PARAMETER)) {
- contentTypeScanner.next(REG_EX_BOUNDARY_PARAMETER);
- MatchResult result = contentTypeScanner.match();
- contentTypeScanner.close();
- if (result.groupCount() == 1 && result.group(1).trim().matches(REG_EX_BOUNDARY)) {
- return trimQuota(result.group(1).trim());
- } else {
- throw new BatchException(BatchException.INVALID_BOUNDARY);
- }
- } else {
- contentTypeScanner.close();
- throw new BatchException(BatchException.MISSING_PARAMETER_IN_CONTENT_TYPE);
- }
- }
-
- private void validateEncoding(final String encoding) throws BatchException {
- if (!BatchHelper.BINARY_ENCODING.equalsIgnoreCase(encoding)) {
- throw new BatchException(BatchException.INVALID_CONTENT_TRANSFER_ENCODING);
- }
- }
-
- private Map<String, String> parseHeaders(final Scanner scanner) throws BatchException {
- Map<String, String> headers = new HashMap<String, String>();
- while (scanner.hasNext() && !(scanner.hasNext(REG_EX_BLANK_LINE))) {
- if (scanner.hasNext(REG_EX_HEADER)) {
- scanner.next(REG_EX_HEADER);
- currentLineNumber++;
- MatchResult result = scanner.match();
- if (result.groupCount() == 2) {
- String headerName = result.group(1).trim().toLowerCase(Locale.ENGLISH);
- String headerValue = result.group(2).trim();
- headers.put(headerName, headerValue);
- }
- } else {
- throw new BatchException(BatchException.INVALID_HEADER.addContent(scanner.next()));
- }
- }
- return headers;
- }
-
- private void parseNewLine(final Scanner scanner) throws BatchException {
- if (scanner.hasNext() && scanner.hasNext(REG_EX_BLANK_LINE)) {
- scanner.next();
- currentLineNumber++;
- } else {
- currentLineNumber++;
- if (scanner.hasNext()) {
- throw new BatchException(BatchException.MISSING_BLANK_LINE.addContent(scanner.next()).addContent(currentLineNumber));
- } else {
- throw new BatchException(BatchException.TRUNCATED_BODY.addContent(currentLineNumber));
-
- }
- }
- }
-
- private String getBaseUri() throws BatchException {
- if (batchRequestPathInfo != null) {
- if (batchRequestPathInfo.getServiceRoot() != null) {
- String baseUri = batchRequestPathInfo.getServiceRoot().toASCIIString();
- if (baseUri.lastIndexOf('/') == baseUri.length() - 1) {
- baseUri = baseUri.substring(0, baseUri.length() - 1);
- }
- for (PathSegment precedingPS : batchRequestPathInfo.getPrecedingSegments()) {
- baseUri = baseUri + "/" + precedingPS.getPath();
- }
- return baseUri;
- }
- } else {
- throw new BatchException(BatchException.INVALID_PATHINFO);
- }
- return null;
- }
-
- private String trimQuota(String boundary) {
- if (boundary.matches("\".*\"")) {
- boundary = boundary.replace("\"", "");
- }
- boundary = boundary.replaceAll("\\)", "\\\\)");
- boundary = boundary.replaceAll("\\(", "\\\\(");
- boundary = boundary.replaceAll("\\?", "\\\\?");
- boundary = boundary.replaceAll("\\+", "\\\\+");
- return boundary;
- }
-
- private List<String> getAcceptHeader(final Map<String, List<String>> headers) {
- List<String> acceptHeaders = new ArrayList<String>();
- List<String> requestAcceptHeaderList = headers.get(HttpHeaders.ACCEPT.toLowerCase(Locale.ENGLISH));
-
- if (requestAcceptHeaderList != null) {
- acceptHeaders = requestAcceptHeaderList;
- }
- return acceptHeaders;
- }
-
- private List<Locale> getAcceptLanguageHeader(final Map<String, List<String>> headers) {
- List<String> requestAcceptLanguageList = headers.get(HttpHeaders.ACCEPT_LANGUAGE.toLowerCase(Locale.ENGLISH));
- List<Locale> acceptLanguages = new ArrayList<Locale>();
- if (requestAcceptLanguageList != null) {
- for (String acceptLanguage : requestAcceptLanguageList) {
- String[] part = acceptLanguage.split("-");
- String language = part[0];
- String country = "";
- if (part.length == 2) {
- country = part[part.length - 1];
- }
- Locale locale = new Locale(language, country);
- acceptLanguages.add(locale);
- }
- }
- return acceptLanguages;
- }
-
- private String getContentTypeHeader(final Map<String, List<String>> headers) {
- List<String> requestContentTypeList = headers.get(HttpHeaders.CONTENT_TYPE.toLowerCase(Locale.ENGLISH));
- String contentType = null;
- if (requestContentTypeList != null) {
- for (String requestContentType : requestContentTypeList) {
- contentType = contentType != null ? contentType + "," + requestContentType : requestContentType;
- }
- }
- return contentType;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/fece4008/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestPartImpl.java
----------------------------------------------------------------------
diff --git a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestPartImpl.java b/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestPartImpl.java
deleted file mode 100644
index 80e5cd8..0000000
--- a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestPartImpl.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * 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
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
-package org.apache.olingo.odata2.core.batch;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.olingo.odata2.api.batch.BatchRequestPart;
-import org.apache.olingo.odata2.api.processor.ODataRequest;
-
-public class BatchRequestPartImpl implements BatchRequestPart {
-
- private List<ODataRequest> requests = new ArrayList<ODataRequest>();
- private boolean isChangeSet;
-
- public BatchRequestPartImpl() {}
-
- public BatchRequestPartImpl(final boolean isChangeSet, final List<ODataRequest> requests) {
- this.isChangeSet = isChangeSet;
- this.requests = requests;
- }
-
- @Override
- public boolean isChangeSet() {
- return isChangeSet;
- }
-
- public void setChangeSet(final boolean isChangeSet) {
- this.isChangeSet = isChangeSet;
- }
-
- @Override
- public List<ODataRequest> getRequests() {
- return Collections.unmodifiableList(requests);
- }
-
- public void setRequests(final List<ODataRequest> requests) {
- this.requests = requests;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/fece4008/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
----------------------------------------------------------------------
diff --git a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java b/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
deleted file mode 100644
index 3eda8e1..0000000
--- a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
+++ /dev/null
@@ -1,120 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * 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
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
-package org.apache.olingo.odata2.core.batch;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.olingo.odata2.api.client.batch.BatchChangeSet;
-import org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart;
-import org.apache.olingo.odata2.api.client.batch.BatchPart;
-import org.apache.olingo.odata2.api.client.batch.BatchQueryPart;
-import org.apache.olingo.odata2.api.commons.HttpContentType;
-import org.apache.olingo.odata2.api.commons.HttpHeaders;
-
-public class BatchRequestWriter {
- private static final String REG_EX_BOUNDARY = "([a-zA-Z0-9_\\-\\.'\\+]{1,70})|\"([a-zA-Z0-9_\\-\\.'\\+\\s\\(\\),/:=\\?]{1,69}[a-zA-Z0-9_\\-\\.'\\+\\(\\),/:=\\?])\""; // See RFC 2046
- private static final String COLON = ":";
- private static final String SP = " ";
- private static final String LF = "\r\n";
- private String batchBoundary;
- private StringBuilder writer = new StringBuilder();
-
- public InputStream writeBatchRequest(final List<BatchPart> batchParts, final String boundary) {
- if (boundary.matches(REG_EX_BOUNDARY)) {
- batchBoundary = boundary;
- } else {
- throw new IllegalArgumentException();
- }
- for (BatchPart batchPart : batchParts) {
- writer.append("--" + boundary).append(LF);
- if (batchPart instanceof BatchChangeSet) {
- appendChangeSet((BatchChangeSet) batchPart);
- } else if (batchPart instanceof BatchQueryPart) {
- BatchQueryPart request = (BatchQueryPart) batchPart;
- appendRequestBodyPart(request.getMethod(), request.getUri(), null, request.getHeaders(), request.getContentId());
-
- }
- }
- writer.append("--").append(boundary).append("--").append(LF).append(LF);
- InputStream batchRequestBody;
- batchRequestBody = new ByteArrayInputStream(BatchHelper.getBytes(writer.toString()));
- return batchRequestBody;
- }
-
- private void appendChangeSet(final BatchChangeSet batchChangeSet) {
- String boundary = BatchHelper.generateBoundary("changeset");
- while (boundary.equals(batchBoundary) || !boundary.matches(REG_EX_BOUNDARY)) {
- boundary = BatchHelper.generateBoundary("changeset");
- }
- writer.append(HttpHeaders.CONTENT_TYPE).append(COLON).append(SP).append(HttpContentType.MULTIPART_MIXED + "; boundary=" + boundary).append(LF).append(LF);
- for (BatchChangeSetPart request : batchChangeSet.getChangeSetParts()) {
- writer.append("--").append(boundary).append(LF);
- appendRequestBodyPart(request.getMethod(), request.getUri(), request.getBody(), request.getHeaders(), request.getContentId());
- }
- writer.append("--").append(boundary).append("--").append(LF).append(LF);
- }
-
- private void appendRequestBodyPart(final String method, final String uri, final String body, final Map<String, String> headers, final String contentId) {
- boolean isContentLengthPresent = false;
- writer.append(HttpHeaders.CONTENT_TYPE).append(COLON).append(SP).append(HttpContentType.APPLICATION_HTTP).append(LF);
- writer.append(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING).append(COLON).append(SP).append("binary").append(LF);
- if (contentId != null) {
- writer.append(BatchHelper.HTTP_CONTENT_ID).append(COLON).append(SP).append(contentId).append(LF);
- }
- String contentLength = getHeaderValue(headers, HttpHeaders.CONTENT_LENGTH);
- if (contentLength != null && !contentLength.isEmpty()) {
- isContentLengthPresent = true;
- }
- writer.append(LF);
- writer.append(method).append(SP).append(uri).append(SP).append("HTTP/1.1");
- writer.append(LF);
-
- if (!isContentLengthPresent && body != null && !body.isEmpty()) {
- writer.append(HttpHeaders.CONTENT_LENGTH).append(COLON).append(SP).append(BatchHelper.getBytes(body).length).append(LF);
-
- }
- appendHeader(headers);
-
- if (body != null && !body.isEmpty()) {
- writer.append(LF);
- writer.append(body);
- }
- writer.append(LF).append(LF);
- }
-
- private void appendHeader(final Map<String, String> headers) {
- for (Map.Entry<String, String> headerMap : headers.entrySet()) {
- String name = headerMap.getKey();
- writer.append(name).append(COLON).append(SP).append(headerMap.getValue()).append(LF);
-
- }
- }
-
- private String getHeaderValue(final Map<String, String> headers, final String headerName) {
- for (Map.Entry<String, String> header : headers.entrySet()) {
- if (headerName.equalsIgnoreCase(header.getKey())) {
- return header.getValue();
- }
- }
- return null;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/fece4008/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseParser.java
----------------------------------------------------------------------
diff --git a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseParser.java b/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseParser.java
deleted file mode 100644
index 15ce60c..0000000
--- a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseParser.java
+++ /dev/null
@@ -1,354 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * 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
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
-package org.apache.olingo.odata2.core.batch;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Scanner;
-import java.util.regex.MatchResult;
-import java.util.regex.Pattern;
-
-import org.apache.olingo.odata2.api.batch.BatchException;
-import org.apache.olingo.odata2.api.client.batch.BatchSingleResponse;
-import org.apache.olingo.odata2.api.commons.HttpContentType;
-import org.apache.olingo.odata2.api.commons.HttpHeaders;
-import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
-
-public class BatchResponseParser {
-
- private static final String LF = "\n";
- private static final String REG_EX_OPTIONAL_WHITESPACE = "\\s?";
- private static final String REG_EX_ZERO_OR_MORE_WHITESPACES = "\\s*";
- private static final String ANY_CHARACTERS = ".*";
-
- private static final Pattern REG_EX_BLANK_LINE = Pattern.compile("(|" + REG_EX_ZERO_OR_MORE_WHITESPACES + ")");
- private static final Pattern REG_EX_HEADER = Pattern.compile("([a-zA-Z\\-]+):" + REG_EX_OPTIONAL_WHITESPACE + "(.*)" + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_VERSION = Pattern.compile("(?:HTTP/[0-9]\\.[0-9])");
- private static final Pattern REG_EX_ANY_BOUNDARY_STRING = Pattern.compile("--" + ANY_CHARACTERS + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_STATUS_LINE = Pattern.compile(REG_EX_VERSION + "\\s" + "([0-9]{3})\\s([\\S ]+)" + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_BOUNDARY_PARAMETER = Pattern.compile(REG_EX_OPTIONAL_WHITESPACE + "boundary=(\".*\"|.*)" + REG_EX_ZERO_OR_MORE_WHITESPACES);
- private static final Pattern REG_EX_CONTENT_TYPE = Pattern.compile(REG_EX_OPTIONAL_WHITESPACE + HttpContentType.MULTIPART_MIXED);
-
- private static final String REG_EX_BOUNDARY = "([a-zA-Z0-9_\\-\\.'\\+]{1,70})|\"([a-zA-Z0-9_\\-\\.'\\+ \\(\\),/:=\\?]{1,69}[a-zA-Z0-9_\\-\\.'\\+\\(\\),/:=\\?])\""; // See RFC 2046
-
- private String contentTypeMime;
- private String boundary;
- private String currentContentId;
- private int currentLineNumber = 0;
-
- public BatchResponseParser(final String contentType) {
- contentTypeMime = contentType;
- }
-
- public List<BatchSingleResponse> parse(final InputStream in) throws BatchException {
- Scanner scanner = new Scanner(in, BatchHelper.DEFAULT_ENCODING).useDelimiter(LF);
- List<BatchSingleResponse> responseList;
- try {
- responseList = Collections.unmodifiableList(parseBatchResponse(scanner));
- } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
- scanner.close();
- try {
- in.close();
- } catch (IOException e) {
- throw new ODataRuntimeException(e);
- }
- }
- return responseList;
- }
-
- private List<BatchSingleResponse> parseBatchResponse(final Scanner scanner) throws BatchException {
- List<BatchSingleResponse> responses = new ArrayList<BatchSingleResponse>();
- if (contentTypeMime != null) {
- boundary = getBoundary(contentTypeMime);
- parsePreamble(scanner);
- final String closeDelimiter = "--" + boundary + "--" + REG_EX_ZERO_OR_MORE_WHITESPACES;
- while (scanner.hasNext() && !scanner.hasNext(closeDelimiter)) {
- responses.addAll(parseMultipart(scanner, boundary, false));
- }
- if (scanner.hasNext(closeDelimiter)) {
- scanner.next(closeDelimiter);
- currentLineNumber++;
- } else {
- throw new BatchException(BatchException.MISSING_CLOSE_DELIMITER.addContent(currentLineNumber));
- }
- } else {
- throw new BatchException(BatchException.MISSING_CONTENT_TYPE);
- }
- return responses;
-
- }
-
- //The method parses additional information prior to the first boundary delimiter line
- private void parsePreamble(final Scanner scanner) {
- while (scanner.hasNext() && !scanner.hasNext(REG_EX_ANY_BOUNDARY_STRING)) {
- scanner.next();
- currentLineNumber++;
- }
- }
-
- private List<BatchSingleResponse> parseMultipart(final Scanner scanner, final String boundary, final boolean isChangeSet) throws BatchException {
- Map<String, String> mimeHeaders = new HashMap<String, String>();
- List<BatchSingleResponse> responses = new ArrayList<BatchSingleResponse>();
- if (scanner.hasNext("--" + boundary + REG_EX_ZERO_OR_MORE_WHITESPACES)) {
- scanner.next();
- currentLineNumber++;
- mimeHeaders = parseMimeHeaders(scanner);
- currentContentId = mimeHeaders.get(BatchHelper.HTTP_CONTENT_ID.toLowerCase(Locale.ENGLISH));
-
- final String contentType = mimeHeaders.get(HttpHeaders.CONTENT_TYPE.toLowerCase(Locale.ENGLISH));
- if (contentType == null) {
- throw new BatchException(BatchException.MISSING_CONTENT_TYPE);
- }
- if (isChangeSet) {
- if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
- validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
- parseNewLine(scanner);// mandatory
- BatchSingleResponseImpl response = parseResponse(scanner, isChangeSet);
- responses.add(response);
- } else {
- throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.APPLICATION_HTTP));
- }
- } else {
- if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
- validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
- parseNewLine(scanner);// mandatory
- BatchSingleResponseImpl response = parseResponse(scanner, isChangeSet);
- responses.add(response);
- } else if (contentType.matches(REG_EX_OPTIONAL_WHITESPACE + HttpContentType.MULTIPART_MIXED + ANY_CHARACTERS)) {
- String changeSetBoundary = getBoundary(contentType);
- if (boundary.equals(changeSetBoundary)) {
- throw new BatchException(BatchException.INVALID_CHANGESET_BOUNDARY.addContent(currentLineNumber));
- }
- parseNewLine(scanner);// mandatory
- Pattern changeSetCloseDelimiter = Pattern.compile("--" + changeSetBoundary + "--" + REG_EX_ZERO_OR_MORE_WHITESPACES);
- while (!scanner.hasNext(changeSetCloseDelimiter)) {
- responses.addAll(parseMultipart(scanner, changeSetBoundary, true));
- }
- scanner.next(changeSetCloseDelimiter);
- currentLineNumber++;
- parseNewLine(scanner);
- } else {
- throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED + " or " + HttpContentType.APPLICATION_HTTP));
- }
- }
- } else if (scanner.hasNext(boundary + REG_EX_ZERO_OR_MORE_WHITESPACES)) {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_BOUNDARY_DELIMITER.addContent(currentLineNumber));
- } else if (scanner.hasNext(REG_EX_ANY_BOUNDARY_STRING)) {
- currentLineNumber++;
- throw new BatchException(BatchException.NO_MATCH_WITH_BOUNDARY_STRING.addContent(boundary).addContent(currentLineNumber));
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.MISSING_BOUNDARY_DELIMITER.addContent(currentLineNumber));
- }
- return responses;
-
- }
-
- private BatchSingleResponseImpl parseResponse(final Scanner scanner, final boolean isChangeSet) throws BatchException {
- BatchSingleResponseImpl response = new BatchSingleResponseImpl();
- if (scanner.hasNext(REG_EX_STATUS_LINE)) {
- scanner.next(REG_EX_STATUS_LINE);
- currentLineNumber++;
- final String statusCode;
- final String statusInfo;
- MatchResult result = scanner.match();
- if (result.groupCount() == 2) {
- statusCode = result.group(1);
- statusInfo = result.group(2);
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_STATUS_LINE.addContent(scanner.next()).addContent(currentLineNumber));
- }
-
- Map<String, String> headers = parseResponseHeaders(scanner);
- parseNewLine(scanner);
- String contentLengthHeader = getHeaderValue(headers, HttpHeaders.CONTENT_LENGTH);
- String body = (contentLengthHeader != null) ? parseBody(scanner, Integer.parseInt(contentLengthHeader)) : parseBody(scanner);
- response.setStatusCode(statusCode);
- response.setStatusInfo(statusInfo);
- response.setHeaders(headers);
- response.setContentId(currentContentId);
- response.setBody(body);
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_STATUS_LINE.addContent(scanner.next()).addContent(currentLineNumber));
- }
- return response;
- }
-
- private void validateEncoding(final String encoding) throws BatchException {
- if (!BatchHelper.BINARY_ENCODING.equalsIgnoreCase(encoding)) {
- throw new BatchException(BatchException.INVALID_CONTENT_TRANSFER_ENCODING);
- }
- }
-
- private Map<String, String> parseMimeHeaders(final Scanner scanner) throws BatchException {
- Map<String, String> headers = new HashMap<String, String>();
- while (scanner.hasNext() && !(scanner.hasNext(REG_EX_BLANK_LINE))) {
- if (scanner.hasNext(REG_EX_HEADER)) {
- scanner.next(REG_EX_HEADER);
- currentLineNumber++;
- MatchResult result = scanner.match();
- if (result.groupCount() == 2) {
- String headerName = result.group(1).trim().toLowerCase(Locale.ENGLISH);
- String headerValue = result.group(2).trim();
- headers.put(headerName, headerValue);
- }
- } else {
- throw new BatchException(BatchException.INVALID_HEADER.addContent(scanner.next()));
- }
- }
- return headers;
- }
-
- private Map<String, String> parseResponseHeaders(final Scanner scanner) throws BatchException {
- Map<String, String> headers = new HashMap<String, String>();
- while (scanner.hasNext() && !scanner.hasNext(REG_EX_BLANK_LINE)) {
- if (scanner.hasNext(REG_EX_HEADER)) {
- scanner.next(REG_EX_HEADER);
- currentLineNumber++;
- MatchResult result = scanner.match();
- if (result.groupCount() == 2) {
- String headerName = result.group(1).trim();
- String headerValue = result.group(2).trim();
- if (BatchHelper.HTTP_CONTENT_ID.equalsIgnoreCase(headerName)) {
- if (currentContentId == null) {
- currentContentId = headerValue;
- }
- } else {
- headers.put(headerName, headerValue);
- }
- }
- } else {
- currentLineNumber++;
- throw new BatchException(BatchException.INVALID_HEADER.addContent(scanner.next()).addContent(currentLineNumber));
- }
- }
- return headers;
- }
-
- private String getHeaderValue(final Map<String, String> headers, final String headerName) {
- for (Map.Entry<String, String> header : headers.entrySet()) {
- if (headerName.equalsIgnoreCase(header.getKey())) {
- return header.getValue();
- }
- }
- return null;
- }
-
- private String parseBody(final Scanner scanner) {
- StringBuilder body = null;
- while (scanner.hasNext() && !scanner.hasNext(REG_EX_ANY_BOUNDARY_STRING)) {
- if (!scanner.hasNext(REG_EX_ZERO_OR_MORE_WHITESPACES)) {
- if (body == null) {
- body = new StringBuilder(scanner.next());
- } else {
- body.append(LF).append(scanner.next());
- }
- } else {
- scanner.next();
- }
- currentLineNumber++;
- }
- String responseBody = body != null ? body.toString() : null;
- return responseBody;
- }
-
- private String parseBody(final Scanner scanner, final int contentLength) {
- StringBuilder body = null;
- int length = 0;
- while (scanner.hasNext() && length < contentLength) {
- if (!scanner.hasNext(REG_EX_ZERO_OR_MORE_WHITESPACES)) {
- String nextLine = scanner.next();
- length += BatchHelper.getBytes(nextLine).length;
- if (body == null) {
- body = new StringBuilder(nextLine);
- } else {
- body.append(LF).append(nextLine);
- }
- } else {
- scanner.next();
- }
- currentLineNumber++;
- if (scanner.hasNext() && scanner.hasNext(REG_EX_BLANK_LINE)) {
- scanner.next();
- currentLineNumber++;
- }
- }
- String responseBody = body != null ? body.toString() : null;
- return responseBody;
- }
-
- private String getBoundary(final String contentType) throws BatchException {
- Scanner contentTypeScanner = new Scanner(contentType).useDelimiter(";\\s?");
- if (contentTypeScanner.hasNext(REG_EX_CONTENT_TYPE)) {
- contentTypeScanner.next(REG_EX_CONTENT_TYPE);
- } else {
- contentTypeScanner.close();
- throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED));
- }
- if (contentTypeScanner.hasNext(REG_EX_BOUNDARY_PARAMETER)) {
- contentTypeScanner.next(REG_EX_BOUNDARY_PARAMETER);
- MatchResult result = contentTypeScanner.match();
- contentTypeScanner.close();
- if (result.groupCount() == 1 && result.group(1).trim().matches(REG_EX_BOUNDARY)) {
- return trimQuota(result.group(1).trim());
- } else {
- throw new BatchException(BatchException.INVALID_BOUNDARY);
- }
- } else {
- contentTypeScanner.close();
- throw new BatchException(BatchException.MISSING_PARAMETER_IN_CONTENT_TYPE);
- }
- }
-
- private void parseNewLine(final Scanner scanner) throws BatchException {
- if (scanner.hasNext() && scanner.hasNext(REG_EX_BLANK_LINE)) {
- scanner.next();
- currentLineNumber++;
- } else {
- currentLineNumber++;
- if (scanner.hasNext()) {
- throw new BatchException(BatchException.MISSING_BLANK_LINE.addContent(scanner.next()).addContent(currentLineNumber));
- } else {
- throw new BatchException(BatchException.TRUNCATED_BODY.addContent(currentLineNumber));
-
- }
- }
- }
-
- private String trimQuota(String boundary) {
- if (boundary.matches("\".*\"")) {
- boundary = boundary.replace("\"", "");
- }
- boundary = boundary.replaceAll("\\)", "\\\\)");
- boundary = boundary.replaceAll("\\(", "\\\\(");
- boundary = boundary.replaceAll("\\?", "\\\\?");
- boundary = boundary.replaceAll("\\+", "\\\\+");
- return boundary;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/fece4008/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponsePartImpl.java
----------------------------------------------------------------------
diff --git a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponsePartImpl.java b/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponsePartImpl.java
deleted file mode 100644
index 486bbdf..0000000
--- a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponsePartImpl.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * 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
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
-package org.apache.olingo.odata2.core.batch;
-
-import java.util.List;
-
-import org.apache.olingo.odata2.api.batch.BatchResponsePart;
-import org.apache.olingo.odata2.api.processor.ODataResponse;
-
-public class BatchResponsePartImpl extends BatchResponsePart {
- private List<ODataResponse> responses;
- private boolean isChangeSet;
-
- @Override
- public List<ODataResponse> getResponses() {
- return responses;
- }
-
- @Override
- public boolean isChangeSet() {
- return isChangeSet;
- }
-
- public class BatchResponsePartBuilderImpl extends BatchResponsePartBuilder {
- private List<ODataResponse> responses;
- private boolean isChangeSet;
-
- @Override
- public BatchResponsePart build() {
- BatchResponsePartImpl.this.responses = responses;
- BatchResponsePartImpl.this.isChangeSet = isChangeSet;
- return BatchResponsePartImpl.this;
- }
-
- @Override
- public BatchResponsePartBuilder responses(final List<ODataResponse> responses) {
- this.responses = responses;
- return this;
- }
-
- @Override
- public BatchResponsePartBuilder changeSet(final boolean isChangeSet) {
- this.isChangeSet = isChangeSet;
- return this;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/fece4008/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseWriter.java
----------------------------------------------------------------------
diff --git a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseWriter.java b/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseWriter.java
deleted file mode 100644
index ae0c07b..0000000
--- a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchResponseWriter.java
+++ /dev/null
@@ -1,158 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * 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
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
-package org.apache.olingo.odata2.core.batch;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.List;
-
-import org.apache.olingo.odata2.api.batch.BatchException;
-import org.apache.olingo.odata2.api.batch.BatchResponsePart;
-import org.apache.olingo.odata2.api.commons.HttpContentType;
-import org.apache.olingo.odata2.api.commons.HttpHeaders;
-import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
-import org.apache.olingo.odata2.api.exception.ODataMessageException;
-import org.apache.olingo.odata2.api.processor.ODataResponse;
-
-public class BatchResponseWriter {
- private static final String COLON = ":";
- private static final String SP = " ";
- private static final String LF = "\r\n";
- private ResponseWriter writer = new ResponseWriter();
-
- public ODataResponse writeResponse(final List<BatchResponsePart> batchResponseParts) throws BatchException {
- String boundary = BatchHelper.generateBoundary("batch");
- appendResponsePart(batchResponseParts, boundary);
- String batchResponseBody = writer.toString();
- return ODataResponse.entity(batchResponseBody).status(HttpStatusCodes.ACCEPTED)
- .header(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + "; boundary=" + boundary)
- .header(HttpHeaders.CONTENT_LENGTH, String.valueOf(writer.length()))
- .build();
- }
-
- private void appendChangeSet(final BatchResponsePart batchResponsePart) throws BatchException {
- String boundary = BatchHelper.generateBoundary("changeset");
- writer.append(HttpHeaders.CONTENT_TYPE).append(COLON).append(SP)
- .append("multipart/mixed; boundary=" + boundary).append(LF).append(LF);
- for (ODataResponse response : batchResponsePart.getResponses()) {
- writer.append("--").append(boundary).append(LF);
- appendResponsePartBody(response);
- }
- writer.append("--").append(boundary).append("--").append(LF).append(LF);
- }
-
- private void appendResponsePart(final List<BatchResponsePart> batchResponseParts, final String boundary) throws BatchException {
- for (BatchResponsePart batchResponsePart : batchResponseParts) {
- writer.append("--").append(boundary).append(LF);
- if (batchResponsePart.isChangeSet()) {
- appendChangeSet(batchResponsePart);
- } else {
- ODataResponse response = batchResponsePart.getResponses().get(0);
- appendResponsePartBody(response);
- }
- }
- writer.append("--").append(boundary).append("--");
- }
-
- private void appendResponsePartBody(final ODataResponse response) throws BatchException {
- writer.append(HttpHeaders.CONTENT_TYPE).append(COLON).append(SP)
- .append(HttpContentType.APPLICATION_HTTP).append(LF);
- writer.append(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING).append(COLON).append(SP)
- .append(BatchHelper.BINARY_ENCODING).append(LF);
- if (response.getHeader(BatchHelper.MIME_HEADER_CONTENT_ID) != null) {
- writer.append(BatchHelper.HTTP_CONTENT_ID).append(COLON).append(SP)
- .append(response.getHeader(BatchHelper.MIME_HEADER_CONTENT_ID)).append(LF);
- }
- writer.append(LF);
- writer.append("HTTP/1.1").append(SP).append(String.valueOf(response.getStatus().getStatusCode())).append(SP)
- .append(response.getStatus().getInfo()).append(LF);
- appendHeader(response);
- if (!HttpStatusCodes.NO_CONTENT.equals(response.getStatus())) {
- String body;
- if (response.getEntity() instanceof InputStream) {
- InputStream in = (InputStream) response.getEntity();
- body = readBody(in);
- } else {
- body = response.getEntity().toString();
- }
- writer.append(HttpHeaders.CONTENT_LENGTH).append(COLON).append(SP)
- .append(String.valueOf(BatchHelper.getBytes(body).length)).append(LF).append(LF);
- writer.append(body);
- }
- writer.append(LF).append(LF);
- }
-
- private void appendHeader(final ODataResponse response) {
- for (String name : response.getHeaderNames()) {
- if (!BatchHelper.MIME_HEADER_CONTENT_ID.equalsIgnoreCase(name) && !BatchHelper.REQUEST_HEADER_CONTENT_ID.equalsIgnoreCase(name)) {
- writer.append(name).append(COLON).append(SP).append(response.getHeader(name)).append(LF);
- } else if (BatchHelper.REQUEST_HEADER_CONTENT_ID.equalsIgnoreCase(name)) {
- writer.append(BatchHelper.HTTP_CONTENT_ID).append(COLON).append(SP)
- .append(response.getHeader(name)).append(LF);
- }
- }
- }
-
- private String readBody(final InputStream in) throws BatchException {
- byte[] tmp = new byte[2048];
- int count;
- BatchException cachedException = null;
- StringBuffer b = new StringBuffer();
- try {
- count = in.read(tmp);
- while (count >= 0) {
- b.append(new String(tmp, 0, count, BatchHelper.DEFAULT_ENCODING));
- count = in.read(tmp);
- }
- } catch (IOException e) {
- cachedException = new BatchException(ODataMessageException.COMMON, e);
- throw cachedException;
- } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
- try {
- in.close();
- } catch (IOException e) {
- if (cachedException != null) {
- throw cachedException;
- }
- }
- }
- return b.toString();
- }
-
- private static class ResponseWriter {
- private StringBuilder sb = new StringBuilder();
- private int length = 0;
-
- public ResponseWriter append(final String content) {
- length += BatchHelper.getBytes(content).length;
- sb.append(content);
- return this;
- }
-
- public int length() {
- return length;
- }
-
- @Override
- public String toString() {
- return sb.toString();
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/fece4008/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchSingleResponseImpl.java
----------------------------------------------------------------------
diff --git a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchSingleResponseImpl.java b/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchSingleResponseImpl.java
deleted file mode 100644
index 337b550..0000000
--- a/aggregated-sources/org.apache.olingo.odata2-parent-sources/org/apache/olingo/odata2/core/batch/BatchSingleResponseImpl.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * 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
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
-package org.apache.olingo.odata2.core.batch;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.olingo.odata2.api.client.batch.BatchSingleResponse;
-
-public class BatchSingleResponseImpl implements BatchSingleResponse {
-
- private String statusCode;
- private String statusInfo;
- private String body;
- private Map<String, String> headers = new HashMap<String, String>();
- private String contentId;
-
- @Override
- public String getStatusCode() {
- return statusCode;
- }
-
- @Override
- public String getStatusInfo() {
- return statusInfo;
- }
-
- @Override
- public String getBody() {
- return body;
- }
-
- @Override
- public Map<String, String> getHeaders() {
- return headers;
- }
-
- @Override
- public String getContentId() {
- return contentId;
- }
-
- @Override
- public String getHeader(final String name) {
- return headers.get(name);
- }
-
- @Override
- public Set<String> getHeaderNames() {
- return headers.keySet();
- }
-
- public void setStatusCode(final String statusCode) {
- this.statusCode = statusCode;
- }
-
- public void setStatusInfo(final String statusInfo) {
- this.statusInfo = statusInfo;
- }
-
- public void setBody(final String body) {
- this.body = body;
- }
-
- public void setHeaders(final Map<String, String> headers) {
- this.headers = headers;
- }
-
- public void setContentId(final String contentId) {
- this.contentId = contentId;
- }
-
-}