mirror of https://github.com/apache/lucene.git
SOLR-5530: Added a NoOpResponseParser for SolrJ which puts the entire raw response into an entry in the NamedList
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1564709 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9cb69be380
commit
e262c75e81
|
@ -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
|
||||
----------------------
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue