Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
briar
briar
Commits
da3b2c15
Commit
da3b2c15
authored
Oct 30, 2018
by
akwizgran
Browse files
Merge branch '41-alias-backend' into 'master'
Add backend support for contact aliases See merge request
!963
parents
db11dad6
ca700d8d
Pipeline
#2651
passed with stage
in 10 minutes and 4 seconds
Changes
26
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
bramble-api/src/main/java/org/briarproject/bramble/api/contact/Contact.java
View file @
da3b2c15
...
...
@@ -4,8 +4,12 @@ import org.briarproject.bramble.api.identity.Author;
import
org.briarproject.bramble.api.identity.AuthorId
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
javax.annotation.Nullable
;
import
javax.annotation.concurrent.Immutable
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
toUtf8
;
@Immutable
@NotNullByDefault
public
class
Contact
{
...
...
@@ -13,13 +17,21 @@ public class Contact {
private
final
ContactId
id
;
private
final
Author
author
;
private
final
AuthorId
localAuthorId
;
@Nullable
private
final
String
alias
;
private
final
boolean
verified
,
active
;
public
Contact
(
ContactId
id
,
Author
author
,
AuthorId
localAuthorId
,
boolean
verified
,
boolean
active
)
{
@Nullable
String
alias
,
boolean
verified
,
boolean
active
)
{
if
(
alias
!=
null
)
{
int
aliasLength
=
toUtf8
(
alias
).
length
;
if
(
aliasLength
==
0
||
aliasLength
>
MAX_AUTHOR_NAME_LENGTH
)
throw
new
IllegalArgumentException
();
}
this
.
id
=
id
;
this
.
author
=
author
;
this
.
localAuthorId
=
localAuthorId
;
this
.
alias
=
alias
;
this
.
verified
=
verified
;
this
.
active
=
active
;
}
...
...
@@ -36,6 +48,11 @@ public class Contact {
return
localAuthorId
;
}
@Nullable
public
String
getAlias
()
{
return
alias
;
}
public
boolean
isVerified
()
{
return
verified
;
}
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java
View file @
da3b2c15
...
...
@@ -10,6 +10,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import
java.util.Collection
;
import
javax.annotation.Nullable
;
@NotNullByDefault
public
interface
ContactManager
{
...
...
@@ -93,6 +95,12 @@ public interface ContactManager {
void
setContactActive
(
Transaction
txn
,
ContactId
c
,
boolean
active
)
throws
DbException
;
/**
* Sets an alias name for the contact or unsets it if alias is null.
*/
void
setContactAlias
(
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
;
/**
* Return true if a contact with this name and public key already exists
*/
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java
View file @
da3b2c15
...
...
@@ -515,6 +515,12 @@ public interface DatabaseComponent {
void
setContactActive
(
Transaction
txn
,
ContactId
c
,
boolean
active
)
throws
DbException
;
/**
* Sets an alias name for the contact or unsets it if alias is null.
*/
void
setContactAlias
(
Transaction
txn
,
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
;
/**
* Sets the given group's visibility to the given contact.
*/
...
...
bramble-core/src/main/java/org/briarproject/bramble/contact/ContactManagerImpl.java
View file @
da3b2c15
...
...
@@ -18,9 +18,13 @@ import java.util.Collection;
import
java.util.List
;
import
java.util.concurrent.CopyOnWriteArrayList
;
import
javax.annotation.Nullable
;
import
javax.annotation.concurrent.ThreadSafe
;
import
javax.inject.Inject
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
toUtf8
;
@ThreadSafe
@NotNullByDefault
class
ContactManagerImpl
implements
ContactManager
{
...
...
@@ -148,6 +152,17 @@ class ContactManagerImpl implements ContactManager {
db
.
setContactActive
(
txn
,
c
,
active
);
}
@Override
public
void
setContactAlias
(
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
{
if
(
alias
!=
null
)
{
int
aliasLength
=
toUtf8
(
alias
).
length
;
if
(
aliasLength
==
0
||
aliasLength
>
MAX_AUTHOR_NAME_LENGTH
)
throw
new
IllegalArgumentException
();
}
db
.
transaction
(
false
,
txn
->
db
.
setContactAlias
(
txn
,
c
,
alias
));
}
@Override
public
boolean
contactExists
(
Transaction
txn
,
AuthorId
remoteAuthorId
,
AuthorId
localAuthorId
)
throws
DbException
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/Database.java
View file @
da3b2c15
...
...
@@ -616,6 +616,12 @@ interface Database<T> {
void
setContactActive
(
T
txn
,
ContactId
c
,
boolean
active
)
throws
DbException
;
/**
* Sets an alias name for a contact.
*/
void
setContactAlias
(
T
txn
,
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
;
/**
* Sets the given group's visibility to the given contact to either
* {@link Visibility VISIBLE} or {@link Visibility SHARED}.
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseComponentImpl.java
View file @
da3b2c15
...
...
@@ -859,6 +859,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
transaction
.
attach
(
new
ContactStatusChangedEvent
(
c
,
active
));
}
@Override
public
void
setContactAlias
(
Transaction
transaction
,
ContactId
c
,
String
alias
)
throws
DbException
{
if
(
transaction
.
isReadOnly
())
throw
new
IllegalArgumentException
();
T
txn
=
unbox
(
transaction
);
if
(!
db
.
containsContact
(
txn
,
c
))
throw
new
NoSuchContactException
();
db
.
setContactAlias
(
txn
,
c
,
alias
);
}
@Override
public
void
setGroupVisibility
(
Transaction
transaction
,
ContactId
c
,
GroupId
g
,
Visibility
v
)
throws
DbException
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseTypes.java
0 → 100644
View file @
da3b2c15
package
org.briarproject.bramble.db
;
class
DatabaseTypes
{
private
final
String
hashType
,
secretType
,
binaryType
;
private
final
String
counterType
,
stringType
;
public
DatabaseTypes
(
String
hashType
,
String
secretType
,
String
binaryType
,
String
counterType
,
String
stringType
)
{
this
.
hashType
=
hashType
;
this
.
secretType
=
secretType
;
this
.
binaryType
=
binaryType
;
this
.
counterType
=
counterType
;
this
.
stringType
=
stringType
;
}
/**
* Replaces database type placeholders in a statement with the actual types.
* These placeholders are currently supported:
* <li> _HASH
* <li> _SECRET
* <li> _BINARY
* <li> _COUNTER
* <li> _STRING
*/
String
replaceTypes
(
String
s
)
{
s
=
s
.
replaceAll
(
"_HASH"
,
hashType
);
s
=
s
.
replaceAll
(
"_SECRET"
,
secretType
);
s
=
s
.
replaceAll
(
"_BINARY"
,
binaryType
);
s
=
s
.
replaceAll
(
"_COUNTER"
,
counterType
);
s
=
s
.
replaceAll
(
"_STRING"
,
stringType
);
return
s
;
}
}
bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
View file @
da3b2c15
...
...
@@ -30,6 +30,8 @@ class H2Database extends JdbcDatabase {
private
static
final
String
BINARY_TYPE
=
"BINARY"
;
private
static
final
String
COUNTER_TYPE
=
"INT NOT NULL AUTO_INCREMENT"
;
private
static
final
String
STRING_TYPE
=
"VARCHAR"
;
private
static
final
DatabaseTypes
dbTypes
=
new
DatabaseTypes
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
);
private
final
DatabaseConfig
config
;
private
final
String
url
;
...
...
@@ -40,8 +42,7 @@ class H2Database extends JdbcDatabase {
@Inject
H2Database
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
messageFactory
,
clock
);
super
(
dbTypes
,
messageFactory
,
clock
);
this
.
config
=
config
;
File
dir
=
config
.
getDatabaseDirectory
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
View file @
da3b2c15
...
...
@@ -30,6 +30,8 @@ class HyperSqlDatabase extends JdbcDatabase {
private
static
final
String
COUNTER_TYPE
=
"INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1)"
;
private
static
final
String
STRING_TYPE
=
"VARCHAR"
;
private
static
final
DatabaseTypes
dbTypes
=
new
DatabaseTypes
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
);
private
final
DatabaseConfig
config
;
private
final
String
url
;
...
...
@@ -40,8 +42,7 @@ class HyperSqlDatabase extends JdbcDatabase {
@Inject
HyperSqlDatabase
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
messageFactory
,
clock
);
super
(
dbTypes
,
messageFactory
,
clock
);
this
.
config
=
config
;
File
dir
=
config
.
getDatabaseDirectory
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
...
...
@@ -78,7 +79,7 @@ class HyperSqlDatabase extends JdbcDatabase {
}
@Override
public
long
getFreeSpace
()
throws
DbException
{
public
long
getFreeSpace
()
{
File
dir
=
config
.
getDatabaseDirectory
();
long
maxSize
=
config
.
getMaxSize
();
long
free
=
dir
.
getFreeSpace
();
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
View file @
da3b2c15
...
...
@@ -56,6 +56,7 @@ import java.util.logging.Logger;
import
javax.annotation.Nullable
;
import
static
java
.
sql
.
Types
.
INTEGER
;
import
static
java
.
sql
.
Types
.
VARCHAR
;
import
static
java
.
util
.
logging
.
Level
.
INFO
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
import
static
org
.
briarproject
.
bramble
.
api
.
db
.
Metadata
.
REMOVE
;
...
...
@@ -83,7 +84,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
abstract
class
JdbcDatabase
implements
Database
<
Connection
>
{
// Package access for testing
static
final
int
CODE_SCHEMA_VERSION
=
4
0
;
static
final
int
CODE_SCHEMA_VERSION
=
4
1
;
// Rotation period offsets for incoming transport keys
private
static
final
int
OFFSET_PREV
=
-
1
;
...
...
@@ -113,6 +114,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+
" authorId _HASH NOT NULL,"
+
" formatVersion INT NOT NULL,"
+
" name _STRING NOT NULL,"
+
" alias _STRING,"
// Null if no alias exists
+
" publicKey _BINARY NOT NULL,"
+
" localAuthorId _HASH NOT NULL,"
+
" verified BOOLEAN NOT NULL,"
...
...
@@ -310,10 +312,9 @@ abstract class JdbcDatabase implements Database<Connection> {
Logger
.
getLogger
(
JdbcDatabase
.
class
.
getName
());
// Different database libraries use different names for certain types
private
final
String
hashType
,
secretType
,
binaryType
;
private
final
String
counterType
,
stringType
;
private
final
MessageFactory
messageFactory
;
private
final
Clock
clock
;
private
final
DatabaseTypes
dbTypes
;
// Locking: connectionsLock
private
final
LinkedList
<
Connection
>
connections
=
new
LinkedList
<>();
...
...
@@ -328,14 +329,9 @@ abstract class JdbcDatabase implements Database<Connection> {
private
final
Lock
connectionsLock
=
new
ReentrantLock
();
private
final
Condition
connectionsChanged
=
connectionsLock
.
newCondition
();
JdbcDatabase
(
String
hashType
,
String
secretType
,
String
binaryType
,
String
counterType
,
String
stringType
,
MessageFactory
messageFactory
,
Clock
clock
)
{
this
.
hashType
=
hashType
;
this
.
secretType
=
secretType
;
this
.
binaryType
=
binaryType
;
this
.
counterType
=
counterType
;
this
.
stringType
=
stringType
;
JdbcDatabase
(
DatabaseTypes
databaseTypes
,
MessageFactory
messageFactory
,
Clock
clock
)
{
this
.
dbTypes
=
databaseTypes
;
this
.
messageFactory
=
messageFactory
;
this
.
clock
=
clock
;
}
...
...
@@ -427,7 +423,11 @@ abstract class JdbcDatabase implements Database<Connection> {
// Package access for testing
List
<
Migration
<
Connection
>>
getMigrations
()
{
return
Arrays
.
asList
(
new
Migration38_39
(),
new
Migration39_40
());
return
Arrays
.
asList
(
new
Migration38_39
(),
new
Migration39_40
(),
new
Migration40_41
(
dbTypes
)
);
}
private
boolean
isCompactionDue
(
Settings
s
)
{
...
...
@@ -486,20 +486,20 @@ abstract class JdbcDatabase implements Database<Connection> {
Statement
s
=
null
;
try
{
s
=
txn
.
createStatement
();
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_SETTINGS
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_LOCAL_AUTHORS
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_CONTACTS
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_GROUPS
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_GROUP_METADATA
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_GROUP_VISIBILITIES
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_MESSAGES
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_MESSAGE_METADATA
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_MESSAGE_DEPENDENCIES
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_OFFERS
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_STATUSES
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_TRANSPORTS
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_OUTGOING_KEYS
));
s
.
executeUpdate
(
insertTypeNam
es
(
CREATE_INCOMING_KEYS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_SETTINGS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_LOCAL_AUTHORS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_CONTACTS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_GROUPS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_GROUP_METADATA
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_GROUP_VISIBILITIES
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_MESSAGES
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_MESSAGE_METADATA
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_MESSAGE_DEPENDENCIES
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_OFFERS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_STATUSES
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_TRANSPORTS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_OUTGOING_KEYS
));
s
.
executeUpdate
(
dbTypes
.
replaceTyp
es
(
CREATE_INCOMING_KEYS
));
s
.
close
();
}
catch
(
SQLException
e
)
{
tryToClose
(
s
);
...
...
@@ -524,15 +524,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
private
String
insertTypeNames
(
String
s
)
{
s
=
s
.
replaceAll
(
"_HASH"
,
hashType
);
s
=
s
.
replaceAll
(
"_SECRET"
,
secretType
);
s
=
s
.
replaceAll
(
"_BINARY"
,
binaryType
);
s
=
s
.
replaceAll
(
"_COUNTER"
,
counterType
);
s
=
s
.
replaceAll
(
"_STRING"
,
stringType
);
return
s
;
}
@Override
public
Connection
startTransaction
()
throws
DbException
{
Connection
txn
;
...
...
@@ -1258,8 +1249,8 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement
ps
=
null
;
ResultSet
rs
=
null
;
try
{
String
sql
=
"SELECT authorId, formatVersion, name,
publicKey
,"
+
" localAuthorId, verified, active"
String
sql
=
"SELECT authorId, formatVersion, name,
alias
,"
+
"
publicKey,
localAuthorId, verified, active"
+
" FROM contacts"
+
" WHERE contactId = ?"
;
ps
=
txn
.
prepareStatement
(
sql
);
...
...
@@ -1269,15 +1260,17 @@ abstract class JdbcDatabase implements Database<Connection> {
AuthorId
authorId
=
new
AuthorId
(
rs
.
getBytes
(
1
));
int
formatVersion
=
rs
.
getInt
(
2
);
String
name
=
rs
.
getString
(
3
);
byte
[]
publicKey
=
rs
.
getBytes
(
4
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
5
));
boolean
verified
=
rs
.
getBoolean
(
6
);
boolean
active
=
rs
.
getBoolean
(
7
);
String
alias
=
rs
.
getString
(
4
);
byte
[]
publicKey
=
rs
.
getBytes
(
5
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
6
));
boolean
verified
=
rs
.
getBoolean
(
7
);
boolean
active
=
rs
.
getBoolean
(
8
);
rs
.
close
();
ps
.
close
();
Author
author
=
new
Author
(
authorId
,
formatVersion
,
name
,
publicKey
);
return
new
Contact
(
c
,
author
,
localAuthorId
,
verified
,
active
);
return
new
Contact
(
c
,
author
,
localAuthorId
,
alias
,
verified
,
active
);
}
catch
(
SQLException
e
)
{
tryToClose
(
rs
);
tryToClose
(
ps
);
...
...
@@ -1292,7 +1285,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ResultSet
rs
=
null
;
try
{
String
sql
=
"SELECT contactId, authorId, formatVersion, name,"
+
" publicKey, localAuthorId, verified, active"
+
"
alias,
publicKey, localAuthorId, verified, active"
+
" FROM contacts"
;
ps
=
txn
.
prepareStatement
(
sql
);
rs
=
ps
.
executeQuery
();
...
...
@@ -1302,14 +1295,15 @@ abstract class JdbcDatabase implements Database<Connection> {
AuthorId
authorId
=
new
AuthorId
(
rs
.
getBytes
(
2
));
int
formatVersion
=
rs
.
getInt
(
3
);
String
name
=
rs
.
getString
(
4
);
byte
[]
publicKey
=
rs
.
getBytes
(
5
);
String
alias
=
rs
.
getString
(
5
);
byte
[]
publicKey
=
rs
.
getBytes
(
6
);
Author
author
=
new
Author
(
authorId
,
formatVersion
,
name
,
publicKey
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
6
));
boolean
verified
=
rs
.
getBoolean
(
7
);
boolean
active
=
rs
.
getBoolean
(
8
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
7
));
boolean
verified
=
rs
.
getBoolean
(
8
);
boolean
active
=
rs
.
getBoolean
(
9
);
contacts
.
add
(
new
Contact
(
contactId
,
author
,
localAuthorId
,
verified
,
active
));
alias
,
verified
,
active
));
}
rs
.
close
();
ps
.
close
();
...
...
@@ -1350,8 +1344,8 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement
ps
=
null
;
ResultSet
rs
=
null
;
try
{
String
sql
=
"SELECT contactId, formatVersion, name,
publicKey
,"
+
" localAuthorId, verified, active"
String
sql
=
"SELECT contactId, formatVersion, name,
alias
,"
+
"
publicKey,
localAuthorId, verified, active"
+
" FROM contacts"
+
" WHERE authorId = ?"
;
ps
=
txn
.
prepareStatement
(
sql
);
...
...
@@ -1362,14 +1356,15 @@ abstract class JdbcDatabase implements Database<Connection> {
ContactId
c
=
new
ContactId
(
rs
.
getInt
(
1
));
int
formatVersion
=
rs
.
getInt
(
2
);
String
name
=
rs
.
getString
(
3
);
byte
[]
publicKey
=
rs
.
getBytes
(
4
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
5
));
boolean
verified
=
rs
.
getBoolean
(
6
);
boolean
active
=
rs
.
getBoolean
(
7
);
String
alias
=
rs
.
getString
(
4
);
byte
[]
publicKey
=
rs
.
getBytes
(
5
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
6
));
boolean
verified
=
rs
.
getBoolean
(
7
);
boolean
active
=
rs
.
getBoolean
(
8
);
Author
author
=
new
Author
(
remote
,
formatVersion
,
name
,
publicKey
);
contacts
.
add
(
new
Contact
(
c
,
author
,
localAuthorId
,
verified
,
active
));
contacts
.
add
(
new
Contact
(
c
,
author
,
localAuthorId
,
alias
,
verified
,
active
));
}
rs
.
close
();
ps
.
close
();
...
...
@@ -2794,6 +2789,25 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
@Override
public
void
setContactAlias
(
Connection
txn
,
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
{
PreparedStatement
ps
=
null
;
try
{
String
sql
=
"UPDATE contacts SET alias = ? WHERE contactId = ?"
;
ps
=
txn
.
prepareStatement
(
sql
);
if
(
alias
==
null
)
ps
.
setNull
(
1
,
VARCHAR
);
else
ps
.
setString
(
1
,
alias
);
ps
.
setInt
(
2
,
c
.
getInt
());
int
affected
=
ps
.
executeUpdate
();
if
(
affected
<
0
||
affected
>
1
)
throw
new
DbStateException
();
ps
.
close
();
}
catch
(
SQLException
e
)
{
tryToClose
(
ps
);
throw
new
DbException
(
e
);
}
}
@Override
public
void
setGroupVisibility
(
Connection
txn
,
ContactId
c
,
GroupId
g
,
boolean
shared
)
throws
DbException
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/Migration40_41.java
0 → 100644
View file @
da3b2c15
package
org.briarproject.bramble.db
;
import
org.briarproject.bramble.api.db.DbException
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.logging.Logger
;
import
javax.annotation.Nullable
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
import
static
java
.
util
.
logging
.
Logger
.
getLogger
;
import
static
org
.
briarproject
.
bramble
.
util
.
LogUtils
.
logException
;
class
Migration40_41
implements
Migration
<
Connection
>
{
private
static
final
Logger
LOG
=
getLogger
(
Migration40_41
.
class
.
getName
());
private
final
DatabaseTypes
dbTypes
;
public
Migration40_41
(
DatabaseTypes
databaseTypes
)
{
this
.
dbTypes
=
databaseTypes
;
}
@Override
public
int
getStartVersion
()
{
return
40
;
}
@Override
public
int
getEndVersion
()
{
return
41
;
}
@Override
public
void
migrate
(
Connection
txn
)
throws
DbException
{
Statement
s
=
null
;
try
{
s
=
txn
.
createStatement
();
s
.
execute
(
"ALTER TABLE contacts"
+
dbTypes
.
replaceTypes
(
" ADD alias _STRING"
));
}
catch
(
SQLException
e
)
{
tryToClose
(
s
);
throw
new
DbException
(
e
);
}
}
private
void
tryToClose
(
@Nullable
Statement
s
)
{
try
{
if
(
s
!=
null
)
s
.
close
();
}
catch
(
SQLException
e
)
{
logException
(
LOG
,
WARNING
,
e
);
}
}
}
bramble-core/src/test/java/org/briarproject/bramble/contact/ContactManagerImplTest.java
View file @
da3b2c15
...
...
@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.identity.Author;
import
org.briarproject.bramble.api.identity.AuthorId
;
import
org.briarproject.bramble.api.transport.KeyManager
;
import
org.briarproject.bramble.test.BrambleMockTestCase
;
import
org.briarproject.bramble.test.DbExpectations
;
import
org.jmock.Expectations
;
import
org.jmock.Mockery
;
import
org.junit.Test
;
...
...
@@ -20,9 +21,11 @@ import java.util.Collection;
import
java.util.Collections
;
import
java.util.Random
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getAuthor
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomId
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getSecretKey
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
...
@@ -35,9 +38,10 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
private
final
ContactId
contactId
=
new
ContactId
(
42
);
private
final
Author
remote
=
getAuthor
();
private
final
AuthorId
local
=
new
AuthorId
(
getRandomId
());
private
final
String
alias
=
getRandomString
(
MAX_AUTHOR_NAME_LENGTH
);
private
final
boolean
verified
=
false
,
active
=
true
;
private
final
Contact
contact
=
new
Contact
(
contactId
,
remote
,
local
,
verified
,
active
);
new
Contact
(
contactId
,
remote
,
local
,
alias
,
verified
,
active
);
public
ContactManagerImplTest
()
{
contactManager
=
new
ContactManagerImpl
(
db
,
keyManager
);
...
...
@@ -131,7 +135,8 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
public
void
testActiveContacts
()
throws
Exception
{
Collection
<
Contact
>
activeContacts
=
Collections
.
singletonList
(
contact
);
Collection
<
Contact
>
contacts
=
new
ArrayList
<>(
activeContacts
);
contacts
.
add
(
new
Contact
(
new
ContactId
(
3
),
remote
,
local
,
true
,
false
));
contacts
.
add
(
new
Contact
(
new
ContactId
(
3
),
remote
,
local
,
alias
,
true
,
false
));
Transaction
txn
=
new
Transaction
(
null
,
true
);
context
.
checking
(
new
Expectations
()
{{
oneOf
(
db
).
startTransaction
(
true
);
...
...
@@ -171,6 +176,23 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
contactManager
.
setContactActive
(
txn
,
contactId
,
active
);
}
@Test
public
void
testSetContactAlias