Skip to content

Commit

Permalink
o remove commons-io usage and use Guava instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason van Zyl committed Mar 31, 2012
1 parent 1aa00a8 commit 755e046
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 95 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@
<version>2.3.10</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
package com.google.sitebricks.mail.imap;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.apache.commons.io.IOUtils;
import org.apache.james.mime4j.codec.DecodeMonitor;
import org.apache.james.mime4j.codec.DecoderUtil;
import org.jetbrains.annotations.TestOnly;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

import org.apache.james.mime4j.codec.DecodeMonitor;
import org.apache.james.mime4j.codec.DecoderUtil;
import org.jetbrains.annotations.TestOnly;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;

/**
* Extracts a full Message body from an IMAP fetch. Specifically
* a "fetch body[]" command which comes back with the raw content of the
Expand Down Expand Up @@ -496,8 +504,8 @@ private static String decode(String body, String encoding, String charset) {
// Second time around. Apparently some are slipping through.
charset = Parsing.stripQuotes(charset);

return IOUtils.toString(
MimeUtility.decode(new ByteArrayInputStream(body.getBytes(charset)), encoding), charset);
return CharStreams.toString(
new InputStreamReader(MimeUtility.decode(new ByteArrayInputStream(body.getBytes(charset)), encoding), charset));
} catch (UnsupportedEncodingException e) {
// In this case, just return it as is and look it up later.
log.warn("Encountered unknown encoding '{}'. Treating it as a raw string.", charset, e);
Expand Down Expand Up @@ -539,7 +547,7 @@ private static byte[] readBodyAsBytes(String transferEncoding,
// Decode if this is encoded as binary-to-text.
if (null != transferEncoding)
try {
bytes = IOUtils.toByteArray(MimeUtility.decode(new ByteArrayInputStream(bytes),
bytes = ByteStreams.toByteArray(MimeUtility.decode(new ByteArrayInputStream(bytes),
transferEncoding));
} catch (MessagingException e) {
log.error("Unable to decode message body, proceeding with raw bytes.", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.google.sitebricks.mail.imap;

import com.google.common.base.Charsets;
import com.google.common.collect.Multimap;
import com.google.common.io.Resources;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.ParseException;
import java.util.Collection;
Expand All @@ -20,7 +18,17 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.testng.Assert.*;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.testng.annotations.Test;

import com.google.common.base.Charsets;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;

/**
* @author dhanji@gmail.com (Dhanji R. Prasanna)
Expand Down Expand Up @@ -90,7 +98,7 @@ public final void testAwkwardGmailEmailStream(boolean forceTruncatorGroping) thr
// Folded headers with tabs + spaces, repeat headers, one body.
Message message = extract.get(0);
String expectedHeaders =
IOUtils.toString(MessageBodyExtractorTest.class.getResourceAsStream("fetch_headers_1.txt"));
CharStreams.toString(new InputStreamReader(MessageBodyExtractorTest.class.getResourceAsStream("fetch_headers_1.txt")));
assertEquals(message.getHeaders().toString(), expectedHeaders);

assertEquals(1, message.getBodyParts().size());
Expand All @@ -100,9 +108,9 @@ public final void testAwkwardGmailEmailStream(boolean forceTruncatorGroping) thr

// We have to compare the raw bytes because the encoded string comes in as ISO-8859-1
// And Java literals are encoded as UTF-8.
assertEquals(part1.getBody().getBytes(), IOUtils.toByteArray(
assertEquals(part1.getBody().getBytes(), ByteStreams.toByteArray(
MessageBodyExtractorTest.class.getResourceAsStream("fetch_body_1_raw.dat")));
assertEquals(new String(part1.getBody().getBytes()), new String(IOUtils.toByteArray(
assertEquals(new String(part1.getBody().getBytes()), new String(ByteStreams.toByteArray(
MessageBodyExtractorTest.class.getResourceAsStream("fetch_body_1_raw.dat"))));

// ------------------------------------------------------------
Expand All @@ -119,8 +127,8 @@ public final void testAwkwardGmailEmailStream(boolean forceTruncatorGroping) thr
part1 = message.getBodyParts().get(0);
assertTrue(part1.getHeaders().isEmpty());
assertNull(part1.getBinBody());
assertEquals(part1.getBody(), IOUtils.toString(
MessageBodyExtractorTest.class.getResourceAsStream("fetch_body_2.txt")));
assertEquals(part1.getBody(), CharStreams.toString(
new InputStreamReader(MessageBodyExtractorTest.class.getResourceAsStream("fetch_body_2.txt"))));

// ------------------------------------------------------------
// Third message.
Expand Down Expand Up @@ -771,7 +779,7 @@ public final void testDecoding() throws MessagingException, IOException {
String charset = "ISO-8859-1";
final byte[] bytes = body.getBytes(charset);
final InputStream decoded = MimeUtility.decode(new ByteArrayInputStream(bytes), encoding);
String result = IOUtils.toString(decoded, charset);
String result = CharStreams.toString(new InputStreamReader(decoded, charset));
assertEquals(result, body);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.google.sitebricks.mail.imap;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import org.apache.commons.io.IOUtils;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import java.io.IOException;
import java.io.StringReader;
Expand All @@ -17,11 +15,14 @@
import java.util.List;
import java.util.Queue;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;

/**
* @author dhanji@gmail.com (Dhanji R. Prasanna)
Expand Down Expand Up @@ -104,7 +105,7 @@ public final void testSizeMarkerRegex() throws IOException, ParseException {
public final void testMultilineUnquotedSubjectWithCrLfs() throws IOException, ParseException {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract =
new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 27012 FETCH (X-GM-THRID 1279068049360518352 X-GM-MSGID 1279068049360518352 X-GM-LABELS () UID 60961 RFC822.SIZE 24305 INTERNALDATE \"27-Aug-2008 05:19:07 +0000\" FLAGS (\\Seen) ENVELOPE (\"27 Aug 2008 01:19:06 -0400\" {93}\n" +
"Ttkii Regency The quick brown fox ran over AK 7 Day Arrival Notice\n" +
" - BATTY SCORTI - 16257294 ((\"Ttkii E-Concierge\" NIL \"Concierge\" \"quickcolamakerXL.com\")) ((\"Ttkii E-Concierge\" NIL \"Concierge\" \"quickcolamakerXL.com\")) ((\"Ttkii E-Concierge\" NIL \"Concierge\" \"quickcolamakerXL.com\")) ((NIL NIL \"BATTY.SCORTI\" \"gmail.com\")) NIL NIL NIL \"<20010927011966.SM02008@CDC0044>\"))\n" +
Expand Down Expand Up @@ -149,7 +150,7 @@ public final void testMultilineUnquotedSubjectWithCrLfs() throws IOException, Pa
public final void testMultilineUnquotedSubjectWithLfs() throws IOException, ParseException {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract =
new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 4017 FETCH (X-GM-THRID 13603320389284585 X-GM-MSGID 1460332038925224585 X-GM-LABELS (\"\\\\Inbox\") UID 5474 RFC822.SIZE 24864 INTERNALDATE \"10-Feb-2011 04:55:27 +0000\" FLAGS (\\Seen) ENVELOPE (\"Thu, 10 Feb 2011 15:55:20 +1100\" {96}\n" +
"ASIX News - Social Innovation Sydney Barcamp - Hub Melbourne news - Social\n" +
"Impact Scholarships ((\"ASIX - Australian Social Innovation eXchange\" NIL \"contact\" \"asix.org.au\")) ((\"ASIX - Australian Social Innovation eXchange\" NIL \"contact\" \"asix.org.au\")) ((\"ASIX - Australian Social Innovation eXchange\" NIL \"contact\" \"asix.org.au\")) ((\"dhanji\" NIL \"mick\" \"rethrick.com\")) NIL NIL NIL \"<E1123Yq-756Z-Fs@c.consumer.fluent.io>\"))\n" +
Expand Down Expand Up @@ -193,7 +194,7 @@ public final void testMultilineUnquotedSubjectWithLfs() throws IOException, Pars
@Test
public void testMutlilineUnquotedWithEmbeddedQuote() throws Exception {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract = new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
final List<MessageStatus> extract = new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 234 FETCH (X-GM-THRID 23432423423 X-GM-MSGID 23432423 X-GM-LABELS () UID 210140 RFC822.SIZE 58816" +
" INTERNALDATE \"17-Oct-2009 17:27:26 +0000\" FLAGS () ENVELOPE (\"17 Oct 2009 13:27:22 -0400\" {75}\n" +
"A Reminder to attend \"The Windows Vista Positioning Disaster:\n" +
Expand All @@ -208,7 +209,7 @@ public void testMutlilineUnquotedWithEmbeddedQuote() throws Exception {
@Test
public void testMutlilineUnquotedWithEmbeddedQuote2() throws Exception {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract = new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
final List<MessageStatus> extract = new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 28468 FETCH (X-GM-THRID 34543535 X-GM-MSGID 345435435 X-GM-LABELS (\"\\\\Important\" Notifications) UID 345534 RFC822.SIZE 84553 INTERNALDATE \"04-Oct-2011 07:30:02 +0000\" FLAGS (\\Seen) ENVELOPE (\"04 Oct 2011 00:30:00 -0700\" {998}\n" +
"<html>\n" +
"<head>\n" +
Expand Down Expand Up @@ -266,7 +267,7 @@ public void testMutlilineUnquotedWithEmbeddedQuote2() throws Exception {
public void testMultilineWithWhitespace() throws Exception {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract =
new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 5553 FETCH (X-GM-THRID 234324 X-GM-MSGID 2343242 X-GM-LABELS () UID 1213437 RFC822.SIZE 1494343 INTERNALDATE \"08-Jun-2006 22:48:06 +0000\" FLAGS () ENVELOPE (\"Fri, 09 Jun 2006 06:50:32 +0800\" \"RE: Long game\" ((NIL NIL \"foo\" \"bar.com.au\")) ((NIL NIL \"foo\" \"bar.com.au\")) ((NIL NIL \"foo\" \"bar.com.au\")) ((NIL NIL \"foo\" \"bar.com.au\"))" +
"(({23}\n" +
"abcdef,\n" +
Expand All @@ -284,7 +285,7 @@ public void testMultilineWithWhitespace() throws Exception {
public void testMultilineUnquotedCC() throws Exception {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract =
new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 67236 FETCH (X-GM-THRID 132443254747 X-GM-MSGID 13256345038 X-GM-LABELS (\"\\\\Inbox\") UID 197888 RFC822.SIZE 34646 INTERNALDATE \"23-Jan-2010 05:06:26 +0000\" FLAGS (\\Seen) " +
"ENVELOPE (\"Sat, 23 Jan 2010 05:04:06 +0000\" \"QUOTED SUBJECT\" (({14}\n" +
"Other\n" +
Expand All @@ -306,7 +307,7 @@ public void testMultilineUnquotedCC() throws Exception {
public void testMultilineNewlineEdgeCase() throws Exception {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract =
new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 44788 FETCH (X-GM-THRID 34543534 X-GM-MSGID 34545 X-GM-LABELS (\"\\\\Inbox\") UID 61056 RFC822.SIZE 6154 INTERNALDATE \"17-May-2007 22:23:15 +0000\" FLAGS () ENVELOPE (\"Thu, 17 May 2007 15:23:14 -0700\" {29}\n" +
"Google Analytics New Version\n" +
" ((NIL NIL \"noreply\" \"google.com\")) ((NIL NIL \"noreply\" \"google.com\")) ((NIL NIL \"noreply\" \"google.com\")) ((NIL NIL \"foo\" \"gmail.com\")) NIL NIL NIL \"<345435@google.com>\"))")));
Expand All @@ -320,7 +321,7 @@ public void testMultilineNewlineEdgeCase() throws Exception {
public void testMultilineUnquotedMonster() throws Exception {
@SuppressWarnings("unchecked")
final List<MessageStatus> extract =
new MessageStatusExtractor().extract(IOUtils.readLines(new StringReader(
new MessageStatusExtractor().extract(CharStreams.readLines(new StringReader(
"* 17594 FETCH (X-GM-THRID 876876876 X-GM-MSGID 876876876876 X-GM-LABELS " +
"(Beer \"\\\\Important\") UID 99879 RFC822.SIZE 2535649 INTERNALDATE \"20-Jul-2011 11:57:32 +0000\" " +
"FLAGS (\\Seen) ENVELOPE (\"Wed, 20 Jul 2011 21:57:32 +1000\" \"subject\" ((\"dude\" NIL \"foo\" \"bar.com.au\")) ((\"dude\" NIL \"foo\" \"bar.com.au\")) ((\"dude\" NIL \"foo\" \"bar.com.au\")) ((\"dude\" NIL \"foo\" \"bar.com.au\")) NIL NIL {1319}\n" +
Expand Down
4 changes: 0 additions & 4 deletions sitebricks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.google.sitebricks;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import com.google.inject.Inject;
Expand All @@ -11,14 +20,6 @@
import com.google.sitebricks.client.Transport;
import com.google.sitebricks.headless.Request;
import com.google.sitebricks.http.Parameters;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.util.Enumeration;
import java.util.Map;

/**
* @author dhanji@gmail.com (Dhanji R. Prasanna)
Expand Down Expand Up @@ -68,7 +69,7 @@ public E as(Class<? extends Transport> transport) {

@Override
public void readTo(OutputStream out) throws IOException {
IOUtils.copy(servletRequest.getInputStream(), out);
ByteStreams.copy(servletRequest.getInputStream(), out);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.google.sitebricks.compiler;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.io.CharStreams;

/**
* @author Dhanji R. Prasanna (dhanji@gmail.com)
*/
Expand All @@ -25,7 +25,7 @@ public TemplateCompileException(Class<?> page, String template,
this.warnings = warnings;
try {
//noinspection unchecked
this.templateLines = IOUtils.readLines(new StringReader(template));
this.templateLines = CharStreams.readLines(new StringReader(template));
} catch (IOException e) {
throw new IllegalStateException("Fatal error, could not read template after compile", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.google.sitebricks.headless;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.sitebricks.client.Transport;
import com.google.sitebricks.client.transport.Text;
import com.google.sitebricks.rendering.Strings;
import com.google.sitebricks.rendering.Templates;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
* A builder implementation of the Reply interface.
Expand Down Expand Up @@ -181,7 +182,7 @@ void populate(Injector injector, HttpServletResponse response) throws IOExceptio
// Stream the response rather than marshalling it through a transport.
InputStream inputStream = (InputStream) entity;
try {
IOUtils.copy(inputStream, response.getOutputStream());
ByteStreams.copy(inputStream, response.getOutputStream());
} finally {
inputStream.close();
}
Expand Down
Loading

0 comments on commit 755e046

Please sign in to comment.