public IMAPResponse readResponse()

in geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/store/imap/connection/IMAPResponseStream.java [86:225]


    public IMAPResponse readResponse() throws MessagingException
      {
        // reset our accumulator
        out.reset();
        // now read a buffer of data
        byte[] data = readData();

        // and create a tokenizer for parsing this down.
        IMAPResponseTokenizer tokenizer = new IMAPResponseTokenizer(data);
        // get the first token.
        Token token = tokenizer.next();

        int type = token.getType();

        // a continuation response.  This will terminate a response set.
        if (type == Token.CONTINUATION) {
            return new IMAPContinuationResponse(data);
        }
        // unsolicited response.  There are multiple forms of these, which might actually be
        // part of the response for the last issued command.
        else if (type == Token.UNTAGGED) {
            // step to the next token, which will give us the type
            token = tokenizer.next();
            // if the token is numeric, then this is a size response in the
            // form "* nn type"
            if (token.isType(Token.NUMERIC)) {
                int size = token.getInteger();

                token = tokenizer.next();

                String keyword = token.getValue();

                // FETCH responses require fairly complicated parsing.  Other
                // size/message updates are fairly generic.
                if (keyword.equals("FETCH")) {
                    return new IMAPFetchResponse(size, data, tokenizer);
                }
                return new IMAPSizeResponse(keyword, size, data);
            }

            // this needs to be an ATOM type, which will tell us what format this untagged
            // response is in.  There are many different untagged formats, some general, some
            // specific to particular command types.
            if (token.getType() != Token.ATOM) {
                try {
                    throw new MessagingException("Unknown server response: " + new String(data, "ISO8859-1"));
                } catch (UnsupportedEncodingException e) {
                    throw new MessagingException("Unknown server response: " + new String(data));
                }
            }

            String keyword = token.getValue();
            // many response are in the form "* OK [keyword value] message".
            if (keyword.equals("OK")) {
                return parseUntaggedOkResponse(data, tokenizer);
            }
            // preauth status response
            else if (keyword.equals("PREAUTH")) {
                return new IMAPServerStatusResponse("PREAUTH", tokenizer.getRemainder(), data);
            }
            // preauth status response
            else if (keyword.equals("BYE")) {
                return new IMAPServerStatusResponse("BYE", tokenizer.getRemainder(), data);
            }
            else if (keyword.equals("BAD")) {
                // these are generally ignored.
                return new IMAPServerStatusResponse("BAD", tokenizer.getRemainder(), data);
            }
            else if (keyword.equals("NO")) {
                // these are generally ignored.
                return new IMAPServerStatusResponse("NO", tokenizer.getRemainder(), data);
            }
            // a complex CAPABILITY response
            else if (keyword.equals("CAPABILITY")) {
                return new IMAPCapabilityResponse(tokenizer, data);
            }
            // a complex LIST response
            else if (keyword.equals("LIST")) {
                return new IMAPListResponse("LIST", data, tokenizer);
            }
            // a complex FLAGS response
            else if (keyword.equals("FLAGS")) {
                // parse this into a flags set.
                return new IMAPFlagsResponse(data, tokenizer);
            }
            // a complex LSUB response (identical in format to LIST)
            else if (keyword.equals("LSUB")) {
                return new IMAPListResponse("LSUB", data, tokenizer);
            }
            // a STATUS response, which will contain a list of elements
            else if (keyword.equals("STATUS")) {
                return new IMAPStatusResponse(data, tokenizer);
            }
            // SEARCH requests return an variable length list of message matches.
            else if (keyword.equals("SEARCH")) {
                return new IMAPSearchResponse(data, tokenizer);
            }
            // ACL requests return an variable length list of ACL values .
            else if (keyword.equals("ACL")) {
                return new IMAPACLResponse(data, tokenizer);
            }
            // LISTRIGHTS requests return a variable length list of RIGHTS values .
            else if (keyword.equals("LISTRIGHTS")) {
                return new IMAPListRightsResponse(data, tokenizer);
            }
            // MYRIGHTS requests return a list of user rights for a mailbox name.
            else if (keyword.equals("MYRIGHTS")) {
                return new IMAPMyRightsResponse(data, tokenizer);
            }
            // QUOTAROOT requests return a list of mailbox quota root names
            else if (keyword.equals("QUOTAROOT")) {
                return new IMAPQuotaRootResponse(data, tokenizer);
            }
            // QUOTA requests return a list of quota values for a root name
            else if (keyword.equals("QUOTA")) {
                return new IMAPQuotaResponse(data, tokenizer);
            }
            else if (keyword.equals("NAMESPACE")) {
                return new IMAPNamespaceResponse(data, tokenizer);
            }
        }
        // begins with a word, this should be the tagged response from the last command.
        else if (type == Token.ATOM) {
            String tag = token.getValue();
            token = tokenizer.next();
            String status = token.getValue();
            //handle plain authentication gracefully, see GERONIMO-6526
            if("+".equals(tag) && status == null) {
            	return new IMAPContinuationResponse(data);
            }                      
            // primary information in one of these is the status field, which hopefully
            // is 'OK'
            return new IMAPTaggedResponse(tag, status, tokenizer.getRemainder(), data);
        }
        try {
            throw new MessagingException("Unknown server response: " + new String(data, "ISO8859-1"));
        } catch (UnsupportedEncodingException e) {
            throw new MessagingException("Unknown server response: " + new String(data));
        }
    }