1.1 --- a/src/org/sonews/daemon/command/OverCommand.java Sun Aug 29 17:28:58 2010 +0200
1.2 +++ b/src/org/sonews/daemon/command/OverCommand.java Sun Sep 11 15:05:04 2011 +0200
1.3 @@ -109,186 +109,153 @@
1.4 public class OverCommand implements Command
1.5 {
1.6
1.7 - public static final int MAX_LINES_PER_DBREQUEST = 200;
1.8 + public static final int MAX_LINES_PER_DBREQUEST = 200;
1.9
1.10 - @Override
1.11 - public String[] getSupportedCommandStrings()
1.12 - {
1.13 - return new String[]{"OVER", "XOVER"};
1.14 - }
1.15 + @Override
1.16 + public String[] getSupportedCommandStrings()
1.17 + {
1.18 + return new String[] {"OVER", "XOVER"};
1.19 + }
1.20
1.21 - @Override
1.22 - public boolean hasFinished()
1.23 - {
1.24 - return true;
1.25 - }
1.26 + @Override
1.27 + public boolean hasFinished()
1.28 + {
1.29 + return true;
1.30 + }
1.31
1.32 - @Override
1.33 - public String impliedCapability()
1.34 - {
1.35 - return null;
1.36 - }
1.37 + @Override
1.38 + public String impliedCapability()
1.39 + {
1.40 + return null;
1.41 + }
1.42
1.43 - @Override
1.44 - public boolean isStateful()
1.45 - {
1.46 - return false;
1.47 - }
1.48 + @Override
1.49 + public boolean isStateful()
1.50 + {
1.51 + return false;
1.52 + }
1.53
1.54 - @Override
1.55 - public void processLine(NNTPConnection conn, final String line, byte[] raw)
1.56 - throws IOException, StorageBackendException
1.57 - {
1.58 - if(conn.getCurrentChannel() == null)
1.59 - {
1.60 - conn.println("412 no newsgroup selected");
1.61 - }
1.62 - else
1.63 - {
1.64 - String[] command = line.split(" ");
1.65 + @Override
1.66 + public void processLine(NNTPConnection conn, final String line, byte[] raw)
1.67 + throws IOException, StorageBackendException
1.68 + {
1.69 + if (conn.getCurrentChannel() == null) {
1.70 + conn.println("412 no newsgroup selected");
1.71 + } else {
1.72 + String[] command = line.split(" ");
1.73
1.74 - // If no parameter was specified, show information about
1.75 - // the currently selected article(s)
1.76 - if(command.length == 1)
1.77 - {
1.78 - final Article art = conn.getCurrentArticle();
1.79 - if(art == null)
1.80 - {
1.81 - conn.println("420 no article(s) selected");
1.82 - return;
1.83 - }
1.84 + // If no parameter was specified, show information about
1.85 + // the currently selected article(s)
1.86 + if (command.length == 1) {
1.87 + final Article art = conn.getCurrentArticle();
1.88 + if (art == null) {
1.89 + conn.println("420 no article(s) selected");
1.90 + return;
1.91 + }
1.92
1.93 - conn.println(buildOverview(art, -1));
1.94 - }
1.95 - // otherwise print information about the specified range
1.96 - else
1.97 - {
1.98 - long artStart;
1.99 - long artEnd = conn.getCurrentChannel().getLastArticleNumber();
1.100 - String[] nums = command[1].split("-");
1.101 - if(nums.length >= 1)
1.102 - {
1.103 - try
1.104 - {
1.105 - artStart = Integer.parseInt(nums[0]);
1.106 - }
1.107 - catch(NumberFormatException e)
1.108 - {
1.109 - Log.get().info(e.getMessage());
1.110 - artStart = Integer.parseInt(command[1]);
1.111 - }
1.112 - }
1.113 - else
1.114 - {
1.115 - artStart = conn.getCurrentChannel().getFirstArticleNumber();
1.116 - }
1.117 + conn.println(buildOverview(art, -1));
1.118 + } // otherwise print information about the specified range
1.119 + else {
1.120 + long artStart;
1.121 + long artEnd = conn.getCurrentChannel().getLastArticleNumber();
1.122 + String[] nums = command[1].split("-");
1.123 + if (nums.length >= 1) {
1.124 + try {
1.125 + artStart = Integer.parseInt(nums[0]);
1.126 + } catch (NumberFormatException e) {
1.127 + Log.get().info(e.getMessage());
1.128 + artStart = Integer.parseInt(command[1]);
1.129 + }
1.130 + } else {
1.131 + artStart = conn.getCurrentChannel().getFirstArticleNumber();
1.132 + }
1.133
1.134 - if(nums.length >=2)
1.135 - {
1.136 - try
1.137 - {
1.138 - artEnd = Integer.parseInt(nums[1]);
1.139 - }
1.140 - catch(NumberFormatException e)
1.141 - {
1.142 - e.printStackTrace();
1.143 - }
1.144 - }
1.145 + if (nums.length >= 2) {
1.146 + try {
1.147 + artEnd = Integer.parseInt(nums[1]);
1.148 + } catch (NumberFormatException e) {
1.149 + e.printStackTrace();
1.150 + }
1.151 + }
1.152
1.153 - if(artStart > artEnd)
1.154 - {
1.155 - if(command[0].equalsIgnoreCase("OVER"))
1.156 - {
1.157 - conn.println("423 no articles in that range");
1.158 - }
1.159 - else
1.160 - {
1.161 - conn.println("224 (empty) overview information follows:");
1.162 - conn.println(".");
1.163 - }
1.164 - }
1.165 - else
1.166 - {
1.167 - for(long n = artStart; n <= artEnd; n += MAX_LINES_PER_DBREQUEST)
1.168 - {
1.169 - long nEnd = Math.min(n + MAX_LINES_PER_DBREQUEST - 1, artEnd);
1.170 - List<Pair<Long, ArticleHead>> articleHeads = conn.getCurrentChannel()
1.171 - .getArticleHeads(n, nEnd);
1.172 - if(articleHeads.isEmpty() && n == artStart
1.173 - && command[0].equalsIgnoreCase("OVER"))
1.174 - {
1.175 - // This reply is only valid for OVER, not for XOVER command
1.176 - conn.println("423 no articles in that range");
1.177 - return;
1.178 - }
1.179 - else if(n == artStart)
1.180 - {
1.181 - // XOVER replies this although there is no data available
1.182 - conn.println("224 overview information follows");
1.183 - }
1.184 + if (artStart > artEnd) {
1.185 + if (command[0].equalsIgnoreCase("OVER")) {
1.186 + conn.println("423 no articles in that range");
1.187 + } else {
1.188 + conn.println("224 (empty) overview information follows:");
1.189 + conn.println(".");
1.190 + }
1.191 + } else {
1.192 + for (long n = artStart; n <= artEnd; n += MAX_LINES_PER_DBREQUEST) {
1.193 + long nEnd = Math.min(n + MAX_LINES_PER_DBREQUEST - 1, artEnd);
1.194 + List<Pair<Long, ArticleHead>> articleHeads = conn.getCurrentChannel().getArticleHeads(n, nEnd);
1.195 + if (articleHeads.isEmpty() && n == artStart
1.196 + && command[0].equalsIgnoreCase("OVER")) {
1.197 + // This reply is only valid for OVER, not for XOVER command
1.198 + conn.println("423 no articles in that range");
1.199 + return;
1.200 + } else if (n == artStart) {
1.201 + // XOVER replies this although there is no data available
1.202 + conn.println("224 overview information follows");
1.203 + }
1.204
1.205 - for(Pair<Long, ArticleHead> article : articleHeads)
1.206 - {
1.207 - String overview = buildOverview(article.getB(), article.getA());
1.208 - conn.println(overview);
1.209 - }
1.210 - } // for
1.211 - conn.println(".");
1.212 - }
1.213 - }
1.214 - }
1.215 - }
1.216 -
1.217 - private String buildOverview(ArticleHead art, long nr)
1.218 - {
1.219 - StringBuilder overview = new StringBuilder();
1.220 - overview.append(nr);
1.221 - overview.append('\t');
1.222 + for (Pair<Long, ArticleHead> article : articleHeads) {
1.223 + String overview = buildOverview(article.getB(), article.getA());
1.224 + conn.println(overview);
1.225 + }
1.226 + } // for
1.227 + conn.println(".");
1.228 + }
1.229 + }
1.230 + }
1.231 + }
1.232
1.233 - String subject = art.getHeader(Headers.SUBJECT)[0];
1.234 - if("".equals(subject))
1.235 - {
1.236 - subject = "<empty>";
1.237 - }
1.238 - overview.append(escapeString(subject));
1.239 - overview.append('\t');
1.240 + private String buildOverview(ArticleHead art, long nr)
1.241 + {
1.242 + StringBuilder overview = new StringBuilder();
1.243 + overview.append(nr);
1.244 + overview.append('\t');
1.245
1.246 - overview.append(escapeString(art.getHeader(Headers.FROM)[0]));
1.247 - overview.append('\t');
1.248 - overview.append(escapeString(art.getHeader(Headers.DATE)[0]));
1.249 - overview.append('\t');
1.250 - overview.append(escapeString(art.getHeader(Headers.MESSAGE_ID)[0]));
1.251 - overview.append('\t');
1.252 - overview.append(escapeString(art.getHeader(Headers.REFERENCES)[0]));
1.253 - overview.append('\t');
1.254 + String subject = art.getHeader(Headers.SUBJECT)[0];
1.255 + if ("".equals(subject)) {
1.256 + subject = "<empty>";
1.257 + }
1.258 + overview.append(escapeString(subject));
1.259 + overview.append('\t');
1.260
1.261 - String bytes = art.getHeader(Headers.BYTES)[0];
1.262 - if("".equals(bytes))
1.263 - {
1.264 - bytes = "0";
1.265 - }
1.266 - overview.append(escapeString(bytes));
1.267 - overview.append('\t');
1.268 + overview.append(escapeString(art.getHeader(Headers.FROM)[0]));
1.269 + overview.append('\t');
1.270 + overview.append(escapeString(art.getHeader(Headers.DATE)[0]));
1.271 + overview.append('\t');
1.272 + overview.append(escapeString(art.getHeader(Headers.MESSAGE_ID)[0]));
1.273 + overview.append('\t');
1.274 + overview.append(escapeString(art.getHeader(Headers.REFERENCES)[0]));
1.275 + overview.append('\t');
1.276
1.277 - String lines = art.getHeader(Headers.LINES)[0];
1.278 - if("".equals(lines))
1.279 - {
1.280 - lines = "0";
1.281 - }
1.282 - overview.append(escapeString(lines));
1.283 - overview.append('\t');
1.284 - overview.append(escapeString(art.getHeader(Headers.XREF)[0]));
1.285 + String bytes = art.getHeader(Headers.BYTES)[0];
1.286 + if ("".equals(bytes)) {
1.287 + bytes = "0";
1.288 + }
1.289 + overview.append(escapeString(bytes));
1.290 + overview.append('\t');
1.291
1.292 - // Remove trailing tabs if some data is empty
1.293 - return overview.toString().trim();
1.294 - }
1.295 -
1.296 - private String escapeString(String str)
1.297 - {
1.298 - String nstr = str.replace("\r", "");
1.299 - nstr = nstr.replace('\n', ' ');
1.300 - nstr = nstr.replace('\t', ' ');
1.301 - return nstr.trim();
1.302 - }
1.303 -
1.304 + String lines = art.getHeader(Headers.LINES)[0];
1.305 + if ("".equals(lines)) {
1.306 + lines = "0";
1.307 + }
1.308 + overview.append(escapeString(lines));
1.309 + overview.append('\t');
1.310 + overview.append(escapeString(art.getHeader(Headers.XREF)[0]));
1.311 +
1.312 + // Remove trailing tabs if some data is empty
1.313 + return overview.toString().trim();
1.314 + }
1.315 +
1.316 + private String escapeString(String str)
1.317 + {
1.318 + String nstr = str.replace("\r", "");
1.319 + nstr = nstr.replace('\n', ' ');
1.320 + nstr = nstr.replace('\t', ' ');
1.321 + return nstr.trim();
1.322 + }
1.323 }