001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.protocols.smtp;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.apache.james.protocols.api.AbstractResponse;
026    
027    /**
028     * Contains an SMTP result
029     */
030    public class SMTPResponse extends AbstractResponse {
031    
032        protected SMTPResponse() {
033            
034        }
035        /**
036         * Construct a new SMTPResponse. The given code and description can not be null, if null an IllegalArgumentException
037         * get thrown
038         * 
039         * @param code the returnCode
040         * @param description the description 
041         */
042        public SMTPResponse(String code, CharSequence description) {
043            super(code, description);
044        }
045        
046        /**
047         * Construct a new SMTPResponse. The given rawLine need to be in format [SMTPResponseReturnCode SMTResponseDescription].
048         * If this is not the case an IllegalArgumentException get thrown.
049         * 
050         * @param rawLine the raw SMTPResponse
051         */
052        /**
053         * Construct a new SMTPResponse. The given rawLine need to be in format [SMTPResponseReturnCode SMTResponseDescription].
054         * If this is not the case an IllegalArgumentException get thrown.
055         * 
056         * @param rawLine the raw SMTPResponse
057         */
058        public SMTPResponse(String rawLine) {
059            this(extractCode(rawLine), extractResponse(rawLine));
060        }
061        
062    
063        private  static String extractCode(String raw) {
064            String args[] = raw.split(" ");
065            if (args != null && args.length > 1) {
066                return args[0];
067                
068            } else {
069                throw new IllegalArgumentException("Invalid Response format. Format should be [Code Description]");
070            }
071        }
072        
073        private  static String extractResponse(String raw) {
074            String args[] = raw.split(" ");
075            if (args != null && args.length > 1) {
076                return args[2];
077                
078            } else {
079                return null;
080            }
081        }
082    
083        /**
084         * @see org.apache.james.protocols.api.Response#getLines()
085         */
086        public List<CharSequence> getLines() {
087            List<CharSequence> responseList = new ArrayList<CharSequence>();
088    
089            for (int k = 0; k < lines.size(); k++) {
090                StringBuffer respBuff = new StringBuffer(256);
091                respBuff.append(getRetCode());
092                if (k == lines.size() - 1) {
093                    respBuff.append(" ");
094                    respBuff.append(lines.get(k));
095    
096                } else {
097                    respBuff.append("-");
098                    respBuff.append(lines.get(k));
099    
100                }
101                responseList.add(respBuff.toString());
102            }
103    
104            return responseList;
105        }
106    
107    
108    }