1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.socket;
21
22 import org.apache.avalon.framework.logger.Logger;
23 import org.apache.james.util.watchdog.Watchdog;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.PrintWriter;
29 import java.net.Socket;
30
31 /**
32 * Common Handler code
33 */
34 public class DelegatingJamesHandler extends AbstractJamesHandler implements ProtocolHandlerHelper {
35
36 protected ProtocolHandler protocolHandler;
37
38
39 public DelegatingJamesHandler(ProtocolHandler delegated) {
40 this.protocolHandler = delegated;
41 this.protocolHandler.setProtocolHandlerHelper(this);
42 }
43
44 /**
45 * This method will be implemented checking for the correct class
46 * type.
47 *
48 * @param theData Configuration Bean.
49 */
50 public void setConfigurationData(Object theData) {
51 protocolHandler.setConfigurationData(theData);
52 }
53
54 /**
55 * Handle the protocol
56 *
57 * @throws IOException get thrown if an IO error is detected
58 */
59 public void handleProtocol() throws IOException {
60 protocolHandler.handleProtocol();
61 }
62
63 /**
64 * Resets the handler data to a basic state.
65 */
66 public void resetHandler() {
67 protocolHandler.resetHandler();
68 remoteHost = null;
69 remoteIP = null;
70 }
71
72 /**
73 * @see org.apache.james.socket.AbstractJamesHandler#errorHandler(java.lang.RuntimeException)
74 */
75 protected void errorHandler(RuntimeException e) {
76 protocolHandler.errorHandler(e);
77 }
78
79 /**
80 * @see org.apache.james.socket.ProtocolHandlerHelper#defaultErrorHandler(java.lang.RuntimeException)
81 */
82 public void defaultErrorHandler(RuntimeException e) {
83 super.errorHandler(e);
84 }
85
86 /**
87 * @see org.apache.james.socket.ProtocolHandlerHelper#getRemoteIP()
88 */
89 public String getRemoteIP() {
90 return remoteIP;
91 }
92
93 /**
94 * @see org.apache.james.socket.ProtocolHandlerHelper#getInputReader()
95 */
96 public CRLFTerminatedReader getInputReader() {
97 return inReader;
98 }
99
100 /**
101 * @see org.apache.james.socket.ProtocolHandlerHelper#getInputStream()
102 */
103 public InputStream getInputStream() {
104 return in;
105 }
106
107 /**
108 * @see org.apache.james.socket.ProtocolHandlerHelper#getOutputStream()
109 */
110 public OutputStream getOutputStream() {
111 return outs;
112 }
113
114 /**
115 * @see org.apache.james.socket.ProtocolHandlerHelper#getOutputWriter()
116 */
117 public PrintWriter getOutputWriter() {
118 return out;
119 }
120
121 /**
122 * @see org.apache.james.socket.ProtocolHandlerHelper#getRemoteHost()
123 */
124 public String getRemoteHost() {
125 return remoteHost;
126 }
127
128 /**
129 * @see org.apache.james.socket.ProtocolHandlerHelper#getAvalonLogger()
130 */
131 public Logger getAvalonLogger() {
132 return getLogger();
133 }
134
135 /**
136 * @see org.apache.james.socket.ProtocolHandlerHelper#getWatchdog()
137 */
138 public Watchdog getWatchdog() {
139 return theWatchdog;
140 }
141
142 /**
143 * @see org.apache.james.socket.ProtocolHandlerHelper#getSocket()
144 */
145 public Socket getSocket() {
146 return socket;
147 }
148 }