Search in sources :

Example 1 with SalesforceConnectorConfig

use of org.teiid.resource.adapter.salesforce.transport.SalesforceConnectorConfig in project teiid by teiid.

the class SalesforceConnectionImpl method login.

private void login(SalesForceManagedConnectionFactory mcf) throws ResourceException {
    config = new SalesforceConnectorConfig();
    String username = mcf.getUsername();
    String password = mcf.getPassword();
    // if security-domain is specified and caller identity is used; then use
    // credentials from subject
    boolean useCXFTransport = mcf.getConfigFile() != null;
    Subject subject = ConnectionContext.getSubject();
    if (subject != null) {
        OAuthCredential oauthCredential = ConnectionContext.getSecurityCredential(subject, OAuthCredential.class);
        if (oauthCredential != null) {
            config.setCredential(OAuthCredential.class.getName(), oauthCredential);
            useCXFTransport = true;
        } else {
            username = ConnectionContext.getUserName(subject, mcf, username);
            password = ConnectionContext.getPassword(subject, mcf, username, password);
        }
    }
    config.setCxfConfigFile(mcf.getConfigFile());
    if (useCXFTransport) {
        config.setTransport(SalesforceCXFTransport.class);
    }
    config.setCompression(true);
    config.setTraceMessage(false);
    // set the catch all properties
    String props = mcf.getConfigProperties();
    if (props != null) {
        Properties p = new Properties();
        try {
            p.load(new StringReader(props));
        } catch (IOException e) {
            throw new ResourceException(e);
        }
        PropertiesUtils.setBeanProperties(config, p, null);
    }
    config.setUsername(username);
    config.setPassword(password);
    config.setAuthEndpoint(mcf.getURL());
    // set proxy if needed
    if (mcf.getProxyURL() != null) {
        try {
            URL proxyURL = new URL(mcf.getProxyURL());
            config.setProxy(proxyURL.getHost(), proxyURL.getPort());
            config.setProxyUsername(mcf.getProxyUsername());
            config.setProxyPassword(mcf.getProxyPassword());
        } catch (MalformedURLException e) {
            throw new ResourceException(e);
        }
    }
    if (mcf.getConnectTimeout() != null) {
        config.setConnectionTimeout((int) Math.min(Integer.MAX_VALUE, mcf.getConnectTimeout()));
    }
    if (mcf.getRequestTimeout() != null) {
        config.setReadTimeout((int) Math.min(Integer.MAX_VALUE, mcf.getRequestTimeout()));
    }
    try {
        partnerConnection = new TeiidPartnerConnection(config);
        String endpoint = config.getServiceEndpoint();
        // The endpoint for the Bulk API service is the same as for the normal
        // SOAP uri until the /Soap/ part. From here it's '/async/versionNumber'
        // $NON-NLS-1$
        int index = endpoint.indexOf("Soap/u/");
        int endIndex = endpoint.indexOf('/', index + 7);
        apiVersion = endpoint.substring(index + 7, endIndex);
        // $NON-NLS-1$ //$NON-NLS-2$
        String bulkEndpoint = endpoint.substring(0, endpoint.indexOf("Soap/")) + "async/" + apiVersion;
        config.setRestEndpoint(bulkEndpoint);
        // This value identifies Teiid as a SF certified solution.
        // It was provided by SF and should not be changed.
        // $NON-NLS-1$
        partnerConnection.setCallOptions("RedHat/MetaMatrix/", null);
        bulkConnection = new BulkConnection(config);
        // Test the connection.
        partnerConnection.getUserInfo();
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        restEndpoint = endpoint.substring(0, endpoint.indexOf("Soap/")) + "data/" + "v30.0";
    } catch (AsyncApiException e) {
        throw new ResourceException(e);
    } catch (ConnectionException e) {
        throw new ResourceException(e);
    }
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Login was successful for username", username);
}
Also used : MalformedURLException(java.net.MalformedURLException) OAuthCredential(org.teiid.OAuthCredential) IOException(java.io.IOException) Properties(java.util.Properties) Subject(javax.security.auth.Subject) URL(java.net.URL) SalesforceConnectorConfig(org.teiid.resource.adapter.salesforce.transport.SalesforceConnectorConfig) StringReader(java.io.StringReader) ResourceException(javax.resource.ResourceException) ConnectionException(com.sforce.ws.ConnectionException)

Example 2 with SalesforceConnectorConfig

use of org.teiid.resource.adapter.salesforce.transport.SalesforceConnectorConfig in project teiid by teiid.

the class TeiidPartnerConnection method login.

public com.sforce.soap.partner.LoginResult login(java.lang.String username, java.lang.String password) throws com.sforce.ws.ConnectionException {
    SalesforceConnectorConfig config = (SalesforceConnectorConfig) getConfig();
    if (config.getCredential(OAuthCredential.class.getName()) == null) {
        return super.login(username, password);
    }
    // for details see
    // https://developer.salesforce.com/blogs/developer-relations/2011/03/oauth-and-the-soap-api.html
    OAuthCredential credential = (OAuthCredential) config.getCredential(OAuthCredential.class.getName());
    String id = credential.getAuthrorizationProperty("id");
    if (id == null) {
        throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection");
    }
    String accessToken = credential.getAuthorizationHeader(null, "POST");
    com.sforce.soap.partner.LoginResult loginResult = null;
    WebClient client = WebClient.create(id);
    client.header(AUTHORIZATION, accessToken);
    String response = client.get(String.class);
    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new ByteArrayInputStream(response.getBytes()));
        doc.getDocumentElement().normalize();
        Element urls = (Element) doc.getDocumentElement().getElementsByTagName("urls").item(0);
        loginResult = new com.sforce.soap.partner.LoginResult();
        // remove "Bearer " prefix.
        loginResult.setSessionId(accessToken.substring(7));
        String endpoint = config.getAuthEndpoint();
        // $NON-NLS-1$
        int index = endpoint.indexOf("Soap/u/");
        String apiVersion = endpoint.substring(index + 7);
        String partnerURL = urls.getElementsByTagName("partner").item(0).getTextContent();
        partnerURL = partnerURL.replace("{version}", apiVersion);
        loginResult.setServerUrl(partnerURL);
    } catch (IOException e) {
        throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
    } catch (ParserConfigurationException e) {
        throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
    } catch (IllegalStateException e) {
        throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
    } catch (SAXException e) {
        throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
    }
    return loginResult;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) ConnectionException(com.sforce.ws.ConnectionException) OAuthCredential(org.teiid.OAuthCredential) IOException(java.io.IOException) Document(org.w3c.dom.Document) WebClient(org.apache.cxf.jaxrs.client.WebClient) SalesforceConnectorConfig(org.teiid.resource.adapter.salesforce.transport.SalesforceConnectorConfig) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConnectionException(com.sforce.ws.ConnectionException)

Aggregations

ConnectionException (com.sforce.ws.ConnectionException)2 IOException (java.io.IOException)2 OAuthCredential (org.teiid.OAuthCredential)2 SalesforceConnectorConfig (org.teiid.resource.adapter.salesforce.transport.SalesforceConnectorConfig)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Properties (java.util.Properties)1 ResourceException (javax.resource.ResourceException)1 Subject (javax.security.auth.Subject)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 SAXException (org.xml.sax.SAXException)1