javax.net.ssl.HttpsURLConnection Problem
Hi,
I want make an HTTPS connection from JBoss:
// build url and connection
URL url =null;
try{
url =new URL(mmsURL);
}
catch (MalformedURLException murle){
logger.fatal("sendMMS() failed build url to mmsURL=" + mmsURL, murle);
thrownew OMAException("MMS::sendMMS() failed build url to mmsURL=" + mmsURL, murle, ErrorIDs.FATAL_ERROR);
}
// open connection
javax.net.ssl.HttpsURLConnection conn =null;
try{
conn = (javax.net.ssl.HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ctx.getSocketFactory());
}
catch (IOException ioe){
logger.fatal("sendMMS() open connection failed to mmsURL=" + mmsURL, ioe);
thrownew OMAException("MMS::sendMMS() open connection failed to mmsURL=" + mmsURL, ioe, ErrorIDs.FATAL_ERROR);
}
But I get an ClassCastException while url.openConnection() an
com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl
What cann I do?
Regards,
Rafal
[1798 byte] By [
rafcioa] at [2007-10-1 6:43:09]

I do this:
SSLContext sslContext = null;
KeyManagerFactory keyManagerFactory = null;
KeyStore keyStore = null;
// read certificate
try {
keyStore = KeyStore.getInstance("PKCS12");
InputStream fis = PropertiesLoader.getInputStream(CERTIFICATE_FILE, getClass());
keyStore.load(fis, password.toCharArray());
}
catch (KeyStoreException kse) {
logger.fatal("sendMMS() failed with KeyStoreException");
throw new OMAException("MMS::sendMMS() failed with KeyStoreException", kse, ErrorIDs.FATAL_ERROR);
}
catch (IOException ioe) {
logger.fatal("sendMMS() failed with IOException");
throw new OMAException("MMS::sendMMS() failed with IOException", ioe, ErrorIDs.FATAL_ERROR);
}
catch (NoSuchAlgorithmException nsae) {
logger.fatal("sendMMS() failed with NoSuchAlgorithmException");
throw new OMAException("MMS::sendMMS() failed with NoSuchAlgorithmException", nsae, ErrorIDs.FATAL_ERROR);
}
catch (CertificateException ce) {
logger.fatal("sendMMS() failed with CertificateException");
throw new OMAException("MMS::sendMMS() failed with CertificateException", ce, ErrorIDs.FATAL_ERROR);
}
try {
keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keyStore, password.toCharArray());
sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
}
catch (KeyStoreException kse) {
logger.fatal("sendMMS() failed with KeyStoreException");
throw new OMAException("MMS::sendMMS() failed with KeyStoreException", kse, ErrorIDs.FATAL_ERROR);
}
catch (NoSuchAlgorithmException nsae) {
logger.fatal("sendMMS() failed with NoSuchAlgorithmException");
throw new OMAException("MMS::sendMMS() failed with NoSuchAlgorithmException", nsae, ErrorIDs.FATAL_ERROR);
}
catch (UnrecoverableKeyException uke) {
logger.fatal("sendMMS() failed with UnrecoverableKeyException");
throw new OMAException("MMS::sendMMS() failed with UnrecoverableKeyException", uke, ErrorIDs.FATAL_ERROR);
}
catch (KeyManagementException kme) {
logger.fatal("sendMMS() failed with KeyManagementException");
throw new OMAException("MMS::sendMMS() failed with KeyManagementException", kme, ErrorIDs.FATAL_ERROR);
}
// build url and connection
URL url = null;
try {
url = new URL(mmsURL);
}
catch (MalformedURLException murle) {
logger.fatal("sendMMS() failed build url to mmsURL=" + mmsURL, murle);
throw new OMAException("MMS::sendMMS() failed build url to mmsURL=" + mmsURL, murle, ErrorIDs.FATAL_ERROR);
}
// open connection
HttpsURLConnection conn = null;
try {
Object o = url.openConnection();
if (o instanceof javax.net.ssl.HttpsURLConnection) {
conn = (javax.net.ssl.HttpsURLConnection) o;
conn.setSSLSocketFactory(sslContext.getSocketFactory());
}
else {
logger.error("sendMMS() class=" + o.getClass());
throw new OMAException("MMS::sendMMS() wrong HTTPS connection [" + o.getClass() + "]", ErrorIDs.FATAL_ERROR);
}
}
catch (IOException ioe) {
logger.fatal("sendMMS() open connection failed to mmsURL=" + mmsURL, ioe);
throw new OMAException("MMS::sendMMS() open connection failed to mmsURL=" + mmsURL, ioe, ErrorIDs.FATAL_ERROR);
}
try {
byte[] bytes = mms.getBytes(Encoding.UTF_8);
List readLines = new ArrayList();
BufferedReader reader = null;
try {
String encodedAuthorization = new String(FastBase64.encode(authorization));
if (logger.isDebugEnabled()) {
logger.debug("sendMMS() Content-Type=" + MimeType.TEXT_XML + "; charset=" + Encoding.UTF_8);
//logger.debug("sendMMS() Content-length=" + String.valueOf(bytes.length));
logger.debug("sendMMS() Authorization=" + "BASIC " + encodedAuthorization);
}
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", MimeType.TEXT_XML + "; charset=" + Encoding.UTF_8);
//conn.setRequestProperty("Content-length", String.valueOf(bytes.length));
conn.setRequestProperty("Authorization", "BASIC " + encodedAuthorization);
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.flush();
out.close();
// reading response from stream
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String responseLine = reader.readLine();
do {
readLines.add(responseLine);
responseLine = reader.readLine();
} while (responseLine != null);
}
catch (IOException ioe) {
logger.fatal("sendMMS() failed sending or reading data", ioe);
throw new OMAException("MMS::sendMMS() failed sending or reading data", ioe, ErrorIDs.FATAL_ERROR);
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException ioe) {
logger.fatal("sendMMS() can't close reader", ioe);
}
}
}
My PCKS#12 file is in the Jar file. And I get this Exception:
2005-03-01 17:59:17,371 FATAL [net.magix.mobile.magix.MMS] sendMMS() failed sending or reading data
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
But if I add this tart parameter -Djavax.net.debug=ssl I can't see any problem.
Regards,
Rafal