fix other checkstyle detected errors

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@59 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2007-05-09 23:37:24 +00:00
parent f6594bcd55
commit 2658817680
10 changed files with 280 additions and 238 deletions

View File

@ -12,8 +12,8 @@ import java.net.Socket;
* Generic connection common to pop3 and smtp implementations * Generic connection common to pop3 and smtp implementations
*/ */
public class AbstractConnection extends Thread { public class AbstractConnection extends Thread {
protected Socket client; protected Socket client;
protected BufferedReader in; protected BufferedReader in;
protected OutputStream os; protected OutputStream os;
// user name and password initialized through connection // user name and password initialized through connection
@ -26,7 +26,7 @@ public class AbstractConnection extends Thread {
// Initialize the streams and start the thread // Initialize the streams and start the thread
public AbstractConnection(Socket clientSocket) { public AbstractConnection(Socket clientSocket) {
client = clientSocket; this.client = clientSocket;
try { try {
in = new BufferedReader(new InputStreamReader(client.getInputStream())); in = new BufferedReader(new InputStreamReader(client.getInputStream()));
os = client.getOutputStream(); os = client.getOutputStream();
@ -34,7 +34,7 @@ public class AbstractConnection extends Thread {
try { try {
client.close(); client.close();
} catch (IOException e2) { } catch (IOException e2) {
DavGatewayTray.error("Exception while getting socket streams", e2); DavGatewayTray.warn("Exception closing client socket", e2);
} }
DavGatewayTray.error("Exception while getting socket streams", e); DavGatewayTray.error("Exception while getting socket streams", e);
return; return;

View File

@ -15,8 +15,12 @@ public abstract class AbstractServer extends Thread {
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
*/ */
public AbstractServer(int port) { public AbstractServer(int port, int defaultPort) {
if (port == 0) {
this.port = defaultPort;
} else {
this.port = port; this.port = port;
}
try { try {
//noinspection SocketOpenedButNotSafelyClosed //noinspection SocketOpenedButNotSafelyClosed
serverSocket = new ServerSocket(port); serverSocket = new ServerSocket(port);
@ -56,7 +60,7 @@ public abstract class AbstractServer extends Thread {
clientSocket.close(); clientSocket.close();
} }
} catch (IOException e) { } catch (IOException e) {
// ignore DavGatewayTray.warn("Exception closing client socket", e);
} }
} }
} }

View File

@ -7,11 +7,15 @@ import davmail.smtp.SmtpServer;
* DavGateway main class * DavGateway main class
*/ */
public class DavGateway { public class DavGateway {
protected static SmtpServer smtpServer; protected DavGateway() {
protected static PopServer popServer; }
private static SmtpServer smtpServer;
private static PopServer popServer;
/** /**
* Start the gateway, listen on spécified smtp and pop3 ports * Start the gateway, listen on spécified smtp and pop3 ports
*
* @param args command line parameter config file path * @param args command line parameter config file path
*/ */
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -16,18 +16,21 @@ import java.net.URL;
* Tray icon handler * Tray icon handler
*/ */
public class DavGatewayTray { public class DavGatewayTray {
protected static final Logger logger = Logger.getLogger("davmail"); protected DavGatewayTray() {
}
// lock for synchronized block protected static final Logger LOGGER = Logger.getLogger("davmail");
protected static final Object lock = new Object();
protected static TrayIcon trayIcon = null; // LOCK for synchronized block
protected static Image image = null; protected static final Object LOCK = new Object();
protected static Image image2 = null;
private static TrayIcon trayIcon = null;
private static Image image = null;
private static Image image2 = null;
public static void switchIcon() { public static void switchIcon() {
try { try {
synchronized (lock) { synchronized (LOCK) {
if (trayIcon.getImage() == image) { if (trayIcon.getImage() == image) {
trayIcon.setImage(image2); trayIcon.setImage(image2);
} else { } else {
@ -35,23 +38,23 @@ public class DavGatewayTray {
} }
} }
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
// ignore, jdk <= 1.6 LOGGER.debug("JDK not at least 1.6, tray not supported");
} }
} }
public static void resetIcon() { public static void resetIcon() {
try { try {
synchronized (lock) { synchronized (LOCK) {
trayIcon.setImage(image); trayIcon.setImage(image);
} }
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
// ignore, jdk <= 1.6 LOGGER.debug("JDK not at least 1.6, tray not supported");
} }
} }
protected static void displayMessage(String message, Priority priority) { protected static void displayMessage(String message, Priority priority) {
synchronized (lock) { synchronized (LOCK) {
if (trayIcon != null) { if (trayIcon != null) {
TrayIcon.MessageType messageType = null; TrayIcon.MessageType messageType = null;
if (priority == Priority.INFO) { if (priority == Priority.INFO) {
@ -66,7 +69,7 @@ public class DavGatewayTray {
} }
trayIcon.setToolTip("DavMail gateway \n" + message); trayIcon.setToolTip("DavMail gateway \n" + message);
} }
logger.log(priority, message); LOGGER.log(priority, message);
} }
} }
@ -109,9 +112,8 @@ public class DavGatewayTray {
// set native look and feel // set native look and feel
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} } catch (Exception e) {
catch (Exception e) { LOGGER.warn("Unable to set system look and feel");
// ignore
} }
// get the SystemTray instance // get the SystemTray instance

View File

@ -10,59 +10,83 @@ import java.io.IOException;
* Settings facade * Settings facade
*/ */
public class Settings { public class Settings {
protected static final Properties settings = new Properties(); protected Settings() {
protected static String configFilePath; }
private static final Properties SETTINGS = new Properties();
private static String configFilePath;
public static synchronized void setConfigFilePath(String value) { public static synchronized void setConfigFilePath(String value) {
configFilePath = value; configFilePath = value;
} }
public static synchronized void load() { public static synchronized void load() {
FileReader fileReader = null;
try { try {
if (configFilePath == null) { if (configFilePath == null) {
//noinspection AccessOfSystemProperties
configFilePath = System.getProperty("user.home") + "/.davmail.properties"; configFilePath = System.getProperty("user.home") + "/.davmail.properties";
} }
File configFile = new File(configFilePath); File configFile = new File(configFilePath);
if (configFile.exists()) { if (configFile.exists()) {
settings.load(new FileReader(configFile)); fileReader = new FileReader(configFile);
SETTINGS.load(fileReader);
} else { } else {
settings.put("davmail.url", "http://exchangeServer/exchange/"); SETTINGS.put("davmail.url", "http://exchangeServer/exchange/");
settings.put("davmail.popPort", "110"); SETTINGS.put("davmail.popPort", "110");
settings.put("davmail.smtpPort", "25"); SETTINGS.put("davmail.smtpPort", "25");
settings.put("davmail.keepDelay", "30"); SETTINGS.put("davmail.keepDelay", "30");
settings.put("davmail.enableProxy", "false"); SETTINGS.put("davmail.enableProxy", "false");
settings.put("davmail.proxyHost", ""); SETTINGS.put("davmail.proxyHost", "");
settings.put("davmail.proxyPort", ""); SETTINGS.put("davmail.proxyPort", "");
settings.put("davmail.proxyUser", ""); SETTINGS.put("davmail.proxyUser", "");
settings.put("davmail.proxyPassword", ""); SETTINGS.put("davmail.proxyPassword", "");
save(); save();
} }
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.error("Unable to load settings: ", e); DavGatewayTray.error("Unable to load settings: ", e);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
DavGatewayTray.debug("Error closing configuration file: ", e);
}
}
} }
} }
public static synchronized void save() { public static synchronized void save() {
FileWriter fileWriter = null;
try { try {
settings.store(new FileWriter(configFilePath), "DavMail settings"); fileWriter = new FileWriter(configFilePath);
SETTINGS.store(fileWriter, "DavMail settings");
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.error("Unable to store settings: ", e); DavGatewayTray.error("Unable to store settings: ", e);
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
DavGatewayTray.debug("Error closing configuration file: ", e);
}
}
} }
} }
public static synchronized String getProperty(String property) { public static synchronized String getProperty(String property) {
return settings.getProperty(property); return SETTINGS.getProperty(property);
} }
public static synchronized void setProperty(String property, String value) { public static synchronized void setProperty(String property, String value) {
settings.setProperty(property, value); SETTINGS.setProperty(property, value);
} }
public static synchronized int getIntProperty(String property) { public static synchronized int getIntProperty(String property) {
int value = 0; int value = 0;
try { try {
String propertyValue = settings.getProperty(property); String propertyValue = SETTINGS.getProperty(property);
if (propertyValue != null && propertyValue.length() > 0) { if (propertyValue != null && propertyValue.length() > 0) {
value = Integer.valueOf(propertyValue); value = Integer.valueOf(propertyValue);
} }

View File

@ -776,7 +776,11 @@ public class ExchangeSession {
int parsedAttachmentIndex = 0; int parsedAttachmentIndex = 0;
try { try {
parsedAttachmentIndex = Integer.parseInt(attachmentName); parsedAttachmentIndex = Integer.parseInt(attachmentName);
} catch (Exception e) {/* ignore */} } catch (Exception e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Current attachment name " + attachmentName + " is not an index", e);
}
}
if (parsedAttachmentIndex == 0) { if (parsedAttachmentIndex == 0) {
Attachment attachment = attachmentsMap.get(attachmentName); Attachment attachment = attachmentsMap.get(attachmentName);
String attachmentContentType = getAttachmentContentType(attachment.href); String attachmentContentType = getAttachmentContentType(attachment.href);
@ -819,7 +823,7 @@ public class ExchangeSession {
try { try {
reader.close(); reader.close();
} catch (IOException e) { } catch (IOException e) {
// ignore LOGGER.warn("Error closing header reader", e);
} }
} }
} }
@ -856,7 +860,7 @@ public class ExchangeSession {
try { try {
reader.close(); reader.close();
} catch (IOException e) { } catch (IOException e) {
// ignore LOGGER.warn("Error closing header reader", e);
} }
} }
} }
@ -887,9 +891,8 @@ public class ExchangeSession {
&& partHeader.contentType.startsWith("multipart") && partHeader.contentType.startsWith("multipart")
&& partHeader.boundary != null) { && partHeader.boundary != null) {
writeMimeMessage(reader, os, partHeader); writeMimeMessage(reader, os, partHeader);
} } else if (attachmentIndex <= 0) {
// body part // body part
else if (attachmentIndex <= 0) {
attachmentIndex++; attachmentIndex++;
writeBody(os, partHeader); writeBody(os, partHeader);
} else { } else {
@ -990,7 +993,7 @@ public class ExchangeSession {
try { try {
bis.close(); bis.close();
} catch (IOException e) { } catch (IOException e) {
// ignore LOGGER.warn("Error closing message input stream", e);
} }
} }
} }
@ -1086,10 +1089,10 @@ public class ExchangeSession {
} }
*/ */
wdr.moveMethod(messageUrl, destination); wdr.moveMethod(messageUrl, destination);
if (wdr.getStatusCode() == 412) { if (wdr.getStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED) {
int count = 2; int count = 2;
// name conflict, try another name // name conflict, try another name
while (wdr.getStatusCode() == 412) { while (wdr.getStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED) {
wdr.moveMethod(messageUrl, destination.substring(0, destination.lastIndexOf('.')) + "-" + count++ + ".eml"); wdr.moveMethod(messageUrl, destination.substring(0, destination.lastIndexOf('.')) + "-" + count++ + ".eml");
} }
} }
@ -1117,7 +1120,7 @@ public class ExchangeSession {
try { try {
reader.close(); reader.close();
} catch (IOException e) { } catch (IOException e) {
// ignore LOGGER.warn("Error closing header reader", e);
} }
} }
} }
@ -1133,7 +1136,7 @@ public class ExchangeSession {
public boolean isConnectionCloseForced() { public boolean isConnectionCloseForced() {
// force connection if attachment not found // force connection if attachment not found
return getStatusCode() == 404; return getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND;
} }
} }
@ -1146,7 +1149,7 @@ public class ExchangeSession {
ConnectionCloseHeadMethod method = new ConnectionCloseHeadMethod(URIUtil.encodePathQuery(decodedPath)); ConnectionCloseHeadMethod method = new ConnectionCloseHeadMethod(URIUtil.encodePathQuery(decodedPath));
wdr.retrieveSessionInstance().executeMethod(method); wdr.retrieveSessionInstance().executeMethod(method);
if (method.getStatusCode() == 404) { if (method.getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
method.releaseConnection(); method.releaseConnection();
System.err.println("Unable to retrieve attachment"); System.err.println("Unable to retrieve attachment");
} }
@ -1203,7 +1206,7 @@ public class ExchangeSession {
GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(messageUrl + "?Cmd=Open&unfiltered=1")); GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(messageUrl + "?Cmd=Open&unfiltered=1"));
wdr.retrieveSessionInstance().executeMethod(getMethod); wdr.retrieveSessionInstance().executeMethod(getMethod);
if (getMethod.getStatusCode() != 200) { if (getMethod.getStatusCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("Unable to get attachments: " + getMethod.getStatusCode() throw new IOException("Unable to get attachments: " + getMethod.getStatusCode()
+ " " + getMethod.getStatusLine()); + " " + getMethod.getStatusLine());
} }

View File

@ -9,14 +9,14 @@ import davmail.AbstractServer;
* Pop3 server * Pop3 server
*/ */
public class ImapServer extends AbstractServer { public class ImapServer extends AbstractServer {
public final static int DEFAULT_PORT = 143; public static final int DEFAULT_PORT = 143;
/** /**
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
*/ */
public ImapServer(int port) { public ImapServer(int port) {
super((port == 0) ? ImapServer.DEFAULT_PORT : port); super(port, ImapServer.DEFAULT_PORT);
} }
public void createConnectionHandler(Socket clientSocket) { public void createConnectionHandler(Socket clientSocket) {

View File

@ -17,7 +17,7 @@ public class PopServer extends AbstractServer {
* @param port pop listen port, 110 if not defined (0) * @param port pop listen port, 110 if not defined (0)
*/ */
public PopServer(int port) { public PopServer(int port) {
super((port == 0) ? PopServer.DEFAULT_PORT : port); super(port, PopServer.DEFAULT_PORT);
} }
public void createConnectionHandler(Socket clientSocket) { public void createConnectionHandler(Socket clientSocket) {

View File

@ -12,7 +12,7 @@ public class SmtpServer extends AbstractServer {
* Start the thread. * Start the thread.
*/ */
public SmtpServer(int port) { public SmtpServer(int port) {
super((port == 0) ? SmtpServer.DEFAULT_PORT : port); super(port, SmtpServer.DEFAULT_PORT);
} }
public void createConnectionHandler(Socket clientSocket) { public void createConnectionHandler(Socket clientSocket) {

View File

@ -7,19 +7,24 @@ import org.apache.commons.httpclient.util.URIUtil;
* *
*/ */
public class TestExchangeSession { public class TestExchangeSession {
protected TestExchangeSession() {
}
public static void main(String[] argv) { public static void main(String[] argv) {
Settings.setConfigFilePath(argv[0]); int currentArg = 0;
Settings.setConfigFilePath(argv[currentArg++]);
Settings.load(); Settings.load();
ExchangeSession session = new ExchangeSession(); ExchangeSession session = new ExchangeSession();
// test auth // test auth
try { try {
ExchangeSession.checkConfig(); ExchangeSession.checkConfig();
session.login(argv[1], argv[2]); session.login(argv[currentArg++], argv[currentArg++]);
ExchangeSession.Folder folder = session.selectFolder(argv[3]); ExchangeSession.Folder folder = session.selectFolder(argv[currentArg++]);
String messageName; String messageName;
messageName = URIUtil.decode(argv[4]); messageName = URIUtil.decode(argv[currentArg]);
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
ExchangeSession.Message messageTest = session.getMessage(folder.folderUrl + "/" + messageName); ExchangeSession.Message messageTest = session.getMessage(folder.folderUrl + "/" + messageName);