How to bypass an invalid ssl certificate in the generated RESTful generated client

Hi

For a usual java barebones client, I can bypass an invalid SSL certificate and perform basic authentication like this

package com.ericsson.neas.opamclient;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import flexjson.JSONDeserializer;

public class Client {

    /**
     * @param args
     * @throws KeyManagementException
     * @throws NoSuchAlgorithmException
     */
    public static void main(String[] args) throws Exception {
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {

            }

            @Override
            public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
            }
        };
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLContext.setDefault(ctx);

        URL url = new URL("https://server:port/app/target/search?name=somecriteria");

        String authStr = "user:password";
        String authEncoded = Base64.encodeBytes(authStr.getBytes());

        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        
        connection.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String paramString, SSLSession paramSSLSession) {
                return true;
            }
        });
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setRequestProperty("Authorization", "Basic " + authEncoded);

        InputStream content = (InputStream) connection.getInputStream();

        JSONDeserializer<Map<String,List<Map<String,Map<String,String>>>>> json = new JSONDeserializer<>();
        Map<String,List<Map<String,Map<String,String>>>> targetCollection = json.deserialize(new InputStreamReader(content));
        List<Map<String,Map<String,String>>> col = targetCollection.get("Target Collection");
        Map<String,Map<String,String>> keyVal = col.get(0);
        Map<String,String> targetVal = keyVal.get("target");
        System.out.println(targetVal);
    }

}

my question is, how do I do the same for the generated by IntelliJ RESTful client?

TIA

Leo

Please sign in to leave a comment.