You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by sh...@apache.org on 2009/02/25 15:13:00 UTC

svn commit: r747790 - in /lucene/solr/trunk: ./ src/solrj/org/apache/solr/client/solrj/impl/ src/solrj/org/apache/solr/client/solrj/request/

Author: shalin
Date: Wed Feb 25 14:13:00 2009
New Revision: 747790

URL: http://svn.apache.org/viewvc?rev=747790&view=rev
Log:
SOLR-973 -- CommonsHttpSolrServer writes the xml directly to the server

Modified:
    lucene/solr/trunk/CHANGES.txt
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java

Modified: lucene/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?rev=747790&r1=747789&r2=747790&view=diff
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Wed Feb 25 14:13:00 2009
@@ -193,6 +193,9 @@
  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)

Modified: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java?rev=747790&r1=747789&r2=747790&view=diff
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java (original)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java Wed Feb 25 14:13:00 2009
@@ -43,39 +43,9 @@
               isNull(updateRequest.getDeleteById()) &&
               isNull(updateRequest.getDeleteQuery())) {
         return null;
-
       }
-
-      final BAOS baos = new BAOS();
-      new JavaBinUpdateRequestCodec().marshal(updateRequest, baos);
-      List<ContentStream> l = new ArrayList<ContentStream>(1);
-      l.add(new ContentStream() {
-        public String getName() {
-          return null;
-        }
-
-        public String getSourceInfo() {
-          return "javabin";
-        }
-
-        public String getContentType() {
-          return "application/octet-stream";
-        }
-
-        public Long getSize() // size if we know it, otherwise null
-        {
-          return new Long(baos.size());
-        }
-
-        public InputStream getStream() throws IOException {
-          return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
-        }
-
-        public Reader getReader() throws IOException {
-          throw new RuntimeException("No reader available . this is a binarystream");
-        }
-      });
-
+      List<ContentStream> l = new ArrayList<ContentStream>();
+      l.add(new LazyContentStream(updateRequest));
       return l;
     } else {
       return super.getContentStreams(req);
@@ -83,11 +53,50 @@
 
   }
 
-  private boolean isNull(List l) {
-    return l == null || l.isEmpty();
+
+  public String getUpdateContentType() {
+    return "application/octet-stream";
+  }
+
+  public ContentStream getContentStream(final UpdateRequest request) throws IOException {
+    final BAOS baos = new BAOS();
+      new JavaBinUpdateRequestCodec().marshal(request, baos);
+    return new ContentStream() {
+      public String getName() {
+        return null;
+      }
+
+      public String getSourceInfo() {
+        return "javabin";
+      }
+
+      public String getContentType() {
+        return "application/octet-stream";
+      }
+
+      public Long getSize() // size if we know it, otherwise null
+      {
+        return new Long(baos.size());
+      }
+
+      public InputStream getStream() throws IOException {
+        return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
+      }
+
+      public Reader getReader() throws IOException {
+        throw new RuntimeException("No reader available . this is a binarystream");
+      }
+    };
   }
 
-  /*
+
+  public void write(SolrRequest request, OutputStream os) throws IOException {
+    if (request instanceof UpdateRequest) {
+      UpdateRequest updateRequest = (UpdateRequest) request;
+      new JavaBinUpdateRequestCodec().marshal(updateRequest, os);
+    } 
+
+  }/*
    * A hack to get access to the protected internal buffer and avoid an additional copy 
    */
   class BAOS extends ByteArrayOutputStream {

Modified: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java?rev=747790&r1=747789&r2=747790&view=diff
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java (original)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java Wed Feb 25 14:13:00 2009
@@ -41,6 +41,7 @@
 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 @@
             }
             // 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;
             }
           }

Modified: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java?rev=747790&r1=747789&r2=747790&view=diff
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java (original)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java Wed Feb 25 14:13:00 2009
@@ -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();
+  }
 }