javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: - java

I'm getting the following error while trying to run on devices below android lollipop and it is working really well for version above lollipop.
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x943e670: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:658 0xb750d3a1:0x00000000)
this is my method to register the user:
String tag_string_req = "register";
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String id = jObj.getString("id");
finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_name", txtSEditName.getText().toString().trim());
params.put("password", txtSEditPhone.getText().toString().trim());
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
My volley singleton looks like this:
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
and my app config file looks like this:
public static String URL_REGISTER = "http://192.168.56.1/MyApp/register.php";
Can anyone help me solve this issue?
Error: javax.net.ssl.SSLHandshakeException:
javax.net.ssl.SSLProtocolException: SSL handshake aborted:

I have tried to sort out the error and this has worked for me. What I have understood was that for devices below that of lollipop the protocols TLSv1.1 and TLSv1.2 are not enabled by default. Inorder to enable them for devices such using jellybean or kitkat we will have to use a SSLSocketFactory.
So now I have made the following change to the getRequestQueue() method of Volley singleton:
public RequestQueue getRequestQueue() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
Log.d("msg", "HI, I m a kitkat phone");
try {
ProviderInstaller.installIfNeeded(getApplicationContext());
} catch (GooglePlayServicesRepairableException e) {
// Indicates that Google Play services is out of date, disabled, etc.
// Prompt the user to install/update/enable Google Play services.
GooglePlayServicesUtil.showErrorNotification(e.getConnectionStatusCode(), getApplicationContext());
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
// Indicates a non-recoverable error; the ProviderInstaller is not able
// to install an up-to-date Provider.
e.printStackTrace();
}
HttpStack stack = null;
try {
stack = new HurlStack(null, new TLSSocketFactory());
} catch (KeyManagementException e) {
e.printStackTrace();
Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
stack = new HurlStack();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
stack = new HurlStack();
}
mRequestQueue = Volley.newRequestQueue(getApplicationContext(), stack);
} else {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
And create a class named TLSSocketFactory.java and add the following code:
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory internalSSLSocketFactory;
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
internalSSLSocketFactory = context.getSocketFactory();
}
#Override
public String[] getDefaultCipherSuites() {
return internalSSLSocketFactory.getDefaultCipherSuites();
}
#Override
public String[] getSupportedCipherSuites() {
return internalSSLSocketFactory.getSupportedCipherSuites();
}
#Override
public Socket createSocket() throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket());
}
#Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
}
#Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
#Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}
#Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
#Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
}
return socket;
}
}
One more step is to add the following implementation in your dependencies of gradle file:
implementation 'com.google.android.gms:play-services-base:11.0.0'
I hope this might help you to resolve this issue.

may be I can help somebody.
in my case the problem was from the webserver. I tried to change the webserver and used Github as a test and every thing went fine.
so, my advise is to check and confirm first the website support required SSLxxxx.

Related

Apache kafka connect - custom websocket connector

The goal is :develop a custom Kafka connector that read ,messages from the websocket in a loop method. I try to give you an example on what I've realized:
I create an interface IWebsocketClientEndpoint
public interface IWebsocketClientEndpoint {
IWebsocketClientEndpoint Connect() ;
void Disconnect() throws IOException;
IWebsocketClientEndpoint addMessageHandler(IMessageHandler msgHandler);
void SendMessage(String message) throws Exception;
void SendMessage(ByteBuffer message) throws Exception;
void SendMessage(Object message) throws Exception;
boolean isOpen();
void Dispose()throws IOException;
}
and a class that implement above interface:
#ClientEndpoint
public class WebsocketClientEndpoint implements IWebsocketClientEndpoint {
private WebSocketContainer _container;
private Session _userSession = null;
private IMessageHandler _messageHandler;
private URI _endpointURI;
private WebsocketClientEndpoint(URI endpointURI) {
try {
_endpointURI = endpointURI;
_container = ContainerProvider.getWebSocketContainer();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private WebsocketClientEndpoint(URI endpointURI, int bufferSize) {
try {
_endpointURI = endpointURI;
_container = ContainerProvider.getWebSocketContainer();
_container.setDefaultMaxBinaryMessageBufferSize(bufferSize);
_container.setDefaultMaxTextMessageBufferSize(bufferSize);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static IWebsocketClientEndpoint Create(URI endpointURI){
return new WebsocketClientEndpoint(endpointURI);
}
public static IWebsocketClientEndpoint Create(URI endpointURI,int bufferSize){
return new WebsocketClientEndpoint(endpointURI,bufferSize);
}
public IWebsocketClientEndpoint Connect() {
try {
_container.connectToServer(this, _endpointURI);
return this;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
#OnOpen
public void onOpen(Session userSession) {
this._userSession = userSession;
if (this._messageHandler != null) {
this._messageHandler.handleOpen("Web socket "+ _endpointURI +" opened");}
}
#OnClose
public void onClose(Session userSession, CloseReason reason) {
this._userSession = null;
if (this._messageHandler != null) {
this._messageHandler.handleClose("Web socket "+ _endpointURI +" closed. Reason: " + reason.getReasonPhrase());}
}
public void Disconnect() throws IOException {
CloseReason reason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE,"Web socket closed by user");
this._userSession.close(reason);
this._userSession = null;
//close notification to caller
if (this._messageHandler != null) {
this._messageHandler.handleClose("Web socket "+ _endpointURI +" closed. Reason: " + reason.getReasonPhrase());}
}
#Override
public IWebsocketClientEndpoint addMessageHandler(IMessageHandler msgHandler) {
this._messageHandler = msgHandler;
return this;
}
#OnMessage
public void onMessage(String message) {
if (this._messageHandler != null) {
this._messageHandler.handleMessage(message);
}
}
#OnMessage
public void onMessage(ByteBuffer bytes) {
if (this._messageHandler != null) {
this._messageHandler.handleMessage(bytes);
}
}
public void SendMessage(String message) throws Exception {
try{
this._userSession.getAsyncRemote().sendText(message);
}catch (Exception ex){
throw ex;
}
}
public void SendMessage(ByteBuffer message) throws Exception {
try{
this._userSession.getAsyncRemote().sendBinary(message);
}catch (Exception ex){
throw ex;
}
}
public void SendMessage(Object message) throws Exception {
this._userSession.getAsyncRemote().sendObject(message);
}catch (Exception ex){
throw ex;
}
}
#Override
public boolean isOpen() {
if (this._userSession != null){
return this._userSession.isOpen();
}
return false;
}
}
The class WebsocketClientEndpoint is dedicated to the creation of websocket and manage of connection, disconnection, send and receive message.
The goal is: how can I adapt the my websocket structure in the kafka connect structure? I could queue the message received ("public void handleMessage(String s)) from the socket in a ConcurrentLinkedQueue, and then, in the kafka connect loop method, unqueue them. But is it the best solution?
Below, the implementation of my Kafka custom connector
My kafka Connector
public class MySourceTask extends SourceTask {
IWebsocketClientEndpoint _clientEndPoint;
#Override
public void start(Map<String, String> props) {
_clientEndPoint = WebsocketClientEndpoint
.Create(new URI(socket))
.Connect();
_clientEndPoint.addMessageHandler(new IMessageHandler() {
#Override
public void handleMessage(String s) {
}
#Override
public void handleMessage(ByteBuffer byteBuffer) {
}
#Override
public void handleClose(String s) {
}
#Override
public void handleOpen(String s) {
}
});
}
#Override
public List<SourceRecord> poll() throws InterruptedException {
return null;
}
#Override
public void stop() {
_clientEndPoint.Dispose();
}
}
Thanks in advance to anyone
I'd suggest adding the interface to the class
extends SourceTask implements IMessageHandler
Then
_clientEndPoint.addMessageHandler(this);
And when you implement handleMessage, add those strings to some queue. Inside the poll method, you would pop data off that queue to create SourceRecord objects to return.
Inside of stop, call this.handleClose and clean up other resources.

Reconnect OkHttp websocket when internet disconnects

I have the following class. I'm trying to have the WebSocket reconnect in case of failure
public class WebSocketClient extends WebSocketListener {
volatile OkHttpClient client;
volatile WebSocket webSocket;
volatile Boolean isConnected = false;
public WebSocketClient() {
Proxy proxy = null;
if (Main.useProxy) {
tinder.CustomProxy proxyCustom = ProxyManager.GetStaticProxy(ThreadLocalManager.account.get().getProxyId());
proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyCustom.getProxyIp(), proxyCustom.getProxyPort()));
}
client = new OkHttpClient.Builder().proxy(proxy).readTimeout(2, TimeUnit.SECONDS)
.connectTimeout(2, TimeUnit.SECONDS).build();
Request request = new Request.Builder().url("wss://echo.com/ws")
.addHeader("Accept-Language", "en").build();
webSocket = client.newWebSocket(request, this);
}
#Override
public void onOpen(WebSocket webSocket, Response response) {
AnsiConsole.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Socket connection successful").reset());
isConnected = true;
}
#Override
public void onMessage(WebSocket webSocket, String text) {
System.out.println("Text MESSAGE: " + text);
}
#Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
}
#Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(1000, null);
System.out.println("CLOSE: " + code + " " + reason);
isConnected = false;
}
#Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
isConnected = false;
AnsiConsole.out
.println(Ansi.ansi().fg(Ansi.Color.RED).a("Socket connection failed! will try to reconnect").reset());
while (!isConnected) {
try {
AnsiConsole.out
.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a("Waiting to try socket connection!").reset());
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Request request = new Request.Builder().url("wss://echo.com/ws")
.addHeader("Accept-Language", "en").build();
webSocket = client.newWebSocket(request, this);
}
if (isConnected) {
AnsiConsole.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Socket connection successful").reset());
}
}
public void close() {
if (webSocket != null) {
webSocket.close(1000, "Connection closed");
}
client.dispatcher().executorService().shutdown();
}
}
The problem is if it takes a few attempts to reconnect, then the onFailure method will get called multiple times. Causing multiple web socket connections instead of one.
How can I have the single connection reconnect when the websocket disconnects?
public class WebSocketClient extends WebSocketListener {
volatile OkHttpClient client;
volatile WebSocket webSocket;
volatile Boolean isConnected = false;
public WebSocketClient() {
Proxy proxy = null;
if (Main.useProxy) {
tinder.CustomProxy proxyCustom = ProxyManager.GetStaticProxy(ThreadLocalManager.account.get().getProxyId());
proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyCustom.getProxyIp(), proxyCustom.getProxyPort()));
}
client = new OkHttpClient.Builder().proxy(proxy).readTimeout(2, TimeUnit.SECONDS)
.connectTimeout(2, TimeUnit.SECONDS).build();
Request request = new Request.Builder().url("wss://echo.com/ws")
.addHeader("Accept-Language", "en").build();
webSocket = client.newWebSocket(request, this);
// First Change
client.connectionPool.evictAll();
}
#Override
public void onOpen(WebSocket webSocket, Response response) {
AnsiConsole.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Socket connection successful").reset());
isConnected = true;
}
#Override
public void onMessage(WebSocket webSocket, String text) {
System.out.println("Text MESSAGE: " + text);
}
#Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
}
#Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(1000, null);
System.out.println("CLOSE: " + code + " " + reason);
isConnected = false;
}
#Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
// Second Change
webSocket.close(1000, null);
close();
Thread.sleep(10000);
Request request = new Request.Builder().url("wss://echo.com/ws")
.addHeader("Accept-Language", "en").build();
webSocket = client.newWebSocket(request, this);
}
public void close() {
if (webSocket != null) {
webSocket.close(1000, "Connection closed");
}
}
}
For multiple idle connection client provide a connectionPool
client.connectionPool().evictAll();
the evictAll() method evicts all the connections.

Netty- ChannelRead reports that Object msg is of SimpleLeakAwareByteBuf Type

I am making a Curl post curl -X POST -d "dsds" 10.0.0.211:5201 to my Netty socket server but in my ChannelRead when I try to cast Object msg into FullHttpRequest It throws following exception.
java.lang.ClassCastException: io.netty.buffer.SimpleLeakAwareByteBuf cannot be cast to io.netty.handler.codec.http.FullHttpRequest
at edu.clemson.openflow.sos.host.netty.HostPacketHandler.channelRead(HostPacketHandler.java:42)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:334)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:326)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1320)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:334)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:905)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:123)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:563)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:504)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:418)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:390)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145)
at java.lang.Thread.run(Thread.java:748)
Following is my Socket Handler class
#ChannelHandler.Sharable
public class HostPacketHandler extends ChannelInboundHandlerAdapter {
private static final Logger log = LoggerFactory.getLogger(HostPacketHandler.class);
private RequestParser request;
public HostPacketHandler(RequestParser request) {
this.request = request;
log.info("Expecting Host at IP {} Port {}",
request.getClientIP(), request.getClientPort());
}
public void setRequestObject(RequestParser requestObject) {
this.request = requestObject;
}
#Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// Discard the received data silently.
InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
log.info("Got Message from {} at Port {}",
socketAddress.getHostName(),
socketAddress.getPort());
//FullHttpRequest request = (FullHttpRequest) msg;
log.info(msg.getClass().getSimpleName());
//((ByteBuf) msg).release();
}
#Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
Pipeline:
public class NettyHostSocketServer implements IClientSocketServer {
protected static boolean isClientHandlerRunning = false;
private static final Logger log = LoggerFactory.getLogger(SocketManager.class);
private static final int CLIENT_DATA_PORT = 9877;
private static final int MAX_CLIENTS = 5;
private HostPacketHandler hostPacketHandler;
public NettyHostSocketServer(RequestParser request) {
hostPacketHandler = new HostPacketHandler(request);
}
private boolean startSocket(int port) {
NioEventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
#Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(
hostPacketHandler);
}
});
ChannelFuture f = b.bind().sync();
log.info("Started host-side socket server at Port {}",CLIENT_DATA_PORT);
return true;
// Need to do socket closing handling. close all the remaining open sockets
//System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());
//f.channel().closeFuture().sync();
} catch (InterruptedException e) {
log.error("Error starting host-side socket");
e.printStackTrace();
return false;
} finally {
//group.shutdownGracefully().sync();
}
}
#Override
public boolean start() {
if (!isClientHandlerRunning) {
isClientHandlerRunning = true;
return startSocket(CLIENT_DATA_PORT);
}
return true;
}
#Override
public int getActiveConnections() {
return 0;
}
}
I also used wireshark to check If I am getting valid packets or not. Below is the screenshot of Wireshark dump.
Your problem is that you never decode the ByteBuf into an actual HttpRequest object which is why you get an error. You can't cast a ByteBuf to a FullHttpRequest object.
You should do something like this:
#Override
public void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(new HttpRequestDecoder()) // Decodes the ByteBuf into a HttpMessage and HttpContent (1)
.addLast(new HttpObjectAggregator(1048576)) // Aggregates the HttpMessage with its following HttpContent into a FullHttpRequest
.addLast(hostPacketHandler);
}
(1) If you also want to send HttpResponse use this handler HttpServerCodec which adds the HttpRequestDecoder and HttpResponseEncoder.

Unable to connect websocket with wss in android

I am trying to connect with secure websocket connection wss:// in android using org.java_websocket.client.WebSocketClient API, but unable to connect with https. However it is working fine with ws://.. Here is my code.
private void connect(String websocketEndPointUrl) throws Exception {
URI uri;
try {
websocketEndPointUrl="wss://echo.websocket.org:443";
Log.i(TAG, " WSURL: " + websocketEndPointUrl);
uri = new URI(websocketEndPointUrl);
} catch (URISyntaxException e) {
Log.e(TAG, e.getMessage());
return;
}
mWebSocketClient = new WebSocketClient(uri,new Draft_17()) {
#Override
public void onOpen(ServerHandshake serverHandshake) {
Log.i("Websocket", "Opened");
}
#Override
public void onMessage(String s) {
//final String message =s;
}
#Override
public void onClose(int i, String s, boolean b) {
Log.i("Websocket", "Closed " + s);
}
#Override
public void onError(Exception e) {
Log.i("Websocket", "Error " + e.getMessage());
}
};
mWebSocketClient.connect();
}
i am using online test websocket url:
ws://echo.websocket.org (port 80) // working with that
wss://echo.websocket.org (port 443)
As per my observation there is no need of certificate required in my code. Can anyone suggest me what is a reason and how i can fix this.
Find a solution. I don't know why this is not a part of the documentation. You just need to set setWebSocketFactory after WebSocketClient initialization and before the .connect() method
mWebSocketClient = new WebSocketClient(uri,new Draft_17())
{
#Override
public void onOpen(ServerHandshake serverHandshake) {
Log.i("Websocket", "Opened");
}
#Override
public void onMessage(String s) {
//final String message =s;
}
#Override
public void onClose(int i, String s, boolean b) {
Log.i("Websocket", "Closed " + s);
}
#Override
public void onError(Exception e) {
Log.i("Websocket", "Error " + e.getMessage());
}
};
if (websocketEndPointUrl.indexOf("wss") == 0)
{
try {
SSLContext sslContext = SSLContext.getDefault();
mWebSocketClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
mWebSocketClient.connect();
In my case, I need two thing to fix "Trust anchor for certification path not found" error when websocket connect():
HttpsURLConnection requests that particular wss host (but in https:// form) successfully at least once.
Then dosetWebSocketFactory() as mentioned in accepted answer. Which this extra method (plus new Draft_17()) only appeared in library version org.java-websocket:Java-WebSocket:1.3.0, not 1.4.0.
Note that don't test with allowAllSSL() like this answer do, which affect the two thing above not working.

Not able receiving all messages, only every second, from Google Cloud Messaging with CCS using Smack

I'm trying to set up a connection to Google Cloud Messaging via Cloud Connection Server (XMPP-Connection) using Smack 4.1.2.
I was already able to establish connection and receive incoming messages. But my problem is the StanzaListener isn't triggered by each message (but only every second one). The Smack Debugging Console shows all "Raw Received Packets", so the sending from Device-to-Cloud works for each message.
Thank you for your help!
Here's my code from the Server App:
My Class GCMServer with Main:
public class GCMServer {
public static final Logger logger = Logger.getLogger(GCMServer.class.getName());
public static SSLContext sslCtx;
public static XMPPTCPConnection connection;
private static final String GCM_SERVER = "gcm.googleapis.com";
private static final int GCM_PORT = 5235;
private static final String GCM_ELEMENT_NAME = "gcm";
private static final String GCM_NAMESPACE = "google:mobile:data";
private static final String YOUR_PROJECT_ID = "xxxxxxxxxxxx";
private static final String YOUR_API_KEY = "xxxx";
public static void main(String[] args) {
ConnectionListener cl;
try {
KeyStore windowsRootTruststore = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI");
windowsRootTruststore.load(null, null);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(windowsRootTruststore);
sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(null, tmf.getTrustManagers(), null);
} catch (KeyStoreException | NoSuchProviderException | IOException | NoSuchAlgorithmException | CertificateException | KeyManagementException ex) {
Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
}
XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
.setSecurityMode(SecurityMode.ifpossible)
.setUsernameAndPassword(YOUR_PROJECT_ID, YOUR_API_KEY)
.setHost(GCM_SERVER)
.setServiceName(GCM_SERVER)
.setPort(5235)
.setDebuggerEnabled(true)
.setCompressionEnabled(false)
.setSocketFactory(sslCtx.getSocketFactory())
.build();
cl = new ConnectionListener() {
#Override
public void connected(XMPPConnection xmppc) {
Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "connected");
System.out.println("Conncetion is secure: "+connection.isSecureConnection());
}
#Override
public void authenticated(XMPPConnection xmppc, boolean bln) {
Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "authenticated");
}
#Override
public void connectionClosed() {
Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "connection closed");
}
#Override
public void connectionClosedOnError(Exception excptn) {
Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "conncetion closed on error");
}
#Override
public void reconnectionSuccessful() {
Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "reconnection successful");
}
#Override
public void reconnectingIn(int i) {
Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "reconnecting..");
}
#Override
public void reconnectionFailed(Exception excptn) {
Logger.getLogger(GCMServer.class.getName()).log(Level.INFO, "reconnection failed");
}
};
connection = new XMPPTCPConnection(conf);
//disable Roster; it seems it's not supported by GCM
Roster roster = Roster.getInstanceFor(connection);
roster.setRosterLoadedAtLogin(false);
try {
connection.connect();
connection.addAsyncStanzaListener(new MyStanzaListener(),new StanzaTypeFilter(Message.class));
connection.addConnectionListener(cl);
connection.login(YOUR_PROJECT_ID + "#gcm.googleapis.com", YOUR_API_KEY);
} catch (SmackException ex) {
Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (XMPPException ex) {
Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
Class MyStanzaListener:
private static class MyStanzaListener implements StanzaListener{
#Override
public void processPacket(Stanza stanza) {
System.out.println("hei ho, new message: " + stanza.toXML());
Message incomingMessage = (Message) stanza;
GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(GCM_NAMESPACE);
String json = gcmPacket.getJson();
try {
Map<String, Object> jsonObject = (Map<String, Object>) JSONValue.parseWithException(json);
Object messageType = jsonObject.get("message_type");
String from = (String)jsonObject.get("from");
String messageId = (String)jsonObject.get("message_id");
String category = (String)jsonObject.get("category");
if(messageType == null) {
String ack = createJsonAck(from, messageId);
System.out.println(ack);
send(ack);
handleMessage(jsonObject);
}
else{
Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, "ERROR IN HANDLING MESSAGE");
}
} catch (ParseException ex) {
Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Methods HandleMessage, CreateJSONAck, Send:
public static void handleMessage(Map<String, Object> jsonObject) {
DBConnect db = new DBConnect();
Map<String, Object> messageData = (Map<String, Object>) jsonObject.get("data");
String phoneNumber = (String)messageData.get("phoneNumber");
String text = (String)messageData.get("message");
db.inBoundMessage(phoneNumber, text);
}
public static String createJsonAck(String to, String messageId) {
Map<String, Object> message = new LinkedHashMap<String, Object>();
message.put("to", to);
message.put("message_id", messageId);
message.put("message_type", "ack");
return JSONValue.toJSONString(message);
}
/**
* Sends a downstream GCM message.
*/
public static void send(String jsonRequest) {
Stanza request = (Stanza)new GcmPacketExtension(jsonRequest).toPacket();
try {
connection.sendStanza(request);
} catch (NotConnectedException ex) {
Logger.getLogger(GCMServer.class.getName()).log(Level.SEVERE, "ERROR WHILE SENDING: ", ex);
}
}
This is a bug (SMACK-695) in Smack 4.1.3, which is fixed/will be fixed in 4.1.4. For more information see https://community.igniterealtime.org/thread/56610.

Categories