1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.imapserver;
21
22 import java.io.IOException;
23 import java.net.Socket;
24
25 import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
26 import org.apache.avalon.framework.container.ContainerUtil;
27 import org.apache.avalon.framework.logger.Logger;
28 import org.apache.james.imap.api.process.ImapSession;
29 import org.apache.james.imap.main.ContextualLog;
30 import org.apache.james.imap.main.ImapRequestHandler;
31 import org.apache.james.imap.main.ImapSessionImpl;
32 import org.apache.james.imap.main.OutputStreamImapResponseWriter;
33 import org.apache.james.imap.encode.ImapResponseComposer;
34 import org.apache.james.imap.encode.base.ImapResponseComposerImpl;
35 import org.apache.james.socket.ProtocolHandler;
36 import org.apache.james.socket.ProtocolHandlerHelper;
37 import org.apache.commons.logging.impl.AvalonLogger;
38
39
40
41
42 public class ImapHandler implements ProtocolHandler
43 {
44
45 private ProtocolHandlerHelper helper;
46
47 private static final byte[] EMERGENCY_SIGNOFF = {'*',' ', 'B', 'Y', 'E', ' ',
48 'S', 'e', 'r', 'v', 'e', 'r', ' ', 'f', 'a', 'u', 'l', 't', '\r', '\n'};
49
50 private final String hello;
51 private final ImapRequestHandler requestHandler;
52 private ImapSession session;
53
54 public ImapHandler(final ImapRequestHandler requestHandler, final String hello) {
55 super();
56 this.requestHandler = requestHandler;
57 this.hello = hello;
58 }
59
60
61 public void setConfigurationData( Object theData )
62 {
63 }
64
65
66
67
68 public void resetHandler() {
69
70
71 try {
72 if (session != null) {
73 session.logout();
74 }
75 } catch (Exception e) {
76 getLogger().warn("Failed to close mailbox: " + e.getMessage());
77 getLogger().debug(e.getMessage(), e);
78 }
79 session = null;
80 }
81
82
83
84
85 public void handleProtocol() throws IOException {
86 getLogger().debug(
87 "Connection from " + helper.getRemoteHost() + " (" + helper.getRemoteIP()
88 + ") opened.");
89 final OutputStreamImapResponseWriter writer = new OutputStreamImapResponseWriter( helper.getOutputStream() );
90 ImapResponseComposer response = new ImapResponseComposerImpl( writer);
91
92
93
94 response.hello(hello);
95
96 setUpSession();
97
98 helper.getWatchdog().start();
99 while ( handleRequest() ) {
100 helper.getWatchdog().reset();
101 }
102 helper.getWatchdog().stop();
103 if (session != null) {
104 session.logout();
105 }
106
107 getLogger().info(
108 "Connection from " + helper.getRemoteHost() + " (" + helper.getRemoteIP()
109 + ") closed.");
110 }
111
112
113
114
115 private void setUpSession() {
116 final ImapSessionImpl session = new ImapSessionImpl();
117 session.setLog(new ContextualLog("[" + helper.getName() + "]", new AvalonLogger(getLogger())));
118 this.session = session;
119 }
120
121 private boolean handleRequest() {
122 final boolean result = requestHandler.handleRequest( helper.getInputStream(), helper.getOutputStream(), session );
123 return result;
124 }
125
126
127
128
129
130
131 public void errorHandler(RuntimeException e) {
132 try {
133 helper.getOutputStream().write(EMERGENCY_SIGNOFF);
134 } catch (Throwable t) {
135 getLogger().debug("Write emergency signoff failed.", t);
136 }
137 helper.defaultErrorHandler(e);
138 }
139
140 public Logger getLogger() {
141 return helper.getAvalonLogger();
142 }
143
144 public void setProtocolHandlerHelper(ProtocolHandlerHelper phh) {
145 this.helper = phh;
146 }
147
148 }
149