From a9b65e3d80b103339153987bfe229cce5c23f21b Mon Sep 17 00:00:00 2001 From: Poorna Chandra Tejasvi Date: Sat, 21 Sep 2019 08:50:49 +0530 Subject: [PATCH] Logging in before attempting to publish --- .../java/iudx/http/HttpServerVerticle.java | 63 +++++++++++-------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/iudx-api-server/src/main/java/iudx/http/HttpServerVerticle.java b/iudx-api-server/src/main/java/iudx/http/HttpServerVerticle.java index 79f6cbe2..6c8b7bdd 100644 --- a/iudx-api-server/src/main/java/iudx/http/HttpServerVerticle.java +++ b/iudx-api-server/src/main/java/iudx/http/HttpServerVerticle.java @@ -137,24 +137,32 @@ public void start(Promise startPromise) throws Exception } - public void getChannel(String id, String apikey)throws Exception + public Channel getChannel(String id, String apikey)throws Exception { - ConnectionFactory factory = new ConnectionFactory(); - - factory.setUsername(id); - factory.setPassword(apikey); - factory.setVirtualHost("/"); - factory.setHost(Utils.getBrokerUrl(id)); - factory.setPort(5672); - factory.setAutomaticRecoveryEnabled(true); - factory.setNetworkRecoveryInterval(10000); + if ( !pool.containsKey(id + ":" + apikey) + || + !pool.get(id + ":" + apikey).isOpen() + ) + { + ConnectionFactory factory = new ConnectionFactory(); + + factory.setUsername(id); + factory.setPassword(apikey); + factory.setVirtualHost("/"); + factory.setHost(Utils.getBrokerUrl(id)); + factory.setPort(5672); + factory.setAutomaticRecoveryEnabled(true); + factory.setNetworkRecoveryInterval(10000); - Connection connection = factory.newConnection(); - Channel channel = connection.createChannel(); - - logger.debug("Rabbitmq channel created"); - - pool.put(id+":"+apikey, channel); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + logger.debug("Rabbitmq channel created"); + + pool.put(id+":"+apikey, channel); + } + + return pool.get(id+":"+apikey); } @@ -3807,19 +3815,22 @@ public void publish(HttpServerRequest req) logger.debug("Exchange="+exchange); logger.debug("Topic="+topic); - String poolId = id + ":" + apikey; + if(!pool.containsKey(id + ":" + apikey)) + { + checkLogin(id, apikey) + .setHandler(login -> { + + if(!login.succeeded()) + { + forbidden(resp); + return; + } + }); + } try { - if ( (!pool.containsKey(poolId)) - || - (!pool.get(poolId).isOpen()) - ) - { - getChannel(id, apikey); - } - - pool.get(poolId).basicPublish(exchange, topic, null, message.getBytes()); + getChannel(id, apikey).basicPublish(exchange, topic, null, message.getBytes()); } catch(Exception e) {