/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed 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 io.groundhog.proxy;

import io.groundhog.capture.CaptureWriter;
import io.groundhog.capture.CaptureRequest;
import io.groundhog.capture.DefaultHttpCaptureDecoder;
import io.groundhog.capture.HttpCaptureDecoder;

import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import org.littleshoot.proxy.HttpFilters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * A capturing {@link HttpFilters}.
 *
 * @author Danny Thomas
 * @since 1.0
 */
public class CaptureHttpFilter implements HttpFilters {
  private static final Logger LOG = LoggerFactory.getLogger(CaptureHttpFilter.class);

  private final HttpCaptureDecoder captureDecoder;
  private final CaptureWriter captureWriter;

  public CaptureHttpFilter(CaptureWriter captureWriter, File uploadLocation) {
    captureDecoder = new DefaultHttpCaptureDecoder(uploadLocation);
    this.captureWriter = checkNotNull(captureWriter);
  }

  @Override
  public HttpResponse requestPre(HttpObject httpObject) {
    checkNotNull(httpObject);
    try {
      captureDecoder.request(httpObject);
    } catch (Exception e) {
      LOG.error("Failed to capture request", e);
    }

    if (httpObject instanceof HttpRequest) {
      rewriteUri((HttpRequest) httpObject);
    }
    return null;
  }

  @Override
  public HttpResponse requestPost(HttpObject httpObject) {
    checkNotNull(httpObject);
    return null;
  }

  @Override
  public HttpObject responsePre(HttpObject httpObject) {
    checkNotNull(httpObject);
    captureDecoder.response(httpObject);
    return httpObject;
  }

  @Override
  public HttpObject responsePost(HttpObject httpObject) {
    checkNotNull(httpObject);
    try {
      CaptureRequest captureRequest = captureDecoder.complete();
      captureWriter.writeAsync(captureRequest);
    } finally {
      captureDecoder.destroy();
    }
    return httpObject;
  }

  void rewriteUri(HttpRequest request) {
    if (!request.getUri().contains("://")) {
      request.setUri("http://localhost:8080" + request.getUri());
    }
  }
}

@@ -674,22 +674,23 @@
 .Capture
-Writer
+Request
 ;%0Aimport
@@ -710,39 +710,38 @@
 .capture.Capture
-Request
+Writer
 ;%0Aimport io.grou
@@ -974,16 +974,68 @@
 sponse;%0A
+import io.netty.handler.codec.http.LastHttpContent;%0A
 import o
@@ -2316,24 +2316,36 @@
 ttpObject);%0A
+    try %7B%0A  
     captureD
@@ -2365,32 +2365,116 @@
 se(httpObject);%0A
+    %7D catch (Exception e) %7B%0A      LOG.error(%22Failed to capture response%22, e);%0A    %7D%0A
     return httpO
@@ -2577,32 +2577,83 @@
 ll(httpObject);%0A
+    if (httpObject instanceof LastHttpContent) %7B%0A  
     try %7B%0A      
@@ -2642,24 +2642,26 @@
       try %7B%0A
+  
       Captur
@@ -2715,24 +2715,26 @@
 te();%0A      
+  
 captureWrite
@@ -2759,24 +2759,118 @@
 reRequest);%0A
+      %7D catch (Exception e) %7B%0A        LOG.error(%22Failed to complete and write request%22, e);%0A  
     %7D finall
@@ -2869,24 +2869,26 @@
 %7D finally %7B%0A
+  
       captur
@@ -2903,24 +2903,32 @@
 .destroy();%0A
+      %7D%0A
     %7D%0A    re

Fix problem with proxy capture caused by multiple destroy calls. Wrap capture logic in try/catch blocks to make sure problems with capture don't affect requests