org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener Java Examples

The following examples show how to use org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DigestingRequestBody.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public DigestingRequestBody(InputStream inputStream,
                            OutputStreamFactory outputStreamFactory,
                            String contentType, long contentLength,
                            ProgressListener progressListener,
                            CancelationSignal cancelationSignal,
                            long contentStart)
{
  Preconditions.checkArgument(contentLength >= contentStart);
  Preconditions.checkArgument(contentStart >= 0);

  this.inputStream         = inputStream;
  this.outputStreamFactory = outputStreamFactory;
  this.contentType         = contentType;
  this.contentLength       = contentLength;
  this.progressListener    = progressListener;
  this.cancelationSignal   = cancelationSignal;
  this.contentStart        = contentStart;
}
 
Example #2
Source File: PushAttachmentData.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public PushAttachmentData(String contentType, InputStream data, long dataSize,
                          OutputStreamFactory outputStreamFactory, ProgressListener listener)
{
  this.contentType         = contentType;
  this.data                = data;
  this.dataSize            = dataSize;
  this.outputStreamFactory = outputStreamFactory;
  this.listener            = listener;
}
 
Example #3
Source File: DigestingRequestBody.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public DigestingRequestBody(InputStream inputStream,
                            OutputStreamFactory outputStreamFactory,
                            String contentType, long contentLength,
                            ProgressListener progressListener)
{
  this.inputStream         = inputStream;
  this.outputStreamFactory = outputStreamFactory;
  this.contentType         = contentType;
  this.contentLength       = contentLength;
  this.progressListener    = progressListener;
}
 
Example #4
Source File: PushAttachmentData.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public PushAttachmentData(String contentType, InputStream data, long dataSize,
                          OutputStreamFactory outputStreamFactory, ProgressListener listener)
{
  this.contentType         = contentType;
  this.data                = data;
  this.dataSize            = dataSize;
  this.outputStreamFactory = outputStreamFactory;
  this.listener            = listener;
}
 
Example #5
Source File: PushServiceSocket.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
private void downloadFromCdn(File destination, String path, int maxSizeBytes, ProgressListener listener)
    throws PushNetworkException, NonSuccessfulResponseCodeException
{
  try (FileOutputStream outputStream = new FileOutputStream(destination)) {
    downloadFromCdn(outputStream, path, maxSizeBytes, listener);
  } catch (IOException e) {
    throw new PushNetworkException(e);
  }
}
 
Example #6
Source File: PushAttachmentData.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public PushAttachmentData(String contentType, InputStream data, long dataSize,
                          OutputStreamFactory outputStreamFactory,
                          ProgressListener listener, CancelationSignal cancelationSignal,
                          ResumableUploadSpec resumableUploadSpec)
{
  this.contentType             = contentType;
  this.data                    = data;
  this.dataSize                = dataSize;
  this.outputStreamFactory     = outputStreamFactory;
  this.resumableUploadSpec     = resumableUploadSpec;
  this.listener                = listener;
  this.cancelationSignal       = cancelationSignal;
}
 
Example #7
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private void downloadFromCdn(File destination, int cdnNumber, String path, long maxSizeBytes, ProgressListener listener)
    throws PushNetworkException, NonSuccessfulResponseCodeException, MissingConfigurationException {
  try (FileOutputStream outputStream = new FileOutputStream(destination, true)) {
    downloadFromCdn(outputStream, destination.length(), cdnNumber, path, maxSizeBytes, listener);
  } catch (IOException e) {
    throw new PushNetworkException(e);
  }
}
 
Example #8
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public void retrieveAttachment(int cdnNumber, SignalServiceAttachmentRemoteId cdnPath, File destination, long maxSizeBytes, ProgressListener listener)
    throws NonSuccessfulResponseCodeException, PushNetworkException, MissingConfigurationException {
  final String path;
  if (cdnPath.getV2().isPresent()) {
    path = String.format(Locale.US, ATTACHMENT_ID_DOWNLOAD_PATH, cdnPath.getV2().get());
  } else {
    path = String.format(Locale.US, ATTACHMENT_KEY_DOWNLOAD_PATH, cdnPath.getV3().get());
  }
  downloadFromCdn(destination, cdnNumber, path, maxSizeBytes, listener);
}
 
Example #9
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private byte[] uploadToCdn2(String resumableUrl, InputStream data, String contentType, long length, OutputStreamFactory outputStreamFactory, ProgressListener progressListener, CancelationSignal cancelationSignal) throws IOException {
  ConnectionHolder connectionHolder = getRandom(cdnClientsMap.get(2), random);
  OkHttpClient     okHttpClient     = connectionHolder.getClient()
                                                      .newBuilder()
                                                      .connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .build();

  ResumeInfo           resumeInfo = getResumeInfo(resumableUrl, length);
  DigestingRequestBody file       = new DigestingRequestBody(data, outputStreamFactory, contentType, length, progressListener, cancelationSignal, resumeInfo.contentStart);

  if (resumeInfo.contentStart == length) {
    Log.w(TAG, "Resume start point == content length");
    try (NowhereBufferedSink buffer = new NowhereBufferedSink()) {
      file.writeTo(buffer);
    }
    return file.getTransmittedDigest();
  }

  Request.Builder request = new Request.Builder().url(resumableUrl)
                                                 .put(file)
                                                 .addHeader("Content-Range", resumeInfo.contentRange);

  if (connectionHolder.getHostHeader().isPresent()) {
    request.header("host", connectionHolder.getHostHeader().get());
  }

  Call call = okHttpClient.newCall(request.build());

  synchronized (connections) {
    connections.add(call);
  }

  try {
    Response response;

    try {
      response = call.execute();
    } catch (IOException e) {
      throw new PushNetworkException(e);
    }

    if (response.isSuccessful()) return file.getTransmittedDigest();
    else                         throw new NonSuccessfulResponseCodeException("Response: " + response);
  } finally {
    synchronized (connections) {
      connections.remove(call);
    }
  }
}
 
Example #10
Source File: PushAttachmentData.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public ProgressListener getListener() {
  return listener;
}
 
Example #11
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private byte[] uploadToCdn0(String path, String acl, String key, String policy, String algorithm,
                            String credential, String date, String signature,
                            InputStream data, String contentType, long length,
                            OutputStreamFactory outputStreamFactory, ProgressListener progressListener,
                            CancelationSignal cancelationSignal)
    throws PushNetworkException, NonSuccessfulResponseCodeException
{
  ConnectionHolder connectionHolder = getRandom(cdnClientsMap.get(0), random);
  OkHttpClient     okHttpClient     = connectionHolder.getClient()
                                                      .newBuilder()
                                                      .connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .build();

  DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, progressListener, cancelationSignal, 0);

  RequestBody requestBody = new MultipartBody.Builder()
                                             .setType(MultipartBody.FORM)
                                             .addFormDataPart("acl", acl)
                                             .addFormDataPart("key", key)
                                             .addFormDataPart("policy", policy)
                                             .addFormDataPart("Content-Type", contentType)
                                             .addFormDataPart("x-amz-algorithm", algorithm)
                                             .addFormDataPart("x-amz-credential", credential)
                                             .addFormDataPart("x-amz-date", date)
                                             .addFormDataPart("x-amz-signature", signature)
                                             .addFormDataPart("file", "file", file)
                                             .build();

  Request.Builder request = new Request.Builder()
                                       .url(connectionHolder.getUrl() + "/" + path)
                                       .post(requestBody);

  if (connectionHolder.getHostHeader().isPresent()) {
    request.addHeader("Host", connectionHolder.getHostHeader().get());
  }

  Call call = okHttpClient.newCall(request.build());

  synchronized (connections) {
    connections.add(call);
  }

  try {
    Response response;

    try {
      response = call.execute();
    } catch (IOException e) {
      throw new PushNetworkException(e);
    }

    if (response.isSuccessful()) return file.getTransmittedDigest();
    else                         throw new NonSuccessfulResponseCodeException("Response: " + response);
  } finally {
    synchronized (connections) {
      connections.remove(call);
    }
  }
}
 
Example #12
Source File: PushAttachmentData.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public ProgressListener getListener() {
  return listener;
}
 
Example #13
Source File: PushServiceSocket.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
public void retrieveAttachment(long attachmentId, File destination, int maxSizeBytes, ProgressListener listener)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  downloadFromCdn(destination, String.format(Locale.US, ATTACHMENT_DOWNLOAD_PATH, attachmentId), maxSizeBytes, listener);
}
 
Example #14
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private void downloadFromCdn(OutputStream outputStream, long offset, int cdnNumber, String path, long maxSizeBytes, ProgressListener listener)
    throws PushNetworkException, NonSuccessfulResponseCodeException, MissingConfigurationException {
  ConnectionHolder[] cdnNumberClients = cdnClientsMap.get(cdnNumber);
  if (cdnNumberClients == null) {
    throw new MissingConfigurationException("Attempted to download from unsupported CDN number: " + cdnNumber + ", Our configuration supports: " + cdnClientsMap.keySet());
  }
  ConnectionHolder   connectionHolder = getRandom(cdnNumberClients, random);
  OkHttpClient       okHttpClient     = connectionHolder.getClient()
                                                        .newBuilder()
                                                        .connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                        .readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                        .build();

  Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + "/" + path).get();

  if (connectionHolder.getHostHeader().isPresent()) {
    request.addHeader("Host", connectionHolder.getHostHeader().get());
  }

  if (offset > 0) {
    Log.i(TAG, "Starting download from CDN with offset " + offset);
    request.addHeader("Range", "bytes=" + offset + "-");
  }

  Call call = okHttpClient.newCall(request.build());

  synchronized (connections) {
    connections.add(call);
  }

  Response     response = null;
  ResponseBody body     = null;

  try {
    response = call.execute();

    if (response.isSuccessful()) {
      body = response.body();

      if (body == null)                        throw new PushNetworkException("No response body!");
      if (body.contentLength() > maxSizeBytes) throw new PushNetworkException("Response exceeds max size!");

      InputStream  in     = body.byteStream();
      byte[]       buffer = new byte[32768];

      int  read      = 0;
      long totalRead = offset;

      while ((read = in.read(buffer, 0, buffer.length)) != -1) {
        outputStream.write(buffer, 0, read);
        if ((totalRead += read) > maxSizeBytes) throw new PushNetworkException("Response exceeded max size!");

        if (listener != null) {
          listener.onAttachmentProgress(body.contentLength() + offset, totalRead);
        }
      }

      return;
    }
  } catch (IOException e) {
    if (body != null) {
      body.close();
    }
    throw new PushNetworkException(e);
  } finally {
    synchronized (connections) {
      connections.remove(call);
    }
  }

  throw new NonSuccessfulResponseCodeException("Response: " + response);
}
 
Example #15
Source File: PushServiceSocket.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
private void downloadFromCdn(OutputStream outputStream, String path, int maxSizeBytes, ProgressListener listener)
    throws PushNetworkException, NonSuccessfulResponseCodeException
{
  ConnectionHolder connectionHolder = getRandom(cdnClients, random);
  OkHttpClient     okHttpClient     = connectionHolder.getClient()
                                                      .newBuilder()
                                                      .connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .build();

  Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + "/" + path).get();

  if (connectionHolder.getHostHeader().isPresent()) {
    request.addHeader("Host", connectionHolder.getHostHeader().get());
  }

  Call call = okHttpClient.newCall(request.build());

  synchronized (connections) {
    connections.add(call);
  }

  Response response;

  try {
    response = call.execute();

    if (response.isSuccessful()) {
      ResponseBody body = response.body();

      if (body == null)                        throw new PushNetworkException("No response body!");
      if (body.contentLength() > maxSizeBytes) throw new PushNetworkException("Response exceeds max size!");

      InputStream  in     = body.byteStream();
      byte[]       buffer = new byte[32768];

      int read, totalRead = 0;

      while ((read = in.read(buffer, 0, buffer.length)) != -1) {
        outputStream.write(buffer, 0, read);
        if ((totalRead += read) > maxSizeBytes) throw new PushNetworkException("Response exceeded max size!");

        if (listener != null) {
          listener.onAttachmentProgress(body.contentLength(), totalRead);
        }
      }

      return;
    }
  } catch (IOException e) {
    throw new PushNetworkException(e);
  } finally {
    synchronized (connections) {
      connections.remove(call);
    }
  }

  throw new NonSuccessfulResponseCodeException("Response: " + response);
}
 
Example #16
Source File: PushServiceSocket.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
private byte[] uploadToCdn(String path, String acl, String key, String policy, String algorithm,
                           String credential, String date, String signature,
                           InputStream data, String contentType, long length,
                           OutputStreamFactory outputStreamFactory, ProgressListener progressListener)
    throws PushNetworkException, NonSuccessfulResponseCodeException
{
  ConnectionHolder connectionHolder = getRandom(cdnClients, random);
  OkHttpClient     okHttpClient     = connectionHolder.getClient()
                                                      .newBuilder()
                                                      .connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
                                                      .build();

  DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, progressListener);

  RequestBody requestBody = new MultipartBody.Builder()
      .setType(MultipartBody.FORM)
      .addFormDataPart("acl", acl)
      .addFormDataPart("key", key)
      .addFormDataPart("policy", policy)
      .addFormDataPart("Content-Type", contentType)
      .addFormDataPart("x-amz-algorithm", algorithm)
      .addFormDataPart("x-amz-credential", credential)
      .addFormDataPart("x-amz-date", date)
      .addFormDataPart("x-amz-signature", signature)
      .addFormDataPart("file", "file", file)
      .build();

  Request.Builder request = new Request.Builder()
                                       .url(connectionHolder.getUrl() + "/" + path)
                                       .post(requestBody);

  if (connectionHolder.getHostHeader().isPresent()) {
    request.addHeader("Host", connectionHolder.getHostHeader().get());
  }

  Call call = okHttpClient.newCall(request.build());

  synchronized (connections) {
    connections.add(call);
  }

  try {
    Response response;

    try {
      response = call.execute();
    } catch (IOException e) {
      throw new PushNetworkException(e);
    }

    if (response.isSuccessful()) return file.getTransmittedDigest();
    else                         throw new NonSuccessfulResponseCodeException("Response: " + response);
  } finally {
    synchronized (connections) {
      connections.remove(call);
    }
  }
}
 
Example #17
Source File: PushAttachmentData.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
public ProgressListener getListener() {
  return listener;
}
 
Example #18
Source File: SignalServiceMessageReceiver.java    From mollyim-android with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Retrieves a SignalServiceAttachment.
 *
 * @param pointer The {@link SignalServiceAttachmentPointer}
 *                received in a {@link SignalServiceDataMessage}.
 * @param destination The download destination for this attachment. If this file exists, it is
 *                    assumed that this is previously-downloaded content that can be resumed.
 * @param listener An optional listener (may be null) to receive callbacks on download progress.
 *
 * @return An InputStream that streams the plaintext attachment contents.
 * @throws IOException
 * @throws InvalidMessageException
 */
public InputStream retrieveAttachment(SignalServiceAttachmentPointer pointer, File destination, long maxSizeBytes, ProgressListener listener)
    throws IOException, InvalidMessageException, MissingConfigurationException {
  if (!pointer.getDigest().isPresent()) throw new InvalidMessageException("No attachment digest!");

  socket.retrieveAttachment(pointer.getCdnNumber(), pointer.getRemoteId(), destination, maxSizeBytes, listener);
  return AttachmentCipherInputStream.createForAttachment(destination, pointer.getSize().or(0), pointer.getKey(), pointer.getDigest().get());
}
 
Example #19
Source File: SignalServiceMessageReceiver.java    From libsignal-service-java with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Retrieves a SignalServiceAttachment.
 *
 * @param pointer The {@link SignalServiceAttachmentPointer}
 *                received in a {@link SignalServiceDataMessage}.
 * @param destination The download destination for this attachment.
 * @param listener An optional listener (may be null) to receive callbacks on download progress.
 *
 * @return An InputStream that streams the plaintext attachment contents.
 * @throws IOException
 * @throws InvalidMessageException
 */
public InputStream retrieveAttachment(SignalServiceAttachmentPointer pointer, File destination, int maxSizeBytes, ProgressListener listener)
    throws IOException, InvalidMessageException
{
  if (!pointer.getDigest().isPresent()) throw new InvalidMessageException("No attachment digest!");

  socket.retrieveAttachment(pointer.getId(), destination, maxSizeBytes, listener);
  return AttachmentCipherInputStream.createForAttachment(destination, pointer.getSize().or(0), pointer.getKey(), pointer.getDigest().get());
}