-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix default-domain api running on tests * Fix merge conflicts * Remove print lines * Adjust end lines in resource files
- Loading branch information
Showing
27 changed files
with
380 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.wire.bots.hold.DAO; | ||
|
||
import com.wire.bots.hold.model.Metadata; | ||
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; | ||
|
||
public interface MetadataDAO { | ||
String FALLBACK_DOMAIN_KEY = "FALLBACK_DOMAIN_KEY"; | ||
|
||
@SqlUpdate("INSERT INTO Metadata (key, value)" + | ||
"VALUES (:key, :value)" + | ||
"ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value") | ||
int insert(@Bind("key") String key, @Bind("value") String value); | ||
|
||
@SqlQuery("SELECT key, value FROM Metadata WHERE key = :key LIMIT 1") | ||
@RegisterColumnMapper(MetadataResultSetMapper.class) | ||
Metadata get(@Bind("key") String key); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/wire/bots/hold/DAO/MetadataResultSetMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.wire.bots.hold.DAO; | ||
|
||
import com.wire.bots.hold.model.Metadata; | ||
import org.jdbi.v3.core.mapper.ColumnMapper; | ||
import org.jdbi.v3.core.statement.StatementContext; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class MetadataResultSetMapper implements ColumnMapper<Metadata> { | ||
|
||
@Override | ||
public Metadata map(ResultSet rs, int columnNumber, StatementContext ctx) throws SQLException { | ||
return new Metadata( | ||
rs.getString("key"), | ||
rs.getString("value") | ||
); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/wire/bots/hold/FallbackDomainFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.wire.bots.hold; | ||
|
||
import com.wire.bots.hold.DAO.MetadataDAO; | ||
import com.wire.bots.hold.model.Metadata; | ||
import com.wire.bots.hold.utils.Cache; | ||
import com.wire.helium.LoginClient; | ||
import com.wire.helium.models.BackendConfiguration; | ||
import com.wire.xenon.exceptions.HttpException; | ||
import com.wire.xenon.tools.Logger; | ||
|
||
import javax.ws.rs.ProcessingException; | ||
|
||
public class FallbackDomainFetcher implements Runnable { | ||
|
||
private final LoginClient loginClient; | ||
private final MetadataDAO metadataDAO; | ||
|
||
/** | ||
* Fetcher and handler for fallback domain. | ||
* <p> | ||
* Fetches from API and compares against database value (if any), then inserts into database and updates cache value. | ||
* If value received from the API is different from what is saved in the database, a [RuntimeException] is thrown. | ||
* </p> | ||
* <p> | ||
* This fallback domain is necessary for LegalHold to work with Federation (as it needs id@domain) and not just the ID anymore. | ||
* In case there is a mismatch we are throwing a RuntimeException so it stops the execution of this app, so in an event | ||
* of already having a defined default domain saved in the database and this app restarts with a different domain | ||
* we don't get mismatching domains. | ||
* </p> | ||
* @param loginClient [{@link LoginClient}] as API to get backend configuration containing default domain. | ||
* @param metadataDAO [{@link MetadataDAO}] as DAO to get/insert default domain to database. | ||
* | ||
* @throws RuntimeException if received domain from API is different from the one saved in the database. | ||
*/ | ||
FallbackDomainFetcher(LoginClient loginClient, MetadataDAO metadataDAO) { | ||
this.loginClient = loginClient; | ||
this.metadataDAO = metadataDAO; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (Cache.getFallbackDomain() != null) { return; } | ||
|
||
Metadata metadata = metadataDAO.get(MetadataDAO.FALLBACK_DOMAIN_KEY); | ||
try { | ||
BackendConfiguration apiVersionResponse = loginClient.getBackendConfiguration(); | ||
|
||
if (metadata == null) { | ||
metadataDAO.insert(MetadataDAO.FALLBACK_DOMAIN_KEY, apiVersionResponse.domain); | ||
Cache.setFallbackDomain(apiVersionResponse.domain); | ||
} else { | ||
if (metadata.value.equals(apiVersionResponse.domain)) { | ||
Cache.setFallbackDomain(apiVersionResponse.domain); | ||
} else { | ||
String formattedExceptionMessage = String.format( | ||
"Database already has a default domain as %s and instead we got %s from the Backend API.", | ||
metadata.value, | ||
apiVersionResponse.domain | ||
); | ||
throw new RuntimeException(formattedExceptionMessage); | ||
} | ||
} | ||
} catch (HttpException exception) { | ||
Logger.exception(exception, "FallbackDomainFetcher.run, exception: %s", exception.getMessage()); | ||
} catch (ProcessingException pexception) { | ||
Logger.info("FallbackDomainFetcher.run, ignoring test exceptions"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.wire.bots.hold.model; | ||
|
||
public class Metadata { | ||
public String key; | ||
public String value; | ||
|
||
public Metadata(String key, String value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} |
Oops, something went wrong.