Search in sources :

Example 1 with FunctionalAPIImplService

use of com.emc.fapiclient.ws.FunctionalAPIImplService in project coprhd-controller by CoprHD.

the class RecoverPointConnection method connect.

/**
 * Connect to RP and return a handle that can be used for FAPI calls
 *
 * @param endpoint - Address to connect to
 * @param username - Username for credentials
 * @param password - Password for credentials
 *
 * @return FunctionalAPIImpl - A handle for FAPI access
 */
public FunctionalAPIImpl connect(URI endpoint, String username, String password) {
    try {
        ignoreCertifications();
    // interceptCertificates();
    } catch (Exception e) {
    // so what?
    }
    String destAddress = endpoint.toASCIIString();
    try {
        URL baseUrl = FunctionalAPIImplService.class.getResource(".");
        URL url = new URL(baseUrl, destAddress);
        final String finalUser = username;
        final String finalPassword = password;
        // Modify the System Property for Max Redirects to a smaller number so we don't hammer
        // the device over and over unnecessarily with bad credentials (if they're bad) as this
        // takes a long time. Default is 20 redirects.
        // However we will save the old redirect value and restore it after we're done.
        String oldMaxRedirectsValue = null;
        try {
            oldMaxRedirectsValue = System.getProperty(SYSPROPS_HTTP_MAX_REDIRECTS);
        } catch (NullPointerException npe) {
            logger.warn("The System property " + SYSPROPS_HTTP_MAX_REDIRECTS + " does not already exist for some reason.");
        }
        // Set the Property for http.maxRedirects to 2 to prevent unnecessary retries
        System.setProperty(SYSPROPS_HTTP_MAX_REDIRECTS, NEW_MAX_REDIRECTS);
        // If we're creating a new connection, we want to clear the cache as it may be holding
        // onto a previously valid connection.
        AuthCacheValue.setAuthCache(new AuthCacheImpl());
        Authenticator.setDefault(null);
        // Create a PasswordAuthentication so when the request is asked via HTTP for authentication,
        // the below will be automatically returned. This is set here, but invoked behind the scenes.
        Authenticator.setDefault(new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(finalUser, finalPassword.toCharArray());
            }
        });
        logger.info("Attempting to connect to service " + FAPI_SERVICENAME + " at url " + FAPI_URL + " using auth credentials for: " + finalUser);
        // Connect to the service
        FunctionalAPIImplService service = new FunctionalAPIImplService(url, new QName(FAPI_URL, FAPI_SERVICENAME));
        FunctionalAPIImpl impl = service.getFunctionalAPIImplPort();
        BindingProvider bp = (BindingProvider) impl;
        Map<String, Object> map = bp.getRequestContext();
        logger.info("RecoverPoint service: Dest: " + destAddress + ", user: " + username);
        map.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, destAddress);
        map.put(BindingProvider.USERNAME_PROPERTY, username);
        map.put(BindingProvider.PASSWORD_PROPERTY, password);
        // Reset the System Property for http.maxRedirects, but only if an existing value was present
        if (oldMaxRedirectsValue != null && !oldMaxRedirectsValue.isEmpty()) {
            System.setProperty(SYSPROPS_HTTP_MAX_REDIRECTS, oldMaxRedirectsValue);
        }
        logger.info("Connected.");
        return impl;
    } catch (MalformedURLException e) {
        logger.error("Failed to create URL for the wsdl Location: " + destAddress);
        logger.error(e.getMessage());
        return null;
    }
}
Also used : AuthCacheImpl(sun.net.www.protocol.http.AuthCacheImpl) MalformedURLException(java.net.MalformedURLException) QName(javax.xml.namespace.QName) BindingProvider(javax.xml.ws.BindingProvider) FunctionalAPIImplService(com.emc.fapiclient.ws.FunctionalAPIImplService) KeyStoreException(java.security.KeyStoreException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) URL(java.net.URL) FunctionalAPIImpl(com.emc.fapiclient.ws.FunctionalAPIImpl) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

FunctionalAPIImpl (com.emc.fapiclient.ws.FunctionalAPIImpl)1 FunctionalAPIImplService (com.emc.fapiclient.ws.FunctionalAPIImplService)1 IOException (java.io.IOException)1 Authenticator (java.net.Authenticator)1 MalformedURLException (java.net.MalformedURLException)1 PasswordAuthentication (java.net.PasswordAuthentication)1 URL (java.net.URL)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 CertificateException (java.security.cert.CertificateException)1 QName (javax.xml.namespace.QName)1 BindingProvider (javax.xml.ws.BindingProvider)1 AuthCacheImpl (sun.net.www.protocol.http.AuthCacheImpl)1