1 /**
2 *
3 * Copyright 2003-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.javamail.store.pop3;
19
20 import java.io.BufferedReader;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import javax.mail.MessagingException;
27 import javax.mail.Session;
28
29 import org.apache.geronimo.javamail.store.pop3.response.POP3ResponseFactory;
30
31 ;
32
33 /**
34 * Builds a basic response out of the input stream received by the connection.
35 * Performs only two basic functions
36 * <ul>
37 * <li>Extrats the status code</li>
38 * <li>If multi-line response then extract the data as an input stream</li>
39 * </ul>
40 *
41 * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
42 */
43
44 public final class POP3ResponseBuilder implements POP3Constants {
45
46 public static POP3Response buildResponse(Session session, BufferedReader reader, boolean isMultiLineResponse)
47 throws MessagingException {
48
49 int status = ERR;
50 InputStream data = null;
51
52 String line;
53 try {
54 line = reader.readLine();
55 } catch (IOException e) {
56 throw new MessagingException("Error in receving response");
57 }
58 if (line == null || line.trim().equals("")) {
59 if (session.getDebug()) {
60 session.getDebugOut().println("Empty Response");
61 }
62 throw new MessagingException("Empty Response");
63 }
64 if (session.getDebug()) {
65 session.getDebugOut().println("Response From Server " + line);
66 }
67
68 if (line.startsWith("+OK")) {
69 status = OK;
70 line = removeStatusField(line);
71 if (isMultiLineResponse) {
72 data = getMultiLineResponse(session, reader);
73 }
74 } else if (line.startsWith("-ERR")) {
75 status = ERR;
76 line = removeStatusField(line);
77 } else {
78 throw new MessagingException("Unexpected response: " + line);
79 }
80
81 return POP3ResponseFactory.getDefaultResponse(status, line, data);
82 }
83
84 private static String removeStatusField(String line) {
85 return line.substring(line.indexOf(SPACE) + 1);
86 }
87
88 /**
89 * This could be a multiline response
90 */
91 private static InputStream getMultiLineResponse(Session session, BufferedReader reader) throws MessagingException {
92
93 int byteRead = -1;
94 int lastByteRead = LF;
95 ByteArrayOutputStream out = new ByteArrayOutputStream();
96 try {
97 while ((byteRead = reader.read()) >= 0) {
98
99
100
101
102 if (lastByteRead == DOT && byteRead == CR) {
103 byteRead = reader.read();
104
105 if (byteRead == LF) {
106
107 break;
108 }
109 }
110
111 out.write(byteRead);
112 lastByteRead = byteRead;
113 }
114
115 if (session.getDebug()) {
116 session.getDebugOut().println("\n============================ Response Content==================\n");
117 session.getDebugOut().write(out.toByteArray());
118 session.getDebugOut().println("\n==============================================================\n");
119 }
120
121 } catch (IOException e) {
122 throw new MessagingException("Error processing a multi-line response", e);
123 }
124
125 return new ByteArrayInputStream(out.toByteArray());
126 }
127
128 }