Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
briar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian Dehm
briar
Commits
8beff5c7
Unverified
Commit
8beff5c7
authored
9 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Use ClientHelper in MessagingManagerImpl.
parent
4ddc34ee
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java
+33
-92
33 additions, 92 deletions
.../src/org/briarproject/messaging/MessagingManagerImpl.java
with
33 additions
and
92 deletions
briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java
+
33
−
92
View file @
8beff5c7
...
@@ -3,19 +3,16 @@ package org.briarproject.messaging;
...
@@ -3,19 +3,16 @@ package org.briarproject.messaging;
import
com.google.inject.Inject
;
import
com.google.inject.Inject
;
import
org.briarproject.api.FormatException
;
import
org.briarproject.api.FormatException
;
import
org.briarproject.api.clients.ClientHelper
;
import
org.briarproject.api.clients.PrivateGroupFactory
;
import
org.briarproject.api.clients.PrivateGroupFactory
;
import
org.briarproject.api.contact.Contact
;
import
org.briarproject.api.contact.Contact
;
import
org.briarproject.api.contact.ContactId
;
import
org.briarproject.api.contact.ContactId
;
import
org.briarproject.api.contact.ContactManager.AddContactHook
;
import
org.briarproject.api.contact.ContactManager.AddContactHook
;
import
org.briarproject.api.contact.ContactManager.RemoveContactHook
;
import
org.briarproject.api.contact.ContactManager.RemoveContactHook
;
import
org.briarproject.api.data.BdfDictionary
;
import
org.briarproject.api.data.BdfDictionary
;
import
org.briarproject.api.data.BdfReader
;
import
org.briarproject.api.data.BdfList
;
import
org.briarproject.api.data.BdfReaderFactory
;
import
org.briarproject.api.data.MetadataEncoder
;
import
org.briarproject.api.data.MetadataParser
;
import
org.briarproject.api.db.DatabaseComponent
;
import
org.briarproject.api.db.DatabaseComponent
;
import
org.briarproject.api.db.DbException
;
import
org.briarproject.api.db.DbException
;
import
org.briarproject.api.db.Metadata
;
import
org.briarproject.api.db.Transaction
;
import
org.briarproject.api.db.Transaction
;
import
org.briarproject.api.messaging.MessagingManager
;
import
org.briarproject.api.messaging.MessagingManager
;
import
org.briarproject.api.messaging.PrivateMessage
;
import
org.briarproject.api.messaging.PrivateMessage
;
...
@@ -27,16 +24,9 @@ import org.briarproject.api.sync.MessageId;
...
@@ -27,16 +24,9 @@ import org.briarproject.api.sync.MessageId;
import
org.briarproject.api.sync.MessageStatus
;
import
org.briarproject.api.sync.MessageStatus
;
import
org.briarproject.util.StringUtils
;
import
org.briarproject.util.StringUtils
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.logging.Logger
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
import
static
org
.
briarproject
.
api
.
messaging
.
MessagingConstants
.
MAX_PRIVATE_MESSAGE_BODY_LENGTH
;
import
static
org
.
briarproject
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
class
MessagingManagerImpl
implements
MessagingManager
,
AddContactHook
,
class
MessagingManagerImpl
implements
MessagingManager
,
AddContactHook
,
RemoveContactHook
{
RemoveContactHook
{
...
@@ -45,25 +35,16 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
...
@@ -45,25 +35,16 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
"6bcdc006c0910b0f44e40644c3b31f1a"
"6bcdc006c0910b0f44e40644c3b31f1a"
+
"8bf9a6d6021d40d219c86b731b903070"
));
+
"8bf9a6d6021d40d219c86b731b903070"
));
private
static
final
Logger
LOG
=
Logger
.
getLogger
(
MessagingManagerImpl
.
class
.
getName
());
private
final
DatabaseComponent
db
;
private
final
DatabaseComponent
db
;
private
final
ClientHelper
clientHelper
;
private
final
PrivateGroupFactory
privateGroupFactory
;
private
final
PrivateGroupFactory
privateGroupFactory
;
private
final
BdfReaderFactory
bdfReaderFactory
;
private
final
MetadataEncoder
metadataEncoder
;
private
final
MetadataParser
metadataParser
;
@Inject
@Inject
MessagingManagerImpl
(
DatabaseComponent
db
,
MessagingManagerImpl
(
DatabaseComponent
db
,
ClientHelper
clientHelper
,
PrivateGroupFactory
privateGroupFactory
,
PrivateGroupFactory
privateGroupFactory
)
{
BdfReaderFactory
bdfReaderFactory
,
MetadataEncoder
metadataEncoder
,
MetadataParser
metadataParser
)
{
this
.
db
=
db
;
this
.
db
=
db
;
this
.
clientHelper
=
clientHelper
;
this
.
privateGroupFactory
=
privateGroupFactory
;
this
.
privateGroupFactory
=
privateGroupFactory
;
this
.
bdfReaderFactory
=
bdfReaderFactory
;
this
.
metadataEncoder
=
metadataEncoder
;
this
.
metadataParser
=
metadataParser
;
}
}
@Override
@Override
...
@@ -77,7 +58,7 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
...
@@ -77,7 +58,7 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
// Attach the contact ID to the group
// Attach the contact ID to the group
BdfDictionary
d
=
new
BdfDictionary
();
BdfDictionary
d
=
new
BdfDictionary
();
d
.
put
(
"contactId"
,
c
.
getId
().
getInt
());
d
.
put
(
"contactId"
,
c
.
getId
().
getInt
());
db
.
mergeGroupMetadata
(
txn
,
g
.
getId
(),
metadataEncoder
.
encode
(
d
)
);
clientHelper
.
mergeGroupMetadata
(
txn
,
g
.
getId
(),
d
);
}
catch
(
FormatException
e
)
{
}
catch
(
FormatException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
...
@@ -100,21 +81,14 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
...
@@ -100,21 +81,14 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
@Override
public
void
addLocalMessage
(
PrivateMessage
m
)
throws
DbException
{
public
void
addLocalMessage
(
PrivateMessage
m
)
throws
DbException
{
try
{
try
{
BdfDictionary
d
=
new
BdfDictionary
();
BdfDictionary
meta
=
new
BdfDictionary
();
d
.
put
(
"timestamp"
,
m
.
getMessage
().
getTimestamp
());
meta
.
put
(
"timestamp"
,
m
.
getMessage
().
getTimestamp
());
if
(
m
.
getParent
()
!=
null
)
if
(
m
.
getParent
()
!=
null
)
d
.
put
(
"parent"
,
m
.
getParent
().
getBytes
());
meta
.
put
(
"parent"
,
m
.
getParent
().
getBytes
());
d
.
put
(
"contentType"
,
m
.
getContentType
());
meta
.
put
(
"contentType"
,
m
.
getContentType
());
d
.
put
(
"local"
,
true
);
meta
.
put
(
"local"
,
true
);
d
.
put
(
"read"
,
true
);
meta
.
put
(
"read"
,
true
);
Metadata
meta
=
metadataEncoder
.
encode
(
d
);
clientHelper
.
addLocalMessage
(
m
.
getMessage
(),
CLIENT_ID
,
meta
,
true
);
Transaction
txn
=
db
.
startTransaction
();
try
{
db
.
addLocalMessage
(
txn
,
m
.
getMessage
(),
CLIENT_ID
,
meta
,
true
);
txn
.
setComplete
();
}
finally
{
db
.
endTransaction
(
txn
);
}
}
catch
(
FormatException
e
)
{
}
catch
(
FormatException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
...
@@ -123,16 +97,8 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
...
@@ -123,16 +97,8 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
@Override
public
ContactId
getContactId
(
GroupId
g
)
throws
DbException
{
public
ContactId
getContactId
(
GroupId
g
)
throws
DbException
{
try
{
try
{
Metadata
meta
;
BdfDictionary
meta
=
clientHelper
.
getGroupMetadataAsDictionary
(
g
);
Transaction
txn
=
db
.
startTransaction
();
return
new
ContactId
(
meta
.
getLong
(
"contactId"
).
intValue
());
try
{
meta
=
db
.
getGroupMetadata
(
txn
,
g
);
txn
.
setComplete
();
}
finally
{
db
.
endTransaction
(
txn
);
}
BdfDictionary
d
=
metadataParser
.
parse
(
meta
);
return
new
ContactId
(
d
.
getLong
(
"contactId"
).
intValue
());
}
catch
(
FormatException
e
)
{
}
catch
(
FormatException
e
)
{
throw
new
DbException
(
e
);
throw
new
DbException
(
e
);
}
}
...
@@ -154,14 +120,16 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
...
@@ -154,14 +120,16 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
@Override
public
Collection
<
PrivateMessageHeader
>
getMessageHeaders
(
ContactId
c
)
public
Collection
<
PrivateMessageHeader
>
getMessageHeaders
(
ContactId
c
)
throws
DbException
{
throws
DbException
{
Map
<
MessageId
,
Metadata
>
metadata
;
Map
<
MessageId
,
BdfDictionary
>
metadata
;
Collection
<
MessageStatus
>
statuses
;
Collection
<
MessageStatus
>
statuses
;
Transaction
txn
=
db
.
startTransaction
();
Transaction
txn
=
db
.
startTransaction
();
try
{
try
{
GroupId
g
=
getContactGroup
(
db
.
getContact
(
txn
,
c
)).
getId
();
GroupId
g
=
getContactGroup
(
db
.
getContact
(
txn
,
c
)).
getId
();
metadata
=
db
.
getMessageMetadata
(
txn
,
g
);
metadata
=
clientHelper
.
getMessageMetadata
AsDictionary
(
txn
,
g
);
statuses
=
db
.
getMessageStatus
(
txn
,
c
,
g
);
statuses
=
db
.
getMessageStatus
(
txn
,
c
,
g
);
txn
.
setComplete
();
txn
.
setComplete
();
}
catch
(
FormatException
e
)
{
throw
new
DbException
(
e
);
}
finally
{
}
finally
{
db
.
endTransaction
(
txn
);
db
.
endTransaction
(
txn
);
}
}
...
@@ -169,18 +137,17 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
...
@@ -169,18 +137,17 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
new
ArrayList
<
PrivateMessageHeader
>();
new
ArrayList
<
PrivateMessageHeader
>();
for
(
MessageStatus
s
:
statuses
)
{
for
(
MessageStatus
s
:
statuses
)
{
MessageId
id
=
s
.
getMessageId
();
MessageId
id
=
s
.
getMessageId
();
Metadata
m
=
metadata
.
get
(
id
);
BdfDictionary
meta
=
metadata
.
get
(
id
);
if
(
m
==
null
)
continue
;
if
(
m
eta
==
null
)
continue
;
try
{
try
{
BdfDictionary
d
=
metadataParser
.
parse
(
m
);
long
timestamp
=
meta
.
getLong
(
"timestamp"
);
long
timestamp
=
d
.
getLong
(
"timestamp"
);
String
contentType
=
meta
.
getString
(
"contentType"
);
String
contentType
=
d
.
getString
(
"contentType"
);
boolean
local
=
meta
.
getBoolean
(
"local"
);
boolean
local
=
d
.
getBoolean
(
"local"
);
boolean
read
=
meta
.
getBoolean
(
"read"
);
boolean
read
=
d
.
getBoolean
(
"read"
);
headers
.
add
(
new
PrivateMessageHeader
(
id
,
timestamp
,
contentType
,
headers
.
add
(
new
PrivateMessageHeader
(
id
,
timestamp
,
contentType
,
local
,
read
,
s
.
isSent
(),
s
.
isSeen
()));
local
,
read
,
s
.
isSent
(),
s
.
isSeen
()));
}
catch
(
FormatException
e
)
{
}
catch
(
FormatException
e
)
{
if
(
LOG
.
isLoggable
(
WARNING
))
LOG
.
log
(
WARNING
,
e
.
toString
(),
e
);
throw
new
DbException
(
e
);
}
}
}
}
return
headers
;
return
headers
;
...
@@ -188,47 +155,21 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
...
@@ -188,47 +155,21 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
@Override
public
byte
[]
getMessageBody
(
MessageId
m
)
throws
DbException
{
public
byte
[]
getMessageBody
(
MessageId
m
)
throws
DbException
{
byte
[]
raw
;
Transaction
txn
=
db
.
startTransaction
();
try
{
try
{
raw
=
db
.
getRawMessage
(
txn
,
m
);
// Parent ID, content type, private message body
txn
.
setComplete
();
BdfList
message
=
clientHelper
.
getMessageAsList
(
m
);
}
finally
{
return
message
.
getRaw
(
2
);
db
.
endTransaction
(
txn
);
}
ByteArrayInputStream
in
=
new
ByteArrayInputStream
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
BdfReader
r
=
bdfReaderFactory
.
createReader
(
in
);
try
{
r
.
readListStart
();
if
(
r
.
hasRaw
())
r
.
skipRaw
();
// Parent ID
else
r
.
skipNull
();
// No parent
r
.
skipString
();
// Content type
byte
[]
messageBody
=
r
.
readRaw
(
MAX_PRIVATE_MESSAGE_BODY_LENGTH
);
r
.
readListEnd
();
if
(!
r
.
eof
())
throw
new
FormatException
();
return
messageBody
;
}
catch
(
FormatException
e
)
{
}
catch
(
FormatException
e
)
{
throw
new
DbException
(
e
);
throw
new
DbException
(
e
);
}
catch
(
IOException
e
)
{
// Shouldn't happen with ByteArrayInputStream
throw
new
RuntimeException
(
e
);
}
}
}
}
@Override
@Override
public
void
setReadFlag
(
MessageId
m
,
boolean
read
)
throws
DbException
{
public
void
setReadFlag
(
MessageId
m
,
boolean
read
)
throws
DbException
{
try
{
try
{
BdfDictionary
d
=
new
BdfDictionary
();
BdfDictionary
meta
=
new
BdfDictionary
();
d
.
put
(
"read"
,
read
);
meta
.
put
(
"read"
,
read
);
Metadata
meta
=
metadataEncoder
.
encode
(
d
);
clientHelper
.
mergeMessageMetadata
(
m
,
meta
);
Transaction
txn
=
db
.
startTransaction
();
try
{
db
.
mergeMessageMetadata
(
txn
,
m
,
meta
);
txn
.
setComplete
();
}
finally
{
db
.
endTransaction
(
txn
);
}
}
catch
(
FormatException
e
)
{
}
catch
(
FormatException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment