Drupal: klient nemusí posílat In-Reply-To hlavičku, ale jen References, kde je víc messageID – vezmeme to poslední.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package org.sonews.daemon;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.net.SocketException;
24 import java.nio.ByteBuffer;
25 import java.nio.CharBuffer;
26 import java.nio.channels.ClosedChannelException;
27 import java.nio.channels.SelectionKey;
28 import java.nio.channels.SocketChannel;
29 import java.nio.charset.Charset;
30 import java.util.Arrays;
31 import java.util.Timer;
32 import java.util.TimerTask;
33 import java.util.logging.Level;
34 import org.sonews.daemon.command.Command;
35 import org.sonews.storage.Article;
36 import org.sonews.storage.Group;
37 import org.sonews.storage.StorageBackendException;
38 import org.sonews.util.Log;
39 import org.sonews.util.Stats;
40 import org.sonews.util.io.CRLFOutputStream;
41 import org.sonews.util.io.SMTPOutputStream;
44 * For every SocketChannel (so TCP/IP connection) there is an instance of
46 * @author Christian Lins
49 public final class NNTPConnection {
51 public static final String NEWLINE = "\r\n"; // RFC defines this as newline
52 public static final String MESSAGE_ID_PATTERN = "<[^>]+>";
53 private static final Timer cancelTimer = new Timer(true); // Thread-safe? True for run as daemon
54 /** SocketChannel is generally thread-safe */
55 private SocketChannel channel = null;
56 private Charset charset = Charset.forName("UTF-8");
57 private Command command = null;
58 private Article currentArticle = null;
59 private Group currentGroup = null;
60 private volatile long lastActivity = System.currentTimeMillis();
61 private ChannelLineBuffers lineBuffers = new ChannelLineBuffers();
62 private int readLock = 0;
63 private final Object readLockGate = new Object();
64 private SelectionKey writeSelKey = null;
66 private String username;
67 private boolean userAuthenticated = false;
69 public NNTPConnection(final SocketChannel channel)
71 if (channel == null) {
72 throw new IllegalArgumentException("channel is null");
75 this.channel = channel;
76 Stats.getInstance().clientConnect();
80 * Tries to get the read lock for this NNTPConnection. This method is Thread-
81 * safe and returns true of the read lock was successfully set. If the lock
82 * is still hold by another Thread the method returns false.
84 boolean tryReadLock() {
85 // As synchronizing simple types may cause deadlocks,
86 // we use a gate object.
87 synchronized (readLockGate) {
91 readLock = Thread.currentThread().hashCode();
98 * Releases the read lock in a Thread-safe way.
99 * @throws IllegalMonitorStateException if a Thread not holding the lock
100 * tries to release it.
102 void unlockReadLock() {
103 synchronized (readLockGate) {
104 if (readLock == Thread.currentThread().hashCode()) {
107 throw new IllegalMonitorStateException();
113 * @return Current input buffer of this NNTPConnection instance.
115 public ByteBuffer getInputBuffer() {
116 return this.lineBuffers.getInputBuffer();
120 * @return Output buffer of this NNTPConnection which has at least one byte
123 public ByteBuffer getOutputBuffer() {
124 return this.lineBuffers.getOutputBuffer();
128 * @return ChannelLineBuffers instance associated with this NNTPConnection.
130 public ChannelLineBuffers getBuffers() {
131 return this.lineBuffers;
135 * @return true if this connection comes from a local remote address.
137 public boolean isLocalConnection() {
138 return ((InetSocketAddress) this.channel.socket().getRemoteSocketAddress()).getHostName().equalsIgnoreCase("localhost");
141 void setWriteSelectionKey(SelectionKey selKey) {
142 this.writeSelKey = selKey;
145 public void shutdownInput() {
147 // Closes the input line of the channel's socket, so no new data
148 // will be received and a timeout can be triggered.
149 this.channel.socket().shutdownInput();
150 } catch (IOException ex) {
151 Log.get().warning("Exception in NNTPConnection.shutdownInput(): " + ex);
155 public void shutdownOutput() {
156 cancelTimer.schedule(new TimerTask() {
160 // Closes the output line of the channel's socket.
161 channel.socket().shutdownOutput();
163 } catch (SocketException ex) {
164 // Socket was already disconnected
165 Log.get().info("NNTPConnection.shutdownOutput(): " + ex);
166 } catch (Exception ex) {
167 Log.get().warning("NNTPConnection.shutdownOutput(): " + ex);
173 public SocketChannel getSocketChannel() {
177 public Article getCurrentArticle() {
178 return this.currentArticle;
181 public Charset getCurrentCharset() {
186 * @return The currently selected communication channel (not SocketChannel)
188 public Group getCurrentChannel() {
189 return this.currentGroup;
192 public void setCurrentArticle(final Article article) {
193 this.currentArticle = article;
196 public void setCurrentGroup(final Group group) {
197 this.currentGroup = group;
200 public long getLastActivity() {
201 return this.lastActivity;
205 * Due to the readLockGate there is no need to synchronize this method.
207 * @throws IllegalArgumentException if raw is null.
208 * @throws IllegalStateException if calling thread does not own the readLock.
210 void lineReceived(byte[] raw) {
212 throw new IllegalArgumentException("raw is null");
215 if (readLock == 0 || readLock != Thread.currentThread().hashCode()) {
216 throw new IllegalStateException("readLock not properly set");
219 this.lastActivity = System.currentTimeMillis();
221 String line = new String(raw, this.charset);
223 // There might be a trailing \r, but trim() is a bad idea
224 // as it removes also leading spaces from long header lines.
225 if (line.endsWith("\r")) {
226 line = line.substring(0, line.length() - 1);
227 raw = Arrays.copyOf(raw, raw.length - 1);
230 Log.get().fine("<< " + line);
232 if (command == null) {
233 command = parseCommandLine(line);
234 assert command != null;
238 // The command object will process the line we just received
240 command.processLine(this, line, raw);
241 } catch (StorageBackendException ex) {
242 Log.get().info("Retry command processing after StorageBackendException");
244 // Try it a second time, so that the backend has time to recover
245 command.processLine(this, line, raw);
247 } catch (ClosedChannelException ex0) {
249 StringBuilder strBuf = new StringBuilder();
250 strBuf.append("Connection to ");
251 strBuf.append(channel.socket().getRemoteSocketAddress());
252 strBuf.append(" closed: ");
254 Log.get().info(strBuf.toString());
255 } catch (Exception ex0a) {
256 ex0a.printStackTrace();
258 } catch (Exception ex1) { // This will catch a second StorageBackendException
261 Log.get().log(Level.WARNING, ex1.getLocalizedMessage(), ex1);
262 println("403 Internal server error");
264 // Should we end the connection here?
265 // RFC says we MUST return 400 before closing the connection
268 } catch (Exception ex2) {
269 ex2.printStackTrace();
273 if (command == null || command.hasFinished()) {
275 charset = Charset.forName("UTF-8"); // Reset to default
280 * This method determines the fitting command processing class.
284 private Command parseCommandLine(String line) {
285 String cmdStr = line.split(" ")[0];
286 return CommandSelector.getInstance().get(cmdStr);
290 * Puts the given line into the output buffer, adds a newline character
291 * and returns. The method returns immediately and does not block until
292 * the line was sent. If line is longer than 510 octets it is split up in
293 * several lines. Each line is terminated by \r\n (NNTPConnection.NEWLINE).
296 public void println(final CharSequence line, final Charset charset)
298 writeToChannel(CharBuffer.wrap(line), charset, line);
299 writeToChannel(CharBuffer.wrap(NEWLINE), charset, null);
303 * Writes the given raw lines to the output buffers and finishes with
304 * a newline character (\r\n).
307 public void println(final byte[] rawLines)
309 this.lineBuffers.addOutputBuffer(ByteBuffer.wrap(rawLines));
310 writeToChannel(CharBuffer.wrap(NEWLINE), charset, null);
314 * Same as {@link #println(byte[]) } but escapes lines containing single dot,
315 * which has special meaning in protocol (end of message).
317 * This method is safe to be used for writing messages – if message contains
318 * a line with single dot, it will be doubled and thus not interpreted
319 * by NNTP client as end of message
322 * @throws IOException
324 public void printlnEscapeDots(final byte[] rawLines) throws IOException {
325 // TODO: optimalizace
327 ByteArrayOutputStream baos = new ByteArrayOutputStream(rawLines.length + 10);
328 CRLFOutputStream crlfStream = new CRLFOutputStream(baos);
329 SMTPOutputStream smtpStream = new SMTPOutputStream(crlfStream);
330 smtpStream.write(rawLines);
332 println(baos.toByteArray());
338 * Encodes the given CharBuffer using the given Charset to a bunch of
339 * ByteBuffers (each 512 bytes large) and enqueues them for writing at the
340 * connected SocketChannel.
341 * @throws java.io.IOException
343 private void writeToChannel(CharBuffer characters, final Charset charset,
344 CharSequence debugLine)
346 if (!charset.canEncode()) {
347 Log.get().severe("FATAL: Charset " + charset + " cannot encode!");
351 // Write characters to output buffers
352 LineEncoder lenc = new LineEncoder(characters, charset);
353 lenc.encode(lineBuffers);
355 enableWriteEvents(debugLine);
358 private void enableWriteEvents(CharSequence debugLine) {
359 // Enable OP_WRITE events so that the buffers are processed
361 this.writeSelKey.interestOps(SelectionKey.OP_WRITE);
362 ChannelWriter.getInstance().getSelector().wakeup();
363 } catch (Exception ex) // CancelledKeyException and ChannelCloseException
365 Log.get().warning("NNTPConnection.writeToChannel(): " + ex);
369 // Update last activity timestamp
370 this.lastActivity = System.currentTimeMillis();
371 if (debugLine != null) {
372 Log.get().fine(">> " + debugLine);
376 public void println(final CharSequence line)
378 println(line, charset);
381 public void print(final String line)
383 writeToChannel(CharBuffer.wrap(line), charset, line);
386 public void setCurrentCharset(final Charset charset) {
387 this.charset = charset;
390 void setLastActivity(long timestamp) {
391 this.lastActivity = timestamp;
395 * @return Current username.
396 * But user may not have been authenticated yet.
397 * You must check {@link #isUserAuthenticated()}
399 public String getUsername() {
404 * This method is to be called from AUTHINFO USER Command implementation.
405 * @param username username from AUTHINFO USER username.
407 public void setUsername(String username) {
408 this.username = username;
412 * @return true if current user (see {@link #getUsername()}) has been succesfully authenticated.
414 public boolean isUserAuthenticated() {
415 return userAuthenticated;
419 * This method is to be called from AUTHINFO PASS Command implementation.
420 * @param userAuthenticated true if user has provided right password in AUTHINFO PASS password.
422 public void setUserAuthenticated(boolean userAuthenticated) {
423 this.userAuthenticated = userAuthenticated;