1818package com .checkpoint .mgmt_api .examples ;
1919
2020import com .checkpoint .mgmt_api .client .*;
21- import com .checkpoint .mgmt_api .objects .GatewayAndServer ;
22- import com .checkpoint .mgmt_api .objects .IndexView ;
23- import com .checkpoint .mgmt_api .objects .Layer ;
24- import com .checkpoint .mgmt_api .objects .PolicyPackage ;
21+ import com .checkpoint .mgmt_api .objects .*;
2522import com .checkpoint .mgmt_api .utils .TarGZUtils ;
2623import org .json .simple .JSONArray ;
2724import org .json .simple .JSONObject ;
@@ -170,24 +167,7 @@ else if(!loginResponse.isSuccess()){
170167
171168 IndexView index = new IndexView ();
172169
173- /*Switching to published session*/
174- if (configuration .getPublishedSessionUid () != null ) {
175- ApiResponse res = null ;
176- try {
177- res = client .apiCall (loginResponse , "switch-session" ,
178- "{\" uid\" : \" " + configuration .getPublishedSessionUid () +"\" }" );
179- }
180- catch (ApiClientException e ) {
181- logoutReportAndExit ("An error occurred while switching to the published session. Exception: " + e .getMessage (), MessageType .SEVERE );
182- }
183- if (res == null || !res .isSuccess ()) {
184- logoutReportAndExit ("Failed to switch published session uid " + configuration .getPublishedSessionUid () +". " + errorResponseToString (res ), MessageType .SEVERE );
185- }
186- index .setPublishedSessionUid (configuration .getPublishedSessionUid ());
187- }
188- else {
189- index .setPublishedSessionUid ("Last published session" );
190- }
170+ handlePublishedSession (index );
191171
192172 /*Update the index page data*/
193173 index .setDomain (configuration .getDomain ());
@@ -207,6 +187,104 @@ else if(!loginResponse.isSuccess()){
207187 logoutReportAndExit ("" , MessageType .EXIT_WITHOUT_MESSAGE );
208188 }
209189
190+ private static void handlePublishedSession (IndexView index )
191+ {
192+ Session session = new Session ();
193+ JSONObject sessionPayload = new JSONObject ();
194+ ApiResponse sessionRes = null ;
195+
196+ //get last published session
197+ try {
198+ sessionRes = client .apiCall (loginResponse , "show-last-published-session" , "{}" );
199+ }
200+ catch (ApiClientException e ) {
201+ logoutReportAndExit ("Failed to show last published session. Exception: " + e .getMessage (), MessageType .SEVERE );
202+ }
203+ if (sessionRes == null || !sessionRes .isSuccess ()) {
204+ //print error: if client asked to switch session in management with no published sessions
205+ if (configuration .getPublishedSessionUid () != null ) {
206+ logoutReportAndExit (errorResponseToString (
207+ sessionRes ) + " - could not switch to the required session: " + configuration .getPublishedSessionUid (),
208+ MessageType .SEVERE );
209+ }
210+ //print info: no published sessions in the management
211+ else {
212+ configuration .getLogger ().info (errorResponseToString (sessionRes ));
213+ }
214+ }
215+ else {
216+ //check if the given id is the last published session and set uid
217+ if (configuration .getPublishedSessionUid () != null ) {
218+ session .setUid (configuration .getPublishedSessionUid ());
219+
220+ if (sessionRes .getPayload ().get ("uid" ).toString ().equals (configuration .getPublishedSessionUid ())) {
221+ session .setLastPublishedSession (true );
222+ }
223+ else {
224+ session .setLastPublishedSession (false );
225+ }
226+ }
227+ else {
228+ session .setUid (sessionRes .getPayload ().get ("uid" ).toString ());
229+ session .setLastPublishedSession (true );
230+ }
231+
232+ //if not the last published session - get the published session
233+ if (!session .getLastPublishedSession ()) {
234+ sessionPayload .put ("uid" , session .getUid ());
235+ try {
236+ sessionRes = client .apiCall (loginResponse , "show-session" , sessionPayload );
237+ }
238+ catch (ApiClientException e ) {
239+ logoutReportAndExit (
240+ "Failed to show the session" + session .getUid () + ". Exception: " + e .getMessage (),
241+ MessageType .SEVERE );
242+ }
243+ if (sessionRes == null || !sessionRes .isSuccess ()) {
244+ logoutReportAndExit (
245+ "Failed to show the session" + session .getUid () + ". Exception: " + errorResponseToString (
246+ sessionRes ), MessageType .SEVERE );
247+ }
248+ }
249+
250+ //get name and published time of the published session
251+ Object sessionName = sessionRes .getPayload ().get ("name" );
252+ Object publishTime = sessionRes .getPayload ().get ("publish-time" );
253+
254+ if (sessionName != null )
255+ session .setName (sessionName .toString ());
256+ else
257+ session .setName ("" );
258+
259+ if (publishTime != null )
260+ session .setPublishTime (((HashMap )publishTime ).get ("iso-8601" ).toString ());
261+ else
262+ logoutReportAndExit ("Input published session is not published - " , MessageType .SEVERE );
263+
264+ //Switching to published session
265+ ApiResponse switchSessionRes = null ;
266+ if (configuration .getPublishedSessionUid () != null ) {
267+ sessionPayload .clear ();
268+ sessionPayload .put ("uid" , session .getUid ());
269+ try {
270+ switchSessionRes = client .apiCall (loginResponse , "switch-session" , sessionPayload );
271+ }
272+ catch (ApiClientException e ) {
273+ logoutReportAndExit (
274+ "An error occurred while switching to the input published session. Exception: " + e .getMessage (),
275+ MessageType .SEVERE );
276+ }
277+ if (switchSessionRes == null || !switchSessionRes .isSuccess ()) {
278+ logoutReportAndExit (
279+ "Failed to switch published session uid " + configuration .getPublishedSessionUid () + ". " + errorResponseToString (
280+ switchSessionRes ), MessageType .SEVERE );
281+ }
282+ }
283+ index .setSession (session );
284+ }
285+
286+ }
287+
210288 /**
211289 * This function check if to login as root or with user name and password
212290 * @return True if the login is as root
0 commit comments