Search in sources :

Example 1 with ExceptionCode

use of com.salesforce.soap.partner.fault.ExceptionCode in project tdi-studio-se by Talend.

the class SObjectsToSchema method login.

boolean login(String userName, String password) throws ServiceException {
    /**
         * Next, the sample client application initializes the binding stub. This is our main interface to the API
         * through which all calls are made. The getSoap method takes an optional parameter, (a java.net.URL) which is
         * the endpoint. For the login call, the parameter always starts with http(s)://login.salesforce.com. After
         * logging in, the sample client application changes the endpoint to the one specified in the returned
         * loginResult object.
         */
    binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
    // Time out after a minute
    binding.setTimeout(60000);
    // Test operation
    LoginResult loginResult;
    try {
        System.out.println("LOGGING IN NOW....");
        loginResult = binding.login(userName, password);
    } catch (LoginFault ex) {
        // The LoginFault derives from AxisFault
        ExceptionCode exCode = ex.getExceptionCode();
        if (exCode == ExceptionCode.FUNCTIONALITY_NOT_ENABLED || exCode == ExceptionCode.INVALID_CLIENT || exCode == ExceptionCode.INVALID_LOGIN || exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_DOMAIN || exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_TIME || exCode == ExceptionCode.ORG_LOCKED || exCode == ExceptionCode.PASSWORD_LOCKOUT || exCode == ExceptionCode.SERVER_UNAVAILABLE || exCode == ExceptionCode.TRIAL_EXPIRED || exCode == ExceptionCode.UNSUPPORTED_CLIENT) {
            System.out.println("Please be sure that you have a valid username " + "and password.");
        } else {
            // Write the fault code to the console
            System.out.println(ex.getExceptionCode());
            // Write the fault message to the console
            System.out.println("An unexpected error has occurred." + ex.getMessage());
        }
        return false;
    } catch (Exception ex) {
        System.out.println("An unexpected error has occurred: " + ex.getMessage());
        ex.printStackTrace();
        return false;
    }
    if (loginResult.isPasswordExpired()) {
        System.out.println("An error has occurred. Your password has expired.");
        return false;
    }
    /**
         * Once the client application has logged in successfully, it will use the results of the login call to reset
         * the endpoint of the service to the virtual server instance that is servicing your organization. To do this,
         * the client application sets the ENDPOINT_ADDRESS_PROPERTY of the binding object using the URL returned from
         * the LoginResult.
         */
    binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
    /**
         * The sample client application now has an instance of the SoapBindingStub that is pointing to the correct
         * endpoint. Next, the sample client application sets a persistent SOAP header (to be included on all subsequent
         * calls that are made with the SoapBindingStub) that contains the valid sessionId for our login credentials. To
         * do this, the sample client application creates a new SessionHeader object and set its sessionId property to
         * the sessionId property from the LoginResult object.
         */
    // Create a new session header object and add the session id
    // from the login return object
    SessionHeader sh = new SessionHeader();
    sh.setSessionId(loginResult.getSessionId());
    /**
         * Next, the sample client application calls the setHeader method of the SoapBindingStub to add the header to
         * all subsequent method calls. This header will persist until the SoapBindingStub is destroyed until the header
         * is explicitly removed. The "SessionHeader" parameter is the name of the header to be added.
         */
    // set the session header for subsequent call authentication
    binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
    return true;
}
Also used : SforceServiceLocator(com.salesforce.soap.partner.SforceServiceLocator) SessionHeader(com.salesforce.soap.partner.SessionHeader) LoginResult(com.salesforce.soap.partner.LoginResult) LoginFault(com.salesforce.soap.partner.fault.LoginFault) ExceptionCode(com.salesforce.soap.partner.fault.ExceptionCode) IOException(java.io.IOException) ServiceException(javax.xml.rpc.ServiceException) RemoteException(java.rmi.RemoteException)

Aggregations

LoginResult (com.salesforce.soap.partner.LoginResult)1 SessionHeader (com.salesforce.soap.partner.SessionHeader)1 SforceServiceLocator (com.salesforce.soap.partner.SforceServiceLocator)1 ExceptionCode (com.salesforce.soap.partner.fault.ExceptionCode)1 LoginFault (com.salesforce.soap.partner.fault.LoginFault)1 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 ServiceException (javax.xml.rpc.ServiceException)1