Apache Solr open-source search software
SOLR-6163: Correctly decode special characters in managed stopwords and synonym endpoints.
Timothy Potterc119cf89a462
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 6cbeb1fc6bc..9c09ebb0b86 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -238,6 +238,9 @@ Bug Fixes* SOLR-6264: Distributed commit and optimize are executed serially across allreplicas. (Mark Miller, Timothy Potter)+* SOLR-6163: Correctly decode special characters in managed stopwords and synonym endpoints.+ (Vitaliy Zhovtyuk, Timo Schmidt via Timothy Potter)+Optimizations---------------------diff --git a/solr/core/src/java/org/apache/solr/rest/RestManager.java b/solr/core/src/java/org/apache/solr/rest/RestManager.javaindex 0def80091f1..9a4acf4779d 100644--- a/solr/core/src/java/org/apache/solr/rest/RestManager.java+++ b/solr/core/src/java/org/apache/solr/rest/RestManager.java@@ -66,6 +66,8 @@ public class RestManager {// used for validating resourceIds provided during registrationprivate static final Pattern resourceIdRegex = Pattern.compile("(/config|/schema)(/.*)");+ private static final boolean DECODE = true;+/*** Used internally to keep track of registrations during core initialization*/@@ -251,21 +253,23 @@ public class RestManager {* to. ManagedResource implementations are heavy-weight objects that live for the duration of* a SolrCore, so this class acts as the proxy between Restlet and a ManagedResource when* doing request processing.+ **/- public static class ManagedEndpoint extends BaseSolrResource+ public static class ManagedEndpoint extends BaseSolrResourceimplements GETable, PUTable, POSTable, DELETEable{/*** Determines the ManagedResource resourceId from the Restlet request.*/- public static String resolveResourceId(Request restletReq) {+ public static String resolveResourceId(Request restletReq) {String resourceId = restletReq.getResourceRef().- getRelativeRef(restletReq.getRootRef().getParentRef()).getPath();+ getRelativeRef(restletReq.getRootRef().getParentRef()).getPath(DECODE);// all resources are registered with the leading slashif (!resourceId.startsWith("/"))resourceId = "/"+resourceId;-++return resourceId;}diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-rest.xml b/solr/core/src/test-files/solr/collection1/conf/schema-rest.xmlindex b3612b57915..4d05f6a4516 100755--- a/solr/core/src/test-files/solr/collection1/conf/schema-rest.xml+++ b/solr/core/src/test-files/solr/collection1/conf/schema-rest.xml@@ -427,7 +427,7 @@<!-- A specialized field for geospatial search. If indexed, this fieldType must not be multivalued. --><fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>- <!-- Field type where stopwords are managed by the REST API -->+ <!-- Field type where english stopwords are managed by the REST API --><fieldtype name="managed_en" class="solr.TextField"><analyzer><tokenizer class="solr.StandardTokenizerFactory"/>@@ -436,6 +436,15 @@</analyzer></fieldtype>+ <!-- Field type where german stopwords are managed by the REST API -->+ <fieldtype name="managed_de" class="solr.TextField">+ <analyzer>+ <tokenizer class="solr.StandardTokenizerFactory"/>+ <filter class="solr.ManagedStopFilterFactory" managed="german" />+ <filter class="solr.ManagedSynonymFilterFactory" managed="german" />+ </analyzer>+ </fieldtype>+</types>diff --git a/solr/core/src/test/org/apache/solr/rest/TestRestManager.java b/solr/core/src/test/org/apache/solr/rest/TestRestManager.javaindex ce116e99710..d82469ae153 100644--- a/solr/core/src/test/org/apache/solr/rest/TestRestManager.java+++ b/solr/core/src/test/org/apache/solr/rest/TestRestManager.java@@ -28,6 +28,8 @@ import org.apache.solr.rest.ManagedResourceStorage.StorageIO;import org.apache.solr.rest.schema.analysis.ManagedWordSetResource;import org.junit.Test;import org.noggit.JSONUtil;+import org.restlet.Request;+import org.restlet.data.Reference;/*** Tests {@link RestManager} functionality, including resource registration,@@ -248,4 +250,30 @@ public class TestRestManager extends SolrRestletTestBase {RestManager restManager2 = new RestManager();restManager2.init(loader, initArgs, storageIO);}++ @Test+ public void testResolveResourceId () throws Exception {+ Request testRequest = new Request();+ Reference rootRef = new Reference("http://solr.apache.org/");+ testRequest.setRootRef(rootRef);++ Reference resourceRef = new Reference("http://solr.apache.org/schema/analysis/synonyms/de");+ testRequest.setResourceRef(resourceRef);++ String resourceId = RestManager.ManagedEndpoint.resolveResourceId(testRequest);+ assertEquals(resourceId, "/schema/analysis/synonyms/de");+ }++ @Test+ public void testResolveResourceIdDecodeUrlEntities () throws Exception {+ Request testRequest = new Request();+ Reference rootRef = new Reference("http://solr.apache.org/");+ testRequest.setRootRef(rootRef);++ Reference resourceRef = new Reference("http://solr.apache.org/schema/analysis/synonyms/de/%C3%84ndern");+ testRequest.setResourceRef(resourceRef);++ String resourceId = RestManager.ManagedEndpoint.resolveResourceId(testRequest);+ assertEquals(resourceId, "/schema/analysis/synonyms/de/Ändern");+ }}\ No newline at end of filediff --git a/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedStopFilterFactory.java b/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedStopFilterFactory.javaindex 8ce6cb52a80..9cf083b1833 100644--- a/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedStopFilterFactory.java+++ b/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedStopFilterFactory.java@@ -185,4 +185,29 @@ public class TestManagedStopFilterFactory extends RestTestBase {// should fail with 404 as foo doesn't existassertJDelete(endpoint + "/foo", "/error/code==404");}++ /**+ * Can we add and remove stopwords with umlauts+ */+ @Test+ public void testCanHandleDecodingAndEncodingForStopwords() throws Exception {+ String endpoint = "/schema/analysis/stopwords/german";++ //initially it should not exist+ assertJQ(endpoint + "/schön", "/error/code==404");++ //now we put a stopword with an umlaut+ assertJPut(endpoint,+ JSONUtil.toJSON(Arrays.asList("schön")),+ "/responseHeader/status==0");++ //let's check if it exists+ assertJQ(endpoint + "/schön", "/schön=='schön'");++ //now let's remove it+ assertJDelete(endpoint + "/schön", "/responseHeader/status==0");++ //and of it is unavailable again+ assertJQ(endpoint + "/schön", "/error/code==404");+ }}diff --git a/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedSynonymFilterFactory.java b/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedSynonymFilterFactory.javaindex deadac0bcbc..4eb1e53df78 100644--- a/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedSynonymFilterFactory.java+++ b/solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedSynonymFilterFactory.java@@ -196,4 +196,41 @@ public class TestManagedSynonymFilterFactory extends RestTestBase {assertJQ(endpoint+"/MB","/MB==['Megabyte','MiB','megabyte']");}++ /**+ * Can we add and remove stopwords with umlauts+ */+ @Test+ public void testCanHandleDecodingAndEncodingForSynonyms() throws Exception {+ String endpoint = "/schema/analysis/synonyms/german";++ assertJQ(endpoint,+ "/synonymMappings/initArgs/ignoreCase==false",+ "/synonymMappings/managedMap=={}");++ // does not exist+ assertJQ(endpoint+"/fröhlich",+ "/error/code==404");++ Map<String,List<String>> syns = new HashMap<>();++ // now put a synonym+ syns.put("fröhlich", Arrays.asList("glücklick"));+ assertJPut(endpoint,+ JSONUtil.toJSON(syns),+ "/responseHeader/status==0");++ // and check if it exists+ assertJQ(endpoint,+ "/synonymMappings/managedMap/fröhlich==['glücklick']");++ // verify delete works+ assertJDelete(endpoint+"/fröhlich",+ "/responseHeader/status==0");+++ // was it really deleted?+ assertJDelete(endpoint+"/fröhlich",+ "/error/code==404");+ }}