diff --git a/src/main/java/com/github/rnewson/couchdb/lucene/Tika.java b/src/main/java/com/github/rnewson/couchdb/lucene/Tika.java index adeb06f0..e59b3f80 100644 --- a/src/main/java/com/github/rnewson/couchdb/lucene/Tika.java +++ b/src/main/java/com/github/rnewson/couchdb/lucene/Tika.java @@ -20,12 +20,11 @@ import java.io.IOException; import java.io.InputStream; -import java.io.Reader; -import org.apache.commons.io.IOUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.lucene.document.Document; +import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.DublinCore; import org.apache.tika.metadata.HttpHeaders; import org.apache.tika.metadata.Metadata; @@ -47,23 +46,18 @@ public void parse(final InputStream in, final String contentType, final String f throws IOException { final Metadata md = new Metadata(); md.set(HttpHeaders.CONTENT_TYPE, contentType); - final Reader reader = tika.parse(in, md); - final String body; try { - try { - body = IOUtils.toString(reader); - } finally { - reader.close(); - } + // Add body text. + doc.add(text(fieldName, tika.parseToString(in, md), false)); } catch (final IOException e) { log.warn("Failed to index an attachment.", e); return; + } catch (final TikaException e) { + log.warn("Failed to parse an attachment.", e); + return; } - // Add body text. - doc.add(text(fieldName, body, false)); - // Add DC attributes. addDublinCoreAttributes(md, doc); } diff --git a/src/main/java/com/github/rnewson/couchdb/rhino/RhinoDocument.java b/src/main/java/com/github/rnewson/couchdb/rhino/RhinoDocument.java index 0fe6ad23..037ef792 100644 --- a/src/main/java/com/github/rnewson/couchdb/rhino/RhinoDocument.java +++ b/src/main/java/com/github/rnewson/couchdb/rhino/RhinoDocument.java @@ -99,7 +99,7 @@ public static void jsFunction_add(final Context cx, final Scriptable thisObj, fi if (args[0] == null) { throw Context.reportRuntimeError("first argument must be non-null."); } - + if (args[0] instanceof Undefined) { // Ignore return; @@ -186,12 +186,7 @@ private void addAttachment(final RhinoAttachment attachment, final String id, fi public Void handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { final HttpEntity entity = response.getEntity(); - final InputStream in = entity.getContent(); - try { - Tika.INSTANCE.parse(in, entity.getContentType().getValue(), attachment.fieldName, out); - } finally { - in.close(); - } + Tika.INSTANCE.parse(entity.getContent(), entity.getContentType().getValue(), attachment.fieldName, out); return null; } };