-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(get-domain-from-backend) Use new Helium API to fetch backend domain #WPB-11287 #27
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f59827
feat(get-domain-from-backend) Use new Helium API to fetch backend domain
spoonman01 a051559
feat(get-domain-from-backend) Use new Helium API to fetch backend domain
alexandreferris 84673c0
feat(get-domain-from-backend) Use new Helium API to fetch backend domain
alexandreferris b2137d6
feat(get-domain-from-backend) Use new Helium API to fetch backend domain
alexandreferris c7e416c
feat(get-domain-from-backend) Use new Helium API to fetch backend domain
alexandreferris dd5ac09
Merge remote-tracking branch 'origin/WPB-11287' into WPB-11287
alexandreferris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") | ||
); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
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; | ||
|
||
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 { | ||
System.out.println("EEEEEEEE -> " + apiVersionResponse.domain); | ||
if (metadata.value.equals(apiVersionResponse.domain)) { | ||
System.out.println("EEEEEEEE -> p1"); | ||
Cache.setFallbackDomain(apiVersionResponse.domain); | ||
} else { | ||
System.out.println("EEEEEEEE -> p2"); | ||
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()); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't be the default domain, it should be the domain of the message you got (well, the domain fo the sender). Someome might have send an image from another domain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the comment might be a bit misleading to what it needs to be done, but at least we have an overview of where it will need to be changed as well when we do the database migrations from UUID to QualifiedID (id, domain)