// Path: src/main/java/com/wandisco/s3hdfs/rewrite/wrapper/RequestStreamWrapper.java
// public class RequestStreamWrapper extends ServletInputStream {
// 
//   private final InputStream _in;
// 
//   public RequestStreamWrapper(InputStream realStream) {
//     this._in = realStream;
//   }
// 
//   @Override
//   public int read() throws IOException {
//     return _in.read();
//   }
// 
//   @Override
//   public int read(byte[] bytes) throws IOException {
//     return _in.read(bytes);
//   }
// 
//   @Override
//   public int read(byte[] bytes, int i, int i2) throws IOException {
//     return _in.read(bytes, i, i2);
//   }
// 
//   @Override
//   public long skip(long l) throws IOException {
//     return _in.skip(l);
//   }
// 
//   @Override
//   public int available() throws IOException {
//     return _in.available();
//   }
// 
//   @Override
//   public void close() throws IOException {
//     _in.close();
//   }
// 
//   @Override
//   public synchronized void mark(int i) {
//     _in.mark(i);
//   }
// 
//   @Override
//   public synchronized void reset() throws IOException {
//     _in.reset();
//   }
// 
//   @Override
//   public boolean markSupported() {
//     return _in.markSupported();
//   }
// 
//   @Override
//   public String toString() {
//     return _in.toString();
//   }
// }
// 
// Path: src/main/java/com/wandisco/s3hdfs/rewrite/wrapper/S3HdfsRequestWrapper.java
// public class S3HdfsRequestWrapper extends HttpServletRequestWrapper {
// 
//   private ServletInputStream inputStream;
// 
//   /**
//    * Constructs a response adaptor wrapping the given response.
//    *
//    * @throws IllegalArgumentException if the response is null
//    */
//   public S3HdfsRequestWrapper(HttpServletRequest request) {
//     super(request);
//   }
// 
//   @Override
//   public ServletInputStream getInputStream() throws IOException {
//     if (inputStream != null)
//       return inputStream;
//     return getRequest().getInputStream();
//   }
// 
//   public void setInputStream(ServletInputStream is) {
//     inputStream = is;
//   }
// 
//   @Override
//   public String toString() {
//     return getRequest().toString();
//   }
// }


import com.wandisco.s3hdfs.rewrite.wrapper.RequestStreamWrapper;
import com.wandisco.s3hdfs.rewrite.wrapper.S3HdfsRequestWrapper;
import org.apache.commons.httpclient.methods.GetMethod;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static com.wandisco.s3hdfs.conf.S3HdfsConstants.HTTP_METHOD.GET;
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wandisco.s3hdfs.rewrite.redirect;




public class CopyFileRedirect extends Redirect {

  public CopyFileRedirect(HttpServletRequest request,
                          HttpServletResponse response) {
    super(request, response, null);
    LOG.debug("Created " + getClass().getSimpleName() + ".");
  }

  /**
   * Sends a PUT command to create the container directory inside of HDFS.
   * It uses the URL from the original request to do so.
   *
   * @param nameNodeHttpHost
   * @param userName
   * @throws IOException
   * @throws ServletException
   */
  public void sendCopy(String nameNodeHttpHost, String userName,
                       String srcBucket, String srcObject)
      throws IOException, ServletException {
    // Set up HttpGet and get original file
    String uri = replaceSrcs(request.getRequestURI(), srcBucket, srcObject);
    String[] nnHost = nameNodeHttpHost.split(":");

    GetMethod httpGet =
        (GetMethod) getHttpMethod(request.getScheme(), nnHost[0],
            Integer.decode(nnHost[1]), "OPEN", userName, uri, GET);

    // Try up to 5 times to get the source file
    httpClient.executeMethod(httpGet);
    LOG.debug("1st response: " + httpGet.getStatusLine().toString());

    for (int i = 0; i < 5 && httpGet.getStatusCode() == 403; i++) {
      httpGet.releaseConnection();
      httpClient.executeMethod(httpGet);
      LOG.debug("Next response: " + httpGet.getStatusLine().toString());
    }

    assert httpGet.getStatusCode() == 200;
    assert request instanceof S3HdfsRequestWrapper;
// Path: src/main/java/com/wandisco/s3hdfs/rewrite/wrapper/RequestStreamWrapper.java
// public class RequestStreamWrapper extends ServletInputStream {
// 
//   private final InputStream _in;
// 
//   public RequestStreamWrapper(InputStream realStream) {
//     this._in = realStream;
//   }
// 
//   @Override
//   public int read() throws IOException {
//     return _in.read();
//   }
// 
//   @Override
//   public int read(byte[] bytes) throws IOException {
//     return _in.read(bytes);
//   }
// 
//   @Override
//   public int read(byte[] bytes, int i, int i2) throws IOException {
//     return _in.read(bytes, i, i2);
//   }
// 
//   @Override
//   public long skip(long l) throws IOException {
//     return _in.skip(l);
//   }
// 
//   @Override
//   public int available() throws IOException {
//     return _in.available();
//   }
// 
//   @Override
//   public void close() throws IOException {
//     _in.close();
//   }
// 
//   @Override
//   public synchronized void mark(int i) {
//     _in.mark(i);
//   }
// 
//   @Override
//   public synchronized void reset() throws IOException {
//     _in.reset();
//   }
// 
//   @Override
//   public boolean markSupported() {
//     return _in.markSupported();
//   }
// 
//   @Override
//   public String toString() {
//     return _in.toString();
//   }
// }
// 
// Path: src/main/java/com/wandisco/s3hdfs/rewrite/wrapper/S3HdfsRequestWrapper.java
// public class S3HdfsRequestWrapper extends HttpServletRequestWrapper {
// 
//   private ServletInputStream inputStream;
// 
//   /**
//    * Constructs a response adaptor wrapping the given response.
//    *
//    * @throws IllegalArgumentException if the response is null
//    */
//   public S3HdfsRequestWrapper(HttpServletRequest request) {
//     super(request);
//   }
// 
//   @Override
//   public ServletInputStream getInputStream() throws IOException {
//     if (inputStream != null)
//       return inputStream;
//     return getRequest().getInputStream();
//   }
// 
//   public void setInputStream(ServletInputStream is) {
//     inputStream = is;
//   }
// 
//   @Override
//   public String toString() {
//     return getRequest().toString();
//   }
// }


// Path: src/main/java/com/wandisco/s3hdfs/rewrite/redirect/CopyFileRedirect.java
import com.wandisco.s3hdfs.rewrite.wrapper.RequestStreamWrapper;
import com.wandisco.s3hdfs.rewrite.wrapper.S3HdfsRequestWrapper;
import org.apache.commons.httpclient.methods.GetMethod;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static com.wandisco.s3hdfs.conf.S3HdfsConstants.HTTP_METHOD.GET;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wandisco.s3hdfs.rewrite.redirect;




public class CopyFileRedirect extends Redirect {

  public CopyFileRedirect(HttpServletRequest request,
                          HttpServletResponse response) {
    super(request, response, null);
    LOG.debug("Created " + getClass().getSimpleName() + ".");
  }

  /**
   * Sends a PUT command to create the container directory inside of HDFS.
   * It uses the URL from the original request to do so.
   *
   * @param nameNodeHttpHost
   * @param userName
   * @throws IOException
   * @throws ServletException
   */
  public void sendCopy(String nameNodeHttpHost, String userName,
                       String srcBucket, String srcObject)
      throws IOException, ServletException {
    // Set up HttpGet and get original file
    String uri = replaceSrcs(request.getRequestURI(), srcBucket, srcObject);
    String[] nnHost = nameNodeHttpHost.split(":");

    GetMethod httpGet =
        (GetMethod) getHttpMethod(request.getScheme(), nnHost[0],
            Integer.decode(nnHost[1]), "OPEN", userName, uri, GET);

    // Try up to 5 times to get the source file
    httpClient.executeMethod(httpGet);
    LOG.debug("1st response: " + httpGet.getStatusLine().toString());

    for (int i = 0; i < 5 && httpGet.getStatusCode() == 403; i++) {
      httpGet.releaseConnection();
      httpClient.executeMethod(httpGet);
      LOG.debug("Next response: " + httpGet.getStatusLine().toString());
    }

    assert httpGet.getStatusCode() == 200;
    assert request instanceof S3HdfsRequestWrapper;
    ((S3HdfsRequestWrapper) request).setInputStream(