[OLINGO-206] Merge remote-tracking branch 'origin/master' into olingo-206-val

This commit is contained in:
Stephan Klevenz 2014-03-18 18:49:31 +01:00
commit f3d5d66735
8 changed files with 457 additions and 41 deletions

View File

@ -0,0 +1,90 @@
/*******************************************************************************
* 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.commons.core;
import java.io.UnsupportedEncodingException;
/**
* Decodes a Java String containing a percent-encoded UTF-8 String value
* into a Java String (in its internal UTF-16 encoding).
*
*/
public class Decoder {
/**
* Decodes a percent-encoded UTF-8 String value into a Java String
* (in its internal UTF-16 encoding).
* @param value the encoded String
* @return the Java String
* @throws IllegalArgumentException if value contains characters not representing UTF-8 bytes
* or ends with an unfinished percent-encoded character
* @throws NumberFormatException if the two characters after a percent character
* are not hexadecimal digits
*/
public static String decode(final String value) throws IllegalArgumentException, NumberFormatException {
if (value == null) {
return value;
}
// Use a tiny finite-state machine to handle decoding on byte level.
// There are only three states:
// -2: normal bytes
// -1: a byte representing the percent character has been read
// >= 0: a byte representing the first half-byte of a percent-encoded byte has been read
// The variable holding the state is also used to store the value of the first half-byte.
byte[] result = new byte[value.length()];
int position = 0;
byte encodedPart = -2;
for (final char c : value.toCharArray()) {
if (c <= Byte.MAX_VALUE) {
if (c == '%') {
if (encodedPart == -2) {
encodedPart = -1;
} else {
throw new IllegalArgumentException();
}
} else if (encodedPart == -1) {
encodedPart = (byte) c;
} else if (encodedPart >= 0) {
final int i = Integer.parseInt(String.valueOf(new char[] { (char) encodedPart, c }), 16);
if (i >= 0) {
result[position++] = (byte) i;
} else {
throw new NumberFormatException();
}
encodedPart = -2;
} else {
result[position++] = (byte) c;
}
} else {
throw new IllegalArgumentException();
}
}
if (encodedPart >= 0) {
throw new IllegalArgumentException();
}
try {
return new String(result, 0, position, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
}
}

View File

@ -0,0 +1,130 @@
/*******************************************************************************
* 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.commons.core;
import java.io.UnsupportedEncodingException;
/**
* Encodes a Java String (in its internal UTF-16 encoding) into its
* percent-encoded UTF-8 representation according to
* <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
* (with consideration of its predecessor RFC 2396).
*
*/
public class Encoder {
/**
* Encodes a Java String (in its internal UTF-16 encoding) into its
* percent-encoded UTF-8 representation according to
* <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>,
* suitable for parts of an OData path segment.
* @param value the Java String
* @return the encoded String
*/
public static String encode(final String value) {
return encoder.encodeInternal(value);
}
// OData has special handling for "'", so we allow that to remain unencoded.
// Other sub-delims not used neither by JAX-RS nor by OData could be added
// if the encoding is considered to be too aggressive.
// RFC 3986 would also allow the gen-delims ":" and "@" to appear literally
// in path-segment parts.
private static final String ODATA_UNENCODED = "'";
// Character classes from RFC 3986
private final static String UNRESERVED = "-._~"; // + ALPHA + DIGIT
// RFC 3986 says: "For consistency, URI producers and normalizers should
// use uppercase hexadecimal digits for all percent-encodings."
private final static String[] hex = { "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", "%08", "%09", "%0A",
"%0B", "%0C", "%0D", "%0E", "%0F", "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", "%18", "%19", "%1A",
"%1B", "%1C", "%1D", "%1E", "%1F", "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", "%29", "%2A",
"%2B", "%2C", "%2D", "%2E", "%2F", "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37", "%38", "%39", "%3A",
"%3B", "%3C", "%3D", "%3E", "%3F", "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47", "%48", "%49", "%4A",
"%4B", "%4C", "%4D", "%4E", "%4F", "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57", "%58", "%59", "%5A",
"%5B", "%5C", "%5D", "%5E", "%5F", "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", "%68", "%69", "%6A",
"%6B", "%6C", "%6D", "%6E", "%6F", "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77", "%78", "%79", "%7A",
"%7B", "%7C", "%7D", "%7E", "%7F", "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87", "%88",
"%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F", "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", "%98",
"%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F", "%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7", "%A8",
"%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF", "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7", "%B8",
"%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF", "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8",
"%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7", "%D8",
"%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8",
"%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", "%F8",
"%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF" };
private static final Encoder encoder = new Encoder(ODATA_UNENCODED);
/** characters to remain unencoded in addition to {@link #UNRESERVED} */
private final String unencoded;
private Encoder(final String unencoded) {
this.unencoded = unencoded == null ? "" : unencoded;
}
/**
* <p>Returns the percent-encoded UTF-8 representation of a String.</p>
* <p>In order to avoid producing percent-encoded CESU-8 (as described in
* the Unicode Consortium's <a href="http://www.unicode.org/reports/tr26/">
* Technical Report #26</a>), this is done in two steps:
* <ol>
* <li>Re-encode the characters from their Java-internal UTF-16 representations
* into their UTF-8 representations.</li>
* <li>Percent-encode each of the bytes in the UTF-8 representation.
* This is possible on byte level because all characters that do not have
* a <code>%xx</code> representation are represented in one byte in UTF-8.</li>
* </ol></p>
* @param input input String
* @return encoded representation
*/
private String encodeInternal(final String input) {
StringBuilder resultStr = new StringBuilder();
try {
for (byte utf8Byte : input.getBytes("UTF-8")) {
final char character = (char) utf8Byte;
if (isUnreserved(character)) {
resultStr.append(character);
} else if (isUnencoded(character)) {
resultStr.append(character);
} else if (utf8Byte >= 0) {
resultStr.append(hex[utf8Byte]);
} else {
// case UTF-8 continuation byte
resultStr.append(hex[256 + utf8Byte]); // index adjusted for the usage of signed bytes
}
}
} catch (final UnsupportedEncodingException e) { // should never happen; UTF-8 is always there
return null;
}
return resultStr.toString();
}
private static boolean isUnreserved(final char character) {
return 'A' <= character && character <= 'Z' // case A..Z
|| 'a' <= character && character <= 'z' // case a..z
|| '0' <= character && character <= '9' // case 0..9
|| UNRESERVED.indexOf(character) >= 0;
}
private boolean isUnencoded(final char character) {
return unencoded.indexOf(character) >= 0;
}
}

View File

@ -0,0 +1,89 @@
/*******************************************************************************
* 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.commons.core;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
/**
*
*/
public class DecoderTest {
@Test
public void asciiCharacters() {
assertNull(Decoder.decode(null));
String s = "azAZ019";
assertEquals(s, Decoder.decode(s));
s = "\"\\`{}|";
assertEquals(s, Decoder.decode(s));
}
@Test
public void asciiControl() {
assertEquals("\u0000\b\t\n\r", Decoder.decode("%00%08%09%0a%0d"));
}
@Test
public void asciiEncoded() {
assertEquals("<>%&", Decoder.decode("%3c%3e%25%26"));
assertEquals(":/?#[]@", Decoder.decode("%3a%2f%3f%23%5b%5d%40"));
assertEquals(" !\"$'()*+,-.", Decoder.decode("%20%21%22%24%27%28%29%2A%2B%2C%2D%2E"));
}
@Test
public void unicodeCharacters() {
assertEquals("", Decoder.decode("%E2%82%AC"));
assertEquals("\uFDFC", Decoder.decode("%EF%B7%BC"));
}
@Test
public void charactersOutsideBmp() {
assertEquals(String.valueOf(Character.toChars(0x1F603)), Decoder.decode("%f0%9f%98%83"));
}
@Test(expected = IllegalArgumentException.class)
public void wrongCharacter() {
Decoder.decode("%20ä");
}
@Test(expected = NumberFormatException.class)
public void wrongPercentNumber() {
Decoder.decode("%-3");
}
@Test(expected = IllegalArgumentException.class)
public void wrongPercentPercent() {
Decoder.decode("%%a");
}
@Test(expected = IllegalArgumentException.class)
public void unfinishedPercent() {
Decoder.decode("%a");
}
@Test(expected = IllegalArgumentException.class)
public void nullByte() {
Decoder.decode("%\u0000ff");
}
}

View File

@ -0,0 +1,106 @@
/*******************************************************************************
* 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.commons.core;
import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Test;
/**
* Tests for percent-encoding.
*
*/
public class EncoderTest {
private final static String RFC3986_UNRESERVED = "-._~"; // + ALPHA + DIGIT
private final static String RFC3986_GEN_DELIMS = ":/?#[]@";
private final static String RFC3986_SUB_DELIMS = "!$&'()*+,;=";
private final static String RFC3986_RESERVED = RFC3986_GEN_DELIMS + RFC3986_SUB_DELIMS;
@Test
public void asciiCharacters() {
final String s = "azAZ019";
assertEquals(s, Encoder.encode(s));
assertEquals(s, Encoder.encode(s));
}
@Test
public void asciiControl() {
assertEquals("%08%09%0A%0D", Encoder.encode("\b\t\n\r"));
}
@Test
public void unsafe() {
assertEquals("%3C%3E%25%26", Encoder.encode("<>%&"));
assertEquals("%22%5C%60%7B%7D%7C", Encoder.encode("\"\\`{}|"));
}
@Test
public void rfc3986Unreserved() {
assertEquals(RFC3986_UNRESERVED, Encoder.encode(RFC3986_UNRESERVED));
}
@Test
public void rfc3986GenDelims() {
assertEquals("%3A%2F%3F%23%5B%5D%40", Encoder.encode(RFC3986_GEN_DELIMS));
}
@Test
public void rfc3986SubDelims() {
assertEquals("%21%24%26'%28%29%2A%2B%2C%3B%3D", Encoder.encode(RFC3986_SUB_DELIMS));
}
@Test
public void rfc3986Reserved() {
assertEquals("%3A%2F%3F%23%5B%5D%40%21%24%26'%28%29%2A%2B%2C%3B%3D", Encoder.encode(RFC3986_RESERVED));
}
@Test
public void unicodeCharacters() {
assertEquals("%E2%82%AC", Encoder.encode(""));
assertEquals("%EF%B7%BC", Encoder.encode("\uFDFC")); // RIAL SIGN
}
@Test
public void charactersOutsideBmp() {
// Unicode characters outside the Basic Multilingual Plane are stored
// in a Java String in two surrogate characters.
final String s = String.valueOf(Character.toChars(0x1F603));
assertEquals("%F0%9F%98%83", Encoder.encode(s));
}
@Test
public void uriDecoding() throws URISyntaxException {
final String decodedValue = RFC3986_UNRESERVED + RFC3986_RESERVED + "0..1..a..z..A..Z..@"
+ "\u2323\uFDFC" + String.valueOf(Character.toChars(0x1F603));
final String encodedPath = Encoder.encode(decodedValue) + "/" + Encoder.encode(decodedValue);
final String encodedQuery = Encoder.encode(decodedValue);
final URI uri = new URI("http://host:80/" + encodedPath + "?" + encodedQuery + "=" + encodedQuery);
assertEquals(uri.getPath(), "/" + decodedValue + "/" + decodedValue);
assertEquals(uri.getQuery(), decodedValue + "=" + decodedValue);
assertEquals(uri.getRawPath(), "/" + encodedPath);
assertEquals(uri.getRawQuery(), encodedQuery + "=" + encodedQuery);
}
}

View File

@ -1,18 +1,18 @@
/*
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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
*
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
@ -91,7 +91,10 @@ public class Parser {
firstSegment = uri.pathSegmentListDecoded.get(0);
}
if (firstSegment.startsWith("$batch")) {
if (firstSegment.length() == 0) {
readQueryParameter = true;
context.contextUriInfo = new UriInfoImpl().setKind(UriInfoKind.service);
} else if (firstSegment.startsWith("$batch")) {
BatchEOFContext ctxBatchEOF =
(BatchEOFContext) parseRule(uri.pathSegmentListDecoded.get(0), ParserEntryRules.Batch);

View File

@ -1,25 +1,23 @@
/*
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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
*
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.parser;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
@ -27,13 +25,14 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.olingo.commons.core.Decoder;
import org.apache.olingo.server.core.uri.parser.RawUri.QueryOption;
public class UriDecoder {
static Pattern uriPattern = Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
public static RawUri decodeUri(final String uri, final int scipSegments) throws UriParserSyntaxException {
public static RawUri decodeUri(final String uri, final int scipSegments) {
RawUri rawUri = new RawUri();
Matcher m = uriPattern.matcher(uri);
@ -52,7 +51,7 @@ public class UriDecoder {
return rawUri;
}
private static void decode(final RawUri rawUri) throws UriParserSyntaxException {
private static void decode(final RawUri rawUri) {
rawUri.pathSegmentListDecoded = new ArrayList<String>();
for (String segment : rawUri.pathSegmentList) {
rawUri.pathSegmentListDecoded.add(decode(segment));
@ -119,19 +118,13 @@ public class UriDecoder {
start = end + 1;
}
list.add(input.substring(start));
return list;
}
public static String decode(final String encoded) throws UriParserSyntaxException {
try {
return URLDecoder.decode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UriParserSyntaxException("Error while decoding");
}
public static String decode(final String encoded) {
return Decoder.decode(encoded);
}
}

View File

@ -122,6 +122,8 @@ public class RawUriTest {
rawUri = runRawParser("nonServiceSegment/entitySet", 1);
checkPath(rawUri, "nonServiceSegment/entitySet", Arrays.asList("entitySet"));
rawUri = runRawParser("http://test.org/a?abc=xx+yz", 0);
}

View File

@ -1,29 +1,29 @@
/*
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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
*
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri.antlr;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import org.apache.olingo.commons.api.ODataApplicationException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.core.Encoder;
import org.apache.olingo.server.api.uri.UriInfoKind;
import org.apache.olingo.server.api.uri.UriResourceKind;
import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind;
@ -2573,7 +2573,10 @@ public class TestFullResourcePath {
@Test
public void misc() {
testUri.run("");
testUri.run("")
.isKind(UriInfoKind.service);
testUri.run("/")
.isKind(UriInfoKind.service);
testUri.run("$all")
.isKind(UriInfoKind.all);
@ -5079,7 +5082,7 @@ public class TestFullResourcePath {
+ "/com.sap.odata.test1.CTBase/com.sap.odata.test1.CTBase").isExSemantic(0);
testUri.runEx("ESTwoKeyNav/com.sap.odata.test1.BFCESTwoKeyNavRTESTwoKeyNav").isExSemantic(0);
// $ref
testUri.runEx("ESTwoKeyNav(PropertyInt16=1,PropertyString='2')/PropertyComplexTwoPrim/$ref").isExSemantic(0);
testUri.runEx("ESTwoKeyNav(PropertyInt16=1,PropertyString='2')/PropertyComplexTwoPrim/$count").isExSemantic(0);
@ -5097,7 +5100,7 @@ public class TestFullResourcePath {
}
public static String encode(final String decoded) throws UnsupportedEncodingException {
return URLEncoder.encode(decoded, "UTF-8");
return Encoder.encode(decoded);
}