You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by lm...@apache.org on 2016/12/19 16:42:04 UTC

knox git commit: KNOX-528 - Support for Apache SOLR REST APIs (John McParland via lmccay)

Repository: knox
Updated Branches:
  refs/heads/master 29538657c -> d4ae9ae52


KNOX-528 - Support for Apache SOLR REST APIs (John McParland via lmccay)

Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/d4ae9ae5
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/d4ae9ae5
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/d4ae9ae5

Branch: refs/heads/master
Commit: d4ae9ae5220f1e2dc2a874592276856f35e54b3a
Parents: 2953865
Author: Larry McCay <lm...@hortonworks.com>
Authored: Mon Dec 19 11:41:54 2016 -0500
Committer: Larry McCay <lm...@hortonworks.com>
Committed: Mon Dec 19 11:41:54 2016 -0500

----------------------------------------------------------------------
 .../rewrite/api/UrlRewriteProcessorTest.java    | 110 +++++++++++++++++--
 .../resources/services/solr/5.5.0/rewrite.xml   |  24 ++++
 .../resources/services/solr/5.5.0/service.xml   |  31 ++++++
 3 files changed, 156 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/d4ae9ae5/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
index 37e2b1a..5135ad9 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
@@ -17,25 +17,33 @@
  */
 package org.apache.hadoop.gateway.filter.rewrite.api;
 
-import org.apache.hadoop.gateway.util.urltemplate.Parser;
-import org.apache.hadoop.gateway.util.urltemplate.Template;
-import org.easymock.EasyMock;
-import org.junit.Test;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.hamcrest.MatcherAssert.assertThat;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.hadoop.gateway.util.urltemplate.Expander;
+import org.apache.hadoop.gateway.util.urltemplate.Matcher;
+import org.apache.hadoop.gateway.util.urltemplate.Parser;
+import org.apache.hadoop.gateway.util.urltemplate.Template;
+import org.easymock.EasyMock;
+import org.junit.Test;
 
 public class UrlRewriteProcessorTest {
 
@@ -273,4 +281,88 @@ public class UrlRewriteProcessorTest {
     processor.destroy();
   }
 
+  /**
+   * Tests the rewrite pattern used for re-writing Solr urls passed through Knox.
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testSolrRewrite() throws Exception {
+    URI inputUri, outputUri;
+    Matcher<Void> matcher;
+    Matcher<Void>.Match match;
+    Template input, pattern, template;
+
+    inputUri = new URI(
+        "https://hortonworks.sandbox.hdp.24.test:8443/gateway/sandbox/solr/TestCollection/select?q=*.*&wt=json&indent=true");
+
+    input = Parser.parseLiteral(inputUri.toString());
+    pattern = Parser.parseTemplate("*://*:*/**/solr/{collection=**}/{query=**}?{**}");
+    template = Parser.parseTemplate("http://sandbox.hortonworks.com/solr/{collection=**}/{query=**}?{**}");
+
+    matcher = new Matcher<Void>();
+    matcher.add(pattern, null);
+    match = matcher.match(input);
+
+    outputUri = Expander.expand(template, match.getParams(), null);
+
+    final String reWrittenScheme = outputUri.getScheme();
+    assertEquals("http", reWrittenScheme);
+
+    final String reWrittenHost = outputUri.getHost();
+    assertEquals("sandbox.hortonworks.com", reWrittenHost);
+
+    final String reWrittenPath = outputUri.getPath();
+    assertEquals("/solr/TestCollection/select", reWrittenPath);
+
+    // Whole thing is (non-deterministicly ordered around the &s):
+    // "q=*.*&wt=json&indent=true"
+    final String reWrittenQuery = outputUri.getQuery();
+
+    // Check individual parameters are present, and have the right value.
+    final Map<String, String> reWrittenParams = mapUrlParameters(reWrittenQuery);
+    assertTrue(reWrittenParams.containsKey("q"));
+    assertEquals("*.*", reWrittenParams.get("q"));
+    assertTrue(reWrittenParams.containsKey("wt"));
+    assertEquals("json", reWrittenParams.get("wt"));
+    assertEquals("true", reWrittenParams.get("indent"));
+  }
+
+  /**
+   * Turn a string containing URL parameters, e.g.
+   * 
+   * <pre>
+   * a=b&c=d&e=f
+   * </pre>
+   * 
+   * into a map such as
+   * <table>
+   * <tr>
+   * <th>Key</th>
+   * <th>Value</th>
+   * </tr>
+   * <tr>
+   * <td>a</td>
+   * <td>b</td>
+   * </tr>
+   * <tr>
+   * <td>c</td>
+   * <td>d</td>
+   * </tr>
+   * </table>
+   * 
+   * @param urlParameters the URL parameter string. Expected to contain something of the form
+   *        "a=b&c=d" etc (i.e. Key=Value separated by &).
+   * @return a map, with the key-values pairs representing the URL parameters.
+   */
+  private Map<String, String> mapUrlParameters(String urlParameters) {
+    final Map<String, String> map = new HashMap<String, String>();
+    for (String pair : urlParameters.split("&")) {
+      String[] kv = pair.split("=");
+      map.put(kv[0], kv[1]);
+    }
+    return map;
+  }
+
+  
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/d4ae9ae5/gateway-service-definitions/src/main/resources/services/solr/5.5.0/rewrite.xml
----------------------------------------------------------------------
diff --git a/gateway-service-definitions/src/main/resources/services/solr/5.5.0/rewrite.xml b/gateway-service-definitions/src/main/resources/services/solr/5.5.0/rewrite.xml
new file mode 100644
index 0000000..b9910ab
--- /dev/null
+++ b/gateway-service-definitions/src/main/resources/services/solr/5.5.0/rewrite.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+   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.
+-->
+<rules>
+  <!--Only supporting Solr queries via Knox -->
+  <rule dir="IN" name="SOLRAPI/solr/inbound/query" pattern="*://*:*/**/solr/{collection=**}/{query=**}?{**}">
+       <rewrite template="{$serviceUrl[SOLRAPI]}/{collection=**}/{query=**}?{**}"/>
+  </rule>
+
+</rules>

http://git-wip-us.apache.org/repos/asf/knox/blob/d4ae9ae5/gateway-service-definitions/src/main/resources/services/solr/5.5.0/service.xml
----------------------------------------------------------------------
diff --git a/gateway-service-definitions/src/main/resources/services/solr/5.5.0/service.xml b/gateway-service-definitions/src/main/resources/services/solr/5.5.0/service.xml
new file mode 100644
index 0000000..e1837c3
--- /dev/null
+++ b/gateway-service-definitions/src/main/resources/services/solr/5.5.0/service.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+   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.
+-->
+<service role="SOLRAPI" name="solr" version="5.5.0">
+    <policies>
+        <policy role="webappsec"/>
+        <policy role="authentication"/>
+        <policy role="rewrite"/>
+        <policy role="authorization"/>
+    </policies>
+    <routes>
+        <route path="/solr/**/**?**">
+             <rewrite apply="SOLRAPI/solr/inbound/query" to="request.url"/>
+        </route>
+    </routes>
+     <dispatch classname="org.apache.hadoop.gateway.dispatch.PassAllHeadersDispatch"/>
+</service>