Пример #1
0
 @Override
 protected void queryCapabilities() throws DavException, IOException, HttpException {
   // prepare remote address book
   hasVCard4 = false;
   davCollection.propfind(0, SupportedAddressData.NAME, GetCTag.NAME);
   SupportedAddressData supportedAddressData =
       (SupportedAddressData) davCollection.properties.get(SupportedAddressData.NAME);
   if (supportedAddressData != null)
     for (MediaType type : supportedAddressData.types)
       if ("text/vcard; version=4.0".equalsIgnoreCase(type.toString())) hasVCard4 = true;
   App.log.info("Server advertises VCard/4 support: " + hasVCard4);
 }
  @Override
  public long open(DataSpec dataSpec) throws HttpDataSourceException {
    this.dataSpec = dataSpec;
    this.bytesRead = 0;
    this.bytesSkipped = 0;
    Request request = makeRequest(dataSpec);
    try {
      response = okHttpClient.newCall(request).execute();
      responseByteStream = response.body().byteStream();
    } catch (IOException e) {
      throw new HttpDataSourceException(
          "Unable to connect to " + dataSpec.uri.toString(), e, dataSpec);
    }

    int responseCode = response.code();

    // Check for a valid response code.
    if (!response.isSuccessful()) {
      Map<String, List<String>> headers = request.headers().toMultimap();
      closeConnectionQuietly();
      throw new InvalidResponseCodeException(responseCode, headers, dataSpec);
    }

    // Check for a valid content type.
    MediaType mediaType = response.body().contentType();
    String contentType = mediaType != null ? mediaType.toString() : null;
    if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) {
      closeConnectionQuietly();
      throw new InvalidContentTypeException(contentType, dataSpec);
    }

    // If we requested a range starting from a non-zero position and received a 200 rather than a
    // 206, then the server does not support partial requests. We'll need to manually skip to the
    // requested position.
    bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;

    // Determine the length of the data to be read, after skipping.
    long contentLength = response.body().contentLength();
    bytesToRead =
        dataSpec.length != C.LENGTH_UNBOUNDED
            ? dataSpec.length
            : contentLength != -1 ? contentLength - bytesToSkip : C.LENGTH_UNBOUNDED;

    opened = true;
    if (listener != null) {
      listener.onTransferStart();
    }

    return bytesToRead;
  }
Пример #3
0
 /**
  * Sets the content type header to the specified media type.
  *
  * @param contentType the media type.
  * @return this
  */
 public Builder contentType(final MediaType contentType) {
   mHeaders.set(CONTENT_TYPE, contentType.toString());
   return this;
 }