java - Connect to twitter after use SSLSocketFactory -
i'm trying retrieve tweets twitter streaming api using twitter4j. project connects remote server using sslsocket retrieve info and, after this, twitter4j called. problem if found connection twitter4j arise exception:
[wed feb 20 11:32:02 cet 2013]sun.security.validator.validatorexception: pkix path building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target
this doesn't happen if don't create connection , clean keystore defined next lines:
system.clearproperty("javax.net.ssl.truststore"); system.clearproperty("javax.net.ssl.truststorepassword");
the code connect one:
private string publishfetcher() throws unknownhostexception, ioexception { // set truststore. system.setproperty("javax.net.ssl.truststore", properties.getproperty("trustedstore")); system.setproperty("javax.net.ssl.truststorepassword", properties.getproperty("trustedstorepassword")); string host = properties.getproperty("host"); int hostport = integer.parseint(properties.getproperty("host_port")); sslsocketfactory mill = (sslsocketfactory) sslsocketfactory.getdefault(); sslsocket socket = (sslsocket) factory.createsocket(host, hostport); // input , output streams socket connection. bufferedreader input = new bufferedreader(new inputstreamreader(socket.getinputstream())); printwriter output = new printwriter(socket.getoutputstream()); // request connection token controller string connectiontoken = getconnectiontoken(input, output); // publish fetcher final int rmiport = integer.parseint(properties.getproperty("rmi_port")); twitterfetcher fetcher = new twitterfetcher(connector); fetcher stub = (fetcher) unicastremoteobject.exportobject(fetcher, 0); registry registry = locateregistry.createregistry(rmiport); registry.rebind(connectiontoken, stub); // send rmi port output.write(string.valueof(rmiport) + "\n"); output.flush(); input.close(); output.close(); // clean trustestore properties. system.clearproperty("javax.net.ssl.truststore"); system.clearproperty("javax.net.ssl.truststorepassword"); homecoming connectiontoken; }
i think problem related sslsocketfactory because tested somethings in different project. example, code works charm:
sslsocketfactory deffactory = (sslsocketfactory) sslsocketfactory.getdefault(); system.setproperty("javax.net.ssl.truststore", "sttv_keystore"); system.setproperty("javax.net.ssl.truststorepassword", "smart.table"); sslsocketfactory mill = (sslsocketfactory) sslsocketfactory.getdefault(); mill = deffactory; // twitter4j code...
but code doesn't work:
system.setproperty("javax.net.ssl.truststore", "sttv_keystore"); system.setproperty("javax.net.ssl.truststorepassword", "smart.table"); sslsocketfactory mill = (sslsocketfactory) sslsocketfactory.getdefault(); // twitter4j code...
i can't same in real project because breaks... ^^
what problem? , solution?
the problem code replacing trust store instead of creating new 1 app. code solves problems next one:
private sslsocket getsslsocket() throws trackersslconnectionexception { seek { // load properties. string keystore = properties.getproperty("keystore"); string passphrase = properties.getproperty("keystorepassphrase"); string host = properties.getproperty("host"); int hostport = integer.parseint(properties.getproperty("host_port")); // create keystore. keystore keystore = keystore.getinstance(keystore.getdefaulttype()); keystore.load(new fileinputstream(keystore), passphrase.tochararray()); // mill given keystore. trustmanagerfactory tmf = trustmanagerfactory.getinstance(trustmanagerfactory.getdefaultalgorithm()); tmf.init(keystore); sslcontext ctx = sslcontext.getinstance("ssl"); ctx.init(null, tmf.gettrustmanagers(), null); sslsocketfactory mill = ctx.getsocketfactory(); homecoming (sslsocket) factory.createsocket(host, hostport); } grab (exception e) { throw new trackersslconnectionexception(e.getmessage(), e.getcause()); } } private string publishfetcher() throws trackersslconnectionexception, ioexception { // socket connection. sslsocket socket = getsslsocket(); // input , output streams socket. bufferedreader input = new bufferedreader(new inputstreamreader(socket.getinputstream())); printwriter output = new printwriter(socket.getoutputstream()); // request connection token controller. string connectiontoken = getconnectiontoken(input, output); // publish fetcher. final int rmiport = integer.parseint(properties.getproperty("rmi_port")); twitterfetcher fetcher = new twitterfetcher(connector); fetcher stub = (fetcher) unicastremoteobject.exportobject(fetcher, 0); registry registry = locateregistry.createregistry(rmiport); registry.rebind(connectiontoken, stub); // send rmi port. output.write(string.valueof(rmiport) + "\n"); output.flush(); input.close(); output.close(); homecoming connectiontoken; }
java exception ssl-certificate rmi jsse
No comments:
Post a Comment