Permalink
Browse files

use new parseToString for simpler handling.

  • Loading branch information...
1 parent 864375b commit 0e03eb2ccb63dfc2c5a8fbb025d9aad9d633b7b8 Robert Newson committed Dec 21, 2009
@@ -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);
}
@@ -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;
}
};

0 comments on commit 0e03eb2

Please sign in to comment.