/**
 * Copyright (c) Anton Johansson <antoon.johansson@gmail.com>
 *
 * 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 com.antonjohansson.vaadin.recaptcha;

import static java.net.URLEncoder.encode;
import static java.util.Objects.requireNonNull;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Verifies reCAPTCHA responses.
 */
class RecaptchaVerifier
{
    private final String secretKey;
    private final String response;
    private final String verifyURL;
    private final String remoteAddress;

    /**
     * Constructs a new {@link RecaptchaVerifier}.
     *
     * @param secretKey The secret key to use when verifying.
     * @param response The received response to verify.
     * @param verifyURL The URL to use when verifying.
     */
    RecaptchaVerifier(String secretKey, String response, String verifyURL)
    {
        this(secretKey, response, verifyURL, "");
    }

    /**
     * Constructs a new {@link RecaptchaVerifier}.
     *
     * @param secretKey The secret key to use when verifying.
     * @param response The received response to verify.
     * @param verifyURL The URL to use when verifying.
     * @param remoteAddress The remote address of the request to verify.
     */
    RecaptchaVerifier(String secretKey, String response, String verifyURL, String remoteAddress)
    {
        this.secretKey = requireNonNull(secretKey);
        this.response = requireNonNull(response);
        this.verifyURL = requireNonNull(verifyURL);
        this.remoteAddress = requireNonNull(remoteAddress);
    }

    /**
     * Gets whether or not this request is verified.
     *
     * @return Returns {@code true} if this request is verified.
     */
    boolean isVerified()
    {
        String json = getJSON();
        return json.contains("\"success\": true");
    }

    private String getJSON()
    {
        HttpURLConnection connection = null;
        try
        {
            String parameters = getParameters();

            URL url = new URL(verifyURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", Integer.toString(parameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");
            connection.setUseCaches(false);
            connection.setDoOutput(true);

            DataOutputStream output = new DataOutputStream(connection.getOutputStream());
            output.writeBytes(parameters);
            output.close();

            InputStream input = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuilder httpResponse = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null)
            {
                httpResponse.append(line);
                httpResponse.append('\r');
            }
            reader.close();
            return httpResponse.toString();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "{}";
        }
        finally
        {
            if (connection != null)
            {
                connection.disconnect();
            }
        }
    }

    private String getParameters() throws UnsupportedEncodingException
    {
        StringBuilder parameters = new StringBuilder()
                .append("secret=")
                .append(encode(secretKey, "UTF-8"))
                .append("&response=")
                .append(encode(response, "UTF-8"));

        if (remoteAddress.length() > 0)
        {
            parameters
                    .append("&remoteip=")
                    .append(encode(remoteAddress, "UTF-8"));
        }

        return parameters.toString();
    }
}

@@ -1205,393 +1205,8 @@
 s;%0A%0A
-    /**%0A     * Constructs a new %7B@link RecaptchaVerifier%7D.%0A     *%0A     * @param secretKey The secret key to use when verifying.%0A     * @param response The received response to verify.%0A     * @param verifyURL The URL to use when verifying.%0A     */%0A    RecaptchaVerifier(String secretKey, String response, String verifyURL)%0A    %7B%0A        this(secretKey, response, verifyURL, %22%22);%0A    %7D%0A%0A
     
@@ -3772,34 +3772,14 @@
     
-StringBuilder parameters =
+return
  new
@@ -3974,88 +3974,9 @@
 8%22))
-;%0A
 %0A
-        if (remoteAddress.length() %3E 0)%0A        %7B%0A            parameters%0A    
     
@@ -4005,28 +4005,24 @@
 remoteip=%22)%0A
-    
             
@@ -4064,17 +4064,16 @@
 UTF-8%22))
-;
 %0A       
@@ -4077,19 +4077,16 @@
     
-%7D%0A%0A
         
 retu
@@ -4085,25 +4085,8 @@
     
-return parameters
 .toS

Remove redundant code