Fix package tangle between cli and operation packages

QueryStringParser has been moved into the operation page. In the
unlikely event that there were any external users of the class, a
deprecated version remains in the cli package for backwards
compatibility. It will be removed in 1.2.

Closes gh-286
This commit is contained in:
Andy Wilkinson
2016-08-19 20:14:16 +01:00
parent 5c6cf7f12f
commit 14dcdb61d0
4 changed files with 98 additions and 73 deletions

View File

@@ -16,78 +16,15 @@
package org.springframework.restdocs.cli;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import org.springframework.restdocs.operation.Parameters;
/**
* A parser for the query string of a URI.
*
* @author Andy Wilkinson
* @deprecated since 1.1.2 in favor of
* {@link org.springframework.restdocs.operation.QueryStringParser}
*/
public class QueryStringParser {
/**
* Parses the query string of the given {@code uri} and returns the resulting
* {@link Parameters}.
*
* @param uri the uri to parse
* @return the parameters parsed from the query string
*/
public Parameters parse(URI uri) {
String query = uri.getRawQuery();
if (query != null) {
return parse(query);
}
return new Parameters();
}
private Parameters parse(String query) {
Parameters parameters = new Parameters();
try (Scanner scanner = new Scanner(query)) {
scanner.useDelimiter("&");
while (scanner.hasNext()) {
processParameter(scanner.next(), parameters);
}
}
return parameters;
}
private void processParameter(String parameter, Parameters parameters) {
String[] components = parameter.split("=");
if (components.length > 0 && components.length < 3) {
if (components.length == 2) {
String name = components[0];
String value = components[1];
parameters.add(decode(name), decode(value));
}
else {
List<String> values = parameters.get(components[0]);
if (values == null) {
parameters.put(components[0], new LinkedList<String>());
}
}
}
else {
throw new IllegalArgumentException(
"The parameter '" + parameter + "' is malformed");
}
}
private String decode(String encoded) {
try {
return URLDecoder.decode(encoded, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException(
"Unable to URL encode " + encoded + " using UTF-8", ex);
}
}
@Deprecated
public class QueryStringParser
extends org.springframework.restdocs.operation.QueryStringParser {
}

View File

@@ -22,7 +22,6 @@ import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import org.springframework.restdocs.cli.QueryStringParser;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.StringUtils;

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2014-2016 the original author or authors.
*
* Licensed 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.springframework.restdocs.operation;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* A parser for the query string of a URI.
*
* @author Andy Wilkinson
*/
public class QueryStringParser {
/**
* Parses the query string of the given {@code uri} and returns the resulting
* {@link Parameters}.
*
* @param uri the uri to parse
* @return the parameters parsed from the query string
*/
public Parameters parse(URI uri) {
String query = uri.getRawQuery();
if (query != null) {
return parse(query);
}
return new Parameters();
}
private Parameters parse(String query) {
Parameters parameters = new Parameters();
try (Scanner scanner = new Scanner(query)) {
scanner.useDelimiter("&");
while (scanner.hasNext()) {
processParameter(scanner.next(), parameters);
}
}
return parameters;
}
private void processParameter(String parameter, Parameters parameters) {
String[] components = parameter.split("=");
if (components.length > 0 && components.length < 3) {
if (components.length == 2) {
String name = components[0];
String value = components[1];
parameters.add(decode(name), decode(value));
}
else {
List<String> values = parameters.get(components[0]);
if (values == null) {
parameters.put(components[0], new LinkedList<String>());
}
}
}
else {
throw new IllegalArgumentException(
"The parameter '" + parameter + "' is malformed");
}
}
private String decode(String encoded) {
try {
return URLDecoder.decode(encoded, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException(
"Unable to URL encode " + encoded + " using UTF-8", ex);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.restdocs.cli;
package org.springframework.restdocs.operation;
import java.net.URI;
import java.util.Arrays;
@@ -23,8 +23,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.operation.Parameters;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasEntry;