001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.mail;
020:
021: import java.io.InputStream;
022: import java.io.IOException;
023: import java.io.BufferedReader;
024: import java.io.InputStreamReader;
025:
026: /**
027: * A wrapper around the raw input from the SMTP server that assembles
028: * multi line responses into a single String.
029: *
030: * <p>The same rules used here would apply to FTP and other Telnet
031: * based protocols as well.</p>
032: *
033: */
034: public class SmtpResponseReader {
035: // CheckStyle:VisibilityModifier OFF - bc
036: protected BufferedReader reader = null;
037: // CheckStyle:VisibilityModifier ON
038: private StringBuffer result = new StringBuffer();
039:
040: /**
041: * Wrap this input stream.
042: * @param in the stream to wrap.
043: */
044: public SmtpResponseReader(InputStream in) {
045: reader = new BufferedReader(new InputStreamReader(in));
046: }
047:
048: /**
049: * Read until the server indicates that the response is complete.
050: *
051: * @return Responsecode (3 digits) + Blank + Text from all
052: * response line concatenated (with blanks replacing the \r\n
053: * sequences).
054: * @throws IOException on error.
055: */
056: public String getResponse() throws IOException {
057: result.setLength(0);
058: String line = reader.readLine();
059: if (line != null && line.length() >= 3) {
060: result.append(line.substring(0, 3));
061: result.append(" ");
062: }
063:
064: while (line != null) {
065: append(line);
066: if (!hasMoreLines(line)) {
067: break;
068: }
069: line = reader.readLine();
070: }
071: return result.toString().trim();
072: }
073:
074: /**
075: * Closes the underlying stream.
076: * @throws IOException on error.
077: */
078: public void close() throws IOException {
079: reader.close();
080: }
081:
082: /**
083: * Should we expect more input?
084: * @param line the line to check.
085: * @return true if there are more lines to check.
086: */
087: protected boolean hasMoreLines(String line) {
088: return line.length() > 3 && line.charAt(3) == '-';
089: }
090:
091: /**
092: * Append the text from this line of the resonse.
093: */
094: private void append(String line) {
095: if (line.length() > 4) {
096: result.append(line.substring(4));
097: result.append(" ");
098: }
099: }
100: }
|