Skip to content

Commit a96bf19

Browse files
author
Achim Kraus
committed
Clear URI-path or -query options on setURI.
Issue eclipse-californium#1894 Signed-off-by: Achim Kraus <[email protected]>
1 parent 7ab0a48 commit a96bf19

File tree

3 files changed

+14
-62
lines changed

3 files changed

+14
-62
lines changed

californium-core/src/main/java/org/eclipse/californium/core/coap/OptionSet.java

+1-18
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ public final class OptionSet {
8888
// Arbitrary options
8989
private List<Option> others;
9090

91-
/**
92-
* {@code true}, if URI-path or URI-query are set independent from
93-
* {@link Request#setURI}. Preserve them from being cleaned up, if the URI
94-
* doesn't contain them.
95-
*/
96-
private boolean explicitUriOptions;
97-
9891
/**
9992
* Creates an empty set of options.
10093
* <p>
@@ -307,7 +300,7 @@ public OptionSet clearIfMatchs() {
307300
/**
308301
* Gets the string value of the Uri-Host option.
309302
*
310-
* @return the Uri-Host or null if the option is not present
303+
* @return the Uri-Host, or {@code null}, if the option is not present
311304
*/
312305
public String getUriHost() {
313306
return uri_host;
@@ -691,7 +684,6 @@ public OptionSet setUriPath(String path) {
691684
public OptionSet addUriPath(String segment) {
692685
checkOptionValue(OptionNumberRegistry.URI_PATH, segment);
693686
getUriPath().add(segment);
694-
this.explicitUriOptions = true;
695687
return this;
696688
}
697689

@@ -886,7 +878,6 @@ public OptionSet setUriQuery(String query) {
886878
public OptionSet addUriQuery(String argument) {
887879
checkOptionValue(OptionNumberRegistry.URI_QUERY, argument);
888880
getUriQuery().add(argument);
889-
this.explicitUriOptions = true;
890881
return this;
891882
}
892883

@@ -1604,14 +1595,6 @@ public List<Option> asSortedList() {
16041595
return options;
16051596
}
16061597

1607-
boolean hasExplicitUriOptions() {
1608-
return explicitUriOptions;
1609-
}
1610-
1611-
void resetExplicitUriOptions() {
1612-
explicitUriOptions = false;
1613-
}
1614-
16151598
/**
16161599
* Add options.
16171600
*

californium-core/src/main/java/org/eclipse/californium/core/coap/Request.java

+11-42
Original file line numberDiff line numberDiff line change
@@ -411,21 +411,12 @@ public Request setProxyScheme(String proxyScheme) {
411411
* options directly in the optons-set using {@link #getOptions()}.
412412
* </p>
413413
* <p>
414-
* Note: if the URI contains a hostname, a DNS lookup may be used to
415-
* resolve the address. That may block for a unknown time. To prevent from
416-
* that, the address may be resolved ahead by {@link InetAddress#getByName(String)}.
414+
* Note: if the URI contains a hostname, a DNS lookup may be used to resolve
415+
* the address. That may block for a unknown time. To prevent from that, the
416+
* address may be resolved ahead by {@link InetAddress#getByName(String)}.
417417
* </p>
418-
* Note: if uri-path or uri-query option was set explicitly before, they are
419-
* not cleaned up, if the URI doesn't contain that part. e.g.
420-
* {@code request.getOptions().setUriQuery("param=2")} and
421-
* {@code request.setURI("coap://host/path")} results in
422-
* {@code "coap://host/path?param=2"}. But
423-
* {@code request.getOptions().setUriQuery("param=2")} and
424-
* {@code request.setURI("coap://host/path?mark")} results in
425-
* {@code "coap://host/path?mark"}.
426-
*
427-
* That will be removed in the next major version! Don't set uri-path or
428-
* uri-query options before the URI!
418+
* Note: since 3.3, uri-path or uri-query options, if required, must be set
419+
* after the URI! The URI will clean them, if not contained in the URI.
429420
*
430421
* Provides a fluent API to chain setters.
431422
*
@@ -473,17 +464,8 @@ public Request setURI(final String uri) {
473464
* resolve the address. That may block for a unknown time. To prevent from
474465
* that, the address may be resolved ahead by {@link InetAddress#getByName(String)}.
475466
* <p>
476-
* Note: if uri-path or uri-query option was set explicitly before, they are
477-
* not cleaned up, if the URI doesn't contain that part. e.g.
478-
* {@code request.getOptions().setUriQuery("param=2")} and
479-
* {@code request.setURI("coap://host/path")} results in
480-
* {@code "coap://host/path?param=2"}. But
481-
* {@code request.getOptions().setUriQuery("param=2")} and
482-
* {@code request.setURI("coap://host/path?mark")} results in
483-
* {@code "coap://host/path?mark"}.
484-
*
485-
* That will be removed in the next major version! Don't set uri-path or
486-
* uri-query options before the URI!
467+
* Note: since 3.3, uri-path or uri-query options, if required, must be set
468+
* after the URI! The URI will clean them, if not contained in the URI.
487469
*
488470
* Provides a fluent API to chain setters.
489471
*
@@ -537,17 +519,8 @@ public Request setURI(final URI uri) {
537519
* strict proxy/CoAP URI exclusion for backwards compatibility, set the
538520
* options directly in the optons-set using {@link #getOptions()}.
539521
* </p>
540-
* Note: if uri-path or uri-query option was set explicitly before, they are
541-
* not cleaned up, if the URI doesn't contain that part. e.g.
542-
* {@code request.getOptions().setUriQuery("param=2")} and
543-
* {@code request.setURI("coap://host/path")} results in
544-
* {@code "coap://host/path?param=2"}. But
545-
* {@code request.getOptions().setUriQuery("param=2")} and
546-
* {@code request.setURI("coap://host/path?mark")} results in
547-
* {@code "coap://host/path?mark"}.
548-
*
549-
* That will be removed in the next major version! Don't set uri-path or
550-
* uri-query options before the URI!
522+
* Note: since 3.3, uri-path or uri-query options, if required, must be set
523+
* after the URI! The URI will clean them, if not contained in the URI.
551524
*
552525
* Provides a fluent API to chain setters.
553526
*
@@ -620,7 +593,6 @@ private void setOptionsInternal(URI uri, InetSocketAddress destination, boolean
620593
throw new NullPointerException("destination address must not be null!");
621594
}
622595
OptionSet options = getOptions();
623-
boolean explicitUriOption = options.hasExplicitUriOptions();
624596
String host = uri.getHost();
625597

626598
if (host != null) {
@@ -670,19 +642,16 @@ private void setOptionsInternal(URI uri, InetSocketAddress destination, boolean
670642
String path = uri.getPath();
671643
if (path != null && path.length() > 1) {
672644
options.setUriPath(path);
673-
} else if (!explicitUriOption) {
645+
} else {
674646
options.clearUriPath();
675647
}
676648
// set Uri-Query options
677649
String query = uri.getQuery();
678650
if (query != null) {
679651
options.setUriQuery(query);
680-
} else if (!explicitUriOption) {
652+
} else {
681653
options.clearUriQuery();
682654
}
683-
if (!explicitUriOption) {
684-
options.resetExplicitUriOptions();
685-
}
686655
}
687656

688657
/**

californium-core/src/test/java/org/eclipse/californium/core/coap/RequestTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,11 @@ public void testSetURITwice() throws UnknownHostException {
274274
assertThat(req.getOptions().getUriPathString(), is("test2"));
275275
assertThat(req.getOptions().getURIQueryCount(), is(0));
276276

277-
// only for 2.x.y, in 3.x.y the uri-query option must be set after the URI
277+
// since 3.3, a preset uri-query option is cleared by the URI
278278
req.getOptions().addUriQuery("param2");
279279
req.setURI("coap://192.168.0.1/test2");
280280
assertThat(req.getOptions().getUriPathString(), is("test2"));
281-
assertThat(req.getOptions().getUriQueryString(), is("param2"));
281+
assertThat(req.getOptions().getURIQueryCount(), is(0));
282282
}
283283

284284
@Test(expected = IllegalStateException.class)

0 commit comments

Comments
 (0)