Skip to content

Commit 0eac803

Browse files
committed
Update WebServiceProxy.
1 parent 66df3fc commit 0eac803

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

.idea/dataSources.local.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '4.8'
17+
version = '4.8.1'
1818

1919
apply plugin: 'java-library'
2020

kilo-client/src/main/java/org/httprpc/kilo/WebServiceProxy.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,6 @@ public Object invoke(Object proxy, Method method, Object[] arguments) throws Thr
342342

343343
var webServiceProxy = new WebServiceProxy(requestMethod.value(), uri);
344344

345-
webServiceProxy.setHeaders(headers);
346-
347345
var parameters = method.getParameters();
348346

349347
var n = parameters.length;
@@ -370,6 +368,8 @@ public Object invoke(Object proxy, Method method, Object[] arguments) throws Thr
370368

371369
webServiceProxy.setArguments(argumentMap);
372370

371+
webServiceProxy.setHeaders(headers);
372+
373373
if (n < parameters.length) {
374374
var body = argumentList.get(n);
375375

@@ -446,8 +446,8 @@ private static <T> T instantiate(Class<T> type) {
446446
private String method;
447447
private URI uri;
448448

449-
private Map<String, Object> headers = mapOf();
450449
private Map<String, Object> arguments = mapOf();
450+
private Map<String, Object> headers = mapOf();
451451

452452
private Object body = null;
453453

@@ -509,51 +509,51 @@ public WebServiceProxy(String method, URI uri) {
509509
}
510510

511511
/**
512-
* Returns the header map.
512+
* Returns the argument map.
513513
*
514514
* @return
515-
* The header map.
515+
* The argument map.
516516
*/
517-
public Map<String, Object> getHeaders() {
518-
return headers;
517+
public Map<String, Object> getArguments() {
518+
return arguments;
519519
}
520520

521521
/**
522-
* Sets the header map.
522+
* Sets the argument map.
523523
*
524-
* @param headers
525-
* The header map.
524+
* @param arguments
525+
* The argument map.
526526
*/
527-
public void setHeaders(Map<String, Object> headers) {
528-
if (headers == null) {
527+
public void setArguments(Map<String, Object> arguments) {
528+
if (arguments == null) {
529529
throw new IllegalArgumentException();
530530
}
531531

532-
this.headers = headers;
532+
this.arguments = arguments;
533533
}
534534

535535
/**
536-
* Returns the argument map.
536+
* Returns the header map.
537537
*
538538
* @return
539-
* The argument map.
539+
* The header map.
540540
*/
541-
public Map<String, Object> getArguments() {
542-
return arguments;
541+
public Map<String, Object> getHeaders() {
542+
return headers;
543543
}
544544

545545
/**
546-
* Sets the argument map.
546+
* Sets the header map.
547547
*
548-
* @param arguments
549-
* The argument map.
548+
* @param headers
549+
* The header map.
550550
*/
551-
public void setArguments(Map<String, Object> arguments) {
552-
if (arguments == null) {
551+
public void setHeaders(Map<String, Object> headers) {
552+
if (headers == null) {
553553
throw new IllegalArgumentException();
554554
}
555555

556-
this.arguments = arguments;
556+
this.headers = headers;
557557
}
558558

559559
/**
@@ -735,14 +735,18 @@ public Object invoke() throws IOException {
735735

736736
// Append query
737737
if (!arguments.isEmpty()) {
738+
if (uri.getRawQuery() != null || uri.getRawFragment() != null) {
739+
throw new IllegalStateException("Query or fragment is already defined.");
740+
}
741+
738742
try {
739743
uri = new URI(String.format("%s?%s", uri, encodeQuery(arguments)));
740744
} catch (URISyntaxException exception) {
741745
throw new IllegalStateException(exception.getMessage());
742746
}
743747
}
744748

745-
// Open URL connection
749+
// Open connection
746750
var connection = (HttpURLConnection)uri.toURL().openConnection();
747751

748752
connection.setRequestMethod(method);
@@ -756,7 +760,7 @@ public Object invoke() throws IOException {
756760
locale.getLanguage().toLowerCase(),
757761
locale.getCountry().toLowerCase()));
758762

759-
// Apply custom headers
763+
// Apply headers
760764
for (Map.Entry<String, ?> entry : headers.entrySet()) {
761765
var key = entry.getKey();
762766
var value = entry.getValue();

kilo-test/src/test/java/org/httprpc/kilo/test/DocumentationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ private void testDocumentation(String name) throws IOException {
4444

4545
var webServiceProxy = new WebServiceProxy("GET", baseURI.resolve(name));
4646

47-
webServiceProxy.setHeaders(mapOf(
48-
entry("Accept", "application/json")
49-
));
50-
5147
webServiceProxy.setArguments(mapOf(
5248
entry("api", "json")
5349
));
5450

51+
webServiceProxy.setHeaders(mapOf(
52+
entry("Accept", "application/json")
53+
));
54+
5555
var actual = webServiceProxy.invoke();
5656

5757
assertEquals(expected, actual);

kilo-test/src/test/java/org/httprpc/kilo/test/PetsTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ private void testPetsStreamJSON() throws IOException {
6868

6969
var webServiceProxy = new WebServiceProxy("GET", baseURI.resolve("pets/stream"));
7070

71-
webServiceProxy.setHeaders(mapOf(
72-
entry("Accept", "application/json")
73-
));
74-
7571
webServiceProxy.setArguments(mapOf(
7672
entry("owner", "Gwen")
7773
));
7874

75+
webServiceProxy.setHeaders(mapOf(
76+
entry("Accept", "application/json")
77+
));
78+
7979
var actual = webServiceProxy.invoke();
8080

8181
assertEquals(expected, actual);
@@ -91,14 +91,14 @@ private void testPetsStreamCSV() throws IOException {
9191

9292
var webServiceProxy = new WebServiceProxy("GET", baseURI.resolve("pets/stream"));
9393

94-
webServiceProxy.setHeaders(mapOf(
95-
entry("Accept", "text/csv")
96-
));
97-
9894
webServiceProxy.setArguments(mapOf(
9995
entry("owner", "Gwen")
10096
));
10197

98+
webServiceProxy.setHeaders(mapOf(
99+
entry("Accept", "text/csv")
100+
));
101+
102102
webServiceProxy.setResponseHandler((inputStream, contentType) -> {
103103
var textDecoder = new TextDecoder();
104104

@@ -129,14 +129,14 @@ private void testPetsStreamMarkup(String name, String mimeType) throws IOExcepti
129129

130130
var webServiceProxy = new WebServiceProxy("GET", baseURI.resolve("pets/stream"));
131131

132-
webServiceProxy.setHeaders(mapOf(
133-
entry("Accept", mimeType)
134-
));
135-
136132
webServiceProxy.setArguments(mapOf(
137133
entry("owner", "Gwen")
138134
));
139135

136+
webServiceProxy.setHeaders(mapOf(
137+
entry("Accept", mimeType)
138+
));
139+
140140
webServiceProxy.setResponseHandler((inputStream, contentType) -> {
141141
var textDecoder = new TextDecoder();
142142

0 commit comments

Comments
 (0)