/*
 * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.analytics.apim.idp.client;

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceIdentityOAuthAdminException;
import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub;
import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO;

import java.rmi.RemoteException;

import static org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING;
import static org.wso2.analytics.apim.idp.client.ApimIdPClientConstants.OAUTH_ADMIN_SERVICE_ENDPOINT_POSTFIX;

/**
 * Client for the OAuth Admin Service.
 */
public class OAuthAdminServiceClient {
  private OAuthAdminServiceStub oAuthAdminServiceStub;

  public OAuthAdminServiceClient(String adminServiceBaseUrl, String sessionCookie) throws AxisFault {
    String endPoint = adminServiceBaseUrl + OAUTH_ADMIN_SERVICE_ENDPOINT_POSTFIX;
    oAuthAdminServiceStub = new OAuthAdminServiceStub(endPoint);

    ServiceClient serviceClient = oAuthAdminServiceStub._getServiceClient();
    Options option = serviceClient.getOptions();
    option.setManageSession(true); 
    option.setProperty(COOKIE_STRING, sessionCookie);
  } 
   
  public OAuthConsumerAppDTO[] getAllOAuthApplicationData() throws RemoteException,
          OAuthAdminServiceIdentityOAuthAdminException {
    return oAuthAdminServiceStub.getAllOAuthApplicationData();
  }

  public OAuthConsumerAppDTO getOAuthApplicationDataByAppName(String oAuthAppName) throws RemoteException,
          OAuthAdminServiceIdentityOAuthAdminException {
    return oAuthAdminServiceStub.getOAuthApplicationDataByAppName(oAuthAppName);
  }
}

@@ -833,16 +833,61 @@
 Client;%0A
+import org.apache.commons.httpclient.Header;%0A
 import o
@@ -1118,27 +1118,122 @@
 ava.
-rmi.RemoteException
+nio.charset.StandardCharsets;%0Aimport java.rmi.RemoteException;%0Aimport java.util.ArrayList;%0Aimport java.util.Base64
 ;%0A%0Ai
@@ -1619,16 +1619,107 @@
 BaseUrl,
+ String adminServiceUsername, String adminServicePassword,%0A                                
  String 
@@ -2060,17 +2060,473 @@
 n(true);
- 
+%0A    String userNamePassword = adminServiceUsername + %22:%22 + adminServicePassword;%0A    String encodedString = Base64.getEncoder().encodeToString(userNamePassword.getBytes(StandardCharsets.UTF_8));%0A    String authorizationHeader = %22Basic %22 + encodedString;%0A    ArrayList%3CHeader%3E headers = new ArrayList%3C%3E();%0A    Header authHeader = new Header(%22Authorization%22, authorizationHeader);%0A    headers.add(authHeader);%0A    option.setProperty(%22HTTP_HEADERS%22, headers);
 %0A    opt

Fix session affinity related issues.