forked from apache/solr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 42 additions & 29 deletions
71
solr/core/src/java/org/apache/solr/request/macro/MacroSanitizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,55 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.request.macro; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.apache.solr.common.util.CollectionUtil; | ||
|
||
public class MacroSanitizer { | ||
|
||
public static Map<String, String[]> sanitize(Map<String, String[]> params) { | ||
Map<String, String[]> sanitized = params; | ||
|
||
// quick peek into the values to check for macros | ||
final boolean needsSanitizing = params.values().stream() | ||
.flatMap(v -> Arrays.stream(v)) | ||
.anyMatch(s -> s.contains(MacroExpander.MACRO_START)); | ||
|
||
if (needsSanitizing) { | ||
sanitized = CollectionUtil.newHashMap(params.size()); | ||
|
||
for (Entry<String, String[]> param : params.entrySet()) { | ||
final String paramName = param.getKey(); | ||
final String[] paramValues = param.getValue(); | ||
final List<String> sanitizedParamValues = new ArrayList<>(paramValues.length); | ||
|
||
for (int i = 0; i < paramValues.length; i++) { | ||
if (!paramValues[i].contains(MacroExpander.MACRO_START)) { | ||
sanitizedParamValues.add(paramValues[i]); | ||
} | ||
} | ||
|
||
sanitized.put(paramName, sanitizedParamValues.toArray(new String[] {})); | ||
} | ||
/** | ||
* Sanitizes macros for the given parameter in the given params set if present | ||
* | ||
* @param param the parameter whose values we should sanitzize | ||
* @param params the parameter set | ||
* @return the sanitized parameter set | ||
*/ | ||
public static Map<String, String[]> sanitize(String param, Map<String, String[]> params) { | ||
// quick peek into the values to check for macros | ||
final boolean needsSanitizing = | ||
params.containsKey(param) | ||
&& Arrays.stream(params.get(param)) | ||
.anyMatch(s -> s.contains(MacroExpander.MACRO_START)); | ||
|
||
if (needsSanitizing) { | ||
final String[] fqs = params.get(param); | ||
final List<String> sanitizedFqs = new ArrayList<>(fqs.length); | ||
|
||
for (int i = 0; i < fqs.length; i++) { | ||
if (!fqs[i].contains(MacroExpander.MACRO_START)) { | ||
sanitizedFqs.add(fqs[i]); | ||
} | ||
|
||
return sanitized; | ||
} | ||
|
||
params.put(param, sanitizedFqs.toArray(new String[] {})); | ||
} | ||
|
||
return params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 48 additions & 28 deletions
76
solr/core/src/test/org/apache/solr/request/macro/MacroSanitizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,62 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.request.macro; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.solr.common.params.CommonParams; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
public class MacroSanitizerTest { | ||
|
||
@Test | ||
public void shouldReturnSameInstanceWhenNotSanitizing() { | ||
// given | ||
Map<String, String[]> params = new HashMap<>(); | ||
|
||
// when | ||
Map<String, String[]> sanitized = MacroSanitizer.sanitize(params); | ||
|
||
// then | ||
assertSame(params, sanitized); | ||
} | ||
|
||
@Test | ||
public void shouldNotSanitizeNonMacros() { | ||
// given | ||
Map<String, String[]> params = new HashMap<>(); | ||
params.put("fq", new String[] { "bee:up", "look:left", "{!collapse tag=collapsing field=bee sort=${collapseSort}}" }); | ||
params.put("q", new String[] { "bee:honey" }); | ||
|
||
// when | ||
Map<String, String[]> sanitized = MacroSanitizer.sanitize(params); | ||
|
||
// then | ||
assertEquals(2, sanitized.size()); | ||
assertEquals(2, sanitized.get("fq").length); | ||
MatcherAssert.assertThat(sanitized.get("fq"), Matchers.arrayContaining("bee:up", "look:left")); | ||
} | ||
@Test | ||
public void shouldReturnSameInstanceWhenNotSanitizing() { | ||
// given | ||
Map<String, String[]> params = new HashMap<>(); | ||
|
||
// when | ||
Map<String, String[]> sanitized = MacroSanitizer.sanitize(CommonParams.FQ, params); | ||
|
||
// then | ||
assertSame(params, sanitized); | ||
} | ||
|
||
@Test | ||
public void shouldNotSanitizeNonMacros() { | ||
// given | ||
Map<String, String[]> params = new HashMap<>(); | ||
params.put( | ||
CommonParams.FQ, | ||
new String[] { | ||
"bee:up", "look:left", "{!collapse tag=collapsing field=bee sort=${collapseSort}}" | ||
}); | ||
params.put("q", new String[] {"bee:honey"}); | ||
|
||
// when | ||
Map<String, String[]> sanitized = MacroSanitizer.sanitize(CommonParams.FQ, params); | ||
|
||
// then | ||
assertEquals(2, sanitized.size()); | ||
assertEquals(2, sanitized.get("fq").length); | ||
MatcherAssert.assertThat(sanitized.get("fq"), Matchers.arrayContaining("bee:up", "look:left")); | ||
} | ||
} |