Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Changes log

- 3.0 Milestone 3 (month-day-2026)
- Breaking changes
- `new Tag()` and `new Tag(String)` now create a **strong** entity tag, as RFC 9110 section
8.8.3 defines it (a tag is strong unless it carries the `W/` prefix): they created a weak
tag, so a server doing `representation.setTag(new Tag("v1"))` sent `ETag: W/"v1"`, which
can never match an `If-Match` or `If-Range` condition (strong comparison). The `ETag`
header written by such a server loses its `W/` prefix. A tag that is really weak must be
created with `new Tag(value, true)` or the new `Tag.weak(String)`; `Tag.strong(String)` is
the explicit counterpart (#1694).
- `CacheDirective` no longer has the `digit` attribute: `isDigit()`, `setDigit(boolean)` and
the constructor `CacheDirective(String, String, boolean)` are removed (use
`new CacheDirective(name, value)`). The attribute had no effect on the header that is
Expand Down
12 changes: 5 additions & 7 deletions org.restlet/src/main/java/org/restlet/data/Conditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
* <br>
* The entity tags are compared as RFC 9110 section 13.1 requires: "If-Match" and "If-Range" use the
* strong comparison ({@link Tag#strongMatch(Tag)}, a weak tag never matches), "If-None-Match" uses
* the weak comparison ({@link Tag#weakMatch(Tag)}). Note that {@code new Tag(String)} creates a
* <b>weak</b> tag: use {@code new Tag(String, false)} for a resource that clients must be able to
* update with "If-Match" or to resume with "If-Range". The dates have a precision of one second,
* and an "If-Modified-Since" date later than the current time is ignored (RFC 2616 section 14.25,
* kept although RFC 9110 does not require it).
* the weak comparison ({@link Tag#weakMatch(Tag)}). A tag created with {@code new Tag(String)} is
* strong; a weak tag ({@link Tag#weak(String)}) never matches an "If-Match" or "If-Range"
* condition. The dates have a precision of one second, and an "If-Modified-Since" date later than
* the current time is ignored (RFC 2616 section 14.25, kept although RFC 9110 does not require it).
*
* @see <a href= "http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.24" >If-Match</a>
* @see <a href= "http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25"
Expand Down Expand Up @@ -80,8 +79,7 @@ public static Conditions ifAbsent() {
* least one second after the modification date (RFC 9110 section 8.8.2.2), otherwise two
* updates within the same second could not be told apart.<br>
* <br>
* Note that {@code new Tag(String)} creates a weak tag, which never matches an "If-Match"
* condition.<br>
* Note that a weak tag ({@link Tag#weak(String)}) never matches an "If-Match" condition.<br>
* <br>
* The date rule compares the modification date with the {@link Response#getDate() date of the
* response}. If the origin server sent an invalid "Date" header, the time of reception is used
Expand Down
36 changes: 31 additions & 5 deletions org.restlet/src/main/java/org/restlet/data/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,53 @@ public static Tag parse(String httpTag) {
return result;
}

/**
* Creates a strong tag: two representations with the same strong tag are identical byte for
* byte.
*
* @param opaqueTag The tag value.
* @return A new strong tag.
*/
public static Tag strong(String opaqueTag) {
return new Tag(opaqueTag, false);
}

/**
* Creates a weak tag: two representations with the same weak tag are only semantically
* equivalent. A weak tag never matches an "If-Match" or "If-Range" condition.
*
* @param opaqueTag The tag value.
* @return A new weak tag.
*/
public static Tag weak(String opaqueTag) {
return new Tag(opaqueTag, true);
}

/** The name. */
private final String name;

/** The tag weakness. */
private final boolean weak;

/**
* Default constructor. The opaque tag is set to null, and the weakness indicator is set to
* true.
* Default constructor. The opaque tag is set to null, and the tag is strong (RFC 9110 section
* 8.8.3: an entity tag is strong unless it carries the "W/" prefix).
*/
public Tag() {
this(null, true);
this(null, false);
}

/**
* Constructor of weak tags.
* Constructor of strong tags. Use {@link #Tag(String, boolean)} or {@link #weak(String)} to
* create a weak tag.<br>
* <br>
* Note that before Restlet 3.0, this constructor created a <b>weak</b> tag, which never matches
* an "If-Match" or "If-Range" condition (strong comparison).
*
* @param opaqueTag The tag value.
*/
public Tag(String opaqueTag) {
this(opaqueTag, true);
this(opaqueTag, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,13 @@ void getStatus_modifiedAfterIfModifiedSince_proceeds() {
conditions.setModifiedSince(T0);
assertNull(conditions.getStatus(Method.GET, true, null, AFTER));
}

/** A tag built with the default constructor is strong, so "If-Match" can match it (#1694). */
@Test
void getStatus_ifMatchWithTheDefaultTag_succeedsOnPut() {
Conditions conditions = new Conditions();
conditions.setMatch(Collections.singletonList(new Tag("v1")));

assertNull(conditions.getStatus(Method.PUT, true, new Tag("v1"), null));
}
}
34 changes: 34 additions & 0 deletions org.restlet/src/test/java/org/restlet/data/TagTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,38 @@ void hashSet_distinguishesWeakAndStrongTags() {
assertTrue(tags.contains(new Tag("1", false)));
assertFalse(tags.contains(WEAK_1));
}

/**
* RFC 9110 section 8.8.3: an entity tag is strong unless it carries the "W/" prefix (#1694).
*/
@Test
void newTag_isStrongByDefault() {
assertFalse(new Tag("v1").isWeak());
assertFalse(new Tag().isWeak());
assertEquals("\"v1\"", new Tag("v1").format());
assertTrue(new Tag("v1").strongMatch(new Tag("v1")));
}

@Test
void explicitWeakness_isKept() {
assertTrue(new Tag("v1", true).isWeak());
assertFalse(new Tag("v1", false).isWeak());
assertEquals("W/\"v1\"", new Tag("v1", true).format());
}

@Test
void factories_createStrongAndWeakTags() {
assertFalse(Tag.strong("v1").isWeak());
assertTrue(Tag.weak("v1").isWeak());
assertEquals(new Tag("v1", false), Tag.strong("v1"));
assertEquals(new Tag("v1", true), Tag.weak("v1"));
assertFalse(Tag.strong("v1").strongMatch(Tag.weak("v1")));
assertTrue(Tag.strong("v1").weakMatch(Tag.weak("v1")));
}

@Test
void parse_andFormat_roundTripWithTheDefaultTag() {
assertEquals(new Tag("v1"), Tag.parse(new Tag("v1").format()));
assertEquals(Tag.weak("v1"), Tag.parse(Tag.weak("v1").format()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright 2005-2026 Qlik
*<p>
* The content of this file is subject to the terms of the Apache 2.0 open
* source license available at https://www.opensource.org/licenses/apache-2.0
*<p>
* Restlet is a registered trademark of QlikTech International AB.
*/
package org.restlet.engine.connector;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.restlet.Context;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.data.Tag;
import org.restlet.engine.Engine;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Finder;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

/**
* Tests, on the wire, that a tag created with {@code new Tag(String)} is written as a strong entity
* tag, so that the concurrency control of "If-Match" works (RFC 9110 sections 8.8.3 and 13.1.1,
* #1694).
*/
class StrongTagWireTestCase {

private static final int TIMEOUT_MS = 10_000;

/** Resource whose representation has a tag made with the default constructor. */
public static class VersionedResource extends ServerResource {

@Get
public Representation read() {
StringRepresentation result = new StringRepresentation("body");
result.setTag(new Tag("v1"));
return result;
}

@Put
public void replace(Representation entity) {
// Accepted.
}
}

private Server server;

@BeforeEach
void setUp() throws Exception {
Engine.clearThreadLocalVariables();
Engine.register();
Engine.getInstance().getRegisteredServers().add(new HttpServerHelper(null));

Context context = new Context();
server =
new Server(context, Protocol.HTTP, 0, new Finder(context, VersionedResource.class));
server.start();
}

@AfterEach
void tearDown() throws Exception {
if (server != null) {
server.stop();
}
Engine.clearThreadLocalVariables();
}

private String send(String method, String extraHeaders) throws IOException {
try (Socket socket = new Socket("localhost", server.getActualPort())) {
socket.setSoTimeout(TIMEOUT_MS);
OutputStream out = socket.getOutputStream();
out.write(
(method
+ " / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\n"
+ "Content-Type: text/plain\r\n"
+ extraHeaders
+ "Connection: close\r\n\r\nbody")
.getBytes(StandardCharsets.ISO_8859_1));
out.flush();
return new String(socket.getInputStream().readAllBytes(), StandardCharsets.ISO_8859_1);
}
}

@Test
void theEtagHeader_ofADefaultTag_isStrong() throws Exception {
String response = send("GET", "");

String lower = response.toLowerCase(Locale.ROOT);
assertTrue(lower.contains("\r\netag: \"v1\"\r\n"), response);
}

@Test
void ifMatch_withTheTagOfADefaultTag_letsTheWriteThrough() throws Exception {
String response = send("PUT", "If-Match: \"v1\"\r\n");

assertTrue(response.startsWith("HTTP/1.1 2"), response);
}
}