예제 #1
0
    @Override
    public byte[] download(String url, String accepts) {
      HttpUrl httpUrl = HttpUrl.parse(url);

      if (httpUrl == null) {
        App.log.log(Level.SEVERE, "Invalid external resource URL", url);
        return null;
      }

      String host = httpUrl.host();
      if (host == null) {
        App.log.log(Level.SEVERE, "External resource URL doesn't specify a host name", url);
        return null;
      }

      OkHttpClient resourceClient = HttpClient.create();

      // authenticate only against a certain host, and only upon request
      resourceClient =
          HttpClient.addAuthentication(
              resourceClient, baseUrl.host(), settings.username(), settings.password());

      // allow redirects
      resourceClient = resourceClient.newBuilder().followRedirects(true).build();

      try {
        Response response =
            resourceClient.newCall(new Request.Builder().get().url(httpUrl).build()).execute();

        ResponseBody body = response.body();
        if (body != null) {
          @Cleanup InputStream stream = body.byteStream();
          if (response.isSuccessful() && stream != null) {
            return IOUtils.toByteArray(stream);
          } else App.log.severe("Couldn't download external resource");
        }
      } catch (IOException e) {
        App.log.log(Level.SEVERE, "Couldn't download external resource", e);
      }
      return null;
    }
예제 #2
0
  @Override
  protected void downloadRemote()
      throws IOException, HttpException, DavException, ContactsStorageException {
    App.log.info("Downloading " + toDownload.size() + " contacts (" + MAX_MULTIGET + " at once)");

    // prepare downloader which may be used to download external resource like contact photos
    Contact.Downloader downloader = new ResourceDownloader(collectionURL);

    // download new/updated VCards from server
    for (DavResource[] bunch :
        ArrayUtils.partition(
            toDownload.toArray(new DavResource[toDownload.size()]), MAX_MULTIGET)) {
      if (Thread.interrupted()) return;

      App.log.info("Downloading " + StringUtils.join(bunch, ", "));

      if (bunch.length == 1) {
        // only one contact, use GET
        DavResource remote = bunch[0];

        ResponseBody body =
            remote.get("text/vcard;version=4.0, text/vcard;charset=utf-8;q=0.8, text/vcard;q=0.5");
        String eTag = ((GetETag) remote.properties.get(GetETag.NAME)).eTag;

        Charset charset = Charsets.UTF_8;
        MediaType contentType = body.contentType();
        if (contentType != null) charset = contentType.charset(Charsets.UTF_8);

        @Cleanup InputStream stream = body.byteStream();
        processVCard(remote.fileName(), eTag, stream, charset, downloader);

      } else {
        // multiple contacts, use multi-get
        List<HttpUrl> urls = new LinkedList<>();
        for (DavResource remote : bunch) urls.add(remote.location);
        davAddressBook().multiget(urls.toArray(new HttpUrl[urls.size()]), hasVCard4);

        // process multiget results
        for (DavResource remote : davCollection.members) {
          String eTag;
          GetETag getETag = (GetETag) remote.properties.get(GetETag.NAME);
          if (getETag != null) eTag = getETag.eTag;
          else throw new DavException("Received multi-get response without ETag");

          Charset charset = Charsets.UTF_8;
          GetContentType getContentType =
              (GetContentType) remote.properties.get(GetContentType.NAME);
          if (getContentType != null && getContentType.type != null) {
            MediaType type = MediaType.parse(getContentType.type);
            if (type != null) charset = type.charset(Charsets.UTF_8);
          }

          AddressData addressData = (AddressData) remote.properties.get(AddressData.NAME);
          if (addressData == null || addressData.vCard == null)
            throw new DavException("Received multi-get response without address data");

          @Cleanup InputStream stream = new ByteArrayInputStream(addressData.vCard.getBytes());
          processVCard(remote.fileName(), eTag, stream, charset, downloader);
        }
      }
    }
  }