RQM Authentication through JAVA REST call is not working.
RQM Authentication through JAVA REST call is not working. It keeps loading, login page doesn't get authenticate.
public class client{
public static void main(String[] args) {
try {
// Create a trust manager that accepts all certificates
disableCertificateVerification();
// Create URL object for the API endpoint
URL url = new URL("https://ib.com:9443/qm/oslc_qm/catalog");
// Open a connection to the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set request method, headers, etc. if needed
connection.setRequestMethod("GET");
// connection.setRequestProperty("Authorization","Bearer <token>");
connection.setRequestProperty("Content-Type", "application/rdf+xml");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("OSLC-Core-Version", "2.0");
//connection.setRequestProperty("User-Agent", "Mozilla/5.0");
String userAgent = "Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko";
connection.setRequestProperty("User-Agent", userAgent);
String username = "admin";
String password = "admin";
String credentials = username + ":" + password;
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes());
String authHeader = "Basic " + encodedCredentials;
connection.setRequestProperty("Authorization", authHeader);
//connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
System.out.println("encodedCredentials" +authHeader);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
System.out.println(+responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
// Read the XML response
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
String xmlData = response.toString();
System.out.println("XML data:\n" + xmlData);
System.out.println("Response: " + response.toString());
}else {
System.out.println("Error: " + responseCode);
}
// Process the XML response
// Disconnect the connection
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void disableCertificateVerification() {
// TODO Auto-generated method stub
try {
// Create a trust manager that does not validate SSL certificates
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Set the SSL context with the trust manager that disables certificate verification
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
}
One answer
First of all, there is no question in the statement above. In fact there is pretty much no useful information in the "question" besides the code.
It is impossible to "remote debug" code in the forum.
Anyway, looking at the code above, I have no reason to believe that it would ever work, because it does not handle authentication challenges. It seems to try to use some kind of BASIC authentication. Maybe.
- Use a tool such as Postman to find out how stuff works.
- Check with the whatever API documentation is available. https://jazz.net/wiki/bin/view/Deployment/CLMProductAPILanding cites an article about authentication. That article is very cryptic, I tried to explain it a bit better here: https://rsjazz.wordpress.com/2021/10/15/elm-authentication/