2008-10-31 13:12:30 -04:00
|
|
|
package davmail.exchange;
|
|
|
|
|
|
|
|
import davmail.Settings;
|
2008-11-03 20:47:10 -05:00
|
|
|
import davmail.http.DavGatewayHttpClientFacade;
|
2008-10-31 13:12:30 -04:00
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
|
|
import org.apache.commons.httpclient.HttpMethod;
|
|
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.NetworkInterface;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create ExchangeSession instances.
|
|
|
|
*/
|
|
|
|
public class ExchangeSessionFactory {
|
|
|
|
/**
|
|
|
|
* Create authenticated Exchange session
|
|
|
|
*
|
|
|
|
* @param userName user login
|
|
|
|
* @param password user password
|
|
|
|
* @return authenticated session
|
|
|
|
* @throws java.io.IOException on error
|
|
|
|
*/
|
|
|
|
public static ExchangeSession getInstance(String userName, String password) throws IOException {
|
|
|
|
try {
|
|
|
|
ExchangeSession session = new ExchangeSession();
|
|
|
|
session.login(userName, password);
|
|
|
|
return session;
|
|
|
|
} catch (IOException e) {
|
|
|
|
if (checkNetwork()) {
|
|
|
|
throw e;
|
|
|
|
} else {
|
2008-11-24 07:35:58 -05:00
|
|
|
throw new NetworkDownException("All network interfaces down !");
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void checkConfig() throws IOException {
|
2008-11-03 20:47:10 -05:00
|
|
|
String url = Settings.getProperty("davmail.url");
|
|
|
|
HttpMethod testMethod = new GetMethod(url);
|
2008-10-31 13:12:30 -04:00
|
|
|
try {
|
|
|
|
|
|
|
|
// create an HttpClient instance
|
2008-11-03 20:47:10 -05:00
|
|
|
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance();
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
// get webmail root url (will not follow redirects)
|
|
|
|
testMethod.setFollowRedirects(false);
|
2008-11-04 05:37:36 -05:00
|
|
|
testMethod.setDoAuthentication(false);
|
2008-10-31 13:12:30 -04:00
|
|
|
int status = httpClient.executeMethod(testMethod);
|
|
|
|
testMethod.releaseConnection();
|
|
|
|
ExchangeSession.LOGGER.debug("Test configuration status: " + status);
|
|
|
|
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_UNAUTHORIZED
|
|
|
|
&& status != HttpStatus.SC_MOVED_TEMPORARILY) {
|
|
|
|
throw new IOException("Unable to connect to OWA at " + url + ", status code " +
|
|
|
|
status + ", check configuration");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (UnknownHostException exc) {
|
2008-11-03 05:56:57 -05:00
|
|
|
String message = "DavMail configuration exception: \n";
|
2008-10-31 13:12:30 -04:00
|
|
|
if (checkNetwork()) {
|
2008-11-03 05:56:57 -05:00
|
|
|
message += "Unknown host " + exc.getMessage();
|
2008-11-24 07:35:58 -05:00
|
|
|
ExchangeSession.LOGGER.error(message, exc);
|
|
|
|
throw new IOException(message);
|
2008-10-31 13:12:30 -04:00
|
|
|
} else {
|
|
|
|
message += "All network interfaces down !";
|
2008-11-24 07:35:58 -05:00
|
|
|
ExchangeSession.LOGGER.error(message, exc);
|
|
|
|
throw new NetworkDownException(message);
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
|
2008-11-24 07:35:58 -05:00
|
|
|
} catch (NetworkDownException exc) {
|
|
|
|
throw exc;
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (Exception exc) {
|
|
|
|
ExchangeSession.LOGGER.error("DavMail configuration exception: \n" + exc.getMessage(), exc);
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException("DavMail configuration exception: \n" + exc.getMessage());
|
2008-11-03 20:47:10 -05:00
|
|
|
} finally {
|
|
|
|
testMethod.releaseConnection();
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if at least one network interface is up and active (i.e. has an address)
|
2008-11-03 05:56:57 -05:00
|
|
|
*
|
2008-10-31 13:12:30 -04:00
|
|
|
* @return true if network available
|
|
|
|
*/
|
|
|
|
protected static boolean checkNetwork() {
|
|
|
|
boolean up = false;
|
|
|
|
Enumeration<NetworkInterface> enumeration;
|
|
|
|
try {
|
|
|
|
enumeration = NetworkInterface.getNetworkInterfaces();
|
|
|
|
while (!up && enumeration.hasMoreElements()) {
|
|
|
|
NetworkInterface networkInterface = enumeration.nextElement();
|
|
|
|
up = networkInterface.isUp() && !networkInterface.isLoopback()
|
|
|
|
&& networkInterface.getInetAddresses().hasMoreElements();
|
|
|
|
}
|
2008-11-03 05:56:57 -05:00
|
|
|
} catch (NoSuchMethodError error) {
|
|
|
|
ExchangeSession.LOGGER.debug("Unable to test network interfaces (not available under Java 1.5)");
|
|
|
|
up = true;
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (SocketException exc) {
|
|
|
|
ExchangeSession.LOGGER.error("DavMail configuration exception: \n Error listing network interfaces " + exc.getMessage(), exc);
|
|
|
|
}
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
}
|