You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2019/12/07 19:09:05 UTC
[cxf] branch 3.2.x-fixes updated (f648ac8 -> cec69f9)
This is an automated email from the ASF dual-hosted git repository.
reta pushed a change to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git.
from f648ac8 Recording .gitmergeinfo Changes
new b0d638f [CXF-8170] Parse HTTP link headers (#610)
new cec69f9 CXF-8170: Parse HTTP link headers. Adding support for whitespace(s) and more tests
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/apache/cxf/jaxrs/impl/ResponseImpl.java | 48 +++++++++++++++----
.../apache/cxf/jaxrs/impl/ResponseImplTest.java | 56 ++++++++++++++++++++++
2 files changed, 96 insertions(+), 8 deletions(-)
[cxf] 02/02: CXF-8170: Parse HTTP link headers. Adding support for
whitespace(s) and more tests
Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
commit cec69f9ba648533245cbd8db319d0da942622093
Author: reta <dr...@gmail.com>
AuthorDate: Sat Dec 7 12:40:33 2019 -0500
CXF-8170: Parse HTTP link headers. Adding support for whitespace(s) and more tests
(cherry picked from commit 056f0f1f845e87575208289765a81344aadc8ef4)
(cherry picked from commit af61934f606c99e8aa86baad813834602ae775d0)
---
.../org/apache/cxf/jaxrs/impl/ResponseImpl.java | 2 +-
.../apache/cxf/jaxrs/impl/ResponseImplTest.java | 32 ++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
index 6a9669e..1dac06b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
@@ -65,7 +65,7 @@ import org.apache.cxf.message.MessageUtils;
public final class ResponseImpl extends Response {
- private static final Pattern LINK_DELIMITER = Pattern.compile(",(?=\\<|$)");
+ private static final Pattern LINK_DELIMITER = Pattern.compile(",\\s*(?=\\<|$)");
private StatusType status;
private Object entity;
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
index 2e7f99f..63b02b1 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
@@ -464,6 +464,38 @@ public class ResponseImplTest extends Assert {
assertTrue(links.contains(Link.fromUri("http://prev").rel("prev").build()));
}
}
+
+ @Test
+ public void testGetLinksMultipleMultiline() {
+ try (ResponseImpl ri = new ResponseImpl(200)) {
+ MetadataMap<String, Object> meta = new MetadataMap<>();
+ ri.addMetadata(meta);
+
+ final Message outMessage = createMessage();
+ outMessage.put(Message.REQUEST_URI, "http://localhost");
+ ri.setOutMessage(outMessage);
+
+ Set<Link> links = ri.getLinks();
+ assertTrue(links.isEmpty());
+
+ meta.add(HttpHeaders.LINK,
+ "</TheBook/chapter2>;\n"
+ + " rel=\"prev\", \n"
+ + " </TheBook/chapter4>;\n"
+ + " rel=\"next\";");
+
+ assertTrue(ri.hasLink("next"));
+ Link next = ri.getLink("next");
+ assertNotNull(next);
+ assertTrue(ri.hasLink("prev"));
+ Link prev = ri.getLink("prev");
+ assertNotNull(prev);
+
+ links = ri.getLinks();
+ assertTrue(links.contains(Link.fromUri("http://localhost/TheBook/chapter4").rel("next").build()));
+ assertTrue(links.contains(Link.fromUri("http://localhost/TheBook/chapter2").rel("prev").build()));
+ }
+ }
public static class StringBean {
private String header;
[cxf] 01/02: [CXF-8170] Parse HTTP link headers (#610)
Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
commit b0d638f75eae19bdce3a66d88fa0552595f2287a
Author: Ben <bm...@gmail.com>
AuthorDate: Fri Dec 6 23:28:32 2019 +0100
[CXF-8170] Parse HTTP link headers (#610)
- if multiple, comma separated links are present in a single header, split up by comma and parse each.
- static final Pattern from open-liberty, see https://github.com/OpenLiberty/open-liberty/pull/8956/commits/e159898d1c9a230f5d71b82414874204a1a66715
Co-authored-by: Adam Anderson <at...@gmail.com>
Signed-off-by: Benjamin Marwell <bm...@gmail.com>
---
.../org/apache/cxf/jaxrs/impl/ResponseImpl.java | 48 ++++++++++++++++++----
.../apache/cxf/jaxrs/impl/ResponseImplTest.java | 24 +++++++++++
2 files changed, 64 insertions(+), 8 deletions(-)
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
index 68aa78f..6a9669e 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
@@ -37,6 +37,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Pattern;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.ResponseProcessingException;
@@ -64,6 +65,8 @@ import org.apache.cxf.message.MessageUtils;
public final class ResponseImpl extends Response {
+ private static final Pattern LINK_DELIMITER = Pattern.compile(",(?=\\<|$)");
+
private StatusType status;
private Object entity;
private Annotation[] entityAnnotations;
@@ -255,10 +258,17 @@ public final class ResponseImpl extends Response {
List<Object> linkValues = metadata.get(HttpHeaders.LINK);
if (linkValues != null) {
for (Object o : linkValues) {
- Link link = o instanceof Link ? (Link)o : Link.valueOf(o.toString());
- if (relation.equals(link.getRel())) {
+ if (o instanceof Link && relation.equals(((Link)o).getRel())) {
return true;
}
+
+ String[] links = LINK_DELIMITER.split(o.toString());
+ for (String splitLink : links) {
+ Link link = Link.valueOf(splitLink);
+ if (relation.equals(link.getRel())) {
+ return true;
+ }
+ }
}
}
return false;
@@ -290,16 +300,38 @@ public final class ResponseImpl extends Response {
}
Set<Link> links = new LinkedHashSet<Link>();
for (Object o : linkValues) {
- Link link = o instanceof Link ? (Link)o : Link.valueOf(o.toString());
- if (!link.getUri().isAbsolute()) {
- URI requestURI = URI.create((String)outMessage.get(Message.REQUEST_URI));
- link = Link.fromLink(link).baseUri(requestURI).build();
- }
- links.add(link);
+ List<Link> parsedLinks = parseLink(o);
+
+ links.addAll(parsedLinks);
}
return links;
}
+ private Link makeAbsoluteLink(Link link) {
+ if (!link.getUri().isAbsolute()) {
+ URI requestURI = URI.create((String)outMessage.get(Message.REQUEST_URI));
+ link = Link.fromLink(link).baseUri(requestURI).build();
+ }
+
+ return link;
+ }
+
+ private List<Link> parseLink(Object o) {
+ if (o instanceof Link) {
+ return Collections.singletonList(makeAbsoluteLink((Link) o));
+ }
+
+ List<Link> links = new ArrayList<>();
+ String[] linkArray = LINK_DELIMITER.split(o.toString());
+
+ for (String textLink : linkArray) {
+ Link link = Link.valueOf(textLink);
+ links.add(makeAbsoluteLink(link));
+ }
+
+ return Collections.unmodifiableList(links);
+ }
+
public <T> T readEntity(Class<T> cls) throws ProcessingException, IllegalStateException {
return readEntity(cls, new Annotation[]{});
}
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
index 687ac23..2e7f99f 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
@@ -441,6 +441,30 @@ public class ResponseImplTest extends Assert {
}
}
+ @Test
+ public void testGetLinksMultiple() {
+ try (ResponseImpl ri = new ResponseImpl(200)) {
+ MetadataMap<String, Object> meta = new MetadataMap<>();
+ ri.addMetadata(meta);
+
+ Set<Link> links = ri.getLinks();
+ assertTrue(links.isEmpty());
+
+ meta.add(HttpHeaders.LINK, "<http://next>;rel=\"next\",<http://prev>;rel=\"prev\"");
+
+ assertTrue(ri.hasLink("next"));
+ Link next = ri.getLink("next");
+ assertNotNull(next);
+ assertTrue(ri.hasLink("prev"));
+ Link prev = ri.getLink("prev");
+ assertNotNull(prev);
+
+ links = ri.getLinks();
+ assertTrue(links.contains(Link.fromUri("http://next").rel("next").build()));
+ assertTrue(links.contains(Link.fromUri("http://prev").rel("prev").build()));
+ }
+ }
+
public static class StringBean {
private String header;