Author: norman
Date: Tue Jan 18 17:01:54 2011
New Revision: 1060468
URL: http://svn.apache.org/viewvc?rev=1060468&view=rev
Log:
Some changes to make it easy to use imap in a nio framework
Added:
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapLineHandler.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapResponseWriter.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ResponseEncoder.java
Removed:
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/ContinuationReader.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ImapSessionImpl.java
Modified:
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapSession.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapRequestHandler.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ChannelImapResponseWriter.java
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/OutputStreamImapResponseWriter.java
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/FakeImapSession.java
james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/IdleProcessor.java
Added:
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapLineHandler.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapLineHandler.java?rev=1060468&view=auto
==============================================================================
---
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapLineHandler.java
(added)
+++
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapLineHandler.java
Tue Jan 18 17:01:54 2011
@@ -0,0 +1,25 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.imap.api.process;
+
+public interface ImapLineHandler {
+
+ public void onLine(ImapSession session, byte[] data);
+}
Modified:
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapSession.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapSession.java?rev=1060468&r1=1060467&r2=1060468&view=diff
==============================================================================
---
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapSession.java
(original)
+++
james/imap/trunk/api/src/main/java/org/apache/james/imap/api/process/ImapSession.java
Tue Jan 18 17:01:54 2011
@@ -127,4 +127,8 @@ public interface ImapSession {
public boolean startCompression();
+ public void pushLineHandler(ImapLineHandler lineHandler);
+
+ public void popLineHandler();
+
}
Modified:
james/imap/trunk/message/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java?rev=1060468&r1=1060467&r2=1060468&view=diff
==============================================================================
---
james/imap/trunk/message/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java
(original)
+++
james/imap/trunk/message/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java
Tue Jan 18 17:01:54 2011
@@ -38,7 +38,6 @@ import java.util.List;
import javax.mail.Flags;
import org.apache.commons.io.IOUtils;
-import org.apache.james.imap.api.ContinuationReader;
import org.apache.james.imap.api.ImapConstants;
import org.apache.james.imap.api.display.HumanReadableText;
import org.apache.james.imap.api.message.IdRange;
@@ -52,7 +51,7 @@ import org.apache.james.imap.api.message
*
* @version $Revision: 109034 $
*/
-public abstract class ImapRequestLineReader implements ContinuationReader {
+public abstract class ImapRequestLineReader {
private static final int QUOTED_BUFFER_INITIAL_CAPACITY = 64;
@@ -180,24 +179,6 @@ public abstract class ImapRequestLineRea
consume();
}
- public String readContinuation() throws IOException {
- // Consume the '\n' from the previous line.
- consume();
-
- StringBuilder sb = new StringBuilder();
- char next = nextChar();
- while (next != '\r') {
- sb.append(next);
- consume();
- next = nextChar();
- }
- consume();
-
- // NOTE: This code leaves the '\n' as next char. This seems to be what
is expected by the code which parses commands.
-
- return sb.toString();
- }
-
/**
* Reads an argument of type "atom" from the request.
Modified:
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapRequestHandler.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapRequestHandler.java?rev=1060468&r1=1060467&r2=1060468&view=diff
==============================================================================
---
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapRequestHandler.java
(original)
+++
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapRequestHandler.java
Tue Jan 18 17:01:54 2011
@@ -102,38 +102,5 @@ public abstract class AbstractImapReques
}
}
- private static final class ResponseEncoder implements Responder {
- private final ImapEncoder encoder;
- private final ImapSession session;
- private final ImapResponseComposer composer;
- private IOException failure;
-
-
- public ResponseEncoder(final ImapEncoder encoder,
- final ImapResponseComposer composer, final ImapSession
session) {
- super();
- this.encoder = encoder;
- this.composer = composer;
- this.session = session;
- }
-
- public void respond(final ImapResponseMessage message) {
- try {
- encoder.encode(message, composer, session);
- } catch (IOException failure) {
- this.failure = failure;
- }
- }
-
- /**
- * Gets the recorded failure.
- *
- * @return the failure, or null when no failure has occurred
- */
- public final IOException getFailure() {
- return failure;
- }
-
- }
}
Added:
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapResponseWriter.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapResponseWriter.java?rev=1060468&view=auto
==============================================================================
---
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapResponseWriter.java
(added)
+++
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/AbstractImapResponseWriter.java
Tue Jan 18 17:01:54 2011
@@ -0,0 +1,246 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.imap.main;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+
+import org.apache.james.imap.api.ImapConstants;
+import org.apache.james.imap.encode.ImapResponseWriter;
+import org.apache.james.imap.message.response.Literal;
+
+public abstract class AbstractImapResponseWriter implements
ImapResponseWriter, ImapConstants{
+
+ private static final int LOWER_CASE_OFFSET = 'a' - 'A';
+
+ private static final int DEFAULT_BUFFER_SIZE = 128;
+
+ private final Charset usAscii;
+
+ private final ByteBuffer buffer;
+
+ private boolean skipNextSpace;
+
+ public AbstractImapResponseWriter() {
+ this(DEFAULT_BUFFER_SIZE);
+ }
+
+ public AbstractImapResponseWriter(final int bufferSize) {
+ skipNextSpace = false;
+ buffer = ByteBuffer.allocate(bufferSize);
+ usAscii = Charset.forName("US-ASCII");
+ }
+
+ /**
+ * Writes the message provided to the client, prepended with the untagged
+ * marker "*".
+ *
+ * @param message
+ * The message to write to the client.
+ * @throws IOException
+ */
+ public void untaggedResponse(String message) throws IOException {
+ untagged();
+ message(message);
+ end();
+ }
+
+ public void byeResponse(String message) throws IOException {
+ untaggedResponse(BYE + SP + message);
+ }
+
+ protected abstract void write(final ByteBuffer buffer) throws IOException;
+
+ private void writeASCII(final String string) throws IOException {
+ final ByteBuffer buffer = usAscii.encode(string);
+ write(buffer);
+ }
+
+ private void write(byte[] bytes) throws IOException {
+ final ByteBuffer wrap = ByteBuffer.wrap(bytes);
+ write(wrap);
+ }
+
+ public void untagged() throws IOException {
+ writeASCII(UNTAGGED);
+ }
+
+ public void tag(String tag) throws IOException {
+ writeASCII(tag);
+ }
+
+ public void message(String message) throws IOException {
+ if (message != null) {
+ space();
+ writeASCII(message);
+ }
+ }
+
+ public void message(long number) throws IOException {
+ space();
+ writeASCII(Long.toString(number));
+ }
+
+ public void responseCode(String responseCode) throws IOException {
+ if (responseCode != null) {
+ writeASCII(" [");
+ writeASCII(responseCode);
+ write(BYTES_CLOSE_SQUARE_BRACKET);
+ }
+ }
+
+ public void end() throws IOException {
+ write(BYTES_LINE_END);
+ }
+
+ public void commandName(String commandName) throws IOException {
+ space();
+ writeASCII(commandName);
+ }
+
+ 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();
+ write(buffer);
+ }
+
+ private void writeIfFull() throws IOException {
+ if (!buffer.hasRemaining()) {
+ buffer.flip();
+ write(buffer);
+ buffer.clear();
+ }
+ }
+
+ public void closeParen() throws IOException {
+ closeBracket(BYTES_CLOSING_PARENTHESIS);
+ }
+
+ private void closeBracket(final byte[] bracket) throws IOException {
+ write(bracket);
+ clearSkipNextSpace();
+ }
+
+ public void openParen() throws IOException {
+ openBracket(BYTES_OPENING_PARENTHESIS);
+ }
+
+ private void openBracket(final byte[] bracket) throws IOException {
+ space();
+ write(bracket);
+ skipNextSpace();
+ }
+
+ private void clearSkipNextSpace() {
+ skipNextSpace = false;
+ }
+
+ public void skipNextSpace() {
+ skipNextSpace = true;
+ }
+
+ public void space() throws IOException {
+ if (skipNextSpace) {
+ skipNextSpace = false;
+ } else {
+ write(BYTES_SPACE);
+ }
+ }
+
+ public void literal(Literal literal) throws IOException {
+ space();
+ write(BYTES_OPEN_BRACE);
+ final long size = literal.size();
+ writeASCII(Long.toString(size));
+ write(BYTES_CLOSE_BRACE);
+ write(BYTES_LINE_END);
+ if (size > 0) {
+ write(literal);
+ }
+ }
+
+ protected abstract void write(Literal literal) throws IOException;
+
+ public void closeSquareBracket() throws IOException {
+ closeBracket(BYTES_CLOSE_SQUARE_BRACKET);
+ }
+
+ public void openSquareBracket() throws IOException {
+ openBracket(BYTES_OPEN_SQUARE_BRACKET);
+ }
+
+ public void upperCaseAscii(String message) throws IOException {
+ upperCaseAscii(message, false);
+ }
+
+ 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();
+ write(buffer);
+ }
+
+ public void quoteUpperCaseAscii(String message) throws IOException {
+ upperCaseAscii(message, true);
+ }
+
+ public void continuation(String message) throws IOException {
+ writeASCII(CONTINUATION + SP + message);
+ }
+
+}
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=1060468&r1=1060467&r2=1060468&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
Tue Jan 18 17:01:54 2011
@@ -22,239 +22,49 @@ package org.apache.james.imap.main;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
-import java.nio.charset.Charset;
-import org.apache.james.imap.api.ImapConstants;
-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 implements ImapConstants,
ImapResponseWriter {
+public class ChannelImapResponseWriter extends AbstractImapResponseWriter {
- private static final int LOWER_CASE_OFFSET = 'a' - 'A';
-
- private static final int DEFAULT_BUFFER_SIZE = 128;
-
- private final Charset usAscii;
private final WritableByteChannel out;
- private final ByteBuffer buffer;
-
- private boolean skipNextSpace;
public ChannelImapResponseWriter(final WritableByteChannel out) {
- this(out, DEFAULT_BUFFER_SIZE);
+ super();
+ this.out = out;
}
public ChannelImapResponseWriter(final WritableByteChannel out,
final int bufferSize) {
+ super(bufferSize);
this.out = out;
- skipNextSpace = false;
- buffer = ByteBuffer.allocate(bufferSize);
- usAscii = Charset.forName("US-ASCII");
+
}
- /**
- * Writes the message provided to the client, prepended with the untagged
- * marker "*".
- *
- * @param message
- * The message to write to the client.
- * @throws IOException
- */
- public void untaggedResponse(String message) throws IOException {
- untagged();
- message(message);
- end();
- }
-
- public void byeResponse(String message) throws IOException {
- untaggedResponse(BYE + SP + message);
- }
- private void write(final ByteBuffer buffer) throws IOException {
+ /*
+ * (non-Javadoc)
+ * @see
org.apache.james.imap.main.AbstractImapResponseWriter#write(java.nio.ByteBuffer)
+ */
+ protected void write(final ByteBuffer buffer) throws IOException {
while (out.write(buffer) > 0) {
// Write all
}
}
- private void writeASCII(final String string) throws IOException {
- final ByteBuffer buffer = usAscii.encode(string);
- write(buffer);
- }
-
- private void write(byte[] bytes) throws IOException {
- final ByteBuffer wrap = ByteBuffer.wrap(bytes);
- write(wrap);
- }
-
- public void untagged() throws IOException {
- writeASCII(UNTAGGED);
- }
-
- public void tag(String tag) throws IOException {
- writeASCII(tag);
- }
-
- public void message(String message) throws IOException {
- if (message != null) {
- space();
- writeASCII(message);
- }
- }
-
- public void message(long number) throws IOException {
- space();
- writeASCII(Long.toString(number));
- }
-
- public void responseCode(String responseCode) throws IOException {
- if (responseCode != null) {
- writeASCII(" [");
- writeASCII(responseCode);
- write(BYTES_CLOSE_SQUARE_BRACKET);
- }
- }
-
- public void end() throws IOException {
- write(BYTES_LINE_END);
- flush();
- }
-
- public void commandName(String commandName) throws IOException {
- space();
- writeASCII(commandName);
- }
-
- 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();
- write(buffer);
- }
-
- private void writeIfFull() throws IOException {
- if (!buffer.hasRemaining()) {
- buffer.flip();
- write(buffer);
- buffer.clear();
- }
- }
-
- public void flush() throws IOException {
- }
-
- public void closeParen() throws IOException {
- closeBracket(BYTES_CLOSING_PARENTHESIS);
- }
-
- private void closeBracket(final byte[] bracket) throws IOException {
- write(bracket);
- clearSkipNextSpace();
- }
-
- public void openParen() throws IOException {
- openBracket(BYTES_OPENING_PARENTHESIS);
- }
- private void openBracket(final byte[] bracket) throws IOException {
- space();
- write(bracket);
- skipNextSpace();
- }
-
- private void clearSkipNextSpace() {
- skipNextSpace = false;
- }
-
- public void skipNextSpace() {
- skipNextSpace = true;
- }
-
- public void space() throws IOException {
- if (skipNextSpace) {
- skipNextSpace = false;
- } else {
- write(BYTES_SPACE);
- }
- }
-
- public void literal(Literal literal) throws IOException {
- space();
- write(BYTES_OPEN_BRACE);
- final long size = literal.size();
- writeASCII(Long.toString(size));
- write(BYTES_CLOSE_BRACE);
- write(BYTES_LINE_END);
- if (size > 0) {
- literal.writeTo(out);
- }
- }
-
- public void closeSquareBracket() throws IOException {
- closeBracket(BYTES_CLOSE_SQUARE_BRACKET);
- }
-
- public void openSquareBracket() throws IOException {
- openBracket(BYTES_OPEN_SQUARE_BRACKET);
- }
-
- public void upperCaseAscii(String message) throws IOException {
- upperCaseAscii(message, false);
- }
-
- 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();
- write(buffer);
+ /*
+ * (non-Javadoc)
+ * @see
org.apache.james.imap.main.AbstractImapResponseWriter#write(org.apache.james.imap.message.response.Literal)
+ */
+ protected void write(Literal literal) throws IOException {
+ literal.writeTo(out);
}
- public void quoteUpperCaseAscii(String message) throws IOException {
- upperCaseAscii(message, true);
- }
-
- public void continuation(String message) throws IOException {
- writeASCII(CONTINUATION + SP + message);
- }
}
Modified:
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/OutputStreamImapResponseWriter.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/main/OutputStreamImapResponseWriter.java?rev=1060468&r1=1060467&r2=1060468&view=diff
==============================================================================
---
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/OutputStreamImapResponseWriter.java
(original)
+++
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/OutputStreamImapResponseWriter.java
Tue Jan 18 17:01:54 2011
@@ -37,7 +37,6 @@ public class OutputStreamImapResponseWri
}
public void flush() throws IOException {
- super.flush();
output.flush();
}
Added:
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ResponseEncoder.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ResponseEncoder.java?rev=1060468&view=auto
==============================================================================
---
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ResponseEncoder.java
(added)
+++
james/imap/trunk/message/src/main/java/org/apache/james/imap/main/ResponseEncoder.java
Tue Jan 18 17:01:54 2011
@@ -0,0 +1,63 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.imap.main;
+
+import java.io.IOException;
+
+import org.apache.james.imap.api.message.response.ImapResponseMessage;
+import org.apache.james.imap.api.process.ImapSession;
+import org.apache.james.imap.api.process.ImapProcessor.Responder;
+import org.apache.james.imap.encode.ImapEncoder;
+import org.apache.james.imap.encode.ImapResponseComposer;
+
+public class ResponseEncoder implements Responder {
+ private final ImapEncoder encoder;
+ private final ImapSession session;
+ private final ImapResponseComposer composer;
+
+ private IOException failure;
+
+
+ public ResponseEncoder(final ImapEncoder encoder,
+ final ImapResponseComposer composer, final ImapSession session) {
+ super();
+ this.encoder = encoder;
+ this.composer = composer;
+ this.session = session;
+ }
+
+ public void respond(final ImapResponseMessage message) {
+ try {
+ encoder.encode(message, composer, session);
+ } catch (IOException failure) {
+ this.failure = failure;
+ }
+ }
+
+ /**
+ * Gets the recorded failure.
+ *
+ * @return the failure, or null when no failure has occurred
+ */
+ public final IOException getFailure() {
+ return failure;
+ }
+
+}
Modified:
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/FakeImapSession.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/FakeImapSession.java?rev=1060468&r1=1060467&r2=1060468&view=diff
==============================================================================
---
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/FakeImapSession.java
(original)
+++
james/imap/trunk/message/src/test/java/org/apache/james/imap/encode/FakeImapSession.java
Tue Jan 18 17:01:54 2011
@@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentHa
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.james.imap.api.ImapSessionState;
+import org.apache.james.imap.api.process.ImapLineHandler;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.api.process.SelectedMailbox;
@@ -110,4 +111,14 @@ public class FakeImapSession implements
return false;
}
+ public void pushLineHandler(ImapLineHandler lineHandler) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void popLineHandler() {
+ // TODO Auto-generated method stub
+
+ }
+
}
Modified:
james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/IdleProcessor.java
URL:
http://svn.apache.org/viewvc/james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/IdleProcessor.java?rev=1060468&r1=1060467&r2=1060468&view=diff
==============================================================================
---
james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/IdleProcessor.java
(original)
+++
james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/IdleProcessor.java
Tue Jan 18 17:01:54 2011
@@ -21,12 +21,10 @@ package org.apache.james.imap.processor;
import static org.apache.james.imap.api.ImapConstants.SUPPORTS_IDLE;
-import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
-import org.apache.james.imap.api.ContinuationReader;
import org.apache.james.imap.api.ImapCommand;
import org.apache.james.imap.api.ImapMessage;
import org.apache.james.imap.api.ImapSessionUtils;
@@ -34,6 +32,7 @@ import org.apache.james.imap.api.display
import org.apache.james.imap.api.message.request.ImapRequest;
import org.apache.james.imap.api.message.response.StatusResponse;
import org.apache.james.imap.api.message.response.StatusResponseFactory;
+import org.apache.james.imap.api.process.ImapLineHandler;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.api.process.SelectedMailbox;
@@ -59,45 +58,50 @@ public class IdleProcessor extends Abstr
return (message instanceof IdleRequest);
}
- protected void doProcess(ImapRequest message, ImapSession session,
- String tag, ImapCommand command, Responder responder) {
+ protected void doProcess(final ImapRequest message, final ImapSession
session,
+ final String tag, final ImapCommand command, final Responder
responder) {
+ final AtomicBoolean closed = new AtomicBoolean(false);
- try {
- IdleRequest request = (IdleRequest) message;
- ContinuationReader reader = request.getContinuationReader();
-
+ try {
responder.respond(new
ContinuationResponse(HumanReadableText.IDLING));
unsolicitedResponses(session, responder, false);
- MailboxManager mailboxManager = getMailboxManager();
- MailboxSession mailboxSession =
ImapSessionUtils.getMailboxSession(session);
- AtomicBoolean closed = new AtomicBoolean(false);
-
- String line = null;
- try {
- SelectedMailbox sm = session.getSelected();
- if(sm != null) {
- mailboxManager.addListener(sm.getPath(),
- new IdleMailboxListener(closed, session, responder),
mailboxSession);
- }
+ final MailboxManager mailboxManager = getMailboxManager();
+ final MailboxSession mailboxSession =
ImapSessionUtils.getMailboxSession(session);
+ SelectedMailbox sm = session.getSelected();
+ if(sm != null) {
+ mailboxManager.addListener(sm.getPath(),
+ new IdleMailboxListener(closed, session, responder),
mailboxSession);
+ }
+
- line = reader.readContinuation();
- } finally {
- synchronized (session) {
+ session.pushLineHandler(new ImapLineHandler() {
+
+ /*
+ * (non-Javadoc)
+ * @see
org.apache.james.imap.api.process.ImapLineHandler#onLine(org.apache.james.imap.api.process.ImapSession,
byte[])
+ */
+ public void onLine(ImapSession session, byte[] data) {
+ String line;
+ if (data.length > 2) {
+ line = new String(data, 0, data.length -2 );
+ } else {
+ line = "";
+ }
+
closed.set(true);
+ session.popLineHandler();
+ if (!"DONE".equals(line.toUpperCase())) {
+ StatusResponse response = factory.taggedBad(tag,
command,
+ HumanReadableText.INVALID_COMMAND);
+ responder.respond(response);
+ } else {
+ okComplete(command, tag, responder);
+ }
}
- }
- if (!"DONE".equals(line.toUpperCase())) {
- StatusResponse response = factory.taggedBad(tag, command,
- HumanReadableText.INVALID_COMMAND);
- responder.respond(response);
- } else {
- okComplete(command, tag, responder);
- }
- } catch (IOException e) {
- // TODO: What should we do here?
- no(command, tag, responder,
HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING);
+ });
} catch (MailboxException e) {
+ closed.set(true);
// TODO: What should we do here?
no(command, tag, responder,
HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]