Saviynt unveils its cutting-edge Intelligence Suite products to revolutionize Identity Security!
Click HERE to see how Saviynt Intelligence is transforming the industry.
Saviynt Copilot Icon

Custom jar not producing output

NM
Esteemed Contributor
Esteemed Contributor

Hi Team,

I have created a custom jar .. followed the document steps added it in under "External Jar" section

created a "invoke jar job. Currently as a test i am just trying to get the access token from my saviynt environment (that is not the complete use case)

I code see below statement getting executed in logs but nothing else and also gives no error

obj.info("testworkjarexecute");

Code-

import java.util.Base64;

import java.io.BufferedReader;

 

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.logging.*;

import java.io.OutputStream;

import java.nio.charset.StandardCharsets;

//import com.google.gson.JsonObject;

 

//import com.google.gson.Gson;

 

 

public class Classcall {

 

 

 

public static void main(String[] args) throws Exception {

String accessToken = "";

try {

Logger obj= Logger.getLogger(Classcall.class.getName());

URL url = new URL("URL");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type", "application/json");

conn.setDoOutput(true);

obj.info("testworkjarexecute");

 

String requestBody = "{\"username\": \"username\", \"password\": \"password\"}";

byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);

OutputStream os = conn.getOutputStream();

os.write(input, 0, input.length);

 

if (conn.getResponseCode() != 200) {

throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());

}

obj.info("--connresponsecode--"+conn.getResponseCode());

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

 

String output;

StringBuilder responseBuilder = new StringBuilder();

while ((output = br.readLine()) != null) {

responseBuilder.append(output);

}

br.close();

obj.info("executed till here");

obj.info("--Entering into main method of class ZipExtractor--"+responseBuilder.toString());

 

//accessToken = response.getAccessToken();

//System.out.println("token---------->" + accessToken);

conn.disconnect();

} catch (Exception e) {

e.printStackTrace();

}

}

}

 

FYI- I haven't added any saviynt certification in certificate management.

Could someone please help me out with this

Thanks


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'
12 REPLIES 12

rushikeshvartak
All-Star
All-Star

Share logs


Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

NM
Esteemed Contributor
Esteemed Contributor

@rushikeshvartak , logs consist of only 3 lines 

Start externaljar job

Print statment testjarexecute

End job

That it..


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'

Try below code.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.*;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class Classcall {

    public static void main(String[] args) throws Exception {
        String accessToken = "";
        Logger obj = Logger.getLogger(Classcall.class.getName());

        try {
            URL url = new URL("URL");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);
            obj.info("Connection initialized");

            String requestBody = "{\"username\": \"username\", \"password\": \"password\"}";
            byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
            OutputStream os = conn.getOutputStream();
            os.write(input, 0, input.length);
            obj.info("Request body sent");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }
            obj.info("HTTP response code: " + conn.getResponseCode());

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            String output;
            StringBuilder responseBuilder = new StringBuilder();

            while ((output = br.readLine()) != null) {
                responseBuilder.append(output);
            }
            br.close();
            obj.info("Response received and read");

            obj.info("Response: " + responseBuilder.toString());
            conn.disconnect();

        } catch (Exception e) {
            obj.log(Level.SEVERE, "Exception occurred", e);
        }
    }
}

Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

NM
Esteemed Contributor
Esteemed Contributor

@rushikeshvartak , thanks .. have done a restart will update you .. in another 15-20 mins.

2) while importing I am having error in moduleinfo.java file and not importing that.. as jar is getting executed and I can see some details in logs i believe it should be alright.


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'

NM
Esteemed Contributor
Esteemed Contributor

Hi @rushikeshvartak ,

Trying to fetch response data so i have used JSONObject class and obviously added dependency in pom.xml file, can't we add Maven project in Saviynt External jar.. if not what is the is an alternative way.


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'

  • create simple java code instead of spring-based.
  • dependency can be uploaded separately 

Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

NM
Esteemed Contributor
Esteemed Contributor

@rushikeshvartak just to clarify if I need to use jsonobject class so you mean I can't use it or I need to download seperate dependency file club it into the executable Java file and make one jar file?

 

@rushikeshvartak , if you can please guide me .. your help us much appreciated 🙂

Update: if we upload the dependency file as in jsonobject class jar file seperately will it work??


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'

It will work


Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

NM
Esteemed Contributor
Esteemed Contributor

@rushikeshvartak , sorry what will work didn't get you ?


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'

  • Build your java code with Intellj and use logger which will also print logs

Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

NM
Esteemed Contributor
Esteemed Contributor

Hi @rushikeshvartak , let me try it.


If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'

rahul_p
Regular Contributor III
Regular Contributor III

Hello @NM ,
Try this :

Spoiler
public String getToken() {
String updateresponse = "";
try {
String tokengen="Bearer "+"<TOKENTOESTABLISHFIRSTCONNECTION>";
System.out.println("Got the token "+tokengen);
String URL ="<URL>/ECM/api/login";
URL updateurl= new URL(URL);
System.out.println(updateurl);
String readLine = null;
JSONObject serverDetailsObject = new JSONObject();
serverDetailsObject.put("username", "<username>");
serverDetailsObject.put("password", "<password>");
String jsonObjectString = serverDetailsObject.toString();
System.out.println("jsonObjectString ==> " + jsonObjectString);
HttpURLConnection conection = (HttpURLConnection) updateurl.openConnection();
conection.setDoInput(true); conection.setDoOutput(true);
conection.setRequestMethod("POST");
conection.setRequestProperty("Accept", "*/*");
conection.setRequestProperty("Content-Type", "application/json");
// String userName = "";
//String userCredentials =
//userName + ":" + userPassword;
// System.out.println(userName);
//LOGGER.info("Username ==> " + userName);
//System.out.println(userPassword); // LOGGER.info("UserPassowrd ==> " +
//userPassword); //String basicAuth = "Basic " +
//Base64.getEncoder().encodeToString(userCredentials.getBytes());

conection.setRequestProperty("Authorization", tokengen);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(conection.getOutputStream());
outputStreamWriter.write(jsonObjectString);
//System.out.println("outputStreamWriter ==> " + outputStreamWriter);
//LOGGER.info("outputStreamWriter ==> " + outputStreamWriter);
outputStreamWriter.flush();
System.out.println("Server object data ==> " + serverDetailsObject);
System.out.println("Connection\n" + conection + "\n");
int responseCode = conection.getResponseCode();
System.out.println("RESPONSE CODE = " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new
InputStreamReader(conection.getInputStream()));
StringBuffer response = new StringBuffer();
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
in.close();
updateresponse = response.toString();
JSONObject obj=new JSONObject(response.toString());

ftoken=obj.get("access_token");
System.out.println(ftoken); // }

} else {

System.out.println("POST NOT WORKED"); }

} catch (Exception e) {

updateresponse = e.getMessage(); e.printStackTrace();

} finally {


}

return ftoken.toString();

}

There is no certificate needed for this.

Regards,
Rahul
Please accept this as solution & give kudos if it resolves your issue.