You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2011/04/07 14:12:21 UTC
svn commit: r1089842 - in /james/imap/trunk/message/src:
main/java/org/apache/james/imap/encode/
main/java/org/apache/james/imap/encode/base/
main/java/org/apache/james/imap/main/
test/java/org/apache/james/imap/encode/ test/java/org/apache/james/imap/...
Author: norman
Date: Thu Apr 7 12:12:20 2011
New Revision: 1089842
URL: http://svn.apache.org/viewvc?rev=1089842&view=rev
Log:
Move high-level operations from ImapResponseWriter to ImapResponseComposer and make it easy to chain operations. See IMAP-279
Removed:
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapResponseWriter.java
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/ImapResponseTest.java
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/MockImapResponseWriter.java
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/StatusResponseEncoderTest.java
james/imap/trunk/message/src/test/java/org/apache/james/imap/main/OutputStreamImapResponseWriterQuoteTest.java
james/imap/trunk/message/src/test/java/org/apache/james/imap/main/OutputStreamImapResponseWriterTest.java
Modified:
james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/ImapResponseWriter.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/base/ImapResponseComposerImpl.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ChannelImapResponseWriter.java
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/base/ByteImapResponseWriter.java
Modified: james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/ImapResponseWriter.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/ImapResponseWriter.java?rev=1089842&r1=1089841&r2=1089842&view=diff
==============================================================================
--- james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/ImapResponseWriter.java (original)
+++ james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/ImapResponseWriter.java Thu Apr 7 12:12:20 2011
@@ -20,6 +20,7 @@
package org.apache.james.imap.encode;
import java.io.IOException;
+import java.nio.ByteBuffer;
import org.apache.james.imap.message.response.Literal;
@@ -32,128 +33,24 @@ import org.apache.james.imap.message.res
* </p>
*/
public interface ImapResponseWriter {
-
- /**
- * Starts an untagged response.
- */
- void untagged() throws IOException;
-
- /**
- * Starts a tagged response.
- *
- * @param tag
- * the tag, not null
- */
- void tag(String tag) throws IOException;
-
- /**
- * Starts a continuation response.
- *
- * @param message
- * the message, not null
- */
- void continuation(String message) throws IOException;
+
/**
- * Writes a command name.
- *
- * @param commandName
- * the command name, not null
- */
- void commandName(String commandName) throws IOException;
-
- /**
- * Writes a message.
- *
- * @param message
- * the message, not null
- */
- void message(String message) throws IOException;
-
- void message(long number) throws IOException;
-
- /**
- * Writes a response code.
- *
- * @param responseCode
- * the response code, not null
- */
- void responseCode(String responseCode) throws IOException;
-
- /**
- * Writes a quoted message.
- *
- * @param message
- * message, not null
- */
- void quote(String message) throws IOException;
-
- /**
- * Opens a parenthesis - writes a <code>(</code>.
- */
- void openParen() throws IOException;
-
- /**
- * Do not write a space before the next production.
- *
- * @throws IOException
- */
- public void skipNextSpace() throws IOException;
-
- /**
- * Closes a parenthesis - writes a <code>)</code>.
- */
- void closeParen() throws IOException;
-
- /**
- * Closes a square bracket - writes a <code>[</code>.
- *
- * @throws IOException
- */
- void openSquareBracket() throws IOException;
-
- /**
- * Closes a square bracket - writes a <code>]</code>.
- *
- * @throws IOException
- */
- void closeSquareBracket() throws IOException;
-
- /**
- * Ends a response.
- *
- */
- void end() throws IOException;
-
- /**
- * Writes literal content
+ * Writes literal content to the client
*
* @param literal
* <code>Literal</code> to be written, not null
* @throws IOException
*/
- void literal(Literal literal) throws IOException;
-
+ void write(Literal literal) throws IOException;
+
/**
- * Writes given message converted to upper case. The message may be assumed
- * to be ASCII encoded. Conversion of characters MUST NOT be performed
- * according to the current locale but as per ASCII.
+ * Write a ByteBuffer to the client
*
- * @param message
- * ASCII encoded, not null
+ * @param buffer
+ * <code>ByteBuffer</code> to be written, not null
* @throws IOException
*/
- void upperCaseAscii(String message) throws IOException;
+ void write(ByteBuffer buffer) throws IOException;
- /**
- * Writes given message converted to upper case. The message may be assumed
- * to be ASCII encoded. Conversion of characters MUST NOT be performed
- * according to the current locale but as per ASCII. The message is
- * surrounded by quotes.
- *
- * @param message
- * ASCII encoded, not null
- * @throws IOException
- */
- void quoteUpperCaseAscii(String message) throws IOException;
}
Modified: james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/base/ImapResponseComposerImpl.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/base/ImapResponseComposerImpl.java?rev=1089842&r1=1089841&r2=1089842&view=diff
==============================================================================
--- james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/base/ImapResponseComposerImpl.java (original)
+++ james/imap/trunk/message/src/main/java/org/apache/james/imap/encode/base/ImapResponseComposerImpl.java Thu Apr 7 12:12:20 2011
@@ -20,6 +20,8 @@
package org.apache.james.imap.encode.base;
import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -55,164 +57,166 @@ public class ImapResponseComposerImpl im
private final ImapResponseWriter writer;
public ImapResponseComposerImpl(final ImapResponseWriter writer) {
- this.writer = writer;
+ this(writer, DEFAULT_BUFFER_SIZE);
}
+ private static final int LOWER_CASE_OFFSET = 'a' - 'A';
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#commandComplete(org.apache.james.imap.api.ImapCommand,
- * java.lang.String, java.lang.String)
- */
- public void commandComplete(final ImapCommand command,
- final String responseCode, final String tag) throws IOException {
- tag(tag);
- message(OK);
- responseCode(responseCode);
- commandName(command);
- message("completed.");
- end();
+ private static final int DEFAULT_BUFFER_SIZE = 128;
+
+ private final Charset usAscii;
+
+ private final ByteBuffer buffer;
+
+ private boolean skipNextSpace;
+
+
+ public ImapResponseComposerImpl(final ImapResponseWriter writer, final int bufferSize) {
+ skipNextSpace = false;
+ buffer = ByteBuffer.allocate(bufferSize);
+ usAscii = Charset.forName("US-ASCII");
+ this.writer = writer;
}
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#untaggedNoResponse(java.lang.String,
- * java.lang.String)
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#untaggedNoResponse(java.lang.String, java.lang.String)
*/
- public void untaggedNoResponse(String displayMessage, String responseCode)
+ public ImapResponseComposer untaggedNoResponse(String displayMessage, String responseCode)
throws IOException {
untagged();
message(NO);
responseCode(responseCode);
message(displayMessage);
end();
+ return this;
}
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#continuationResponse(String)
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#continuationResponse(java.lang.String)
*/
- public void continuationResponse(String message) throws IOException {
- writer.continuation(message);
+ public ImapResponseComposer continuationResponse(String message) throws IOException {
+ writeASCII(CONTINUATION + SP + message);
end();
+ return this;
}
- /**
- * @throws IOException
+
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#flagsResponse(javax.mail.Flags)
*/
- public void flagsResponse(Flags flags) throws IOException {
+ public ImapResponseComposer flagsResponse(Flags flags) throws IOException {
untagged();
flags(flags);
end();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#existsResponse(long)
*/
- public void existsResponse(long count) throws IOException {
+ public ImapResponseComposer existsResponse(long count) throws IOException {
untagged();
message(count);
message(EXISTS);
end();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#recentResponse(long)
*/
- public void recentResponse(long count) throws IOException {
+ public ImapResponseComposer recentResponse(long count) throws IOException {
untagged();
message(count);
message(RECENT);
end();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#expungeResponse(long)
*/
- public void expungeResponse(long msn) throws IOException {
+ public ImapResponseComposer expungeResponse(long msn) throws IOException {
untagged();
message(msn);
message(EXPUNGE);
end();
+ return this;
}
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#commandResponse(org.apache.james.imap.api.ImapCommand,
- * java.lang.String)
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#commandResponse(org.apache.james.imap.api.ImapCommand, java.lang.String)
*/
- public void commandResponse(ImapCommand command, String message)
+ public ImapResponseComposer commandResponse(ImapCommand command, String message)
throws IOException {
untagged();
- commandName(command);
+ commandName(command.getName());
message(message);
end();
+ return this;
}
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#taggedResponse(java.lang.String,
- * java.lang.String)
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#taggedResponse(java.lang.String, java.lang.String)
*/
- public void taggedResponse(String message, String tag) throws IOException {
+ public ImapResponseComposer taggedResponse(String message, String tag) throws IOException {
tag(tag);
message(message);
end();
+ return this;
}
- /**
- * @throws IOException
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#untaggedResponse(java.lang.String)
*/
- public void untaggedResponse(String message) throws IOException {
+ public ImapResponseComposer untaggedResponse(String message) throws IOException {
untagged();
message(message);
end();
+ return this;
}
- /**
- * @throws IOException
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#byeResponse(java.lang.String)
*/
- public void byeResponse(String message) throws IOException {
+ public ImapResponseComposer byeResponse(String message) throws IOException {
untaggedResponse(BYE + SP + message);
+ return this;
}
-
- /**
- * @throws IOException
+
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#hello(java.lang.String)
*/
- public void hello(String message) throws IOException {
+ public ImapResponseComposer hello(String message) throws IOException {
untaggedResponse(OK + SP + message);
+ return this;
}
- /**
- * @throws IOException
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#untagged()
*/
- public void untagged() throws IOException {
- writer.untagged();
- }
-
- private void commandName(final ImapCommand command) throws IOException {
- final String name = command.getName();
- commandName(name);
- }
-
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#commandName(java.lang.String)
- */
- public void commandName(final String name) throws IOException {
- writer.commandName(name);
+ public ImapResponseComposer untagged() throws IOException {
+ writeASCII(UNTAGGED);
+ return this;
}
- /**
- * @throws IOException
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#message(java.lang.String)
*/
public ImapResponseComposer message(final String message)
@@ -221,52 +225,46 @@ public class ImapResponseComposerImpl im
// TODO: consider message normalisation
// TODO: CR/NFs in message must be replaced
// TODO: probably best done in the writer
- writer.message(message);
+ space();
+ writeASCII(message);
+
}
return this;
}
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#message(long)
- */
- public void message(final long number) throws IOException {
- writer.message(number);
- }
-
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#responseCode(java.lang.String)
- */
- public void responseCode(final String responseCode) throws IOException {
+ private void responseCode(final String responseCode) throws IOException {
if (responseCode != null && !"".equals(responseCode)) {
- writer.responseCode(responseCode);
+ writeASCII(" [");
+ writeASCII(responseCode);
+ write(BYTE_CLOSE_SQUARE_BRACKET);
}
}
- /**
- * @throws IOException
+
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#end()
*/
- public void end() throws IOException {
- writer.end();
+ public ImapResponseComposer end() throws IOException {
+ write(LINE_END.getBytes());
+ return this;
}
- /**
- * @throws IOException
+
+ /*
+ * (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#tag(java.lang.String)
*/
- public void tag(String tag) throws IOException {
- writer.tag(tag);
+ public ImapResponseComposer tag(String tag) throws IOException {
+ writeASCII(tag);
+ return this;
}
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#statusResponse(java.lang.String,
- * org.apache.james.imap.api.ImapCommand, java.lang.String,
- * java.lang.String, Collection, long, java.lang.String)
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#statusResponse(java.lang.String, org.apache.james.imap.api.ImapCommand, java.lang.String, java.lang.String, java.util.Collection, boolean, long, java.lang.String)
*/
- public void statusResponse(String tag, ImapCommand command, String type,
+ public ImapResponseComposer statusResponse(String tag, ImapCommand command, String type,
String responseCode, Collection<String> parameters, boolean useParens, long number, String text)
throws IOException {
if (tag == null) {
@@ -282,8 +280,8 @@ public class ImapResponseComposerImpl im
message(number);
}
if (parameters != null && !parameters.isEmpty()) {
- if(useParens)
- openParen();
+ if(useParens)
+ openParen();
for (Iterator<String> it = parameters.iterator(); it.hasNext();) {
final String parameter = it.next();
message(parameter);
@@ -294,20 +292,21 @@ public class ImapResponseComposerImpl im
closeSquareBracket();
}
if (command != null) {
- commandName(command);
+ commandName(command.getName());
}
if (text != null && !"".equals(text)) {
message(text);
}
end();
+ return this;
}
- /**
- * @throws IOException
- * @see org.apache.james.imap.encode.ImapResponseComposer#statusResponse(Long,
- * Long, Long, Long, Long, String)
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#statusResponse(java.lang.Long, java.lang.Long, java.lang.Long, java.lang.Long, java.lang.Long, java.lang.String)
*/
- public void statusResponse(Long messages, Long recent, Long uidNext,
+ public ImapResponseComposer statusResponse(Long messages, Long recent, Long uidNext,
Long uidValidity, Long unseen, String mailboxName)
throws IOException {
untagged();
@@ -347,13 +346,14 @@ public class ImapResponseComposerImpl im
closeParen();
end();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#listResponse(java.lang.String, java.util.List, java.lang.String, java.lang.String)
*/
- public void listResponse(String typeName, List<String> attributes,
+ public ImapResponseComposer listResponse(String typeName, List<String> attributes,
char hierarchyDelimiter, String name) throws IOException {
untagged();
message(typeName);
@@ -374,14 +374,7 @@ public class ImapResponseComposerImpl im
quote(name);
end();
- }
-
- /*
- * (non-Javadoc)
- * @see org.apache.james.imap.encode.ImapResponseComposer#quote(java.lang.String)
- */
- public void quote(String message) throws IOException {
- writer.quote(message);
+ return this;
}
/*
@@ -389,7 +382,7 @@ public class ImapResponseComposerImpl im
* @see org.apache.james.imap.encode.ImapResponseComposer#closeParen()
*/
public ImapResponseComposer closeParen() throws IOException {
- writer.closeParen();
+ closeBracket(BYTE_CLOSING_PARENTHESIS);
return this;
}
@@ -398,7 +391,7 @@ public class ImapResponseComposerImpl im
* @see org.apache.james.imap.encode.ImapResponseComposer#openParen()
*/
public ImapResponseComposer openParen() throws IOException {
- writer.openParen();
+ openBracket(BYTE_OPENING_PARENTHESIS);
return this;
}
@@ -406,11 +399,12 @@ public class ImapResponseComposerImpl im
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#searchResponse(long[])
*/
- public void searchResponse(long[] ids) throws IOException {
+ public ImapResponseComposer searchResponse(long[] ids) throws IOException {
untagged();
message(ImapConstants.SEARCH_RESPONSE_NAME);
message(ids);
end();
+ return this;
}
private void message(long[] ids) throws IOException {
@@ -427,7 +421,7 @@ public class ImapResponseComposerImpl im
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#flags(javax.mail.Flags)
*/
- public void flags(Flags flags) throws IOException {
+ public ImapResponseComposer flags(Flags flags) throws IOException {
message(FLAGS);
openParen();
if (flags.contains(Flags.Flag.ANSWERED)) {
@@ -449,41 +443,37 @@ public class ImapResponseComposerImpl im
message("\\Seen");
}
closeParen();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#closeFetchResponse()
*/
- public void closeFetchResponse() throws IOException {
+ public ImapResponseComposer closeFetchResponse() throws IOException {
closeParen();
end();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#openFetchResponse(long)
*/
- public void openFetchResponse(long msn) throws IOException {
+ public ImapResponseComposer openFetchResponse(long msn) throws IOException {
untagged();
message(msn);
message(FETCH);
openParen();
+ return this;
}
- /*
- * (non-Javadoc)
- * @see org.apache.james.imap.encode.ImapResponseComposer#literal(org.apache.james.imap.message.response.Literal)
- */
- public void literal(Literal literal) throws IOException {
- writer.literal(literal);
- }
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#address(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
- public void address(String name, String domainList, String mailbox,
+ public ImapResponseComposer address(String name, String domainList, String mailbox,
String host) throws IOException {
skipNextSpace();
openParen();
@@ -492,48 +482,53 @@ public class ImapResponseComposerImpl im
nillableQuote(mailbox);
nillableQuote(host);
closeParen();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#endAddresses()
*/
- public void endAddresses() throws IOException {
+ public ImapResponseComposer endAddresses() throws IOException {
closeParen();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#endEnvelope(java.lang.String, java.lang.String)
*/
- public void endEnvelope(String inReplyTo, String messageId)
+ public ImapResponseComposer endEnvelope(String inReplyTo, String messageId)
throws IOException {
nillableQuote(inReplyTo);
nillableQuote(messageId);
closeParen();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#nil()
*/
- public void nil() throws IOException {
+ public ImapResponseComposer nil() throws IOException {
message(NIL);
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#startAddresses()
*/
- public void startAddresses() throws IOException {
+ public ImapResponseComposer startAddresses() throws IOException {
openParen();
+ return this;
}
/*
* (non-Javadoc)
* @see org.apache.james.imap.encode.ImapResponseComposer#startEnvelope(java.lang.String, java.lang.String, boolean)
*/
- public void startEnvelope(String date, String subject,
+ public ImapResponseComposer startEnvelope(String date, String subject,
boolean prefixWithName) throws IOException {
if (prefixWithName) {
message(ENVELOPE);
@@ -541,6 +536,7 @@ public class ImapResponseComposerImpl im
openParen();
nillableQuote(date);
nillableQuote(subject);
+ return this;
}
/*
@@ -557,21 +553,6 @@ public class ImapResponseComposerImpl im
return this;
}
- /*
- * (non-Javadoc)
- * @see org.apache.james.imap.encode.ImapResponseComposer#skipNextSpace()
- */
- public void skipNextSpace() throws IOException {
- writer.skipNextSpace();
- }
-
- public void closeSquareBracket() throws IOException {
- writer.closeSquareBracket();
- }
-
- public void openSquareBracket() throws IOException {
- writer.openSquareBracket();
- }
/*
* (non-Javadoc)
@@ -617,7 +598,7 @@ public class ImapResponseComposerImpl im
if (message == null) {
nil();
} else {
- writer.upperCaseAscii(message);
+ upperCaseAscii(message, false);
}
return this;
}
@@ -631,7 +612,7 @@ public class ImapResponseComposerImpl im
if (message == null) {
nil();
} else {
- writer.quoteUpperCaseAscii(message);
+ upperCaseAscii(message, true);
}
return this;
}
@@ -649,4 +630,163 @@ public class ImapResponseComposerImpl im
end();
return this;
}
+
+
+ private void writeASCII(final String string) throws IOException {
+ final ByteBuffer buffer = usAscii.encode(string);
+ writer.write(buffer);
+ }
+
+ private void write(byte[] bytes) throws IOException {
+ final ByteBuffer wrap = ByteBuffer.wrap(bytes);
+ writer.write(wrap);
+ }
+
+ private void write(byte b) throws IOException {
+ final ByteBuffer wrap = ByteBuffer.wrap(new byte[] {b});
+ writer.write(wrap);
+ }
+
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#message(long)
+ */
+ public ImapResponseComposer message(long number) throws IOException {
+ space();
+ writeASCII(Long.toString(number));
+ return this;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#commandName(java.lang.String)
+ */
+ public ImapResponseComposer commandName(String commandName) throws IOException {
+ space();
+ writeASCII(commandName);
+ return this;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#quote(java.lang.String)
+ */
+ public void quote(String message) throws IOException {
+ space();
+ final int length = message.length();
+ buffer.clear();
+ buffer.put(BYTE_DQUOTE);
+ for (int i = 0; i < length; i++) {
+ writeIfFull();
+ char character = message.charAt(i);
+ if (character == ImapConstants.BACK_SLASH || character == DQUOTE) {
+ buffer.put(BYTE_BACK_SLASH);
+ }
+ writeIfFull();
+ // 7-bit ASCII only
+ if (character > 128) {
+ buffer.put(BYTE_QUESTION);
+ } else {
+ buffer.put((byte) character);
+ }
+ }
+ writeIfFull();
+ buffer.put(BYTE_DQUOTE);
+ buffer.flip();
+ writer.write(buffer);
+ }
+
+ private void writeIfFull() throws IOException {
+ if (!buffer.hasRemaining()) {
+ buffer.flip();
+ writer.write(buffer);
+ buffer.clear();
+ }
+ }
+
+
+
+ private void closeBracket(final byte bracket) throws IOException {
+ write(bracket);
+ clearSkipNextSpace();
+ }
+
+
+ private void openBracket(final byte bracket) throws IOException {
+ space();
+ write(bracket);
+ skipNextSpace();
+ }
+
+ private void clearSkipNextSpace() {
+ skipNextSpace = false;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#skipNextSpace()
+ */
+ public void skipNextSpace() {
+ skipNextSpace = true;
+ }
+
+ private void space() throws IOException {
+ if (skipNextSpace) {
+ skipNextSpace = false;
+ } else {
+ write(SP.getBytes());
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.encode.ImapResponseComposer#literal(org.apache.james.imap.message.response.Literal)
+ */
+ public void literal(Literal literal) throws IOException {
+ space();
+ write(BYTE_OPEN_BRACE);
+ final long size = literal.size();
+ writeASCII(Long.toString(size));
+ write(BYTE_CLOSE_BRACE);
+ write(LINE_END.getBytes());
+ if (size > 0) {
+ writer.write(literal);
+ }
+ }
+
+ private void closeSquareBracket() throws IOException {
+ closeBracket(BYTE_CLOSE_SQUARE_BRACKET);
+ }
+
+ private void openSquareBracket() throws IOException {
+ openBracket(BYTE_OPEN_SQUARE_BRACKET);
+ }
+
+
+ private void upperCaseAscii(String message, boolean quote)
+ throws IOException {
+ space();
+ final int length = message.length();
+ buffer.clear();
+ if (quote) {
+ buffer.put(BYTE_DQUOTE);
+ }
+ for (int i = 0; i < length; i++) {
+ writeIfFull();
+ final char next = message.charAt(i);
+ if (next >= 'a' && next <= 'z') {
+ buffer.put((byte) (next - LOWER_CASE_OFFSET));
+ } else {
+ buffer.put((byte) (next));
+ }
+ }
+ writeIfFull();
+ if (quote) {
+ buffer.put(BYTE_DQUOTE);
+ }
+ buffer.flip();
+ writer.write(buffer);
+ }
+
}
Modified: james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ChannelImapResponseWriter.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ChannelImapResponseWriter.java?rev=1089842&r1=1089841&r2=1089842&view=diff
==============================================================================
--- james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ChannelImapResponseWriter.java (original)
+++ james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ChannelImapResponseWriter.java Thu Apr 7 12:12:20 2011
@@ -23,13 +23,14 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
+import org.apache.james.imap.encode.ImapResponseWriter;
import org.apache.james.imap.message.response.Literal;
/**
* Class providing methods to send response messages from the server to the
* client.
*/
-public class ChannelImapResponseWriter extends AbstractImapResponseWriter {
+public class ChannelImapResponseWriter implements ImapResponseWriter {
private final WritableByteChannel out;
@@ -40,19 +41,12 @@ public class ChannelImapResponseWriter e
this.out = out;
}
- public ChannelImapResponseWriter(final WritableByteChannel out,
- final int bufferSize) {
- super(bufferSize);
- this.out = out;
-
- }
-
/*
* (non-Javadoc)
- * @see org.apache.james.imap.main.AbstractImapResponseWriter#write(java.nio.ByteBuffer)
+ * @see org.apache.james.imap.encode.ImapResponseWriter#write(java.nio.ByteBuffer)
*/
- protected void write(final ByteBuffer buffer) throws IOException {
+ public void write(final ByteBuffer buffer) throws IOException {
while (out.write(buffer) > 0) { // NOPMD false positive
// Write all
}
@@ -61,9 +55,9 @@ public class ChannelImapResponseWriter e
/*
* (non-Javadoc)
- * @see org.apache.james.imap.main.AbstractImapResponseWriter#write(org.apache.james.imap.message.response.Literal)
+ * @see org.apache.james.imap.encode.ImapResponseWriter#write(org.apache.james.imap.message.response.Literal)
*/
- protected void write(Literal literal) throws IOException {
+ public void write(Literal literal) throws IOException {
literal.writeTo(out);
}
Modified: james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/base/ByteImapResponseWriter.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/base/ByteImapResponseWriter.java?rev=1089842&r1=1089841&r2=1089842&view=diff
==============================================================================
--- james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/base/ByteImapResponseWriter.java (original)
+++ james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/base/ByteImapResponseWriter.java Thu Apr 7 12:12:20 2011
@@ -21,7 +21,7 @@ package org.apache.james.imap.encode.bas
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.PrintWriter;
+import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
@@ -35,177 +35,31 @@ import org.apache.james.imap.message.res
*/
public class ByteImapResponseWriter implements ImapConstants, ImapResponseWriter {
- private static final int LOWER_CASE_OFFSET = 'a' - 'A';
-
- private PrintWriter writer;
private ByteArrayOutputStream out;
+ private WritableByteChannel channel;
- private boolean skipNextSpace;
public ByteImapResponseWriter() {
clear();
}
public byte[] getBytes() throws Exception {
- writer.flush();
out.flush();
return out.toByteArray();
}
-
- /**
- * Writes the message provided to the client, prepended with the untagged
- * marker "*".
- *
- * @param message
- * The message to write to the client.
- */
- public void untaggedResponse(String message) {
- untagged();
- message(message);
- end();
- }
-
- public void byeResponse(String message) {
- untaggedResponse(BYE + SP + message);
- }
-
- public void untagged() {
- writer.print(UNTAGGED);
- }
-
- public void tag(String tag) {
- writer.print(tag);
- }
-
- public void message(String message) {
- if (message != null) {
- space();
- writer.print(message);
- }
- }
-
- public void message(long number) {
- space();
- writer.print(number);
- }
-
- public void responseCode(String responseCode) {
- if (responseCode != null) {
- writer.print(" [");
- writer.print(responseCode);
- writer.print("]");
- }
- }
-
- public void end() {
- writer.println();
- writer.flush();
- }
-
- public void commandName(String commandName) {
- space();
- writer.print(commandName);
- }
-
- public void quote(String message) {
- space();
- writer.print(DQUOTE);
- final int length = message.length();
- for (int i = 0; i < length; i++) {
- char character = message.charAt(i);
- if (character == ImapConstants.BACK_SLASH || character == DQUOTE) {
- writer.print(ImapConstants.BACK_SLASH);
- }
- writer.print(character);
- }
- writer.print(DQUOTE);
- }
-
- public void closeParen() {
- closeBracket(CLOSING_PARENTHESIS);
- }
-
- private void closeBracket(final char bracket) {
- writer.print(bracket);
- clearSkipNextSpace();
- }
-
- public void openParen() {
- openBracket(OPENING_PARENTHESIS);
- }
-
- private void openBracket(final char bracket) {
- space();
- writer.print(bracket);
- skipNextSpace();
- }
-
+
public void clear() {
this.out = new ByteArrayOutputStream();
- this.writer = new InternetPrintWriter(out, true);
- this.skipNextSpace = false;
- }
-
- private void clearSkipNextSpace() {
- skipNextSpace = false;
- }
-
- public void skipNextSpace() {
- skipNextSpace = true;
- }
-
- public void space() {
- if (skipNextSpace) {
- skipNextSpace = false;
- } else {
- writer.print(SP_CHAR);
- }
+ channel = Channels.newChannel(out);
}
- public void literal(Literal literal) throws IOException {
- space();
- writer.flush();
- WritableByteChannel channel = Channels.newChannel(out);
+ public void write(Literal literal) throws IOException {
literal.writeTo(channel);
- writer.flush();
- }
-
- public void closeSquareBracket() throws IOException {
- closeBracket(CLOSING_SQUARE_BRACKET);
}
-
- public void openSquareBracket() throws IOException {
- openBracket(OPENING_SQUARE_BRACKET);
- }
-
- public void upperCaseAscii(String message) throws IOException {
- upperCaseAscii(message, false);
- }
-
- private void upperCaseAscii(String message, boolean quote) {
- space();
- if (quote)
- writer.print(DQUOTE);
- // message is ASCII
- final int length = message.length();
- for (int i = 0; i < length; i++) {
- final char next = message.charAt(i);
- if (next >= 'a' && next <= 'z') {
- writer.print(next + LOWER_CASE_OFFSET);
- } else {
- writer.print(next);
- }
+ public void write(ByteBuffer buffer) throws IOException {
+ while (channel.write(buffer) > 0) { // NOPMD false positive
+ // Write all
}
- if (quote)
- writer.print(DQUOTE);
- }
-
- public void quoteUpperCaseAscii(String message) {
- upperCaseAscii(message, true);
- }
-
- public void continuation(String message) throws IOException {
- writer.print(CONTINUATION + SP + message);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org