SOLR-973 -- CommonsHttpSolrServer writes the xml directly to the server

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@747790 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-02-25 14:13:00 +00:00
parent 3afcd7a175
commit 47487967fc
4 changed files with 166 additions and 43 deletions

View File

@ -193,6 +193,9 @@ Optimizations
7. SOLR-921: SolrResourceLoader must cache short class name vs fully qualified classname
(Noble Paul, hossman via shalin)
8. SOLR-973: CommonsHttpSolrServer writes the xml directly to the server.
(Noble Paul via shalin)
Bug Fixes
----------------------
1. SOLR-774: Fixed logging level display (Sean Timm via Otis Gospodnetic)

View File

@ -43,13 +43,25 @@ public class BinaryRequestWriter extends RequestWriter {
isNull(updateRequest.getDeleteById()) &&
isNull(updateRequest.getDeleteQuery())) {
return null;
}
List<ContentStream> l = new ArrayList<ContentStream>();
l.add(new LazyContentStream(updateRequest));
return l;
} else {
return super.getContentStreams(req);
}
}
public String getUpdateContentType() {
return "application/octet-stream";
}
public ContentStream getContentStream(final UpdateRequest request) throws IOException {
final BAOS baos = new BAOS();
new JavaBinUpdateRequestCodec().marshal(updateRequest, baos);
List<ContentStream> l = new ArrayList<ContentStream>(1);
l.add(new ContentStream() {
new JavaBinUpdateRequestCodec().marshal(request, baos);
return new ContentStream() {
public String getName() {
return null;
}
@ -74,20 +86,17 @@ public class BinaryRequestWriter extends RequestWriter {
public Reader getReader() throws IOException {
throw new RuntimeException("No reader available . this is a binarystream");
}
});
return l;
} else {
return super.getContentStreams(req);
};
}
public void write(SolrRequest request, OutputStream os) throws IOException {
if (request instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) request;
new JavaBinUpdateRequestCodec().marshal(updateRequest, os);
}
private boolean isNull(List l) {
return l == null || l.isEmpty();
}
/*
}/*
* A hack to get access to the protected internal buffer and avoid an additional copy
*/
class BAOS extends ByteArrayOutputStream {

View File

@ -41,6 +41,7 @@ import org.apache.commons.httpclient.NoHttpResponseException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.PartBase;
@ -350,17 +351,39 @@ public class CommonsHttpSolrServer extends SolrServer
}
// It is has one stream, it is the post body, put the params in the URL
else {
String pstr = ClientUtils.toQueryString( params, false );
PostMethod post = new PostMethod( url+pstr );
String pstr = ClientUtils.toQueryString(params, false);
PostMethod post = new PostMethod(url + pstr);
// Single stream as body
// Using a loop just to get the first one
for( ContentStream content : streams ) {
post.setRequestEntity(
new InputStreamRequestEntity( content.getStream(), content.getContentType())
);
final ContentStream[] contentStream = new ContentStream[1];
for (ContentStream content : streams) {
contentStream[0] = content;
break;
}
if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
post.setRequestEntity(new RequestEntity() {
public long getContentLength() {
return -1;
}
public String getContentType() {
return contentStream[0].getContentType();
}
public boolean isRepeatable() {
return false;
}
public void writeRequest(OutputStream outputStream) throws IOException {
((RequestWriter.LazyContentStream) contentStream[0]).writeTo(outputStream);
}
}
);
} else {
post.setRequestEntity(new InputStreamRequestEntity(contentStream[0].getStream(), contentStream[0].getContentType()));
}
method = post;
}
}

View File

@ -18,26 +18,114 @@
package org.apache.solr.client.solrj.request;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* A RequestWriter is used to write requests to Solr.
* <p/>
* A subclass can override the methods in this class to supply a custom format in which a request can be sent.
* @since solr 1.4
*
* @version $Id$
* @since solr 1.4
*/
public class RequestWriter {
public Collection<ContentStream> getContentStreams(SolrRequest req) throws IOException {
if (req instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) req;
if (isEmpty(updateRequest)) return null;
List<ContentStream> l = new ArrayList<ContentStream>();
l.add(new LazyContentStream(updateRequest));
return l;
}
return req.getContentStreams();
}
private boolean isEmpty(UpdateRequest updateRequest) {
return isNull(updateRequest.getDocuments()) &&
isNull(updateRequest.getDeleteById()) &&
isNull(updateRequest.getDeleteQuery());
}
public String getPath(SolrRequest req) {
return req.getPath();
}
public ContentStream getContentStream(UpdateRequest req) throws IOException {
return new ContentStreamBase.StringStream(req.getXML());
}
public void write(SolrRequest request, OutputStream os) throws IOException {
if (request instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) request;
OutputStreamWriter writer = new OutputStreamWriter(os);
updateRequest.writeXML(writer);
writer.flush();
}
}
public String getUpdateContentType() {
return ClientUtils.TEXT_XML;
}
public class LazyContentStream implements ContentStream {
ContentStream contentStream = null;
UpdateRequest req = null;
public LazyContentStream(UpdateRequest req) {
this.req = req;
}
private ContentStream getDelegate() {
if (contentStream == null) {
try {
contentStream = getContentStream(req);
} catch (IOException e) {
throw new RuntimeException("Unable to write xml into a stream", e);
}
}
return contentStream;
}
public String getName() {
return getDelegate().getName();
}
public String getSourceInfo() {
return getDelegate().getSourceInfo();
}
public String getContentType() {
return getUpdateContentType();
}
public Long getSize() {
return getDelegate().getSize();
}
public InputStream getStream() throws IOException {
return getDelegate().getStream();
}
public Reader getReader() throws IOException {
return getDelegate().getReader();
}
public void writeTo(OutputStream os) throws IOException {
write(req, os);
}
}
protected boolean isNull(List l) {
return l == null || l.isEmpty();
}
}