Author: norman Date: Sun Dec 25 19:21:44 2011 New Revision: 1224632 URL: http://svn.apache.org/viewvc?rev=1224632&view=rev Log: Start to add interfaces and classes for IMAP.See PROTOCOLS-73
Added: james/protocols/trunk/imap/src/ james/protocols/trunk/imap/src/main/ james/protocols/trunk/imap/src/main/java/ james/protocols/trunk/imap/src/main/java/org/ james/protocols/trunk/imap/src/main/java/org/apache/ james/protocols/trunk/imap/src/main/java/org/apache/james/ james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/ james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/ james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPRequest.java (with props) james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPSession.java (with props) james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/core/ james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/core/IMAPCommandDispatcher.java (with props) Added: james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPRequest.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPRequest.java?rev=1224632&view=auto ============================================================================== --- james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPRequest.java (added) +++ james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPRequest.java Sun Dec 25 19:21:44 2011 @@ -0,0 +1,143 @@ +/**************************************************************** + * 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.protocols.imap; + +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Locale; + +import org.apache.james.protocols.api.Request; + +public class IMAPRequest implements Request { + + private static final String US_ASCII = "US_ASCII"; + + private static final String CRLF = "\r\n"; + private final Collection<ByteBuffer> lines; + private final String tag; + private final String command; + + public IMAPRequest(Collection<ByteBuffer> lines) { + this.lines = lines; + ByteBuffer buf = lines.iterator().next(); + buf.rewind(); + + this.tag = read(buf); + this.command = read(buf).toUpperCase(Locale.US); + } + + public IMAPRequest(ByteBuffer line) { + this(Arrays.asList(line)); + } + + private String read(ByteBuffer buf) { + StringBuilder sb = new StringBuilder(); + int i; + while ((i = buf.get()) != ' ') { + sb.append((byte) i); + } + return sb.toString(); + } + + /** + * Return the tag of the request + * + * @return tag + */ + public String getTag() { + return tag; + } + + /* + * (non-Javadoc) + * @see org.apache.james.protocols.api.Request#getArgument() + */ + public String getArgument() { + int tagOffeset = tag.length() + command.length() + 2; + StringBuilder sb = new StringBuilder(); + Iterator<ByteBuffer> linesIt = lines.iterator(); + + while (linesIt.hasNext()){ + ByteBuffer line = linesIt.next(); + byte[] buf; + if (line.hasArray()) { + buf = line.array(); + } else { + line.rewind(); + buf = new byte[line.remaining() - tagOffeset]; + line.get(buf, tagOffeset, line.remaining()); + } + try { + sb.append(new String(buf, US_ASCII)); + } catch (UnsupportedEncodingException e) { + // Should never happend + e.printStackTrace(); + } + if (linesIt.hasNext()) { + sb.append(CRLF); + } + } + return sb.toString(); + } + + /* + * (non-Javadoc) + * @see org.apache.james.protocols.api.Request#getCommand() + */ + public String getCommand() { + return command; + } + + /** + * Return an {@link Iterator} which holds all argument lines. The returned {@link ByteBuffer}'s will be + * rewind by calling {@link ByteBuffer#rewind()} before return them + * + * @return arguments + */ + public Iterator<ByteBuffer> getArguments() { + return new Iterator<ByteBuffer>() { + boolean first = true; + Iterator<ByteBuffer> buffIt = lines.iterator(); + + public boolean hasNext() { + return buffIt.hasNext(); + } + + public ByteBuffer next() { + ByteBuffer buf = buffIt.next(); + buf.rewind(); + + if (first) { + first = false; + buf.position(getTag().length() + getCommand().length() + 2); + buf = buf.slice(); + } + return buf; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + +} Propchange: james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPRequest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPSession.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPSession.java?rev=1224632&view=auto ============================================================================== --- james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPSession.java (added) +++ james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPSession.java Sun Dec 25 19:21:44 2011 @@ -0,0 +1,42 @@ +/**************************************************************** + * 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.protocols.imap; + +import org.apache.james.protocols.api.ProtocolSession; +import org.apache.james.protocols.api.handler.LineHandler; + +public interface IMAPSession extends ProtocolSession{ + + /** + * Put a new line handler in the chain + * @param overrideCommandHandler + */ + void pushLineHandler(LineHandler<IMAPSession> overrideCommandHandler); + + /** + * Pop the last command handler + */ + void popLineHandler(); + + /** + * Return the size of the pushed {@link LineHandler} + * @return size of the pushed line handler + */ + int getPushedLineHandlerCount(); +} Propchange: james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/IMAPSession.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/core/IMAPCommandDispatcher.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/core/IMAPCommandDispatcher.java?rev=1224632&view=auto ============================================================================== --- james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/core/IMAPCommandDispatcher.java (added) +++ james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/core/IMAPCommandDispatcher.java Sun Dec 25 19:21:44 2011 @@ -0,0 +1,81 @@ +/**************************************************************** + * 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.protocols.imap.core; + +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.james.protocols.api.ProtocolSession.State; +import org.apache.james.protocols.api.Request; +import org.apache.james.protocols.api.Response; +import org.apache.james.protocols.api.handler.CommandDispatcher; +import org.apache.james.protocols.api.handler.MultiLineHandler; +import org.apache.james.protocols.imap.IMAPRequest; +import org.apache.james.protocols.imap.IMAPSession; + +public class IMAPCommandDispatcher extends CommandDispatcher<IMAPSession>{ + + private final static Pattern LITERAL_PATTERN = Pattern.compile(".*\\{(\\d+)\\}.*"); + + @Override + protected Request parseRequest(IMAPSession session, ByteBuffer buffer) throws Exception { + IMAPRequest request = new IMAPRequest(buffer); + Matcher matcher = LITERAL_PATTERN.matcher(request.getArgument()); + if (matcher.matches()) { + final long bytesToRead = Long.parseLong(matcher.group(1)); + MultiLineHandler<IMAPSession> handler = new MultiLineHandler<IMAPSession>() { + + private static final String BYTES_READ = "BYTES_READ"; + + /* + * (non-Javadoc) + * @see org.apache.james.protocols.api.handler.MultiLineHandler#isReady(org.apache.james.protocols.api.ProtocolSession, java.nio.ByteBuffer) + */ + protected boolean isReady(IMAPSession session, ByteBuffer line) { + long bytesRead = (Long) session.setAttachment(BYTES_READ, null, State.Transaction); + bytesRead += line.remaining(); + if (bytesRead >= bytesToRead) { + return true; + } else { + session.setAttachment(BYTES_READ, bytesRead, State.Transaction); + return false; + } + } + + @Override + protected Response onLines(IMAPSession session, Collection<ByteBuffer> lines) { + session.popLineHandler(); + return dispatchCommandHandlers(session, new IMAPRequest(lines)); + } + }; + buffer.rewind(); + + // push the line to the handler + handler.onLine(session, buffer); + + session.pushLineHandler(handler); + return null; + + } else { + return request; + } + } +} Propchange: james/protocols/trunk/imap/src/main/java/org/apache/james/protocols/imap/core/IMAPCommandDispatcher.java ------------------------------------------------------------------------------ svn:mime-type = text/plain --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org