"URL is null",
ContentHandlerException.TYPE_UNKNOWN);
}
// Open a connection to the content.
Connection conn = null;
int rc = 0;
try {
while (true) {
// Loop to enable redirects.
conn = Connector.open(url);
if (conn instanceof HttpConnection) {
HttpConnection httpc = (HttpConnection)conn;
httpc.setRequestMethod(httpc.HEAD);
// Get the response code
rc = httpc.getResponseCode();
if (rc == HttpConnection.HTTP_OK) {
type = httpc.getType();
if (type != null) {
// Check for and remove any parameters (rfc2616)
int ndx = type.indexOf(';');
if (ndx >= 0) {
type = type.substring(0, ndx);
}
type = type.trim();
}
if (type == null || type.length() == 0) {
type = null;
throw new ContentHandlerException(
"unable to determine type",
ContentHandlerException.TYPE_UNKNOWN);
}
break;
} else if (rc == HttpConnection.HTTP_TEMP_REDIRECT ||
rc == HttpConnection.HTTP_MOVED_TEMP ||
rc == HttpConnection.HTTP_MOVED_PERM) {
// Get the new location and close the connection
url = httpc.getHeaderField("location");
conn.close();
conn = null;
continue; // restart with the new url
} else {
throw new IOException("http status: " + rc);
}
} else {
// Not HTTP, this isn't going to work
// TBD: Check suffixes
throw new ContentHandlerException(
"URL scheme not supported",
ContentHandlerException.TYPE_UNKNOWN);
}
}
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception ex) {
}
}
}
return type;