diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/Path.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/Path.java index 302a42bfa..723cbc437 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/Path.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/Path.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 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. @@ -15,6 +15,8 @@ */ package org.springframework.data.rest.core; +import java.util.regex.Pattern; + import org.springframework.util.StringUtils; /** @@ -49,8 +51,14 @@ public class Path { this.path = cleanUp ? cleanUp(path) : path; } + /** + * Returns whether the given reference String matches the current {@link Path}. + * + * @param reference + * @return + */ public boolean matches(String reference) { - return this.path.matches(String.format(MATCH_PATTERN, reference)); + return reference == null ? false : this.path.matches(String.format(MATCH_PATTERN, Pattern.quote(reference))); } /** diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/PathUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/PathUnitTests.java index f6ca513fc..1d02f4262 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/PathUnitTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/PathUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 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. @@ -67,4 +67,20 @@ public class PathUnitTests { public void doesNotPrefixAbsoluteUris() { assertThat(new Path("http://localhost").toString(), is("http://localhost")); } + + /** + * @see DATAREST-222 + */ + @Test + public void doesNotMatchIfReferenceContainsReservedCharacters() { + assertThat(new Path("/foobar").matches("barfoo{?foo}"), is(false)); + } + + /** + * @see DATAREST-222 + */ + @Test + public void doesNotMatchNullReference() { + assertThat(new Path("/foobar").matches(null), is(false)); + } }