01: package pygmy.nntp;
02:
03: import pygmy.core.ServerSocketEndPoint;
04: import pygmy.core.ConfigOption;
05:
06: import java.io.IOException;
07: import java.net.Socket;
08: import java.util.Properties;
09: import java.util.logging.Logger;
10: import java.util.logging.Level;
11:
12: public class NntpEndPoint extends ServerSocketEndPoint {
13: private static final Logger log = Logger
14: .getLogger(NntpEndPoint.class.getName());
15:
16: private static final ConfigOption POSTING_OPTION = new ConfigOption(
17: "posting", "true", "Enable posting");
18:
19: public NntpEndPoint() {
20: }
21:
22: protected Runnable createRunnable(Socket client, Properties config)
23: throws IOException {
24: return new NntpRunnable(client, config);
25: }
26:
27: public class NntpRunnable implements Runnable {
28: Socket client;
29: Properties config;
30:
31: public NntpRunnable(Socket client, Properties config) {
32: this .client = client;
33: this .config = config;
34: }
35:
36: public void run() {
37: try {
38: handleConnection(client);
39: } catch (IOException ioe) {
40: log.log(Level.WARNING,
41: "IOException while reading socket.", ioe);
42: } finally {
43: try {
44: client.close();
45: } catch (IOException e) {
46: log.log(Level.WARNING,
47: "Exception while closing socket.", e);
48: }
49: }
50: }
51:
52: private void handleConnection(Socket client) throws IOException {
53: NntpRequest request = new NntpRequest(client, config,
54: client.getInputStream());
55: NntpResponse response = new NntpResponse(client
56: .getOutputStream());
57: sendGreeting(response);
58: while (!request.isDone()) {
59: try {
60: request.nextCommand();
61: if (!request.isDone()
62: && !server.post(request, response)) {
63: log.log(Level.WARNING, "Command "
64: + request.getCommand()
65: + " not recognized.");
66: response.sendResponse(500,
67: "command not recognized");
68: }
69: } catch (Exception e) {
70: log
71: .log(
72: Level.WARNING,
73: "Exception received while handling command.",
74: e);
75: response.sendResponse(503,
76: "program fault - command not performed");
77: }
78: }
79: response.sendResponse(205, "closing connection - goodbye!");
80: }
81:
82: private void sendGreeting(NntpResponse response)
83: throws IOException {
84: Boolean canPost = POSTING_OPTION.getBoolean(server,
85: getName());
86: config.put("posting", canPost.toString());
87: if (canPost.booleanValue()) {
88: response.respondHello(client.getLocalAddress()
89: .getHostName());
90: } else {
91: response.respondHelloNoPosting(client.getLocalAddress()
92: .getHostName());
93: }
94: }
95: }
96: }
|