Exemplo n.º 1
0
  @Deprecated
  private Request buildMultipartFormRequest(
      String url,
      String[] fileKeys,
      File[] files,
      WeakHashMap<String, String> params,
      Object... tag) {
    MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

    if (params != null) {
      Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
      while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        builder.addFormDataPart(entry.getKey(), entry.getValue());
      }
    }

    if (files != null && files.length > 0) {
      for (int i = 0; i < files.length; i++) {
        File file = files[i];
        String fileName = file.getName();
        MediaType mediaType = MediaType.parse(guessMediaType(fileName));
        builder.addFormDataPart(fileKeys[i], fileName, RequestBody.create(mediaType, file));
      }
    }
    RequestBody requestBody = builder.build();
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(url);
    requestBuilder.post(requestBody);
    if (tag.length > 0) {
      requestBuilder.tag(tag[0]);
    }
    return requestBuilder.build();
  }
Exemplo n.º 2
0
  /** A debugging routine. */
  public synchronized void dump() {
    Iterator<Map.Entry<Thread, ThreadStatus>> i = mThreadStatus.entrySet().iterator();

    while (i.hasNext()) {
      Map.Entry<Thread, ThreadStatus> entry = i.next();
      Log.v(
          TAG,
          "[Dump] Thread "
              + entry.getKey()
              + " ("
              + entry.getKey().getId()
              + ")'s status is "
              + entry.getValue());
    }
  }
Exemplo n.º 3
0
 // Can't throw.
 private void markAcquiredConnectionsLocked(AcquiredConnectionStatus status) {
   if (!mAcquiredConnections.isEmpty()) {
     ArrayList<SQLiteConnection> keysToUpdate =
         new ArrayList<SQLiteConnection>(mAcquiredConnections.size());
     for (Map.Entry<SQLiteConnection, AcquiredConnectionStatus> entry :
         mAcquiredConnections.entrySet()) {
       AcquiredConnectionStatus oldStatus = entry.getValue();
       if (status != oldStatus && oldStatus != AcquiredConnectionStatus.DISCARD) {
         keysToUpdate.add(entry.getKey());
       }
     }
     final int updateCount = keysToUpdate.size();
     for (int i = 0; i < updateCount; i++) {
       mAcquiredConnections.put(keysToUpdate.get(i), status);
     }
   }
 }
Exemplo n.º 4
0
 @Deprecated
 private Request buildPostFormRequest(
     String url, WeakHashMap<String, String> params, Object... tag) {
   FormBody.Builder builder = new FormBody.Builder();
   Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
   while (entries.hasNext()) {
     Map.Entry<String, String> entry = entries.next();
     builder.add(entry.getKey(), entry.getValue());
   }
   RequestBody formBody = builder.build();
   Request.Builder requestBuilder = new Request.Builder();
   requestBuilder.url(url);
   requestBuilder.post(formBody);
   if (tag.length > 0) {
     requestBuilder.tag(tag[0]);
   }
   return requestBuilder.build();
 }
Exemplo n.º 5
0
  @Deprecated
  private Request buildMultipartFormRequest2(
      String url,
      String[] fileKeys,
      File[] files,
      WeakHashMap<String, String> params,
      Object... tag) {

    MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

    if (params != null) {
      Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
      while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        builder.addPart(
            Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\""),
            RequestBody.create(null, entry.getValue()));
      }
    }

    if (files != null) {
      RequestBody fileBody = null;
      for (int i = 0; i < files.length; i++) {
        File file = files[i];
        String fileName = file.getName();
        fileBody = RequestBody.create(MediaType.parse(guessMediaType(fileName)), file);
        builder.addPart(
            Headers.of(
                "Content-Disposition",
                "form-data; name=\"" + fileKeys[i] + "\"; filename=\"" + fileName + "\""),
            fileBody);
      }
    }

    RequestBody requestBody = builder.build();
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(url);
    requestBuilder.post(requestBody);
    if (tag.length > 0) {
      requestBuilder.tag(tag[0]);
    }
    return requestBuilder.build();
  }
Exemplo n.º 6
0
        @Override
        public void run() {
          if (logDEBUG) Logger.debug(this, "Running prefetch checker...");
          ArrayList<USK> toFetch = null;
          long now = System.currentTimeMillis();
          boolean empty = true;
          synchronized (USKManager.this) {
            for (Map.Entry<USK, Long> entry : temporaryBackgroundFetchersPrefetch.entrySet()) {
              empty = false;
              if (entry.getValue() > 0 && now - entry.getValue() >= PREFETCH_DELAY) {
                if (toFetch == null) toFetch = new ArrayList<USK>();
                USK clear = entry.getKey();
                long l = lookupLatestSlot(clear);
                if (lookupKnownGood(clear) < l) toFetch.add(clear.copy(l));
                entry.setValue(-1L); // Reset counter until new data comes in
              } else {
                if (logMINOR)
                  Logger.minor(
                      this, "Not prefetching: " + entry.getKey() + " : " + entry.getValue());
              }
            }
          }
          if (toFetch == null) return;
          for (final USK key : toFetch) {
            final long l = key.suggestedEdition;
            if (logMINOR)
              Logger.minor(
                  this, "Prefetching content for background fetch for edition " + l + " on " + key);
            FetchContext fctx =
                new FetchContext(realFetchContext, FetchContext.IDENTICAL_MASK, false, null);
            final ClientGetter get =
                new ClientGetter(
                    new ClientGetCallback() {

                      @Override
                      public void onFailure(
                          FetchException e, ClientGetter state, ObjectContainer container) {
                        if (e.newURI != null) {
                          if (logMINOR)
                            Logger.minor(this, "Prefetch succeeded with redirect for " + key);
                          updateKnownGood(key, l, context);
                          return;
                        } else {
                          if (logMINOR)
                            Logger.minor(this, "Prefetch failed later: " + e + " for " + key, e);
                          // Ignore
                        }
                      }

                      @Override
                      public void onSuccess(
                          FetchResult result, ClientGetter state, ObjectContainer container) {
                        if (logMINOR) Logger.minor(this, "Prefetch succeeded for " + key);
                        result.asBucket().free();
                        updateKnownGood(key, l, context);
                      }

                      @Override
                      public void onMajorProgress(ObjectContainer container) {
                        // Ignore
                      }
                    },
                    key.getURI().sskForUSK() /* FIXME add getSSKURI() */,
                    fctx,
                    RequestStarter.UPDATE_PRIORITY_CLASS,
                    rcBulk,
                    new NullBucket(),
                    null,
                    null);
            try {
              get.start(null, context);
            } catch (FetchException e) {
              if (logMINOR) Logger.minor(this, "Prefetch failed: " + e, e);
              // Ignore
            }
          }
          if (!empty) schedulePrefetchChecker();
        }