src/org/sonews/storage/Article.java
changeset 72 aae4b4688700
parent 37 74139325d305
child 101 d54786065fa3
     1.1 --- a/src/org/sonews/storage/Article.java	Sun Aug 29 18:17:37 2010 +0200
     1.2 +++ b/src/org/sonews/storage/Article.java	Wed Oct 12 00:11:25 2011 +0200
     1.3 @@ -15,7 +15,6 @@
     1.4   *   You should have received a copy of the GNU General Public License
     1.5   *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     1.6   */
     1.7 -
     1.8  package org.sonews.storage;
     1.9  
    1.10  import java.io.ByteArrayInputStream;
    1.11 @@ -27,11 +26,13 @@
    1.12  import java.util.ArrayList;
    1.13  import java.util.Enumeration;
    1.14  import java.util.List;
    1.15 +import java.util.logging.Level;
    1.16  import javax.mail.Header;
    1.17  import javax.mail.Message;
    1.18  import javax.mail.MessagingException;
    1.19  import javax.mail.internet.InternetHeaders;
    1.20  import org.sonews.config.Config;
    1.21 +import org.sonews.util.Log;
    1.22  
    1.23  /**
    1.24   * Represents a newsgroup article.
    1.25 @@ -39,20 +40,18 @@
    1.26   * @author Denis Schwerdel
    1.27   * @since n3tpd/0.1
    1.28   */
    1.29 -public class Article extends ArticleHead
    1.30 -{
    1.31 +public class Article extends ArticleHead {
    1.32  
    1.33  	/**
    1.34  	 * Loads the Article identified by the given ID from the JDBCDatabase.
    1.35  	 * @param messageID
    1.36  	 * @return null if Article is not found or if an error occurred.
    1.37  	 */
    1.38 -	public static Article getByMessageID(final String messageID)
    1.39 -	{
    1.40 +	public static Article getByMessageID(final String messageID) {
    1.41  		try {
    1.42  			return StorageManager.current().getArticle(messageID);
    1.43  		} catch (StorageBackendException ex) {
    1.44 -			ex.printStackTrace();
    1.45 +			Log.get().log(Level.WARNING, ex.getLocalizedMessage(), ex);
    1.46  			return null;
    1.47  		}
    1.48  	}
    1.49 @@ -61,26 +60,24 @@
    1.50  	/**
    1.51  	 * Default constructor.
    1.52  	 */
    1.53 -	public Article()
    1.54 -	{
    1.55 +	public Article() {
    1.56  	}
    1.57  
    1.58  	/**
    1.59  	 * Creates a new Article object using the date from the given
    1.60  	 * raw data.
    1.61  	 */
    1.62 -	public Article(String headers, byte[] body)
    1.63 -	{
    1.64 +	public Article(String headers, byte[] body) {
    1.65  		try {
    1.66  			this.body = body;
    1.67  
    1.68  			// Parse the header
    1.69  			this.headers = new InternetHeaders(
    1.70 -				new ByteArrayInputStream(headers.getBytes()));
    1.71 +					new ByteArrayInputStream(headers.getBytes()));
    1.72  
    1.73  			this.headerSrc = headers;
    1.74  		} catch (MessagingException ex) {
    1.75 -			ex.printStackTrace();
    1.76 +			Log.get().log(Level.WARNING, ex.getLocalizedMessage(), ex);
    1.77  		}
    1.78  	}
    1.79  
    1.80 @@ -93,8 +90,7 @@
    1.81  	 * @throws MessagingException
    1.82  	 */
    1.83  	public Article(final Message msg)
    1.84 -		throws IOException, MessagingException
    1.85 -	{
    1.86 +			throws IOException, MessagingException {
    1.87  		this.headers = new InternetHeaders();
    1.88  
    1.89  		for (Enumeration e = msg.getAllHeaders(); e.hasMoreElements();) {
    1.90 @@ -116,8 +112,7 @@
    1.91  	 * @throws IOException
    1.92  	 */
    1.93  	private byte[] readContent(Message in)
    1.94 -		throws IOException, MessagingException
    1.95 -	{
    1.96 +			throws IOException, MessagingException {
    1.97  		ByteArrayOutputStream out = new ByteArrayOutputStream();
    1.98  		in.writeTo(out);
    1.99  		return out.toByteArray();
   1.100 @@ -127,8 +122,7 @@
   1.101  	 * Removes the header identified by the given key.
   1.102  	 * @param headerKey
   1.103  	 */
   1.104 -	public void removeHeader(final String headerKey)
   1.105 -	{
   1.106 +	public void removeHeader(final String headerKey) {
   1.107  		this.headers.removeHeader(headerKey);
   1.108  		this.headerSrc = null;
   1.109  	}
   1.110 @@ -139,8 +133,7 @@
   1.111  	 * change persistent.
   1.112  	 * Note: a Message-ID should never be changed and only generated once.
   1.113  	 */
   1.114 -	private String generateMessageID()
   1.115 -	{
   1.116 +	private String generateMessageID() {
   1.117  		String randomString;
   1.118  		MessageDigest md5;
   1.119  		try {
   1.120 @@ -150,17 +143,17 @@
   1.121  			md5.update(getHeader(Headers.SUBJECT)[0].getBytes());
   1.122  			md5.update(getHeader(Headers.FROM)[0].getBytes());
   1.123  			byte[] result = md5.digest();
   1.124 -			StringBuffer hexString = new StringBuffer();
   1.125 +			StringBuilder hexString = new StringBuilder();
   1.126  			for (int i = 0; i < result.length; i++) {
   1.127  				hexString.append(Integer.toHexString(0xFF & result[i]));
   1.128  			}
   1.129  			randomString = hexString.toString();
   1.130 -		} catch (NoSuchAlgorithmException e) {
   1.131 -			e.printStackTrace();
   1.132 +		} catch (NoSuchAlgorithmException ex) {
   1.133 +			Log.get().log(Level.WARNING, ex.getLocalizedMessage(), ex);
   1.134  			randomString = UUID.randomUUID().toString();
   1.135  		}
   1.136  		String msgID = "<" + randomString + "@"
   1.137 -			+ Config.inst().get(Config.HOSTNAME, "localhost") + ">";
   1.138 +				+ Config.inst().get(Config.HOSTNAME, "localhost") + ">";
   1.139  
   1.140  		this.headers.setHeader(Headers.MESSAGE_ID, msgID);
   1.141  
   1.142 @@ -170,16 +163,14 @@
   1.143  	/**
   1.144  	 * Returns the body string.
   1.145  	 */
   1.146 -	public byte[] getBody()
   1.147 -	{
   1.148 +	public byte[] getBody() {
   1.149  		return body;
   1.150  	}
   1.151  
   1.152  	/**
   1.153  	 * @return Numerical IDs of the newsgroups this Article belongs to.
   1.154  	 */
   1.155 -	public List<Group> getGroups()
   1.156 -	{
   1.157 +	public List<Group> getGroups() {
   1.158  		String[] groupnames = getHeader(Headers.NEWSGROUPS)[0].split(",");
   1.159  		ArrayList<Group> groups = new ArrayList<Group>();
   1.160  
   1.161 @@ -188,20 +179,19 @@
   1.162  				newsgroup = newsgroup.trim();
   1.163  				Group group = StorageManager.current().getGroup(newsgroup);
   1.164  				if (group != null && // If the server does not provide the group, ignore it
   1.165 -					!groups.contains(group)) // Yes, there may be duplicates
   1.166 +						!groups.contains(group)) // Yes, there may be duplicates
   1.167  				{
   1.168  					groups.add(group);
   1.169  				}
   1.170  			}
   1.171  		} catch (StorageBackendException ex) {
   1.172 -			ex.printStackTrace();
   1.173 +			Log.get().log(Level.WARNING, ex.getLocalizedMessage(), ex);
   1.174  			return null;
   1.175  		}
   1.176  		return groups;
   1.177  	}
   1.178  
   1.179 -	public void setBody(byte[] body)
   1.180 -	{
   1.181 +	public void setBody(byte[] body) {
   1.182  		this.body = body;
   1.183  	}
   1.184  
   1.185 @@ -209,8 +199,7 @@
   1.186  	 *
   1.187  	 * @param groupname Name(s) of newsgroups
   1.188  	 */
   1.189 -	public void setGroup(String groupname)
   1.190 -	{
   1.191 +	public void setGroup(String groupname) {
   1.192  		this.headers.setHeader(Headers.NEWSGROUPS, groupname);
   1.193  	}
   1.194  
   1.195 @@ -219,8 +208,7 @@
   1.196  	 * is empty, a new Message-ID is created.
   1.197  	 * @return Message-ID of this Article.
   1.198  	 */
   1.199 -	public String getMessageID()
   1.200 -	{
   1.201 +	public String getMessageID() {
   1.202  		String[] msgID = getHeader(Headers.MESSAGE_ID);
   1.203  		return msgID[0].equals("") ? generateMessageID() : msgID[0];
   1.204  	}
   1.205 @@ -229,8 +217,7 @@
   1.206  	 * @return String containing the Message-ID.
   1.207  	 */
   1.208  	@Override
   1.209 -	public String toString()
   1.210 -	{
   1.211 +	public String toString() {
   1.212  		return getMessageID();
   1.213  	}
   1.214  }