示例#1
0
  /**
   * Retrieve the bytes at the specified path.
   *
   * @param path - encoded but not fully qualified. Must NOT be slash prefixed as it will be
   *     appended to the host's url
   * @return
   * @throws com.ettrema.httpclient.HttpException
   */
  public synchronized byte[] get(String path)
      throws com.ettrema.httpclient.HttpException, NotAuthorizedException, BadRequestException,
          ConflictException, NotFoundException {
    String url = this.encodedUrl() + path;
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
      transferService.get(
          url,
          new StreamReceiver() {

            @Override
            public void receive(InputStream in) {
              try {
                IOUtils.copy(in, out);
              } catch (IOException ex) {
                throw new RuntimeException(ex);
              }
            }
          },
          null,
          null);
    } catch (CancelledException ex) {
      throw new RuntimeException("Should never happen because no progress listener is set", ex);
    }
    return out.toByteArray();
  }
示例#2
0
  public synchronized void doGet(Path path, final java.io.File file, ProgressListener listener)
      throws IOException, NotFoundException, com.ettrema.httpclient.HttpException,
          CancelledException, NotAuthorizedException, BadRequestException, ConflictException {
    LogUtils.trace(log, "doGet", path);
    if (fileSyncer != null) {
      fileSyncer.download(this, path, file, listener);
    } else {
      String url = this.buildEncodedUrl(path);
      transferService.get(
          url,
          new StreamReceiver() {

            @Override
            public void receive(InputStream in) throws IOException {
              OutputStream out = null;
              BufferedOutputStream bout = null;
              try {
                out = FileUtils.openOutputStream(file);
                bout = new BufferedOutputStream(out);
                IOUtils.copy(in, bout);
                bout.flush();
              } finally {
                IOUtils.closeQuietly(bout);
                IOUtils.closeQuietly(out);
              }
            }
          },
          null,
          listener);
    }
  }
示例#3
0
 /**
  * Uploads the data. Does not do any file syncronisation
  *
  * @param newUri - encoded full URL
  * @param content
  * @param contentLength
  * @param contentType
  * @return - the result code
  */
 public synchronized int doPut(
     String newUri,
     InputStream content,
     Long contentLength,
     String contentType,
     ProgressListener listener) {
   LogUtils.trace(log, "doPut", newUri);
   return transferService.put(newUri, content, contentLength, contentType, listener);
 }
示例#4
0
 /** @param timeout the timeout to set */
 public void setTimeout(int timeout) {
   this.timeout = timeout;
   transferService.setTimeout(timeout);
 }
示例#5
0
 /**
  * @param url - fully qualified and encoded URL
  * @param receiver
  * @param rangeList - if null does a normal GET request
  * @throws com.ettrema.httpclient.HttpException
  * @throws com.ettrema.httpclient.Utils.CancelledException
  */
 public synchronized void doGet(
     String url, StreamReceiver receiver, List<Range> rangeList, ProgressListener listener)
     throws com.ettrema.httpclient.HttpException, Utils.CancelledException, NotAuthorizedException,
         BadRequestException, ConflictException, NotFoundException {
   transferService.get(url, receiver, rangeList, listener);
 }
示例#6
0
  public Host(
      String server,
      String rootPath,
      Integer port,
      String user,
      String password,
      ProxyDetails proxyDetails,
      int timeoutMillis,
      Cache<Folder, List<Resource>> cache,
      FileSyncer fileSyncer) {
    super(
        (cache != null
            ? cache
            : new MemoryCache<Folder, List<Resource>>("resource-cache-default", 50, 20)));
    if (server == null) {
      throw new IllegalArgumentException("host name cannot be null");
    }
    this.rootPath = rootPath;
    this.timeout = timeoutMillis;
    this.server = server;
    this.port = port;
    this.user = user;
    this.password = password;
    client = new DefaultHttpClient();
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 10000);
    HttpConnectionParams.setSoTimeout(params, 10000);

    if (user != null) {
      client
          .getCredentialsProvider()
          .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
      PreemptiveAuthInterceptor interceptor = new PreemptiveAuthInterceptor();
      client.addRequestInterceptor(interceptor);
    }

    HttpRequestRetryHandler handler = new DefaultHttpRequestRetryHandler(0, false);
    client.setHttpRequestRetryHandler(handler);

    if (proxyDetails != null) {
      if (proxyDetails.isUseSystemProxy()) {
        System.setProperty("java.net.useSystemProxies", "true");
      } else {
        System.setProperty("java.net.useSystemProxies", "false");
        if (proxyDetails.getProxyHost() != null && proxyDetails.getProxyHost().length() > 0) {
          HttpHost proxy =
              new HttpHost(proxyDetails.getProxyHost(), proxyDetails.getProxyPort(), "http");
          client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
          if (proxyDetails.hasAuth()) {
            client
                .getCredentialsProvider()
                .setCredentials(
                    new AuthScope(proxyDetails.getProxyHost(), proxyDetails.getProxyPort()),
                    new UsernamePasswordCredentials(
                        proxyDetails.getUserName(), proxyDetails.getPassword()));
          }
        }
      }
    }
    transferService = new TransferService(client, connectionListeners);
    transferService.setTimeout(timeoutMillis);
    this.fileSyncer = fileSyncer;
  }