You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ma...@apache.org on 2014/02/14 17:02:11 UTC
svn commit: r1568340 [2/2] - in /lucene/dev/branches/branch_4x: ./ solr/
solr/contrib/ solr/contrib/clustering/src/test-files/clustering/solr/
solr/contrib/dataimporthandler/src/test-files/dih/solr/
solr/contrib/dataimporthandler/src/test/org/apache/so...
Modified: lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.java (original)
+++ lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.java Fri Feb 14 16:02:09 2014
@@ -43,7 +43,7 @@ public class SolrExampleJettyTest extend
{
try {
// setup the server...
- String url = "http://127.0.0.1/?core=xxx";
+ String url = "http" + (isSSLMode() ? "s" : "") + "://127.0.0.1/?core=xxx";
HttpSolrServer s = new HttpSolrServer( url );
Assert.fail( "CommonsHttpSolrServer should not allow a path with a parameter: "+s.getBaseURL() );
}
Modified: lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java (original)
+++ lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java Fri Feb 14 16:02:09 2014
@@ -146,7 +146,7 @@ public class BasicHttpSolrServerTest ext
public void testConnectionRefused() throws MalformedURLException {
int unusedPort = findUnusedPort(); // XXX even if fwe found an unused port
// it might not be unused anymore
- HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:" + unusedPort
+ HttpSolrServer server = new HttpSolrServer("http" + (isSSLMode() ? "s" : "") + "://127.0.0.1:" + unusedPort
+ "/solr");
server.setConnectionTimeout(500);
SolrQuery q = new SolrQuery("*:*");
Copied: lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/common/util/URLUtilTest.java (from r1566456, lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/common/util/URLUtilTest.java)
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/common/util/URLUtilTest.java?p2=lucene/dev/branches/branch_4x/solr/solrj/src/test/org/apache/solr/common/util/URLUtilTest.java&p1=lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/common/util/URLUtilTest.java&r1=1566456&r2=1568340&rev=1568340&view=diff
==============================================================================
(empty)
Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/BaseDistributedSearchTestCase.java Fri Feb 14 16:02:09 2014
@@ -36,8 +36,9 @@ import junit.framework.Assert;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.search.FieldCache;
-import org.apache.lucene.util. _TestUtil;
import org.apache.lucene.util.Constants;
+import org.apache.lucene.util._TestUtil;
+import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
@@ -45,7 +46,6 @@ import org.apache.solr.client.solrj.impl
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
-import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
@@ -55,8 +55,8 @@ import org.apache.solr.common.util.Named
import org.apache.solr.schema.TrieDateField;
import org.apache.solr.util.AbstractSolrTestCase;
import org.eclipse.jetty.servlet.ServletHolder;
-import org.junit.BeforeClass;
import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -123,7 +123,7 @@ public abstract class BaseDistributedSea
log.info("Setting hostContext system property: " + hc);
System.setProperty("hostContext", hc);
}
-
+
/**
* Clears the "hostContext" system property
* @see #initHostContext
@@ -369,14 +369,14 @@ public abstract class BaseDistributedSea
boolean stopAtShutdown = true;
JettySolrRunner jetty = new JettySolrRunner
(solrHome.getAbsolutePath(), context, 0, solrConfigOverride, schemaOverride, stopAtShutdown,
- getExtraServlets(), null, getExtraRequestFilters());
+ getExtraServlets(), sslConfig, getExtraRequestFilters());
jetty.setShards(shardList);
jetty.setDataDir(dataDir);
if (explicitCoreNodeName) {
jetty.setCoreNodeName(Integer.toString(nodeCnt.incrementAndGet()));
}
jetty.start();
-
+
return jetty;
}
@@ -393,7 +393,8 @@ public abstract class BaseDistributedSea
protected SolrServer createNewSolrServer(int port) {
try {
// setup the server...
- String url = "http://127.0.0.1:" + port + context;
+ String urlScheme = isSSLMode() ? "https" : "http";
+ String url = urlScheme + "://127.0.0.1:" + port + context;
HttpSolrServer s = new HttpSolrServer(url);
s.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
s.setSoTimeout(60000);
@@ -931,4 +932,5 @@ public abstract class BaseDistributedSea
FileUtils.copyFile(new File(getSolrHome(), solrxml), new File(jettyHome, "solr.xml"));
}
}
+
}
Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java Fri Feb 14 16:02:09 2014
@@ -38,72 +38,16 @@ abstract public class SolrJettyTestBase
{
private static Logger log = LoggerFactory.getLogger(SolrJettyTestBase.class);
- private static File TEST_KEYSTORE;
- static {
- TEST_KEYSTORE = (null == ExternalPaths.SOURCE_HOME)
- ? null : new File(ExternalPaths.SOURCE_HOME, "example/etc/solrtest.keystore");
- }
-
- private static void initSSLConfig(SSLConfig sslConfig, String keystorePath) {
- sslConfig.useSsl = false;
- sslConfig.clientAuth = false;
- sslConfig.keyStore = keystorePath;
- sslConfig.keyStorePassword = "secret";
- sslConfig.trustStore = keystorePath;
- sslConfig.trustStorePassword = "secret";
- }
-
- /**
- * Returns the File object for the example keystore used when this baseclass randomly
- * uses SSL. May be null ifthis test does not appear to be running as part of the
- * standard solr distribution and does not have access to the example configs.
- *
- * @lucene.internal
- */
- protected static File getExampleKeystoreFile() {
- return TEST_KEYSTORE;
- }
@BeforeClass
public static void beforeSolrJettyTestBase() throws Exception {
-
-
- // only randomize SSL if we are a solr test with access to the example keystore
- if (null == getExampleKeystoreFile()) {
- log.info("Solr's example keystore not defined (not a solr test?) skipping SSL randomization");
- return;
- }
-
- assertTrue("test keystore does not exist, randomized ssl testing broken: " +
- getExampleKeystoreFile().getAbsolutePath(),
- getExampleKeystoreFile().exists() );
-
}
public static JettySolrRunner jetty;
public static int port;
public static SolrServer server = null;
public static String context;
-
- public static SSLConfig getSSLConfig() {
- SSLConfig sslConfig = new SSLConfig();
-
- final boolean trySsl = random().nextBoolean();
- final boolean trySslClientAuth = random().nextBoolean();
-
- log.info("Randomized ssl ({}) and clientAuth ({})", trySsl,
- trySslClientAuth);
- String keystorePath = null == TEST_KEYSTORE ? null : TEST_KEYSTORE
- .getAbsolutePath();
- initSSLConfig(sslConfig, keystorePath);
-
- sslConfig.useSsl = trySsl;
- sslConfig.clientAuth = trySslClientAuth;
-
- initSSLConfig(sslConfig, keystorePath);
- return sslConfig;
- }
public static JettySolrRunner createJetty(String solrHome, String configFile, String schemaFile, String context,
boolean stopAtShutdown, SortedMap<ServletHolder,String> extraServlets)
@@ -118,7 +62,7 @@ abstract public class SolrJettyTestBase
context = context==null ? "/solr" : context;
SolrJettyTestBase.context = context;
- jetty = new JettySolrRunner(solrHome, context, 0, configFile, schemaFile, stopAtShutdown, extraServlets, getSSLConfig());
+ jetty = new JettySolrRunner(solrHome, context, 0, configFile, schemaFile, stopAtShutdown, extraServlets, sslConfig);
jetty.start();
port = jetty.getLocalPort();
Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java Fri Feb 14 16:02:09 2014
@@ -22,6 +22,10 @@ import static com.google.common.base.Pre
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@@ -40,12 +44,20 @@ import javax.xml.xpath.XPathExpressionEx
import org.apache.commons.codec.Charsets;
import org.apache.commons.io.FileUtils;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.QuickPatchThreadsFilter;
import org.apache.lucene.util._TestUtil;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner.SSLConfig;
+import org.apache.solr.client.solrj.impl.HttpClientConfigurer;
+import org.apache.solr.client.solrj.impl.HttpClientUtil;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.cloud.IpTables;
import org.apache.solr.common.SolrDocument;
@@ -74,6 +86,7 @@ import org.apache.solr.schema.SchemaFiel
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.servlet.DirectSolrConnection;
import org.apache.solr.util.AbstractSolrTestCase;
+import org.apache.solr.util.ExternalPaths;
import org.apache.solr.util.RevertDefaultThreadHandlerRule;
import org.apache.solr.util.TestHarness;
import org.junit.AfterClass;
@@ -107,6 +120,7 @@ public abstract class SolrTestCaseJ4 ext
private static String coreName = ConfigSolrXmlOld.DEFAULT_DEFAULT_CORE_NAME;
public static int DEFAULT_CONNECTION_TIMEOUT = 30000; // default socket connection timeout in ms
+ protected static volatile SSLConfig sslConfig = new SSLConfig();
@ClassRule
public static TestRule solrClassRules =
@@ -129,6 +143,37 @@ public abstract class SolrTestCaseJ4 ext
startTrackingZkClients();
ignoreException("ignore_exception");
newRandomConfig();
+ sslConfig = getSSLConfig();
+
+
+ if(sslConfig != null && sslConfig.useSsl) {
+ // SolrCloud tests should usually clear this
+ System.setProperty("urlScheme", "https");
+
+ // Turn off two-way SSL since it isn't configured below...
+ sslConfig.clientAuth = false;
+ HttpClientUtil.setConfigurer(new HttpClientConfigurer(){
+ @SuppressWarnings("deprecation")
+ protected void configure(DefaultHttpClient httpClient, SolrParams config) {
+ super.configure(httpClient, config);
+ SchemeRegistry registry = httpClient.getConnectionManager().getSchemeRegistry();
+ // Make sure no tests cheat by using HTTP
+ registry.unregister("http");
+ try {
+ // Don't complain that we are using self-signed certs during the test
+ registry.register(new Scheme("https", 443, new SSLSocketFactory(new TrustSelfSignedStrategy())));
+ } catch (KeyManagementException ex) {
+ throw new IllegalStateException("Unable to setup https scheme for HTTPClient to test SSL.", ex);
+ } catch (UnrecoverableKeyException ex) {
+ throw new IllegalStateException("Unable to setup https scheme for HTTPClient to test SSL.", ex);
+ } catch (NoSuchAlgorithmException ex) {
+ throw new IllegalStateException("Unable to setup https scheme for HTTPClient to test SSL.", ex);
+ } catch (KeyStoreException ex) {
+ throw new IllegalStateException("Unable to setup https scheme for HTTPClient to test SSL.", ex);
+ }
+ }
+ });
+ }
}
@AfterClass
@@ -144,9 +189,44 @@ public abstract class SolrTestCaseJ4 ext
System.clearProperty("tests.shardhandler.randomSeed");
System.clearProperty("enable.update.log");
System.clearProperty("useCompoundFile");
+ System.clearProperty("urlScheme");
+
+ if(sslConfig != null && sslConfig.useSsl) {
+ HttpClientUtil.setConfigurer(new HttpClientConfigurer());
+ }
IpTables.unblockAllPorts();
}
+
+ private static File TEST_KEYSTORE;
+ static {
+ TEST_KEYSTORE = (null == ExternalPaths.SOURCE_HOME)
+ ? null : new File(ExternalPaths.SOURCE_HOME, "example/etc/solrtest.keystore");
+ }
+
+ protected boolean isSSLMode() {
+ return sslConfig != null && sslConfig.useSsl;
+ }
+
+ private static void initSSLConfig(SSLConfig sslConfig, String keystorePath) {
+ sslConfig.useSsl = false;
+ sslConfig.clientAuth = false;
+ sslConfig.keyStore = keystorePath;
+ sslConfig.keyStorePassword = "secret";
+ sslConfig.trustStore = keystorePath;
+ sslConfig.trustStorePassword = "secret";
+ }
+
+ /**
+ * Returns the File object for the example keystore used when this baseclass randomly
+ * uses SSL. May be null ifthis test does not appear to be running as part of the
+ * standard solr distribution and does not have access to the example configs.
+ *
+ * @lucene.internal
+ */
+ protected static File getExampleKeystoreFile() {
+ return TEST_KEYSTORE;
+ }
private static boolean changedFactory = false;
private static String savedFactory;
@@ -174,6 +254,40 @@ public abstract class SolrTestCaseJ4 ext
}
}
+ private static SSLConfig getSSLConfig() {
+ // test has disabled
+ if (sslConfig == null) {
+ SSLConfig sslConfig = new SSLConfig();
+ return sslConfig;
+ }
+
+ // only randomize SSL if we are a solr test with access to the example keystore
+ if (null == getExampleKeystoreFile()) {
+ log.info("Solr's example keystore not defined (not a solr test?) skipping SSL randomization");
+ return null;
+ }
+
+ assertTrue("test keystore does not exist, randomized ssl testing broken: " +
+ getExampleKeystoreFile().getAbsolutePath(),
+ getExampleKeystoreFile().exists() );
+
+ SSLConfig sslConfig = new SSLConfig();
+
+ final boolean trySsl = random().nextBoolean();
+ final boolean trySslClientAuth = false; // TODO: random().nextBoolean();
+
+ log.info("Randomized ssl ({}) and clientAuth ({})", trySsl,
+ trySslClientAuth);
+ String keystorePath = null == TEST_KEYSTORE ? null : TEST_KEYSTORE
+ .getAbsolutePath();
+ initSSLConfig(sslConfig, keystorePath);
+
+ sslConfig.useSsl = trySsl;
+ sslConfig.clientAuth = trySslClientAuth;
+
+ return sslConfig;
+ }
+
/**
* Call this from @BeforeClass to set up the test harness and update handler with no cores.
*
Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java Fri Feb 14 16:02:09 2014
@@ -28,6 +28,7 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -40,11 +41,13 @@ import java.util.concurrent.atomic.Atomi
import org.apache.commons.io.FilenameUtils;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.lucene.util.LuceneTestCase.Slow;
+import org.apache.solr.SolrJettyTestBase;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner.SSLConfig;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.request.QueryRequest;
@@ -70,6 +73,7 @@ import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.servlet.SolrDispatchFilter;
import org.apache.solr.update.DirectUpdateHandler2;
+import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@@ -202,6 +206,19 @@ public abstract class AbstractFullDistri
} else {
System.clearProperty("numShards");
}
+
+ if (isSSLMode()) {
+ System.clearProperty("urlScheme");
+ ZkStateReader zkStateReader = new ZkStateReader(zkServer.getZkAddress(),
+ AbstractZkTestCase.TIMEOUT, AbstractZkTestCase.TIMEOUT);
+ try {
+ zkStateReader.getZkClient().create(ZkStateReader.CLUSTER_PROPS,
+ ZkStateReader.toJSON(Collections.singletonMap("urlScheme","https")),
+ CreateMode.PERSISTENT, true);
+ } finally {
+ zkStateReader.close();
+ }
+ }
}
@BeforeClass
@@ -454,7 +471,7 @@ public abstract class AbstractFullDistri
String solrConfigOverride) throws Exception {
JettySolrRunner jetty = new JettySolrRunner(getSolrHome(), context, 0,
- solrConfigOverride, null, false, getExtraServlets(), null, getExtraRequestFilters());
+ solrConfigOverride, null, false, getExtraServlets(), sslConfig, getExtraRequestFilters());
jetty.setShards(shardList);
jetty.setDataDir(getDataDir(dataDir));
jetty.start();
@@ -468,7 +485,7 @@ public abstract class AbstractFullDistri
solrHome = getRelativeSolrHomePath(solrHome);
}
- JettySolrRunner jetty = new JettySolrRunner(solrHome.getPath(), context, 0, solrConfigOverride, schemaOverride, false, getExtraServlets(), null, getExtraRequestFilters());
+ JettySolrRunner jetty = new JettySolrRunner(solrHome.getPath(), context, 0, solrConfigOverride, schemaOverride, false, getExtraServlets(), sslConfig, getExtraRequestFilters());
jetty.setShards(shardList);
jetty.setDataDir(getDataDir(dataDir));
jetty.start();
@@ -1659,7 +1676,8 @@ public abstract class AbstractFullDistri
protected SolrServer createNewSolrServer(int port) {
try {
// setup the server...
- String url = "http://127.0.0.1:" + port + context +
+ String urlScheme = isSSLMode() ? "https" : "http";
+ String url = urlScheme + "://127.0.0.1:" + port + context +
(context.endsWith("/") ? "" : "/") + DEFAULT_COLLECTION;
HttpSolrServer s = new HttpSolrServer(url);
s.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
@@ -1773,6 +1791,7 @@ public abstract class AbstractFullDistri
}
return commondCloudSolrServer;
}
+
public static String getUrlFromZk(ClusterState clusterState, String collection) {
Map<String,Slice> slices = clusterState.getCollection(collection).getSlicesMap();
@@ -1821,4 +1840,4 @@ public abstract class AbstractFullDistri
fail("Could not find the new collection - " + exp.code() + " : " + collectionClient.getBaseURL());
}
-}
+}
\ No newline at end of file
Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java Fri Feb 14 16:02:09 2014
@@ -29,21 +29,20 @@ import org.apache.solr.common.params.Mul
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.servlet.SolrRequestParsers;
import org.eclipse.jetty.servlet.ServletHolder;
+import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
-import org.junit.AfterClass;
-
abstract public class RestTestBase extends SolrJettyTestBase {
private static final Logger log = LoggerFactory.getLogger(RestTestBase.class);
protected static RestTestHarness restTestHarness;
- @AfterClass
- public static void cleanUpHarness() {
- restTestHarness = null;
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ // sslConfig = null;
}
-
+
public static void createJettyAndHarness
(String solrHome, String configFile, String schemaFile, String context,
boolean stopAtShutdown, SortedMap<ServletHolder,String> extraServlets) throws Exception {
Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java?rev=1568340&r1=1568339&r2=1568340&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java Fri Feb 14 16:02:09 2014
@@ -16,23 +16,30 @@ package org.apache.solr.util;
* limitations under the License.
*/
-import org.apache.commons.io.IOUtils;
-
-import javax.xml.xpath.XPathExpressionException;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.io.StringWriter;
-import java.net.HttpURLConnection;
-import java.net.URL;
import java.net.URLEncoder;
+import javax.xml.xpath.XPathExpressionException;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
+import org.apache.solr.client.solrj.impl.HttpClientUtil;
+import org.apache.solr.common.params.ModifiableSolrParams;
+
/**
* Facilitates testing Solr's REST API via a provided embedded Jetty
*/
public class RestTestHarness extends BaseTestHarness {
private RESTfulServerProvider serverProvider;
+ private HttpClient httpClient = HttpClientUtil.createClient(new
+ ModifiableSolrParams());
public RestTestHarness(RESTfulServerProvider serverProvider) {
this.serverProvider = serverProvider;
@@ -83,22 +90,7 @@ public class RestTestHarness extends Bas
* @exception Exception any exception in the response.
*/
public String query(String request) throws Exception {
- URL url = new URL(getBaseURL() + request);
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- InputStream inputStream = null;
- StringWriter strWriter;
- try {
- try {
- inputStream = connection.getInputStream();
- } catch (IOException e) {
- inputStream = connection.getErrorStream();
- }
- strWriter = new StringWriter();
- IOUtils.copy(new InputStreamReader(inputStream, "UTF-8"), strWriter);
- } finally {
- IOUtils.closeQuietly(inputStream);
- }
- return strWriter.toString();
+ return getResponse(new HttpGet(getBaseURL() + request));
}
/**
@@ -110,27 +102,11 @@ public class RestTestHarness extends Bas
* @return The response to the PUT request
*/
public String put(String request, String content) throws IOException {
- URL url = new URL(getBaseURL() + request);
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- connection.setDoOutput(true);
- connection.setRequestMethod("PUT");
- OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
- out.write(content);
- out.close();
- InputStream inputStream = null;
- StringWriter stringWriter;
- try {
- try {
- inputStream = connection.getInputStream();
- } catch (IOException e) {
- inputStream = connection.getErrorStream();
- }
- stringWriter = new StringWriter();
- IOUtils.copy(new InputStreamReader(inputStream, "UTF-8"), stringWriter);
- } finally {
- IOUtils.closeQuietly(inputStream);
- }
- return stringWriter.toString();
+ HttpPut httpPut = new HttpPut(getBaseURL() + request);
+ httpPut.setEntity(new StringEntity(content, ContentType.create(
+ "application/json", "utf-8")));
+
+ return getResponse(httpPut);
}
/**
@@ -142,29 +118,11 @@ public class RestTestHarness extends Bas
* @return The response to the PUT request
*/
public String post(String request, String content) throws IOException {
- URL url = new URL(getBaseURL() + request);
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- connection.setDoOutput(true);
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
-
- OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
- out.write(content);
- out.close();
- InputStream inputStream = null;
- StringWriter stringWriter;
- try {
- try {
- inputStream = connection.getInputStream();
- } catch (IOException e) {
- inputStream = connection.getErrorStream();
- }
- stringWriter = new StringWriter();
- IOUtils.copy(new InputStreamReader(inputStream, "UTF-8"), stringWriter);
- } finally {
- IOUtils.closeQuietly(inputStream);
- }
- return stringWriter.toString();
+ HttpPost httpPost = new HttpPost(getBaseURL() + request);
+ httpPost.setEntity(new StringEntity(content, ContentType.create(
+ "application/json", "utf-8")));
+
+ return getResponse(httpPost);
}
@@ -202,4 +160,14 @@ public class RestTestHarness extends Bas
throw new RuntimeException(e);
}
}
+
+ private String getResponse(HttpUriRequest request) throws IOException {
+ HttpEntity entity = null;
+ try {
+ entity = httpClient.execute(request).getEntity();
+ return EntityUtils.toString(entity, "UTF-8");
+ } finally {
+ EntityUtils.consumeQuietly(entity);
+ }
+ }
}