/*
 * 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 org.apache.gobblin.service;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Base64;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.linkedin.restli.server.resources.BaseResource;
import com.typesafe.config.Config;

/**
 * Use this class to get who sends the request.
 */
public abstract class RequesterService {

  protected Config sysConfig;

  public RequesterService(Config config) {
    sysConfig = config;
  }

  public static final String REQUESTER_LIST = "gobblin.service.requester.list";

  private static final ObjectMapper objectMapper = new ObjectMapper();

  /**
   * <p> This implementation converts a given list to a json string.
   * Due to json string may have reserved keyword that can confuse
   * {@link Config}, we first use Base64 to encode the json string,
   * then use URL encoding to remove characters like '+,/,='.
   */
  public String serialize(List<ServiceRequester> requestersList) throws IOException {
    String arrayToJson = objectMapper.writeValueAsString(requestersList);
    String encodedString = Base64.getEncoder().encodeToString(arrayToJson.getBytes("UTF-8"));
    return URLEncoder.encode(encodedString, "UTF-8");
  }

  /**
   * <p> This implementation decode a given string encoded by
   * {@link #serialize(List)}.
   */
  public List<ServiceRequester> deserialize(String encodedString) throws IOException {
    String urlDecoded = URLDecoder.decode(encodedString, "UTF-8");
    byte[] decodedBytes = Base64.getDecoder().decode(urlDecoded);
    String serialized = new String(decodedBytes, "UTF-8");
    TypeReference<List<ServiceRequester>> mapType = new TypeReference<List<ServiceRequester>>() {};
    List<ServiceRequester> jsonToPersonList = objectMapper.readValue(serialized, mapType);
    return jsonToPersonList;
  }

  protected abstract List<ServiceRequester> findRequesters(BaseResource resource);
}

@@ -1793,16 +1793,23 @@
  public 
+static 
 String s
@@ -1845,25 +1845,24 @@
 r%3E requester
-s
 List) throws
@@ -1887,27 +1887,24 @@
  String 
-arrayToJson
+jsonList
  = objec
@@ -1939,17 +1939,16 @@
 equester
-s
 List);%0A 
@@ -1957,29 +1957,25 @@
  String 
-encodedString
+base64Str
  = Base6
@@ -2008,19 +2008,16 @@
 ing(
-arrayToJson
+jsonList
 .get
@@ -2058,37 +2058,33 @@
 oder.encode(
-encodedString
+base64Str
 , %22UTF-8%22);%0A
@@ -2202,16 +2202,23 @@
  public 
+static 
 List%3CSer
@@ -2298,26 +2298,25 @@
  String 
-urlDecoded
+base64Str
  = URLDe
@@ -2410,18 +2410,17 @@
 ode(
-urlDecoded
+base64Str
 );%0A 
@@ -2429,26 +2429,24 @@
  String 
-serialized
+jsonList
  = new S
@@ -2602,28 +2602,25 @@
 uester%3E 
-jsonToPerson
+requester
 List = o
@@ -2645,18 +2645,16 @@
 lue(
-serialized
+jsonList
 , ma
@@ -2676,20 +2676,17 @@
 urn 
-jsonToPerson
+requester
 List

Change method to static for RequesterService serder