diff --git a/src/main/java/org/springframework/hateoas/Links.java b/src/main/java/org/springframework/hateoas/Links.java index 178a90cd..f58554b4 100644 --- a/src/main/java/org/springframework/hateoas/Links.java +++ b/src/main/java/org/springframework/hateoas/Links.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.springframework.util.StringUtils; @@ -30,6 +32,8 @@ import org.springframework.util.StringUtils; */ public class Links implements Iterable { + private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("(<[^>]*>;rel=\"[^\"]*\")"); + static final Links NO_LINKS = new Links(Collections. emptyList()); private final List links; @@ -109,12 +113,12 @@ public class Links implements Iterable { return NO_LINKS; } - String[] elements = source.split(","); - List links = new ArrayList(elements.length); + Matcher matcher = LINK_HEADER_PATTERN.matcher(source); + List links = new ArrayList(); - for (String element : elements) { + while (matcher.find()) { - Link link = Link.valueOf(element); + Link link = Link.valueOf(matcher.group()); if (link != null) { links.add(link); @@ -122,6 +126,7 @@ public class Links implements Iterable { } return new Links(links); + } /** diff --git a/src/test/java/org/springframework/hateoas/LinksUnitTest.java b/src/test/java/org/springframework/hateoas/LinksUnitTest.java index c93dd012..b16004af 100644 --- a/src/test/java/org/springframework/hateoas/LinksUnitTest.java +++ b/src/test/java/org/springframework/hateoas/LinksUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ public class LinksUnitTest { static final String FIRST = ";rel=\"foo\""; static final String SECOND = ";rel=\"bar\""; + static final String WITH_COMMA = ";rel=\"foo\""; static final String LINKS = StringUtils.collectionToCommaDelimitedString(Arrays.asList(FIRST, SECOND)); @@ -60,4 +61,20 @@ public class LinksUnitTest { public void getSingleLinkByRel() { assertThat(reference.getLink("bar"), is(new Link("/somethingElse", "bar"))); } + + /** + * @see #440 + */ + @Test + public void parsesLinkWithComma() { + + Link withComma = new Link("http://localhost:8080/test?page=0&filter=foo,bar", "foo"); + + assertThat(Links.valueOf(WITH_COMMA).getLink("foo"), is(withComma)); + + Links twoWithCommaInFirst = Links.valueOf(WITH_COMMA.concat(",").concat(SECOND)); + + assertThat(twoWithCommaInFirst.getLink("foo"), is(withComma)); + assertThat(twoWithCommaInFirst.getLink("bar"), is(new Link("/somethingElse", "bar"))); + } }