Skip to content

Commit 14dcdb6

Browse files
committed
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
1 parent 5c6cf7f commit 14dcdb6

File tree

4 files changed

+98
-73
lines changed

4 files changed

+98
-73
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/QueryStringParser.java

+5-68
Original file line numberDiff line numberDiff line change
@@ -16,78 +16,15 @@
1616

1717
package org.springframework.restdocs.cli;
1818

19-
import java.io.UnsupportedEncodingException;
20-
import java.net.URI;
21-
import java.net.URLDecoder;
22-
import java.util.LinkedList;
23-
import java.util.List;
24-
import java.util.Scanner;
25-
26-
import org.springframework.restdocs.operation.Parameters;
27-
2819
/**
2920
* A parser for the query string of a URI.
3021
*
3122
* @author Andy Wilkinson
23+
* @deprecated since 1.1.2 in favor of
24+
* {@link org.springframework.restdocs.operation.QueryStringParser}
3225
*/
33-
public class QueryStringParser {
34-
35-
/**
36-
* Parses the query string of the given {@code uri} and returns the resulting
37-
* {@link Parameters}.
38-
*
39-
* @param uri the uri to parse
40-
* @return the parameters parsed from the query string
41-
*/
42-
public Parameters parse(URI uri) {
43-
String query = uri.getRawQuery();
44-
if (query != null) {
45-
return parse(query);
46-
}
47-
return new Parameters();
48-
}
49-
50-
private Parameters parse(String query) {
51-
Parameters parameters = new Parameters();
52-
try (Scanner scanner = new Scanner(query)) {
53-
scanner.useDelimiter("&");
54-
while (scanner.hasNext()) {
55-
processParameter(scanner.next(), parameters);
56-
}
57-
}
58-
return parameters;
59-
}
60-
61-
private void processParameter(String parameter, Parameters parameters) {
62-
String[] components = parameter.split("=");
63-
if (components.length > 0 && components.length < 3) {
64-
if (components.length == 2) {
65-
String name = components[0];
66-
String value = components[1];
67-
parameters.add(decode(name), decode(value));
68-
}
69-
else {
70-
List<String> values = parameters.get(components[0]);
71-
if (values == null) {
72-
parameters.put(components[0], new LinkedList<String>());
73-
}
74-
}
75-
}
76-
else {
77-
throw new IllegalArgumentException(
78-
"The parameter '" + parameter + "' is malformed");
79-
}
80-
}
81-
82-
private String decode(String encoded) {
83-
try {
84-
return URLDecoder.decode(encoded, "UTF-8");
85-
}
86-
catch (UnsupportedEncodingException ex) {
87-
throw new IllegalStateException(
88-
"Unable to URL encode " + encoded + " using UTF-8", ex);
89-
}
90-
91-
}
26+
@Deprecated
27+
public class QueryStringParser
28+
extends org.springframework.restdocs.operation.QueryStringParser {
9229

9330
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/Parameters.java

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25-
import org.springframework.restdocs.cli.QueryStringParser;
2625
import org.springframework.util.LinkedMultiValueMap;
2726
import org.springframework.util.StringUtils;
2827

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2014-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.operation;
18+
19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URI;
21+
import java.net.URLDecoder;
22+
import java.util.LinkedList;
23+
import java.util.List;
24+
import java.util.Scanner;
25+
26+
/**
27+
* A parser for the query string of a URI.
28+
*
29+
* @author Andy Wilkinson
30+
*/
31+
public class QueryStringParser {
32+
33+
/**
34+
* Parses the query string of the given {@code uri} and returns the resulting
35+
* {@link Parameters}.
36+
*
37+
* @param uri the uri to parse
38+
* @return the parameters parsed from the query string
39+
*/
40+
public Parameters parse(URI uri) {
41+
String query = uri.getRawQuery();
42+
if (query != null) {
43+
return parse(query);
44+
}
45+
return new Parameters();
46+
}
47+
48+
private Parameters parse(String query) {
49+
Parameters parameters = new Parameters();
50+
try (Scanner scanner = new Scanner(query)) {
51+
scanner.useDelimiter("&");
52+
while (scanner.hasNext()) {
53+
processParameter(scanner.next(), parameters);
54+
}
55+
}
56+
return parameters;
57+
}
58+
59+
private void processParameter(String parameter, Parameters parameters) {
60+
String[] components = parameter.split("=");
61+
if (components.length > 0 && components.length < 3) {
62+
if (components.length == 2) {
63+
String name = components[0];
64+
String value = components[1];
65+
parameters.add(decode(name), decode(value));
66+
}
67+
else {
68+
List<String> values = parameters.get(components[0]);
69+
if (values == null) {
70+
parameters.put(components[0], new LinkedList<String>());
71+
}
72+
}
73+
}
74+
else {
75+
throw new IllegalArgumentException(
76+
"The parameter '" + parameter + "' is malformed");
77+
}
78+
}
79+
80+
private String decode(String encoded) {
81+
try {
82+
return URLDecoder.decode(encoded, "UTF-8");
83+
}
84+
catch (UnsupportedEncodingException ex) {
85+
throw new IllegalStateException(
86+
"Unable to URL encode " + encoded + " using UTF-8", ex);
87+
}
88+
89+
}
90+
91+
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/QueryStringParserTests.java renamed to spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/QueryStringParserTests.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.restdocs.cli;
17+
package org.springframework.restdocs.operation;
1818

1919
import java.net.URI;
2020
import java.util.Arrays;
@@ -23,8 +23,6 @@
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
2525

26-
import org.springframework.restdocs.operation.Parameters;
27-
2826
import static org.hamcrest.CoreMatchers.equalTo;
2927
import static org.hamcrest.CoreMatchers.is;
3028
import static org.hamcrest.Matchers.hasEntry;

0 commit comments

Comments
 (0)