Пример #1
0
  @SuppressWarnings("deprecation")
  public Optional<MesosFileChunkObject> read(
      String slaveHostname, String fullPath, Optional<Long> offset, Optional<Long> length)
      throws SlaveNotFoundException {
    try {
      final AsyncHttpClient.BoundRequestBuilder builder =
          asyncHttpClient
              .prepareGet(String.format("http://%s:5051/files/read.json", slaveHostname))
              .addQueryParameter("path", fullPath);

      PerRequestConfig timeoutConfig = new PerRequestConfig();
      timeoutConfig.setRequestTimeoutInMs((int) configuration.getSandboxHttpTimeoutMillis());
      builder.setPerRequestConfig(timeoutConfig);

      if (offset.isPresent()) {
        builder.addQueryParameter("offset", offset.get().toString());
      }

      if (length.isPresent()) {
        builder.addQueryParameter("length", length.get().toString());
      }

      final Response response = builder.execute().get();

      if (response.getStatusCode() == 404) {
        return Optional.absent();
      }

      if (response.getStatusCode() != 200) {
        throw new RuntimeException(
            String.format("Got HTTP %s from Mesos slave", response.getStatusCode()));
      }

      return Optional.of(
          objectMapper.readValue(response.getResponseBodyAsStream(), MesosFileChunkObject.class));
    } catch (ConnectException ce) {
      throw new SlaveNotFoundException(ce);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
Пример #2
0
 private BoundRequestBuilder prepare(BoundRequestBuilder builder) {
   if (this.username != null && this.password != null && this.scheme != null) {
     AuthScheme authScheme;
     switch (this.scheme) {
       case DIGEST:
         authScheme = AuthScheme.DIGEST;
         break;
       case NTLM:
         authScheme = AuthScheme.NTLM;
         break;
       case KERBEROS:
         authScheme = AuthScheme.KERBEROS;
         break;
       case SPNEGO:
         authScheme = AuthScheme.SPNEGO;
         break;
       case BASIC:
         authScheme = AuthScheme.BASIC;
         break;
       default:
         throw new RuntimeException(
             "Scheme " + this.scheme + " not supported by the UrlFetch WS backend.");
     }
     builder.setRealm(
         (new RealmBuilder())
             .setScheme(authScheme)
             .setPrincipal(this.username)
             .setPassword(this.password)
             .setUsePreemptiveAuth(true)
             .build());
   }
   for (String key : this.headers.keySet()) {
     builder.addHeader(key, headers.get(key));
   }
   builder.setFollowRedirects(this.followRedirects);
   PerRequestConfig perRequestConfig = new PerRequestConfig();
   perRequestConfig.setRequestTimeoutInMs(this.timeout * 1000);
   builder.setPerRequestConfig(perRequestConfig);
   return builder;
 }
  public void configureRequest(
      final RequestBuilder requestBuilder, final ClientRequest cr, final boolean needsBody) {
    final Map<String, Object> props = cr.getProperties();

    // Set the read timeout
    final Integer readTimeout = (Integer) props.get(AhcConfig.PROPERTY_READ_TIMEOUT);
    if (readTimeout != null) {
      final PerRequestConfig c = new PerRequestConfig();
      c.setRequestTimeoutInMs(readTimeout);
      requestBuilder.setPerRequestConfig(c);
    }
    if (cr.getEntity() != null && needsBody) {
      final RequestEntityWriter re = getRequestEntityWriter(cr);

      final ByteArrayOutputStream baos = new ByteArrayOutputStream();
      try {
        re.writeRequestEntity(
            new CommittingOutputStream(baos) {
              @Override
              protected void commit() throws IOException {
                configureHeaders(cr.getHeaders(), requestBuilder);
              }
            });
      } catch (final IOException ex) {
        throw new ClientHandlerException(ex);
      }

      final byte[] content = baos.toByteArray();
      requestBuilder.setBody(
          new Request.EntityWriter() {
            @Override
            public void writeEntity(final OutputStream out) throws IOException {
              out.write(content);
            }
          });
    } else {
      configureHeaders(cr.getHeaders(), requestBuilder);
    }
  }