private void connect() throws ConnectorException {
        try {
            String endpoint = ApimanPathUtils.join(api.getEndpoint(), request.getDestination());

            if (request.getQueryParams() != null && !request.getQueryParams().isEmpty()) {
                String delim = "?"; //$NON-NLS-1$
                for (Entry<String, String> entry : request.getQueryParams()) {
                    endpoint += delim + entry.getKey();
                    if (entry.getValue() != null) {
                        endpoint += "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
                    }
                    delim = "&"; //$NON-NLS-1$
                }
            }
            URL url = new URL(endpoint);
            OkUrlFactory factory = new OkUrlFactory(client);
            connection = factory.open(url);

            boolean isSsl = connection instanceof HttpsURLConnection;

            if(requiredAuthType == RequiredAuthType.MTLS && !isSsl) {
                throw new ConnectorException("Mutually authenticating TLS requested, but insecure endpoint protocol was indicated."); //$NON-NLS-1$
            }

            if (requiredAuthType == RequiredAuthType.BASIC) {
                BasicAuthOptions options = new BasicAuthOptions(api.getEndpointProperties());
                if (options.getUsername() != null && options.getPassword() != null) {
                    if (options.isRequireSSL() && !isSsl) {
                        throw new ConnectorException("Endpoint security requested (BASIC auth) but endpoint is not secure (SSL)."); //$NON-NLS-1$
                    }

                    String up = options.getUsername() + ':' + options.getPassword();
                    StringBuilder builder = new StringBuilder();
                    builder.append("Basic "); //$NON-NLS-1$
                    builder.append(Base64.encodeBase64String(up.getBytes()));
                    connection.setRequestProperty("Authorization", builder.toString()); //$NON-NLS-1$
                    connectorConfig.suppressRequestHeader("Authorization"); //$NON-NLS-1$
                }
            }

            if (hasDataPolicy) {
                connectorConfig.suppressRequestHeader("Content-Length"); //$NON-NLS-1$
            }

            if (isSsl) {
                HttpsURLConnection https = (HttpsURLConnection) connection;
                SSLSocketFactory socketFactory = sslStrategy.getSocketFactory();
                https.setSSLSocketFactory(socketFactory);
                https.setHostnameVerifier(sslStrategy.getHostnameVerifier());
            }

            setConnectTimeout(connection);
            setReadTimeout(connection);
            if (request.getType().equalsIgnoreCase("PUT") || request.getType().equalsIgnoreCase("POST")) { //$NON-NLS-1$ //$NON-NLS-2$
                connection.setDoOutput(true);
            } else {
                connection.setDoOutput(false);
            }
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod(request.getType());

            // Set the request headers
            for (Entry<String, String> entry : request.getHeaders()) {
                String hkey = entry.getKey();
                String hval = entry.getValue();
                if (!connectorConfig.getSuppressedRequestHeaders().contains(hkey)) {
                    connection.addRequestProperty(hkey, hval);
                }
            }

            // Set or reset mandatory headers
            connection.setRequestProperty("Host", url.getHost() + determinePort(url)); //$NON-NLS-1$
            connection.connect();
            connected = true;
        } catch (IOException error) {
            handleConnectionError(error);
        }
    }
private void connect ( ) throws ConnectorException { try { String endpoint = ApimanPathUtils . join ( api . getEndpoint ( ) , request . getDestination ( ) ) ; if ( request . getQueryParams ( ) != null && ! request . getQueryParams ( ) . isEmpty ( ) ) { String delim = "?" ; for ( Entry < String , String > entry : request . getQueryParams ( ) ) { endpoint += delim + entry . getKey ( ) ; if ( entry . getValue ( ) != null ) { endpoint += "=" + URLEncoder . encode ( entry . getValue ( ) , "UTF-8" ) ; } delim = "&" ; } } URL url = new URL ( endpoint ) ; OkUrlFactory factory = new OkUrlFactory ( client ) ; connection = factory . open ( url ) ; boolean isSsl = connection instanceof HttpsURLConnection ; if ( requiredAuthType == RequiredAuthType . MTLS && ! isSsl ) { throw new ConnectorException ( "Mutually authenticating TLS requested, but insecure endpoint protocol was indicated." ) ; } if ( requiredAuthType == RequiredAuthType . BASIC ) { BasicAuthOptions options = new BasicAuthOptions ( api . getEndpointProperties ( ) ) ; if ( options . getUsername ( ) != null && options . getPassword ( ) != null ) { if ( options . isRequireSSL ( ) && ! isSsl ) { throw new ConnectorException ( "Endpoint security requested (BASIC auth) but endpoint is not secure (SSL)." ) ; } String up = options . getUsername ( ) + ':' + options . getPassword ( ) ; StringBuilder builder = new StringBuilder ( ) ; builder . append ( "Basic " ) ; builder . append ( Base64 . encodeBase64String ( up . getBytes ( ) ) ) ; connection . setRequestProperty ( "Authorization" , builder . toString ( ) ) ; connectorConfig . suppressRequestHeader ( "Authorization" ) ; } } if ( hasDataPolicy ) { connectorConfig . suppressRequestHeader ( "Content-Length" ) ; } if ( isSsl ) { HttpsURLConnection https = ( HttpsURLConnection ) connection ; SSLSocketFactory socketFactory = sslStrategy . getSocketFactory ( ) ; https . setSSLSocketFactory ( socketFactory ) ; https . setHostnameVerifier ( sslStrategy . getHostnameVerifier ( ) ) ; } setConnectTimeout ( connection ) ; setReadTimeout ( connection ) ; if ( request . getType ( ) . equalsIgnoreCase ( "PUT" ) || request . getType ( ) . equalsIgnoreCase ( "POST" ) ) { connection . setDoOutput ( true ) ; } else { connection . setDoOutput ( false ) ; } connection . setDoInput ( true ) ; connection . setUseCaches ( false ) ; connection . setRequestMethod ( request . getType ( ) ) ; for ( Entry < String , String > entry : request . getHeaders ( ) ) { String hkey = entry . getKey ( ) ; String hval = entry . getValue ( ) ; if ( ! connectorConfig . getSuppressedRequestHeaders ( ) . contains ( hkey ) ) { connection . addRequestProperty ( hkey , hval ) ; } } connection . setRequestProperty ( "Host" , url . getHost ( ) + determinePort ( url ) ) ; connection . connect ( ) ; connected = true ; } catch ( IOException error ) { handleConnectionError ( error ) ; } }
Connects to the back end system.