mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
Use ClientHelper in PrivateMessageFactoryImpl.
This commit is contained in:
@@ -19,6 +19,7 @@ import android.widget.Toast;
|
|||||||
import org.briarproject.R;
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.BriarActivity;
|
import org.briarproject.android.BriarActivity;
|
||||||
import org.briarproject.android.util.BriarRecyclerView;
|
import org.briarproject.android.util.BriarRecyclerView;
|
||||||
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.android.AndroidNotificationManager;
|
import org.briarproject.api.android.AndroidNotificationManager;
|
||||||
import org.briarproject.api.contact.Contact;
|
import org.briarproject.api.contact.Contact;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
@@ -47,8 +48,6 @@ import org.briarproject.api.sync.Message;
|
|||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.GeneralSecurityException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -430,9 +429,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
try {
|
try {
|
||||||
storeMessage(privateMessageFactory.createPrivateMessage(
|
storeMessage(privateMessageFactory.createPrivateMessage(
|
||||||
groupId, timestamp, null, "text/plain", body));
|
groupId, timestamp, null, "text/plain", body));
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (FormatException e) {
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
package org.briarproject.api.messaging;
|
package org.briarproject.api.messaging;
|
||||||
|
|
||||||
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.GeneralSecurityException;
|
|
||||||
|
|
||||||
public interface PrivateMessageFactory {
|
public interface PrivateMessageFactory {
|
||||||
|
|
||||||
PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
||||||
MessageId parent, String contentType, byte[] body)
|
MessageId parent, String contentType, byte[] body)
|
||||||
throws IOException, GeneralSecurityException;
|
throws FormatException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,15 @@
|
|||||||
package org.briarproject.messaging;
|
package org.briarproject.messaging;
|
||||||
|
|
||||||
import org.briarproject.api.data.BdfWriter;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.data.BdfWriterFactory;
|
import org.briarproject.api.clients.ClientHelper;
|
||||||
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.messaging.PrivateMessage;
|
import org.briarproject.api.messaging.PrivateMessage;
|
||||||
import org.briarproject.api.messaging.PrivateMessageFactory;
|
import org.briarproject.api.messaging.PrivateMessageFactory;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
import org.briarproject.api.sync.Message;
|
import org.briarproject.api.sync.Message;
|
||||||
import org.briarproject.api.sync.MessageFactory;
|
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.GeneralSecurityException;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
|
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
|
||||||
@@ -21,36 +17,25 @@ import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESS
|
|||||||
|
|
||||||
class PrivateMessageFactoryImpl implements PrivateMessageFactory {
|
class PrivateMessageFactoryImpl implements PrivateMessageFactory {
|
||||||
|
|
||||||
private final MessageFactory messageFactory;
|
private final ClientHelper clientHelper;
|
||||||
private final BdfWriterFactory bdfWriterFactory;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PrivateMessageFactoryImpl(MessageFactory messageFactory,
|
PrivateMessageFactoryImpl(ClientHelper clientHelper) {
|
||||||
BdfWriterFactory bdfWriterFactory) {
|
this.clientHelper = clientHelper;
|
||||||
this.messageFactory = messageFactory;
|
|
||||||
this.bdfWriterFactory = bdfWriterFactory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
public PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
||||||
MessageId parent, String contentType, byte[] body)
|
MessageId parent, String contentType, byte[] body)
|
||||||
throws IOException, GeneralSecurityException {
|
throws FormatException {
|
||||||
// Validate the arguments
|
// Validate the arguments
|
||||||
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
|
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
if (body.length > MAX_PRIVATE_MESSAGE_BODY_LENGTH)
|
if (body.length > MAX_PRIVATE_MESSAGE_BODY_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
// Serialise the message
|
// Serialise the message
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
BdfList message = BdfList.of(parent, contentType, body);
|
||||||
BdfWriter w = bdfWriterFactory.createWriter(out);
|
Message m = clientHelper.createMessage(groupId, timestamp, message);
|
||||||
w.writeListStart();
|
|
||||||
if (parent == null) w.writeNull();
|
|
||||||
else w.writeRaw(parent.getBytes());
|
|
||||||
w.writeString(contentType);
|
|
||||||
w.writeRaw(body);
|
|
||||||
w.writeListEnd();
|
|
||||||
Message m = messageFactory.createMessage(groupId, timestamp,
|
|
||||||
out.toByteArray());
|
|
||||||
return new PrivateMessage(m, parent, contentType);
|
return new PrivateMessage(m, parent, contentType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user