private void connectAgent() throws IOException, HttpException,
UnsupportedAuthenticationException,
AuthenticationCancelledException {
HttpResponse response = null;
HttpAuthenticator authenticator = null;
String ticketToSend = ticket;
boolean doPreemptive = ticketIsPassword;
try {
for (int i = 0; i < 3; i++) {
HttpClient client = getHttpClient();
if (doPreemptive) {
client.setCredentials(new PasswordCredentials(username,
ticket));
client.setPreferredAuthentication("Basic");
}
else {
client.setCredentials(null);
}
// #ifdef DEBUG
log.info("Registering with the server"); //$NON-NLS-1$
log.info("Server is " + (isSecure ? "https://" : "http://") + getAditoHost() //$NON-NLS-1$
+ ":" + getAditoPort()); //$NON-NLS-1$
// #endif
GetMethod post = new GetMethod("/agent"); //$NON-NLS-1$
client.setPreemtiveAuthentication(doPreemptive);
if (!doPreemptive && ticket != null) {
post.setParameter("ticket", ticket); //$NON-NLS-1$
}
post.setParameter(
"agentType", getConfiguration().getAgentType()); //$NON-NLS-1$ //$NON-NLS-2$
post.setParameter("locale", Locale.getDefault().toString()); //$NON-NLS-1$
response = client.execute(post);
if (response.getStatus() == 302) {
// Reset the client
this.client = null;
URL url = new URL(response.getHeaderField("Location")); //$NON-NLS-1$
aditoHostname = url.getHost();
if (url.getPort() > 0)
aditoPort = url.getPort();
continue;
} else if (response.getStatus() == 200) {
con.addListener(this);
httpConnection = response.getConnection(); // Preserve the
// connection
con.registerRequestHandler(MESSAGE_REQUEST, this);
con.registerRequestHandler(SHUTDOWN_REQUEST, this);
con.registerRequestHandler(OPEN_URL_REQUEST, this);
con.registerRequestHandler(UPDATE_RESOURCES_REQUEST, this);
// Start the protocol
con.startProtocol(
response.getConnection().getInputStream(), response
.getConnection().getOutputStream(), true);
// Synchronize and read back server information
Request syncRequest = new Request(SYNCHRONIZED_REQUEST);
con.sendRequest(syncRequest, true);
if (syncRequest.getRequestData() == null)
throw new IOException(
"Server failed to return version data");
ByteArrayReader reader = new ByteArrayReader(syncRequest
.getRequestData());
serverVersion = reader.readString();
/**
* Initialize the managers. Tunnels are no longer recorded
* here unless they are active. This simplifies the agent by
* making it respond to start and stop requests from the new
* persistent connection with Adito.
*/
tunnelManager = new TunnelManager(this);
applicationManager = new ApplicationManager(this);
webForwardManager = new WebForwardManager(this);
networkPlaceManager = new NetworkPlaceManager(this);
updateResources(-1);
return;
} else if (response.getStatus() == 401) {
authenticator = HttpAuthenticatorFactory
.createAuthenticator(
response.getConnection(),
response
.getHeaderFields("WWW-Authenticate"),
"WWW-Authenticate", "Authorization",
HttpAuthenticatorFactory.BASIC, post
.getURI());
if (authenticator.wantsPrompt()) {
if ( !( defaultAuthenticationPrompt != null ? defaultAuthenticationPrompt.promptForCredentials(false, authenticator) :
getGUI().promptForCredentials(false, authenticator) ) ) {
throw new AuthenticationCancelledException();
}
}