Skip to content

Commit

Permalink
Add session information to the output Json
Browse files Browse the repository at this point in the history
Add session information to the output Json when running the tool with --published-session-uid.
The option will be supported only for customers with specific HF installed
  • Loading branch information
chkp-yaelg authored Dec 25, 2018
1 parent dde68aa commit d7a7612
Showing 1 changed file with 100 additions and 22 deletions.
122 changes: 100 additions & 22 deletions src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
package com.checkpoint.mgmt_api.examples;

import com.checkpoint.mgmt_api.client.*;
import com.checkpoint.mgmt_api.objects.GatewayAndServer;
import com.checkpoint.mgmt_api.objects.IndexView;
import com.checkpoint.mgmt_api.objects.Layer;
import com.checkpoint.mgmt_api.objects.PolicyPackage;
import com.checkpoint.mgmt_api.objects.*;
import com.checkpoint.mgmt_api.utils.TarGZUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
Expand Down Expand Up @@ -170,24 +167,7 @@ else if(!loginResponse.isSuccess()){

IndexView index = new IndexView();

/*Switching to published session*/
if(configuration.getPublishedSessionUid() != null) {
ApiResponse res = null;
try {
res = client.apiCall(loginResponse, "switch-session",
"{\"uid\" : \""+ configuration.getPublishedSessionUid() +"\"}");
}
catch (ApiClientException e) {
logoutReportAndExit("An error occurred while switching to the published session. Exception: "+ e.getMessage(), MessageType.SEVERE);
}
if (res == null || !res.isSuccess()) {
logoutReportAndExit("Failed to switch published session uid "+ configuration.getPublishedSessionUid() +". " + errorResponseToString(res), MessageType.SEVERE);
}
index.setPublishedSessionUid(configuration.getPublishedSessionUid());
}
else{
index.setPublishedSessionUid("Last published session");
}
handlePublishedSession(index);

/*Update the index page data*/
index.setDomain(configuration.getDomain());
Expand All @@ -207,6 +187,104 @@ else if(!loginResponse.isSuccess()){
logoutReportAndExit("", MessageType.EXIT_WITHOUT_MESSAGE);
}

private static void handlePublishedSession(IndexView index)
{
Session session = new Session();
JSONObject sessionPayload = new JSONObject();
ApiResponse sessionRes = null;

//get last published session
try {
sessionRes = client.apiCall(loginResponse, "show-last-published-session", "{}");
}
catch (ApiClientException e) {
logoutReportAndExit("Failed to show last published session. Exception: "+ e.getMessage(), MessageType.SEVERE);
}
if (sessionRes == null || !sessionRes.isSuccess()) {
//print error: if client asked to switch session in management with no published sessions
if (configuration.getPublishedSessionUid() != null) {
logoutReportAndExit(errorResponseToString(
sessionRes) + " - could not switch to the required session: " + configuration.getPublishedSessionUid(),
MessageType.SEVERE);
}
//print info: no published sessions in the management
else {
configuration.getLogger().info(errorResponseToString(sessionRes));
}
}
else {
//check if the given id is the last published session and set uid
if (configuration.getPublishedSessionUid() != null) {
session.setUid(configuration.getPublishedSessionUid());

if (sessionRes.getPayload().get("uid").toString().equals(configuration.getPublishedSessionUid())) {
session.setLastPublishedSession(true);
}
else {
session.setLastPublishedSession(false);
}
}
else {
session.setUid(sessionRes.getPayload().get("uid").toString());
session.setLastPublishedSession(true);
}

//if not the last published session - get the published session
if (!session.getLastPublishedSession()) {
sessionPayload.put("uid", session.getUid());
try {
sessionRes = client.apiCall(loginResponse, "show-session", sessionPayload);
}
catch (ApiClientException e) {
logoutReportAndExit(
"Failed to show the session" + session.getUid() + ". Exception: " + e.getMessage(),
MessageType.SEVERE);
}
if (sessionRes == null || !sessionRes.isSuccess()) {
logoutReportAndExit(
"Failed to show the session" + session.getUid() + ". Exception: " + errorResponseToString(
sessionRes), MessageType.SEVERE);
}
}

//get name and published time of the published session
Object sessionName = sessionRes.getPayload().get("name");
Object publishTime = sessionRes.getPayload().get("publish-time");

if(sessionName != null)
session.setName(sessionName.toString());
else
session.setName("");

if(publishTime != null)
session.setPublishTime(((HashMap)publishTime).get("iso-8601").toString());
else
logoutReportAndExit("Input published session is not published - ", MessageType.SEVERE);

//Switching to published session
ApiResponse switchSessionRes = null;
if (configuration.getPublishedSessionUid() != null) {
sessionPayload.clear();
sessionPayload.put("uid", session.getUid());
try {
switchSessionRes = client.apiCall(loginResponse, "switch-session", sessionPayload);
}
catch (ApiClientException e) {
logoutReportAndExit(
"An error occurred while switching to the input published session. Exception: " + e.getMessage(),
MessageType.SEVERE);
}
if (switchSessionRes == null || !switchSessionRes.isSuccess()) {
logoutReportAndExit(
"Failed to switch published session uid " + configuration.getPublishedSessionUid() + ". " + errorResponseToString(
switchSessionRes), MessageType.SEVERE);
}
}
index.setSession(session);
}

}

/**
* This function check if to login as root or with user name and password
* @return True if the login is as root
Expand Down

0 comments on commit d7a7612

Please sign in to comment.