NewGroupsOrNewsQuery.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.nntp;

  18. import java.util.Calendar;
  19. import java.util.Objects;

  20. /**
  21.  * The NewGroupsOrNewsQuery class. This is used to issue NNTP NEWGROUPS and NEWNEWS queries, implemented by
  22.  * {@link org.apache.commons.net.nntp.NNTPClient#listNewNewsgroups listNewNewsGroups } and {@link org.apache.commons.net.nntp.NNTPClient#listNewNews listNewNews
  23.  * } respectively. It prevents you from having to format date, time, distribution, and newgroup arguments.
  24.  * <p>
  25.  * You might use the class as follows:
  26.  * </p>
  27.  *
  28.  * <pre>
  29.  * query = new NewsGroupsOrNewsQuery(new GregorianCalendar(97, 11, 15), false);
  30.  * query.addDistribution("comp");
  31.  * NewsgroupInfo[] newsgroups = client.listNewgroups(query);
  32.  * </pre>
  33.  *
  34.  * This will retrieve the list of newsgroups starting with the comp. distribution prefix created since midnight 11/15/97.
  35.  *
  36.  * @see NNTPClient
  37.  */

  38. public final class NewGroupsOrNewsQuery {
  39.     private final String date, time;
  40.     private StringBuffer distributions;
  41.     private StringBuffer newsgroups;
  42.     private final boolean isGMT;

  43.     /**
  44.      * Creates a new query using the given time as a reference point.
  45.      *
  46.      * @param date The date since which new groups or news have arrived.
  47.      * @param gmt  True if the date should be considered as GMT, false if not.
  48.      */
  49.     public NewGroupsOrNewsQuery(final Calendar date, final boolean gmt) {
  50.         int num;
  51.         String str;
  52.         final StringBuilder buffer;

  53.         this.distributions = null;
  54.         this.newsgroups = null;
  55.         this.isGMT = gmt;

  56.         buffer = new StringBuilder();

  57.         // Get year
  58.         num = date.get(Calendar.YEAR);
  59.         str = Integer.toString(num);
  60.         num = str.length();

  61.         if (num >= 2) {
  62.             buffer.append(str.substring(num - 2));
  63.         } else {
  64.             buffer.append("00");
  65.         }

  66.         // Get month
  67.         num = date.get(Calendar.MONTH) + 1;
  68.         str = Integer.toString(num);
  69.         num = str.length();

  70.         if (num == 1) {
  71.             buffer.append('0');
  72.             buffer.append(str);
  73.         } else if (num == 2) {
  74.             buffer.append(str);
  75.         } else {
  76.             buffer.append("01");
  77.         }

  78.         // Get day
  79.         num = date.get(Calendar.DAY_OF_MONTH);
  80.         str = Integer.toString(num);
  81.         num = str.length();

  82.         if (num == 1) {
  83.             buffer.append('0');
  84.             buffer.append(str);
  85.         } else if (num == 2) {
  86.             buffer.append(str);
  87.         } else {
  88.             buffer.append("01");
  89.         }

  90.         this.date = buffer.toString();

  91.         buffer.setLength(0);

  92.         // Get hour
  93.         num = date.get(Calendar.HOUR_OF_DAY);
  94.         str = Integer.toString(num);
  95.         num = str.length();

  96.         if (num == 1) {
  97.             buffer.append('0');
  98.             buffer.append(str);
  99.         } else if (num == 2) {
  100.             buffer.append(str);
  101.         } else {
  102.             buffer.append("00");
  103.         }

  104.         // Get minutes
  105.         num = date.get(Calendar.MINUTE);
  106.         str = Integer.toString(num);
  107.         num = str.length();

  108.         if (num == 1) {
  109.             buffer.append('0');
  110.             buffer.append(str);
  111.         } else if (num == 2) {
  112.             buffer.append(str);
  113.         } else {
  114.             buffer.append("00");
  115.         }

  116.         // Get seconds
  117.         num = date.get(Calendar.SECOND);
  118.         str = Integer.toString(num);
  119.         num = str.length();

  120.         if (num == 1) {
  121.             buffer.append('0');
  122.             buffer.append(str);
  123.         } else if (num == 2) {
  124.             buffer.append(str);
  125.         } else {
  126.             buffer.append("00");
  127.         }

  128.         this.time = buffer.toString();
  129.     }

  130.     /**
  131.      * Add a distribution group to the query. The distribution part of a newsgroup is the segment of the name preceding the first dot (e.g., comp, alt, rec).
  132.      * Only those newsgroups matching one of the distributions or, in the case of NEWNEWS, an article in a newsgroup matching one of the distributions, will be
  133.      * reported as a query result. Adding distributions is purely optional.
  134.      *
  135.      * @param distribution A distribution to add to the query.
  136.      */
  137.     public void addDistribution(final String distribution) {
  138.         if (distributions != null) {
  139.             distributions.append(',');
  140.         } else {
  141.             distributions = new StringBuffer();
  142.         }
  143.         distributions.append(distribution);
  144.     }

  145.     /**
  146.      * Add a newsgroup to the list of newsgroups being queried. Newsgroups added this way are only meaningful to the NEWNEWS command. Newsgroup names may
  147.      * include the <code>*</code> wildcard, as in <code>comp.lang.*</code> or <code>comp.lang.java.*</code>. Adding at least one newsgroup is mandatory for
  148.      * the NEWNEWS command.
  149.      *
  150.      * @param newsgroup The newsgroup to add to the list of groups to be checked for new news.
  151.      */
  152.     public void addNewsgroup(final String newsgroup) {
  153.         if (newsgroups != null) {
  154.             newsgroups.append(',');
  155.         } else {
  156.             newsgroups = new StringBuffer();
  157.         }
  158.         newsgroups.append(newsgroup);
  159.     }

  160.     /**
  161.      * Return the NNTP query formatted date (year, month, day in the form YYMMDD).
  162.      *
  163.      * @return The NNTP query formatted date.
  164.      */
  165.     public String getDate() {
  166.         return date;
  167.     }

  168.     /**
  169.      * Return the comma separated list of distributions. This may be null if there are no distributions.
  170.      *
  171.      * @return The list of distributions, which may be null if no distributions have been specified.
  172.      */
  173.     public String getDistributions() {
  174.         return Objects.toString(distributions, null);
  175.     }

  176.     /**
  177.      * Return the comma separated list of newsgroups. This may be null if there are no newsgroups
  178.      *
  179.      * @return The list of newsgroups, which may be null if no newsgroups have been specified.
  180.      */
  181.     public String getNewsgroups() {
  182.         return Objects.toString(newsgroups, null);
  183.     }

  184.     /**
  185.      * Return the NNTP query formatted time (hour, minutes, seconds in the form HHMMSS).
  186.      *
  187.      * @return The NNTP query formatted time.
  188.      */
  189.     public String getTime() {
  190.         return time;
  191.     }

  192.     /**
  193.      * Return whether or not the query date should be treated as GMT.
  194.      *
  195.      * @return True if the query date is to be treated as GMT, false if not.
  196.      */
  197.     public boolean isGMT() {
  198.         return isGMT;
  199.     }

  200.     /**
  201.      * Add a newsgroup to the list of newsgroups being queried, but indicate that group should not be checked for new news. Newsgroups added this way are only
  202.      * meaningful to the NEWNEWS command. Newsgroup names may include the <code>*</code> wildcard, as in <code>comp.lang.*</code> or
  203.      * <code>comp.lang.java.*</code>.
  204.      * <p>
  205.      * The following would create a query that searched for new news in all comp.lang.java newsgroups except for comp.lang.java.advocacy.
  206.      * </p>
  207.      *
  208.      * <pre>
  209.      * query.addNewsgroup("comp.lang.java.*");
  210.      * query.omitNewsgroup("comp.lang.java.advocacy");
  211.      * </pre>
  212.      *
  213.      * @param newsgroup The newsgroup to add to the list of groups to be checked for new news, but which should be omitted from the search for new news.
  214.      */
  215.     public void omitNewsgroup(final String newsgroup) {
  216.         addNewsgroup("!" + newsgroup);
  217.     }
  218. }