コード例 #1
0
  @Test
  public void testGetSessionId() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class);
    LoginResult loginResult = Mockito.mock(LoginResult.class);
    connector.setConnection(partnerConnection);
    connector.setLoginResult(loginResult);
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    connector.setBulkConnection(bulkConnection);
    when(loginResult.getSessionId()).thenReturn(MOCKED_ID);

    assertEquals(connector.getSessionId(), MOCKED_ID);
  }
コード例 #2
0
  public void connect() throws KettleException {

    try {
      this.binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
      if (log.isDetailed()) {
        log.logDetailed(
            BaseMessages.getString(
                PKG,
                "SalesforceInput.Log.LoginURL",
                binding._getProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY)));
      }

      // Set timeout
      if (getTimeOut() > 0) {
        this.binding.setTimeout(getTimeOut());
        if (log.isDebug()) {
          log.logDebug(
              BaseMessages.getString(PKG, "SalesforceInput.Log.SettingTimeout", "" + this.timeout));
        }
      }

      // Set URL
      this.binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, getURL());

      // Do we need compression?
      if (isUsingCompression()) {
        this.binding._setProperty(HTTPConstants.MC_ACCEPT_GZIP, useCompression);
        this.binding._setProperty(HTTPConstants.MC_GZIP_REQUEST, useCompression);
      }
      if (isRollbackAllChangesOnError()) {
        // Set the SOAP header to rollback all changes
        // unless all records are processed successfully.
        AllOrNoneHeader allOrNoneHeader = new AllOrNoneHeader();
        allOrNoneHeader.setAllOrNone(true);
        this.binding.setHeader(
            new SforceServiceLocator().getServiceName().getNamespaceURI(),
            "AllOrNoneHeader",
            allOrNoneHeader);
      }
      // Attempt the login giving the user feedback
      if (log.isDetailed()) {
        log.logDetailed(BaseMessages.getString(PKG, "SalesforceInput.Log.LoginNow"));
        log.logDetailed("----------------------------------------->");
        log.logDetailed(BaseMessages.getString(PKG, "SalesforceInput.Log.LoginURL", getURL()));
        log.logDetailed(
            BaseMessages.getString(PKG, "SalesforceInput.Log.LoginUsername", getUsername()));
        if (getModule() != null) {
          log.logDetailed(
              BaseMessages.getString(PKG, "SalesforceInput.Log.LoginModule", getModule()));
        }
        if (getCondition() != null) {
          log.logDetailed(
              BaseMessages.getString(PKG, "SalesforceInput.Log.LoginCondition", getCondition()));
        }
        log.logDetailed("<-----------------------------------------");
      }

      // Login
      this.loginResult = getBinding().login(getUsername(), getPassword());

      if (log.isDebug()) {
        log.logDebug(
            BaseMessages.getString(PKG, "SalesforceInput.Log.SessionId")
                + " : "
                + this.loginResult.getSessionId());
        log.logDebug(
            BaseMessages.getString(PKG, "SalesforceInput.Log.NewServerURL")
                + " : "
                + this.loginResult.getServerUrl());
      }

      // set the session header for subsequent call authentication
      this.binding._setProperty(
          SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, this.loginResult.getServerUrl());

      // Create a new session header object and set the session id to that
      // returned by the login
      SessionHeader sh = new SessionHeader();
      sh.setSessionId(loginResult.getSessionId());
      this.binding.setHeader(
          new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);

      // Return the user Infos
      this.userInfo = this.binding.getUserInfo();
      if (log.isDebug()) {
        log.logDebug(
            BaseMessages.getString(PKG, "SalesforceInput.Log.UserInfos")
                + " : "
                + this.userInfo.getUserFullName());
        log.logDebug("----------------------------------------->");
        log.logDebug(
            BaseMessages.getString(PKG, "SalesforceInput.Log.UserName")
                + " : "
                + this.userInfo.getUserFullName());
        log.logDebug(
            BaseMessages.getString(PKG, "SalesforceInput.Log.UserEmail")
                + " : "
                + this.userInfo.getUserEmail());
        log.logDebug(
            BaseMessages.getString(PKG, "SalesforceInput.Log.UserLanguage")
                + " : "
                + this.userInfo.getUserLanguage());
        log.logDebug(
            BaseMessages.getString(PKG, "SalesforceInput.Log.UserOrganization")
                + " : "
                + this.userInfo.getOrganizationName());
        log.logDebug("<-----------------------------------------");
      }

      this.serverTimestamp = getBinding().getServerTimestamp().getTimestamp().getTime();
      if (log.isDebug()) {
        BaseMessages.getString(PKG, "SalesforceInput.Log.ServerTimestamp", getServerTimestamp());
      }

      if (log.isDetailed()) {
        log.logDetailed(BaseMessages.getString(PKG, "SalesforceInput.Log.Connected"));
      }

    } 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) {
        throw new KettleException(
            BaseMessages.getString(PKG, "SalesforceInput.Error.InvalidUsernameOrPassword"));
      }
      throw new KettleException(
          BaseMessages.getString(PKG, "SalesforceInput.Error.Connection"), ex);
    } catch (Exception e) {
      throw new KettleException(BaseMessages.getString(PKG, "SalesforceInput.Error.Connection"), e);
    }
  }