/*
 * Druid - a distributed column store.
 * Copyright (C) 2012  Metamarkets Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.metamx.druid.common.s3;

import com.google.common.base.Throwables;
import com.metamx.common.logger.Logger;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.concurrent.Callable;

/**
 *
 */
public class S3Utils
{
  private static final Logger log = new Logger(S3Utils.class);

  public static void putFileToS3(
      File localFile, RestS3Service s3Client, String outputS3Bucket, String outputS3Path
  )
      throws S3ServiceException, IOException, NoSuchAlgorithmException
  {
    S3Object s3Obj = new S3Object(localFile);
    s3Obj.setBucketName(outputS3Bucket);
    s3Obj.setKey(outputS3Path);

    log.info("Uploading file[%s] to [s3://%s/%s]", localFile, s3Obj.getBucketName(), s3Obj.getKey());
    s3Client.putObject(new S3Bucket(outputS3Bucket), s3Obj);
  }

  public static void putFileToS3WrapExceptions(
      File localFile, RestS3Service s3Client, String outputS3Bucket, String outputS3Path
  )
  {
    try {
      putFileToS3(localFile, s3Client, outputS3Bucket, outputS3Path);
    }
    catch (S3ServiceException e) {
      throw new RuntimeException(e);
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
    catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }

  public static void closeStreamsQuietly(S3Object s3Obj)
  {
    if (s3Obj == null) {
      return;
    }

    try {
      s3Obj.closeDataInputStream();
    }
    catch (IOException e) {

    }
  }

  /**
   * Retries S3 operations that fail due to io-related exceptions. Service-level exceptions (access denied, file not
   * found, etc) are not retried.
   */
  public static <T> T retryS3Operation(Callable<T> f) throws ServiceException, InterruptedException
  {
    int nTry = 0;
    final int maxTries = 3;
    final long baseSleepMillis = 1000;
    final double fuzziness = 0.2;
    while (true) {
      try {
        nTry++;
        return f.call();
      }
      catch (ServiceException e) {
        if (nTry <= maxTries &&
            (e.getCause() instanceof IOException ||
             (e.getErrorCode() != null && e.getErrorCode().equals("RequestTimeout")))) {
          // Retryable
          final long sleepMillis = Math.max(
              baseSleepMillis,
              (long) (baseSleepMillis * Math.pow(2, nTry) *
                      (1 + new Random().nextGaussian() * fuzziness))
          );
          log.info(e, "S3 fail on try %d/%d, retrying in %,dms.", nTry, maxTries, sleepMillis);
          Thread.sleep(sleepMillis);
        } else {
          throw e;
        }
      }
      catch (Exception e) {
        throw Throwables.propagate(e);
      }
    }
  }
}

@@ -1426,967 +1426,8 @@
 );%0A%0A
-  public static void putFileToS3(%0A      File localFile, RestS3Service s3Client, String outputS3Bucket, String outputS3Path%0A  )%0A      throws S3ServiceException, IOException, NoSuchAlgorithmException%0A  %7B%0A    S3Object s3Obj = new S3Object(localFile);%0A    s3Obj.setBucketName(outputS3Bucket);%0A    s3Obj.setKey(outputS3Path);%0A%0A    log.info(%22Uploading file%5B%25s%5D to %5Bs3://%25s/%25s%5D%22, localFile, s3Obj.getBucketName(), s3Obj.getKey());%0A    s3Client.putObject(new S3Bucket(outputS3Bucket), s3Obj);%0A  %7D%0A%0A  public static void putFileToS3WrapExceptions(%0A      File localFile, RestS3Service s3Client, String outputS3Bucket, String outputS3Path%0A  )%0A  %7B%0A    try %7B%0A      putFileToS3(localFile, s3Client, outputS3Bucket, outputS3Path);%0A    %7D%0A    catch (S3ServiceException e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A    catch (IOException e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A    catch (NoSuchAlgorithmException e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A  %7D%0A%0A
   pu

Remove unused methods from S3Utils