[OLINGO-659] Several fixes for issues from static code check

This commit is contained in:
Michael Bolz 2015-08-07 10:48:51 +02:00
parent 15a06522e9
commit 24668aa2db
11 changed files with 126 additions and 65 deletions

View File

@ -57,7 +57,7 @@ public class BatchHandler {
private void validateContentType(final ODataRequest request) throws BatchDeserializerException {
// This method does validation.
BatchParserCommon.getContentType(request.getHeader(HttpHeader.CONTENT_TYPE), ContentType.MULTIPART_MIXED, 0);
BatchParserCommon.parseContentType(request.getHeader(HttpHeader.CONTENT_TYPE), ContentType.MULTIPART_MIXED, 0);
}
private void validateHttpMethod(final ODataRequest request) throws BatchDeserializerException {

View File

@ -113,7 +113,7 @@ public class BatchBodyPart implements BatchPart {
private boolean isContentTypeMultiPartMixed(final String contentType) {
try {
BatchParserCommon.getContentType(contentType, ContentType.MULTIPART_MIXED, 0);
BatchParserCommon.parseContentType(contentType, ContentType.MULTIPART_MIXED, 0);
return true;
} catch (final BatchDeserializerException e) {
return false;

View File

@ -44,7 +44,7 @@ public class BatchParserCommon {
public static final String BINARY_ENCODING = "binary";
public static String getBoundary(final String contentType, final int line) throws BatchDeserializerException {
final ContentType type = getContentType(contentType, ContentType.MULTIPART_MIXED, line);
final ContentType type = parseContentType(contentType, ContentType.MULTIPART_MIXED, line);
final Map<String, String> parameters = type.getParameters();
for (final Map.Entry<String, String> entries : parameters.entrySet()) {
if (BOUNDARY.equalsIgnoreCase(entries.getKey())) {
@ -61,7 +61,18 @@ public class BatchParserCommon {
BatchDeserializerException.MessageKeys.MISSING_BOUNDARY_DELIMITER, Integer.toString(line));
}
public static ContentType getContentType(final String contentType, final ContentType expected, final int line)
/**
* Get the content type based on <code>contentType</code> parameter.
* If this content type is not compatible to the expected ContentType a
* BatchDeserializerException is thrown.
*
* @param contentType content type string which is parsed
* @param expected content type to which the parsed must be compatible
* @param line parsed line
* @return the parsed content type or if not compatible or parseable an exception is thrown (never returns null)
* @throws BatchDeserializerException
*/
public static ContentType parseContentType(final String contentType, final ContentType expected, final int line)
throws BatchDeserializerException {
ContentType type;
try {

View File

@ -35,10 +35,7 @@ public class BatchTransformatorCommon {
throw new BatchDeserializerException("Missing content type", MessageKeys.MISSING_CONTENT_TYPE,
Integer.toString(headers.getLineNumber()));
}
if (BatchParserCommon.getContentType(contentTypes.get(0), expected, headers.getLineNumber()) == null) {
throw new BatchDeserializerException("Invalid content type", MessageKeys.INVALID_CONTENT_TYPE,
expected.toContentTypeString());
}
BatchParserCommon.parseContentType(contentTypes.get(0), expected, headers.getLineNumber());
}
public static void validateContentTransferEncoding(final Header headers) throws BatchDeserializerException {

View File

@ -227,14 +227,14 @@ public class UriInfoImpl implements UriInfo {
if (item instanceof SystemQueryOptionImpl) {
setSystemQueryOption((SystemQueryOptionImpl) item);
} else if (item instanceof CustomQueryOptionImpl) {
addCustomQueryOption(item);
addCustomQueryOption((CustomQueryOptionImpl) item);
}
}
return this;
}
public void addCustomQueryOption(final QueryOptionImpl item) {
customQueryOptions.add((CustomQueryOptionImpl) item);
public void addCustomQueryOption(final CustomQueryOptionImpl item) {
customQueryOptions.add(item);
if (item.getName().startsWith("@")) {
aliasToValue.put(item.getName(), item.getText());
}

View File

@ -465,22 +465,23 @@ public class Parser {
boolean first = true;
System.out.println("input: " + input);
String nL = "\n";
String out = "[" + nL;
StringBuilder out = new StringBuilder("[").append(nL);
for (Token token : list) {
if (!first) {
out += ",";
out.append(",");
first = false;
}
int index = token.getType();
out.append("\"").append(token.getText()).append("\"").append(" ");
if (index != -1) {
out += "\"" + token.getText() + "\"" + " " + UriLexer.tokenNames[index] + nL;
out.append(UriLexer.tokenNames[index]);
} else {
out += "\"" + token.getText() + "\"" + " " + index + nL;
out.append(index);
}
out.append(nL);
}
out += ']';
System.out.println("tokens: " + out);
return;
out.append(']');
System.out.println("tokens: " + out.toString());
}
}

View File

@ -59,15 +59,13 @@ public class UriDecoder {
private static List<RawUri.QueryOption> splitOptions(final String queryOptionString) {
if (queryOptionString == null) {
return Collections.<RawUri.QueryOption> emptyList();
return Collections.emptyList();
}
List<RawUri.QueryOption> queryOptionList = new ArrayList<RawUri.QueryOption>();
for (String option : split(queryOptionString, '&')) {
if (option.length() != 0) {
final List<String> pair = splitFirst(option, '=');
queryOptionList.add(new RawUri.QueryOption(pair.get(0), pair.get(1)));
}
for (String option : splitSkipEmpty(queryOptionString, '&')) {
final List<String> pair = splitFirst(option, '=');
queryOptionList.add(new RawUri.QueryOption(pair.get(0), pair.get(1)));
}
return queryOptionList;
}
@ -82,29 +80,67 @@ public class UriDecoder {
}
private static List<String> splitPath(final String path, final int skipSegments) {
List<String> list = split(path, '/');
// Empty path segments of the resource path are removed.
while (list.remove("")) {
// this place intentionally left blank
}
List<String> list = splitSkipEmpty(path, '/');
return skipSegments > 0 ? list.subList(skipSegments, list.size()) : list;
}
public static List<String> split(final String input, final char c) {
static List<String> split(final String input, final char c) {
return split(input, c, false);
}
static List<String> splitSkipEmpty(final String input, final char c) {
if(input.isEmpty() || input.length() == 1 && input.charAt(0) == c) {
return Collections.emptyList();
}
List<String> list = new LinkedList<String>();
int start = 0;
int end = -1;
int end;
while ((end = input.indexOf(c, start)) >= 0) {
list.add(input.substring(start, end));
if(start != end) {
list.add(input.substring(start, end));
}
start = end + 1;
}
list.add(input.substring(start));
if(input.charAt(input.length()-1) != c) {
list.add(input.substring(start));
}
return list;
}
static List<String> split(final String input, final char c, boolean skipEmpty) {
if(skipEmpty && (input.isEmpty() || input.length() == 1 && input.charAt(0) == c)) {
return Collections.emptyList();
}
List<String> list = new LinkedList<String>();
int start = 0;
int end;
while ((end = input.indexOf(c, start)) >= 0) {
if(skipEmpty) {
if(start != end) {
list.add(input.substring(start, end));
}
} else {
list.add(input.substring(start, end));
}
start = end + 1;
}
if(skipEmpty) {
if(input.charAt(input.length()-1) != c) {
list.add(input.substring(start));
}
} else {
list.add(input.substring(start));
}
return list;
}

View File

@ -360,13 +360,13 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor<Object> {
// get function from function import
EdmFunction function = edmFunctionImport.getUnboundFunction(names);
if (function == null) {
String tmp = "";
StringBuilder tmp = new StringBuilder();
for (String name : names) {
tmp += (tmp.length() != 0 ? "," : "") + name;
tmp.append((tmp.length() != 0 ? "," : "")).append(name);
}
throw wrap(new UriParserSemanticException("Function of functionimport '" + edmFunctionImport.getName()
+ "' with parameters [" + tmp + "] not found",
UriParserSemanticException.MessageKeys.FUNCTION_NOT_FOUND, edmFunctionImport.getName(), tmp));
+ "' with parameters [" + tmp.toString() + "] not found",
UriParserSemanticException.MessageKeys.FUNCTION_NOT_FOUND, edmFunctionImport.getName(), tmp.toString()));
}
uriResource.setFunction(edmFunctionImport.getUnboundFunction(names));

View File

@ -54,23 +54,23 @@ public class ExpandItemImpl implements ExpandItem {
public ExpandItemImpl setSystemQueryOption(final SystemQueryOptionImpl sysItem) {
if (sysItem.getKind() == SystemQueryOptionKind.EXPAND) {
if (sysItem instanceof ExpandOptionImpl) {
expandOption = (ExpandOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.FILTER) {
} else if (sysItem instanceof FilterOptionImpl) {
filterOption = (FilterOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.COUNT) {
} else if (sysItem instanceof CountOptionImpl) {
inlineCountOption = (CountOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.ORDERBY) {
} else if (sysItem instanceof OrderByOptionImpl) {
orderByOption = (OrderByOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.SEARCH) {
} else if (sysItem instanceof SearchOptionImpl) {
searchOption = (SearchOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.SELECT) {
} else if (sysItem instanceof SelectOptionImpl) {
selectOption = (SelectOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.SKIP) {
} else if (sysItem instanceof SkipOptionImpl) {
skipOption = (SkipOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.TOP) {
} else if (sysItem instanceof TopOptionImpl) {
topOption = (TopOptionImpl) sysItem;
} else if (sysItem.getKind() == SystemQueryOptionKind.LEVELS) {
} else if (sysItem instanceof LevelsExpandOption) {
levelsExpandOption = (LevelsExpandOption) sysItem;
}
return this;

View File

@ -502,22 +502,22 @@ public class UriValidator {
private void validateNoQueryOptionsForHttpMethod(final UriInfo uriInfo, final HttpMethod httpMethod)
throws UriValidationException {
if (!uriInfo.getSystemQueryOptions().isEmpty()) {
String options = "";
StringBuilder options = new StringBuilder();
for (SystemQueryOption option : uriInfo.getSystemQueryOptions()) {
options = options + option.getName() + " ";
options.append(option.getName()).append(" ");
}
throw new UriValidationException("System query option " + options + " not allowed for method "
throw new UriValidationException("System query option " + options.toString() + " not allowed for method "
+ httpMethod, UriValidationException.MessageKeys.SYSTEM_QUERY_OPTION_NOT_ALLOWED_FOR_HTTP_METHOD,
options, httpMethod.toString());
options.toString(), httpMethod.toString());
}
}
private boolean isAction(final UriInfo uriInfo) {
List<UriResource> uriResourceParts = uriInfo.getUriResourceParts();
if (!uriResourceParts.isEmpty()) {
return UriResourceKind.action == uriResourceParts.get(uriResourceParts.size() - 1).getKind();
if (uriResourceParts.isEmpty()) {
return false;
}
return false;
return UriResourceKind.action == uriResourceParts.get(uriResourceParts.size() - 1).getKind();
}
private void validateKeyPredicates(final UriInfo uriInfo) throws UriValidationException {
@ -586,7 +586,8 @@ public class UriValidator {
if (last != null
&& (last.getKind() == UriResourceKind.primitiveProperty
|| last.getKind() == UriResourceKind.complexProperty
|| last.getKind() == UriResourceKind.value && previous.getKind() == UriResourceKind.primitiveProperty)) {
|| (last.getKind() == UriResourceKind.value
&& previous != null && previous.getKind() == UriResourceKind.primitiveProperty))) {
final EdmProperty property = ((UriResourceProperty)
(last.getKind() == UriResourceKind.value ? previous : last)).getProperty();
if (method == HttpMethod.PATCH && property.isCollection()) {

View File

@ -16,17 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.core.uri;
package org.apache.olingo.server.core.uri.parser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.olingo.server.core.uri.parser.RawUri;
import org.apache.olingo.server.core.uri.parser.UriDecoder;
import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException;
import org.junit.Test;
public class RawUriTest {
@ -126,12 +124,29 @@ public class RawUriTest {
@Test
public void testSplit() {
UriDecoder.split("", '/');
UriDecoder.split("/", '/');
UriDecoder.split("a", '/');
UriDecoder.split("a/", '/');
UriDecoder.split("/a", '/');
UriDecoder.split("a/a", '/');
assertEquals(Arrays.asList(""), UriDecoder.split("", '/'));
assertEquals(Arrays.asList("", ""), UriDecoder.split("/", '/'));
assertEquals(Arrays.asList("a"), UriDecoder.split("a", '/'));
assertEquals(Arrays.asList("a", ""), UriDecoder.split("a/", '/'));
assertEquals(Arrays.asList("", "a"), UriDecoder.split("/a", '/'));
assertEquals(Arrays.asList("a", "a"), UriDecoder.split("a/a", '/'));
assertEquals(Arrays.asList("", "a", "a"), UriDecoder.split("/a/a", '/'));
// with skip
assertTrue(UriDecoder.split("", '/', true).isEmpty());
assertTrue(UriDecoder.split("/", '/', true).isEmpty());
assertEquals(Arrays.asList("a"), UriDecoder.split("a", '/', true));
assertEquals(Arrays.asList("a"), UriDecoder.split("a/", '/', true));
assertEquals(Arrays.asList("a"), UriDecoder.split("/a", '/', true));
assertEquals(Arrays.asList("a", "a"), UriDecoder.split("a/a", '/', true));
assertEquals(Arrays.asList("a", "a"), UriDecoder.split("/a/a", '/', true));
// with skip
assertTrue(UriDecoder.splitSkipEmpty("", '/').isEmpty());
assertTrue(UriDecoder.splitSkipEmpty("/", '/').isEmpty());
assertEquals(Arrays.asList("a"), UriDecoder.splitSkipEmpty("a", '/'));
assertEquals(Arrays.asList("a"), UriDecoder.splitSkipEmpty("a/", '/'));
assertEquals(Arrays.asList("a"), UriDecoder.splitSkipEmpty("/a", '/'));
assertEquals(Arrays.asList("a", "a"), UriDecoder.splitSkipEmpty("a/a", '/'));
assertEquals(Arrays.asList("a", "a"), UriDecoder.splitSkipEmpty("/a/a", '/'));
}
private void checkPath(final RawUri rawUri, final String path, final List<String> list) {