Skip to content

Commit

Permalink
Logging in before attempting to publish
Browse files Browse the repository at this point in the history
  • Loading branch information
pct960 committed Sep 21, 2019
1 parent 718e1cd commit a9b65e3
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions iudx-api-server/src/main/java/iudx/http/HttpServerVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,32 @@ public void start(Promise<Void> 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);
}


Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit a9b65e3

Please sign in to comment.