1 36 package org.columba.ristretto.smtp.parser; 37 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 41 import org.columba.ristretto.parser.ParserException; 42 import org.columba.ristretto.smtp.SMTPResponse; 43 44 49 public class SMTPResponseParser { 50 51 private static final Pattern responsePattern = 52 Pattern.compile("(\\d+)" + "(-| )?" + "([^\r\n]+)?\r\n"); 56 private static final Pattern greetingPattern = 57 Pattern.compile("^([^\\s]*)" + " ?(.*)$"); 60 71 public static SMTPResponse parse(CharSequence in) throws ParserException { 72 Matcher matcher = responsePattern.matcher(in); 73 if (matcher.matches()) { 74 int code = Integer.parseInt(matcher.group(1)); 75 if (code != 220) { 76 return new SMTPResponse( 77 code, 78 matcher.group(2) != null 79 ? matcher.group(2).equals("-") 80 : false, 81 matcher.group(3)); 82 } else { 83 Matcher greetingMatcher = 85 greetingPattern.matcher(matcher.group(3)); 86 if (greetingMatcher.matches()) { 87 return new SMTPResponse( 88 code, 89 matcher.group(2) != null 90 ? matcher.group(2).equals("-") 91 : false, 92 greetingMatcher.group(2), 93 greetingMatcher.group(1)); 94 95 } else { 96 throw new ParserException(in.toString()); 97 } 98 } 99 } 100 101 throw new ParserException(in.toString()); 102 } 103 104 } 105 | Popular Tags |