Skip to content

Commit

Permalink
Release (#38)
Browse files Browse the repository at this point in the history
* Lithium 3.0.4

* Check for entity

* Catch ProcessingException when posting IncomingMessage back to conv
  • Loading branch information
dkovacevic authored Feb 15, 2021
1 parent 9b55a8f commit 2e72147
Show file tree
Hide file tree
Showing 28 changed files with 177 additions and 260 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ RUN mvn -Dmaven.test.skip=true package
FROM wirebot/runtime

COPY --from=build /app/target/roman.jar /opt/roman/
# COPY target/roman.jar /opt/roman/roman.jar

COPY roman.yaml /etc/roman/

# create version file
ARG release_version=development
ENV RELEASE_FILE_PATH=/opt/roman/release.txt
RUN echo $release_version > /opt/roman/release.txt
# TODO - uncomment this when migration to JSON logging is finalized
#ENV APPENDER_TYPE=json-console

# ENV APPENDER_TYPE=json-console

WORKDIR /opt/roman

Expand Down
16 changes: 6 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>roman</artifactId>
<groupId>com.wire.bots</groupId>
<version>0.1.1</version>
<version>0.2.0</version>

<repositories>
<repository>
Expand All @@ -19,21 +19,22 @@
</repositories>

<properties>
<lithium.version>2.37.0</lithium.version>
<dropwizard.version>2.0.11</dropwizard.version>
<lithium.version>3.0.4</lithium.version>
<maven.test.skip>true</maven.test.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.wire.bots</groupId>
<groupId>com.wire</groupId>
<artifactId>lithium</artifactId>
<version>${lithium.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>${dropwizard.version}</version>
<version>2.0.12</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -42,11 +43,6 @@
<version>2.23.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jdbi</artifactId>
<version>2.0.0-rc9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
1 change: 1 addition & 0 deletions roman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ logging:
appenders:
- type: ${APPENDER_TYPE:-console}
loggers:
"org.reflections.Reflections": ERROR
"com.wire.bots.logger": ${LOG_LEVEL:-INFO}

swagger:
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/wire/bots/roman/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@
import com.wire.bots.roman.filters.ServiceTokenAuthenticationFilter;
import com.wire.bots.roman.model.Config;
import com.wire.bots.roman.resources.*;
import com.wire.bots.sdk.ClientRepo;
import com.wire.bots.sdk.MessageHandlerBase;
import com.wire.bots.sdk.Server;
import com.wire.bots.sdk.factories.CryptoFactory;
import com.wire.bots.sdk.factories.StorageFactory;
import io.dropwizard.bundles.redirect.PathRedirect;
import io.dropwizard.bundles.redirect.RedirectBundle;
import com.wire.lithium.ClientRepo;
import com.wire.lithium.Server;
import com.wire.xenon.MessageHandlerBase;
import com.wire.xenon.factories.CryptoFactory;
import com.wire.xenon.factories.StorageFactory;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.websockets.WebsocketBundle;
import io.jsonwebtoken.security.Keys;
import org.jdbi.v3.core.Jdbi;

import java.security.Key;

Expand Down Expand Up @@ -62,12 +61,11 @@ public void initialize(Bootstrap<Config> bootstrap) {

bootstrap.addBundle(new WebsocketBundle(WebSocket.class));
bootstrap.addCommand(new UpdateCertCommand());
bootstrap.addBundle(new RedirectBundle(new PathRedirect("/", "/swagger#/default")));
}

@Override
protected MessageHandlerBase createHandler(Config config, Environment env) {
this.messageHandler = new MessageHandler(jdbi, getClient());
this.messageHandler = new MessageHandler(getJdbi(), getClient());
return messageHandler;
}

Expand All @@ -90,6 +88,8 @@ protected void onRun(Config config, Environment env) {
ProviderClient providerClient = new ProviderClient(getClient(), config.apiHost);
Sender sender = new Sender(getRepo());

final Jdbi jdbi = getJdbi();

addResource(new ProviderResource(jdbi, providerClient));
addResource(new ServiceResource(jdbi, providerClient));
addResource(new ConversationResource(sender));
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/com/wire/bots/roman/CachedClientRepo.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
package com.wire.bots.roman;

import com.wire.bots.cryptobox.CryptoException;
import com.wire.bots.sdk.API;
import com.wire.bots.sdk.BotClient;
import com.wire.bots.sdk.ClientRepo;
import com.wire.bots.sdk.WireClient;
import com.wire.bots.sdk.crypto.Crypto;
import com.wire.bots.sdk.factories.CryptoFactory;
import com.wire.bots.sdk.factories.StorageFactory;
import com.wire.bots.sdk.models.otr.Missing;
import com.wire.bots.sdk.models.otr.Recipients;
import com.wire.bots.sdk.server.model.NewBot;
import com.wire.bots.sdk.tools.Logger;
import com.wire.lithium.API;
import com.wire.lithium.BotClient;
import com.wire.lithium.ClientRepo;
import com.wire.xenon.WireClient;
import com.wire.xenon.backend.models.NewBot;
import com.wire.xenon.crypto.Crypto;
import com.wire.xenon.factories.CryptoFactory;
import com.wire.xenon.factories.StorageFactory;
import com.wire.xenon.models.otr.Missing;
import com.wire.xenon.models.otr.Recipients;
import com.wire.xenon.tools.Logger;

import javax.ws.rs.client.Client;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class CachedClientRepo extends ClientRepo {

private ConcurrentHashMap<UUID, _BotClient> clients = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, _BotClient> clients = new ConcurrentHashMap<>();

CachedClientRepo(Client httpClient, CryptoFactory cf, StorageFactory sf) {
super(httpClient, cf, sf);
}

@Override
public WireClient getClient(UUID botId) {

return clients.computeIfAbsent(botId, x -> {
try {
NewBot state = sf.create(botId).getState();
Expand All @@ -45,7 +44,7 @@ public WireClient getClient(UUID botId) {
private static class _BotClient extends BotClient {

_BotClient(NewBot state, Crypto crypto, API api) {
super(state, crypto, api);
super(api, crypto, state);
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/wire/bots/roman/DAO/BotsDAO.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.wire.bots.roman.DAO;

import com.wire.bots.roman.DAO.mappers.BotsMapper;
import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.SqlQuery;
import org.skife.jdbi.v2.sqlobject.SqlUpdate;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import java.util.List;
import java.util.UUID;
Expand All @@ -15,12 +15,12 @@ int insert(@Bind("bot") UUID bot,
@Bind("provider") UUID provider);

@SqlQuery("SELECT provider AS uuid FROM Bots WHERE id = :bot")
@RegisterMapper(BotsMapper.class)
@RegisterColumnMapper(BotsMapper.class)
UUID getProviderId(@Bind("bot") UUID bot);


@SqlQuery("SELECT id AS uuid FROM Bots WHERE provider = :providerId")
@RegisterMapper(BotsMapper.class)
@RegisterColumnMapper(BotsMapper.class)
List<UUID> getBotIds(@Bind("providerId") UUID providerId);

@SqlUpdate("DELETE FROM Bots WHERE id = :botId")
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/wire/bots/roman/DAO/ProvidersDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.wire.bots.roman.DAO.mappers.ProviderMapper;
import com.wire.bots.roman.model.Provider;
import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.SqlQuery;
import org.skife.jdbi.v2.sqlobject.SqlUpdate;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -45,18 +45,18 @@ int updateServiceName(@Bind("id") UUID id,
@Bind("name") String name);

@SqlQuery("SELECT * FROM Providers WHERE email = :email")
@RegisterMapper(ProviderMapper.class)
@RegisterColumnMapper(ProviderMapper.class)
Provider get(@Bind("email") String email);

@SqlQuery("SELECT * FROM Providers")
@RegisterMapper(ProviderMapper.class)
@RegisterColumnMapper(ProviderMapper.class)
List<Provider> selectAll();

@SqlQuery("SELECT * FROM Providers WHERE id = :id")
@RegisterMapper(ProviderMapper.class)
@RegisterColumnMapper(ProviderMapper.class)
Provider get(@Bind("id") UUID id);

@SqlQuery("SELECT * FROM Providers WHERE service_auth = :auth")
@RegisterMapper(ProviderMapper.class)
@RegisterColumnMapper(ProviderMapper.class)
Provider getByAuth(@Bind("auth") String auth);
}
12 changes: 6 additions & 6 deletions src/main/java/com/wire/bots/roman/DAO/mappers/BotsMapper.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.wire.bots.roman.DAO.mappers;

import com.wire.bots.sdk.tools.Logger;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import com.wire.xenon.tools.Logger;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;

import javax.annotation.Nullable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class BotsMapper implements ResultSetMapper<UUID> {
public class BotsMapper implements ColumnMapper<UUID> {
@Override
@Nullable
public UUID map(int i, ResultSet rs, StatementContext statementContext) {
public UUID map(ResultSet rs, int columnNumber, StatementContext ctx) {
try {
return getUuid(rs, "uuid");
} catch (SQLException e) {
Logger.error("BotsMapper: i: %d, e: %s", i, e);
Logger.error("BotsMapper: i: %d, e: %s", columnNumber, e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.wire.bots.roman.DAO.mappers;

import com.wire.bots.roman.model.Provider;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class ProviderMapper implements ResultSetMapper<Provider> {
public class ProviderMapper implements ColumnMapper<Provider> {
@Override
public Provider map(int i, ResultSet rs, StatementContext statementContext) throws SQLException {
public Provider map(ResultSet rs, int columnNumber, StatementContext ctx) throws SQLException {
Provider ret = new Provider();
ret.id = getUuid(rs, "id");
ret.email = rs.getString("email");
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/wire/bots/roman/ImageProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wire.bots.roman;

import com.wire.bots.sdk.assets.Picture;
import com.wire.xenon.assets.Picture;

import javax.imageio.ImageIO;
import java.awt.*;
Expand Down Expand Up @@ -29,8 +29,7 @@ private static Boolean shouldScaleOriginalSize(double width, double height, doub

private static Size getScaledSize(double origWidth, double origHeight, double dimension) {
Size ret = new Size();
double op1 = dimension / origWidth < dimension / origHeight ? dimension / origWidth
: dimension / origHeight;
double op1 = Math.min(dimension / origWidth, dimension / origHeight);
double op2 = dimension / Math.sqrt(origWidth * origHeight);
double scale = Math.max(op1, op2);
double width = Math.ceil(scale * origWidth);
Expand Down
47 changes: 32 additions & 15 deletions src/main/java/com/wire/bots/roman/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import com.wire.bots.roman.model.OutgoingMessage;
import com.wire.bots.roman.model.Poll;
import com.wire.bots.roman.model.Provider;
import com.wire.bots.sdk.MessageHandlerBase;
import com.wire.bots.sdk.WireClient;
import com.wire.bots.sdk.assets.DeliveryReceipt;
import com.wire.bots.sdk.models.*;
import com.wire.bots.sdk.server.model.NewBot;
import com.wire.bots.sdk.server.model.SystemMessage;
import com.wire.bots.sdk.server.model.User;
import com.wire.bots.sdk.tools.Logger;
import org.skife.jdbi.v2.DBI;

import com.wire.xenon.MessageHandlerBase;
import com.wire.xenon.WireClient;
import com.wire.xenon.assets.DeliveryReceipt;
import com.wire.xenon.backend.models.NewBot;
import com.wire.xenon.backend.models.SystemMessage;
import com.wire.xenon.backend.models.User;
import com.wire.xenon.models.*;
import com.wire.xenon.tools.Logger;
import org.jdbi.v3.core.Jdbi;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
Expand All @@ -38,7 +39,7 @@ public class MessageHandler extends MessageHandlerBase {
private final BotsDAO botsDAO;
private Sender sender;

MessageHandler(DBI jdbi, Client jerseyClient) {
MessageHandler(Jdbi jdbi, Client jerseyClient) {
this.jerseyClient = jerseyClient;
providersDAO = jdbi.onDemand(ProvidersDAO.class);
botsDAO = jdbi.onDemand(BotsDAO.class);
Expand Down Expand Up @@ -296,15 +297,23 @@ private boolean send(OutgoingMessage message) {
.header("Authorization", "Bearer " + provider.serviceAuth)
.post(Entity.entity(message, MediaType.APPLICATION_JSON));

Logger.debug("MessageHandler.send: `%s` bot: %s, provider: %s, status: %d",
Logger.debug("MessageHandler.send: Sent: `%s` bot: %s, provider: %s, status: %d",
message.type,
message.botId,
providerId,
post.getStatus());

final IncomingMessage incomingMessage = post.readEntity(IncomingMessage.class);
if (incomingMessage != null) {
sender.send(incomingMessage, message.botId);
if (post.hasEntity()) {
final IncomingMessage incomingMessage = getIncomingMessage(post);
if (incomingMessage != null) {
Logger.debug("MessageHandler.send: `%s` bot: %s, provider: %s, posting IncomingMessage: type: %s",
message.type,
message.botId,
providerId,
incomingMessage.type
);
sender.send(incomingMessage, message.botId);
}
}

return post.getStatus() == 200;
Expand All @@ -317,6 +326,14 @@ private boolean send(OutgoingMessage message) {
}
}

private IncomingMessage getIncomingMessage(Response post) {
try {
return post.readEntity(IncomingMessage.class);
} catch (ProcessingException e) {
return null;
}
}

private void sendDeliveryReceipt(WireClient client, UUID messageId, UUID userId) {
try {
client.send(new DeliveryReceipt(messageId), userId);
Expand Down
Loading

0 comments on commit 2e72147

Please sign in to comment.