package snove.seo.redirectcheck.http;


import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import snove.seo.redirectcheck.domain.HttpRequest;
import snove.seo.redirectcheck.model.HttpResponse;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;


public final class DefaultHttpRequest implements HttpRequest {

    private final URI uri;
    private final HttpMethod method;

    public DefaultHttpRequest(URI uri, HttpMethod method) {
        this.uri = uri;
        this.method = method;
    }


    @Override
    public HttpResponse execute() throws IOException, URISyntaxException {


        HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();

        connection.setRequestMethod(method.name());
        connection.setInstanceFollowRedirects(false);
        connection.connect();


        URI dstURI = uri;

        int status = connection.getResponseCode();
        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_MOVED_TEMP
                    || status == HttpURLConnection.HTTP_MOVED_PERM
                    || status == HttpURLConnection.HTTP_SEE_OTHER) {
                URI location = new URI(encodeURIComponent(connection.getHeaderField("location")));

                if (location.isAbsolute()) {
                    dstURI = location;
                } else {
                    dstURI = dstURI.resolve(location);
                }
            }
        }


        return new HttpResponse(HttpStatus.valueOf(connection.getResponseCode()), dstURI);
    }

    private String encodeURIComponent(String s) {

        return s
                .replaceAll("\\s", "%20")
                .replaceAll("\\%21", "!")
                .replaceAll("\\%27", "'")
                .replaceAll("\\%28", "(")
                .replaceAll("\\%29", ")")
                .replaceAll("\\%7E", "~");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DefaultHttpRequest that = (DefaultHttpRequest) o;

        if (uri != null ? !uri.equals(that.uri) : that.uri != null) return false;
        return method == that.method;

    }

    @Override
    public int hashCode() {
        int result = uri != null ? uri.hashCode() : 0;
        result = 31 * result + (method != null ? method.hashCode() : 0);
        return result;
    }
}

@@ -592,25 +592,24 @@
 hod;%0A    %7D%0A%0A
-%0A
     @Overrid
@@ -686,17 +686,16 @@
 tion %7B%0A%0A
-%0A
         
@@ -913,17 +913,16 @@
 ect();%0A%0A
-%0A
         
@@ -1694,17 +1694,16 @@
 ng s) %7B%0A
-%0A
         
@@ -1710,33 +1710,16 @@
 return s
-%0A                
 .replace
@@ -1727,18 +1727,18 @@
 ll(%22
-%5C%5Cs
+%2520
 %22, %22
-%2520
+%5C%5Cs
 %22)%0A 
@@ -1765,18 +1765,16 @@
 aceAll(%22
-%5C%5C
 %2521%22, %22!
@@ -1805,18 +1805,16 @@
 aceAll(%22
-%5C%5C
 %2527%22, %22'
@@ -1845,18 +1845,16 @@
 aceAll(%22
-%5C%5C
 %2528%22, %22(
@@ -1885,18 +1885,16 @@
 aceAll(%22
-%5C%5C
 %2529%22, %22)
@@ -1925,18 +1925,16 @@
 aceAll(%22
-%5C%5C
 %257E%22, %22~

removed redundant characters