// Copyright 2020 Google LLC
//
// 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
//
//     https://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.google.step.YOUR_PROJECT_NAME_HERE.servlets;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

/** Servlet that handles searches. */
@WebServlet("/search")
public class SearchServlet extends HttpServlet {

  private final HttpClient httpClient =
      HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build();

  private static String encodeValue(String value) {
    try {
      return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException ex) {
      throw new RuntimeException(ex.getCause());
    }
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    String query = encodeValue(request.getParameter("q"));
    List<String> allLinks = new ArrayList<>();

    String w3GoogleCSE =
        "https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:pat2rmwjvb8&q=";

    String stackGoogleCSE =
        "https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:6b8rs1ze5oy&q=";

    String geeksGoogleCSE =
        "https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:ieq7z84z2qq&q=";

    try {
      String w3Link = getLink(w3GoogleCSE + query);
      allLinks.add(w3Link);
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      String stackLink = getLink(stackGoogleCSE + query);
      allLinks.add(stackLink);
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      String geeksLink = getLink(geeksGoogleCSE + query);
      allLinks.add(geeksLink);
    } catch (Exception e) {
      e.printStackTrace();
    }

    response.setContentType("text/html;");
    response.getWriter().println(allLinks);
  }

  private String getLink(String cse) throws Exception {

    HttpRequest w3LinkRequest =
        HttpRequest.newBuilder()
            .GET()
            .uri(URI.create(cse))
            .setHeader("User-Agent", "Java 11 HttpClient Bot")
            .build();

    try {
      HttpResponse<String> w3LinkResponse =
          httpClient.send(w3LinkRequest, HttpResponse.BodyHandlers.ofString());

      JSONObject obj = new JSONObject(w3LinkResponse.body());
      String link = obj.getJSONArray("items").getJSONObject(0).getString("link");

      return link;

    } catch (Exception e) {
      e.printStackTrace();
      throw new Exception("Link not found!");
    }
  }
}

@@ -1312,16 +1312,550 @@
 vlet %7B%0A%0A
+  private static final String W3_CSE =%0A      %22https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:pat2rmwjvb8&q=%22;%0A%0A  private static final String STACK_CSE =%0A      %22https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:6b8rs1ze5oy&q=%22;%0A%0A  private static final String GEEKS_CSE =%0A      %22https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:ieq7z84z2qq&q=%22;%0A%0A
   privat
@@ -2452,506 +2452,8 @@
 );%0A%0A
-    String w3GoogleCSE =%0A        %22https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:pat2rmwjvb8&q=%22;%0A%0A    String stackGoogleCSE =%0A        %22https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:6b8rs1ze5oy&q=%22;%0A%0A    String geeksGoogleCSE =%0A        %22https://www.googleapis.com/customsearch/v1?key=AIzaSyCSKQy6t7HgEc1zI2thwgGncFVzyB8zZ00&cx=010448377421452380243:ieq7z84z2qq&q=%22;%0A%0A
     
@@ -2492,16 +2492,11 @@
 ink(
-w3Google
+W3_
 CSE 
@@ -2642,19 +2642,14 @@
 ink(
-stackGoogle
+STACK_
 CSE 
@@ -2798,19 +2798,14 @@
 ink(
-geeksGoogle
+GEEKS_
 CSE 
@@ -3031,19 +3031,19 @@
 (String 
-cse
+CSE
 ) throws
@@ -3172,11 +3172,11 @@
 ate(
-cse
+CSE
 ))%0A 
@@ -3394,17 +3394,16 @@
 ing());%0A
-%0A
       JS
@@ -3538,17 +3538,16 @@
 link%22);%0A
-%0A
       re
@@ -3557,17 +3557,16 @@
 n link;%0A
-%0A
     %7D ca

Make CSE variables global, delete extra lines