SOLR-96: Fix XML parsing in XMLUpdateRequestHandler and DocumentAnalysisRequestHandler to respect charset from XML file and only use HTTP header's "Content-Type" as a "hint"

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1066819 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2011-02-03 14:49:06 +00:00
parent 7161062eee
commit b2e9fa1d26
3 changed files with 34 additions and 18 deletions

View File

@ -658,6 +658,10 @@ Bug Fixes
* SOLR-2156: SnapPuller fails to clean Old Index Directories on Full Copy
(Jayendra Patil via yonik)
* SOLR-96: Fix XML parsing in XMLUpdateRequestHandler and
DocumentAnalysisRequestHandler to respect charset from XML file and only
use HTTP header's "Content-Type" as a "hint". (Uwe Schindler)
Other Changes
----------------------

View File

@ -27,6 +27,7 @@ import org.apache.solr.common.params.AnalysisParams;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.request.SolrQueryRequest;
@ -41,7 +42,7 @@ import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStream;
import java.util.*;
/**
@ -157,10 +158,14 @@ public class DocumentAnalysisRequestHandler extends AnalysisRequestHandlerBase {
request.setShowMatch(showMatch);
ContentStream stream = extractSingleContentStream(req);
Reader reader = stream.getReader();
XMLStreamReader parser = inputFactory.createXMLStreamReader(reader);
InputStream is = null;
XMLStreamReader parser = null;
try {
is = stream.getStream();
final String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
parser = (charset == null) ?
inputFactory.createXMLStreamReader(is) : inputFactory.createXMLStreamReader(is, charset);
while (true) {
int event = parser.next();
@ -182,8 +187,8 @@ public class DocumentAnalysisRequestHandler extends AnalysisRequestHandlerBase {
}
} finally {
parser.close();
IOUtils.closeQuietly(reader);
if (parser != null) parser.close();
IOUtils.closeQuietly(is);
}
}

View File

@ -24,6 +24,7 @@ import org.apache.solr.update.DeleteUpdateCommand;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
@ -36,8 +37,8 @@ import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.transform.TransformerConfigurationException;
import java.io.Reader;
import java.io.StringReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
@ -57,22 +58,28 @@ class XMLLoader extends ContentStreamLoader {
@Override
public void load(SolrQueryRequest req, SolrQueryResponse rsp, ContentStream stream) throws Exception {
errHeader = "XMLLoader: " + stream.getSourceInfo();
Reader reader = null;
InputStream is = null;
XMLStreamReader parser = null;
try {
reader = stream.getReader();
is = stream.getStream();
final String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
if (XmlUpdateRequestHandler.log.isTraceEnabled()) {
String body = IOUtils.toString(reader);
XmlUpdateRequestHandler.log.trace("body", body);
reader = new StringReader(body);
final byte[] body = IOUtils.toByteArray(is);
// TODO: The charset may be wrong, as the real charset is later
// determined by the XML parser, the content-type is only used as a hint!
XmlUpdateRequestHandler.log.trace("body", new String(body, (charset == null) ?
ContentStreamBase.DEFAULT_CHARSET : charset));
IOUtils.closeQuietly(is);
is = new ByteArrayInputStream(body);
}
XMLStreamReader parser = inputFactory.createXMLStreamReader(reader);
parser = (charset == null) ?
inputFactory.createXMLStreamReader(is) : inputFactory.createXMLStreamReader(is, charset);
this.processUpdate(req, processor, parser);
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e.getMessage(), e);
} finally {
IOUtils.closeQuietly(reader);
if (parser != null) parser.close();
IOUtils.closeQuietly(is);
}
}