You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2014/02/05 12:03:26 UTC
svn commit: r1564709 - in /lucene/dev/trunk/solr: CHANGES.txt
solrj/src/java/org/apache/solr/client/solrj/impl/NoOpResponseParser.java
solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java
Author: shalin
Date: Wed Feb 5 11:03:26 2014
New Revision: 1564709
URL: http://svn.apache.org/r1564709
Log:
SOLR-5530: Added a NoOpResponseParser for SolrJ which puts the entire raw response into an entry in the NamedList
Added:
lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/NoOpResponseParser.java (with props)
lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java (with props)
Modified:
lucene/dev/trunk/solr/CHANGES.txt
Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1564709&r1=1564708&r2=1564709&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Wed Feb 5 11:03:26 2014
@@ -169,6 +169,10 @@ New Features
* SOLR-5623: Better diagnosis of RuntimeExceptions in analysis
(Benson Margulies)
+* SOLR-5530: Added a NoOpResponseParser for SolrJ which puts the entire raw
+ response into an entry in the NamedList.
+ (Upayavira, Vitaliy Zhovtyuk via shalin)
+
Bug Fixes
----------------------
Added: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/NoOpResponseParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/NoOpResponseParser.java?rev=1564709&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/NoOpResponseParser.java (added)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/impl/NoOpResponseParser.java Wed Feb 5 11:03:26 2014
@@ -0,0 +1,83 @@
+package org.apache.solr.client.solrj.impl;
+
+/*
+ * 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.
+ */
+
+import org.apache.commons.io.IOUtils;
+import org.apache.solr.client.solrj.ResponseParser;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.util.NamedList;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringWriter;
+
+/**
+ * Simply puts the entire response into an entry in a NamedList.
+ * This parser isn't parse response into a QueryResponse.
+ */
+public class NoOpResponseParser extends ResponseParser {
+
+ private String writerType = "xml";
+
+ public NoOpResponseParser() {
+ }
+
+ public NoOpResponseParser(String writerType) {
+ this.writerType = writerType;
+ }
+
+ @Override
+ public String getWriterType() {
+ return writerType;
+ }
+
+ public void setWriterType(String writerType) {
+ this.writerType = writerType;
+ }
+
+ @Override
+ public NamedList<Object> processResponse(Reader reader) {
+ try {
+ StringWriter writer = new StringWriter();
+ IOUtils.copy(reader, writer);
+ String output = writer.toString();
+ NamedList<Object> list = new NamedList<>();
+ list.add("response", output);
+ return list;
+ } catch (IOException e) {
+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "parsing error", e);
+ }
+ }
+
+ @Override
+ public NamedList<Object> processResponse(InputStream body, String encoding) {
+ try {
+ StringWriter writer = new StringWriter();
+ IOUtils.copy(body, writer, encoding);
+ String output = writer.toString();
+ NamedList<Object> list = new NamedList<>();
+ list.add("response", output);
+ return list;
+ } catch (IOException e) {
+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "parsing error", e);
+ }
+ }
+
+}
+
Added: lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java?rev=1564709&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java (added)
+++ lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/response/NoOpResponseParserTest.java Wed Feb 5 11:03:26 2014
@@ -0,0 +1,131 @@
+package org.apache.solr.client.solrj.response;
+
+/*
+ * 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.
+ */
+
+import org.apache.commons.io.IOUtils;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.solr.SolrJettyTestBase;
+import org.apache.solr.client.solrj.ResponseParser;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrServer;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.impl.HttpSolrServer;
+import org.apache.solr.client.solrj.impl.NoOpResponseParser;
+import org.apache.solr.client.solrj.impl.XMLResponseParser;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.SolrResourceLoader;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.List;
+
+/**
+ * A test for parsing Solr response from query by NoOpResponseParser.
+ * @see org.apache.solr.client.solrj.impl.NoOpResponseParser
+ * @see <a href="https://issues.apache.org/jira/browse/SOLR-5530">SOLR-5530</a>
+ */
+public class NoOpResponseParserTest extends SolrJettyTestBase {
+
+ private static InputStream getResponse() throws IOException {
+ return new SolrResourceLoader(null, null).openResource("solrj/sampleDateFacetResponse.xml");
+ }
+
+ @BeforeClass
+ public static void beforeTest() throws Exception {
+ createJetty(ExternalPaths.EXAMPLE_HOME, null, null);
+ }
+
+ @Before
+ public void doBefore() throws IOException, SolrServerException {
+ //add document and commit, and ensure it's there
+ SolrServer server1 = getSolrServer();
+ SolrInputDocument doc = new SolrInputDocument();
+ doc.addField("id", "1234");
+ server1.add(doc);
+ server1.commit();
+ }
+
+ /**
+ * Parse response from query using NoOpResponseParser.
+ * @throws Exception
+ */
+ @Test
+ public void testQueryParse() throws Exception {
+ HttpSolrServer server = (HttpSolrServer) createNewSolrServer();
+ SolrQuery query = new SolrQuery("id:1234");
+ QueryRequest req = new QueryRequest(query);
+ server.setParser(new NoOpResponseParser());
+ NamedList<Object> resp = server.request(req);
+ String responseString = (String) resp.get("response");
+
+ assertResponse(responseString);
+ }
+
+ private void assertResponse(String responseString) {
+ ResponseParser xmlResponseParser = new XMLResponseParser();
+ NamedList expectedResponse = xmlResponseParser.processResponse(IOUtils.toInputStream(responseString), "UTF-8");
+ List<SolrDocument> documentList = (List<SolrDocument>) expectedResponse.getAll("response").get(0);
+ assertEquals(1, documentList.size());
+ SolrDocument solrDocument = documentList.get(0);
+ assertEquals("1234", String.valueOf(solrDocument.getFieldValue("id")));
+ }
+
+ /**
+ * Parse response from java.io.Reader.
+ * @throws Exception
+ */
+ @Test
+ public void testReaderResponse() throws Exception {
+ NoOpResponseParser parser = new NoOpResponseParser();
+ try (final InputStream is = getResponse()) {
+ assertNotNull(is);
+ Reader in = new InputStreamReader(is, "UTF-8");
+ NamedList<Object> response = parser.processResponse(in);
+ assertNotNull(response.get("response"));
+ String expectedResponse = IOUtils.toString(getResponse(), "UTF-8");
+ assertEquals(expectedResponse, response.get("response"));
+ }
+
+ }
+
+ /**
+ * Parse response from java.io.InputStream.
+ * @throws Exception
+ */
+ @Test
+ public void testInputStreamResponse() throws Exception {
+ NoOpResponseParser parser = new NoOpResponseParser();
+ try (final InputStream is = getResponse()) {
+ assertNotNull(is);
+ NamedList<Object> response = parser.processResponse(is, "UTF-8");
+
+ assertNotNull(response.get("response"));
+ String expectedResponse = IOUtils.toString(getResponse(), "UTF-8");
+ assertEquals(expectedResponse, response.get("response"));
+ }
+ }
+}