com.streamsets.pipeline.api.Label Java Examples

The following examples show how to use com.streamsets.pipeline.api.Label. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigGroupExtractor.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public List<ErrorMessage> validate(Class klass, Object contextMsg) {
  List<ErrorMessage> errors = new ArrayList<>();
  List<ConfigGroups> allConfigGroups = getAllConfigGroups(klass);
  Set<String> allGroupNames = new HashSet<>();
  if (!allConfigGroups.isEmpty()) {
    for (ConfigGroups configGroups : allConfigGroups) {
      Class<? extends Label> gKlass = configGroups.value();
      if (!gKlass.isEnum()) {
        errors.add(new ErrorMessage(DefinitionError.DEF_100, contextMsg, gKlass.getSimpleName()));
      } else {
        for (Label label : gKlass.getEnumConstants()) {
          String groupName = label.toString();
          if (allGroupNames.contains(groupName)) {
            errors.add(new ErrorMessage(DefinitionError.DEF_101, contextMsg, groupName));
          }
          allGroupNames.add(groupName);
        }
      }
    }
  }
  return errors;
}
 
Example #2
Source File: TestRemoteConnector.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private RemoteConnector createRemoteConnector(RemoteConfigBean conf) {
  RemoteConnector connector = new RemoteConnector(conf) {
    @Override
    protected void initAndConnect(
        List<Stage.ConfigIssue> issues, ConfigIssueContext context, URI remoteURI, Label remoteGroup, Label credGroup
    ) {
    }

    @Override
    public void verifyAndReconnect() throws StageException {
    }

    @Override
    public void close() throws IOException {
    }
  };
  return Mockito.spy(connector);
}
 
Example #3
Source File: TestRemoteConnector.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public void testGetURIInvalid(String address, Errors exepctedError) throws Exception {
  List<Stage.ConfigIssue> issues = new ArrayList<>();
  Label group = Mockito.mock(Label.class);
  Mockito.when(group.getLabel()).thenReturn("REMOTE");
  ConfigIssueContext context = Mockito.mock(ConfigIssueContext.class);
  Mockito.when(context.createConfigIssue(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
      .thenAnswer(new Answer<ConfigIssue>() {
        @Override
        public ConfigIssue answer(InvocationOnMock invocation) throws Throwable {
          String label = invocation.getArgumentAt(0, String.class);
          Assert.assertEquals(group.getLabel(), label);
          Errors error = invocation.getArgumentAt(2, Errors.class);
          Assert.assertEquals(exepctedError, error);

          String message = invocation.getArgumentAt(3, String.class);
          ConfigIssue configIssue = Mockito.mock(ConfigIssue.class);
          Mockito.when(configIssue.toString()).thenReturn(message);
          return configIssue;
        }
      });
  RemoteConfigBean conf = new RemoteConfigBean();
  conf.remoteAddress = address;
  URI uri = RemoteConnector.getURI(conf, issues, context, group);
  Assert.assertNull(uri);
  assertNumIssues(issues, 1);
}
 
Example #4
Source File: RemoteConnectorTestBase.java    From datacollector with Apache License 2.0 6 votes vote down vote up
protected List<Stage.ConfigIssue> initAndCheckIssue(
    RemoteConnector connector,
    String expectedConfigGroup,
    String expectedConfigName,
    ErrorCode expectedErrorCode,
    Object... expectedArgs
) {
  List<Stage.ConfigIssue> issues = new ArrayList<>();
  Label remoteGroup = Mockito.mock(Label.class);
  Mockito.when(remoteGroup.getLabel()).thenReturn("SFTP/FTP/FTPS");
  Label credGroup = Mockito.mock(Label.class);
  Mockito.when(credGroup.getLabel()).thenReturn("CREDENTIALS");
  connector.initAndConnect(
      issues,
      createContextAndCheckIssue(expectedConfigGroup, expectedConfigName, expectedErrorCode, expectedArgs),
      URI.create(connector.remoteConfig.remoteAddress),
      remoteGroup,
      credGroup
  );
  return issues;
}
 
Example #5
Source File: RemoteConnectorTestBase.java    From datacollector with Apache License 2.0 6 votes vote down vote up
protected List<Stage.ConfigIssue> initWithNoIssues(
    RemoteConnector connector
) {
  List<Stage.ConfigIssue> issues = new ArrayList<>();
  Label remoteGroup = Mockito.mock(Label.class);
  Mockito.when(remoteGroup.getLabel()).thenReturn("SFTP/FTP/FTPS");
  Label credGroup = Mockito.mock(Label.class);
  Mockito.when(credGroup.getLabel()).thenReturn("CREDENTIALS");
  connector.initAndConnect(
      issues,
      Mockito.mock(Stage.Context.class),
      URI.create(connector.remoteConfig.remoteAddress),
      remoteGroup,
      credGroup
  );
  return issues;
}
 
Example #6
Source File: RemoteConnector.java    From datacollector with Apache License 2.0 6 votes vote down vote up
protected String resolveCredential(
    CredentialValue credentialValue,
    String config,
    List<Stage.ConfigIssue> issues,
    ConfigIssueContext context,
    Label group
) {
  try {
    return credentialValue.get();
  } catch (StageException e) {
    issues.add(context.createConfigIssue(
        group.getLabel(),
        config,
        Errors.REMOTE_08,
        e.toString()
    ));
  }
  return null;
}
 
Example #7
Source File: StageDefinition.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private List<String> getStreamLabels(ClassLoader classLoader, String streamsLabelProviderClass, boolean localized) {
  List<String> list = new ArrayList<>();
  if (streamsLabelProviderClass != null) {
    try {
      String rbName = (localized) ? streamsLabelProviderClass + "-bundle" : null;
      Class klass = classLoader.loadClass(streamsLabelProviderClass);
      boolean isLabel = Label.class.isAssignableFrom(klass);
      for (Object e : klass.getEnumConstants()) {

        String label = (isLabel) ? ((Label) e).getLabel() : ((Enum) e).name();
        if (rbName != null) {
          label = new LocalizableMessage(classLoader, rbName, ((Enum)e).name(), label, null).getLocalized();
        }
        list.add(label);
      }
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
  return list;
}
 
Example #8
Source File: BaseEnumChooserValues.java    From datacollector-api with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a <code>ChooserValues</code> with the specified enum values.
 *
 * @param enums enums for the <code>ChooserValues</code>.
 */
@SuppressWarnings("unchecked")
public BaseEnumChooserValues(T ... enums) {
  Utils.checkNotNull(enums, "enums");
  Utils.checkArgument(enums.length > 0, "array enum cannot have zero elements");
  resourceBundle = enums[0].getClass().getName() + "-bundle";
  boolean isEnumWithLabels = enums[0] instanceof Label;
  values = new ArrayList<>(enums.length);
  labels = new ArrayList<>(enums.length);
  for (T e : enums) {
    String value = e.name();
    values.add(value);
    String label = isEnumWithLabels ? ((Label)e).getLabel() : value;
    labels.add(label);
  }
  values = Collections.unmodifiableList(values);
  labels = Collections.unmodifiableList(labels);
}
 
Example #9
Source File: RemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected String resolveUsername(
    URI remoteURI,
    List<Stage.ConfigIssue> issues,
    ConfigIssueContext context,
    Label group
) {
  String userInfo = remoteURI.getUserInfo();
  if (userInfo != null) {
    if (userInfo.contains(USER_INFO_SEPARATOR)) {
      return userInfo.substring(0, userInfo.indexOf(USER_INFO_SEPARATOR));
    }
    return userInfo;
  }
  return resolveCredential(remoteConfig.username, CONF_PREFIX + "username", issues, context, group);
}
 
Example #10
Source File: RemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected String resolvePassword(
    URI remoteURI,
    List<Stage.ConfigIssue> issues,
    ConfigIssueContext context,
    Label group
) {
  String userInfo = remoteURI.getUserInfo();
  if (userInfo != null && userInfo.contains(USER_INFO_SEPARATOR)) {
    return userInfo.substring(userInfo.indexOf(USER_INFO_SEPARATOR) + 1);
  }
  return resolveCredential(remoteConfig.password, CONF_PREFIX + "password", issues, context, group);
}
 
Example #11
Source File: RemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected abstract void initAndConnect(
    List<Stage.ConfigIssue> issues,
    ConfigIssueContext context,
    URI remoteURI,
    Label remoteGroup,
    Label credGroup
);
 
Example #12
Source File: FTPRemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void setFtpsUserKeyManagerOrTrustManager(
    KeyStore keystore,
    String fileConfigName,
    String keyStorePassword,
    boolean isKeyStore, // or truststore
    List<Stage.ConfigIssue> issues,
    ConfigIssueContext context,
    Label group
) {
  try {
    if (isKeyStore) {
      FtpsFileSystemConfigBuilder.getInstance().setKeyManager(
          options,
          KeyManagerUtils.createClientKeyManager(keystore, null, keyStorePassword)
      );
    } else {
      FtpsFileSystemConfigBuilder.getInstance().setTrustManager(
          options,
          TrustManagerUtils.getDefaultTrustManager(keystore)
      );
    }
  } catch (GeneralSecurityException e) {
    issues.add(context.createConfigIssue(group.getLabel(),
        fileConfigName,
        Errors.REMOTE_15,
        isKeyStore ? "key" : "trust",
        e.getMessage(),
        e
    ));
  }
}
 
Example #13
Source File: ConfigGroupExtractor.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public ConfigGroupDefinition extract(Class klass, Object contextMsg) {
  List<ErrorMessage> errors = validate(klass, contextMsg);
  if (errors.isEmpty()) {
    List<ConfigGroups> allConfigGroups = getAllConfigGroups(klass);
    Set<String> allGroupNames = new HashSet<>();
    Map<String, List<String>> classNameToGroupsMap = new HashMap<>();
    List<Map<String, String>> groupNameToLabelMapList = new ArrayList<>();
    if (!allConfigGroups.isEmpty()) {
      for (ConfigGroups configGroups : allConfigGroups) {
        Class<? extends Label> gKlass = configGroups.value();
        List<String> groupNames = new ArrayList<>();
        classNameToGroupsMap.put(gKlass.getName(), groupNames);
        for (Label label : gKlass.getEnumConstants()) {
          String groupName = label.toString();
          Map<String, String> groupNameToLabelMap = new LinkedHashMap<>();
          allGroupNames.add(groupName);
          groupNames.add(groupName);
          groupNameToLabelMap.put("name", groupName);
          groupNameToLabelMap.put("label", label.getLabel());
          groupNameToLabelMapList.add(groupNameToLabelMap);
        }
      }
    }
    return new ConfigGroupDefinition(allGroupNames, classNameToGroupsMap, groupNameToLabelMapList);
  } else {
    throw new IllegalArgumentException(Utils.format("Invalid ConfigGroup definition: {}", errors));
  }
}
 
Example #14
Source File: TestRemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveCredential() throws Exception {
  Label group = Mockito.mock(Label.class);
  Mockito.when(group.getLabel()).thenReturn("CREDENTIALS");
  CredentialValue cred = Mockito.mock(CredentialValue.class);
  Mockito.when(cred.get()).thenReturn("bar");
  List<Stage.ConfigIssue> issues = new ArrayList<>();
  ConfigIssueContext context = Mockito.mock(ConfigIssueContext.class);
  RemoteConfigBean conf = new RemoteConfigBean();
  RemoteConnector connector = createRemoteConnector(conf);

  String resolved = connector.resolveCredential(cred, "foo", issues, context, group);
  Assert.assertEquals("bar", resolved);
  assertNumIssues(issues, 0);
}
 
Example #15
Source File: TestRemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveCredentialIssue() throws Exception {
  Label group = Mockito.mock(Label.class);
  Mockito.when(group.getLabel()).thenReturn("CREDENTIALS");
  CredentialValue cred = Mockito.mock(CredentialValue.class);
  Mockito.when(cred.get()).thenThrow(new StageException(Errors.REMOTE_08, "foo"));
  List<Stage.ConfigIssue> issues = new ArrayList<>();
  ConfigIssueContext context = Mockito.mock(ConfigIssueContext.class);
  Mockito.when(context.createConfigIssue(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
      .thenAnswer(new Answer<ConfigIssue>() {
        @Override
        public ConfigIssue answer(InvocationOnMock invocation) throws Throwable {
          String label = invocation.getArgumentAt(0, String.class);
          Assert.assertEquals(group.getLabel(), label);
          Errors error = invocation.getArgumentAt(2, Errors.class);
          Assert.assertEquals(Errors.REMOTE_08, error);

          String message = invocation.getArgumentAt(3, String.class);
          ConfigIssue configIssue = Mockito.mock(ConfigIssue.class);
          Mockito.when(configIssue.toString()).thenReturn(message);
          return configIssue;
        }
      });
  RemoteConfigBean conf = new RemoteConfigBean();
  RemoteConnector connector = createRemoteConnector(conf);

  String resolved = connector.resolveCredential(cred, "foo", issues, context, group);
  Assert.assertNull(resolved);
  assertNumIssues(issues, 1);
  Assert.assertTrue(issues.get(0).toString().contains("foo"));
}
 
Example #16
Source File: TestRemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveUsername() throws Exception {
  List<Stage.ConfigIssue> issues = new ArrayList<>();
  Label group = Mockito.mock(Label.class);
  Mockito.when(group.getLabel()).thenReturn("CREDENTIALS");
  ConfigIssueContext context = Mockito.mock(ConfigIssueContext.class);
  RemoteConfigBean conf = new RemoteConfigBean();
  CredentialValue cred = Mockito.mock(CredentialValue.class);
  Mockito.when(cred.get()).thenReturn("user3");
  conf.username = cred;

  URI uri = URI.create("sftp://user1:pass@host");
  RemoteConnector connector = createRemoteConnector(conf);
  String resolved = connector.resolveUsername(uri, issues, context, group);
  Assert.assertEquals("user1", resolved);
  Mockito.verify(connector,
      Mockito.never()).resolveCredential(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
  assertNumIssues(issues, 0);

  uri = URI.create("sftp://user2@host");
  resolved = connector.resolveUsername(uri, issues, context, group);
  Assert.assertEquals("user2", resolved);
  Mockito.verify(connector,
      Mockito.never()).resolveCredential(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
  assertNumIssues(issues, 0);

  uri = URI.create("sftp://host");
  resolved = connector.resolveUsername(uri, issues, context, group);
  Assert.assertEquals("user3", resolved);
  Mockito.verify(connector, Mockito.times(1))
      .resolveCredential(cred, "conf.remoteConfig.username", issues, context, group);
  assertNumIssues(issues, 0);
}
 
Example #17
Source File: TestRemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolvePassword() throws Exception {
  List<Stage.ConfigIssue> issues = new ArrayList<>();
  Label group = Mockito.mock(Label.class);
  Mockito.when(group.getLabel()).thenReturn("CREDENTIALS");
  ConfigIssueContext context = Mockito.mock(ConfigIssueContext.class);
  RemoteConfigBean conf = new RemoteConfigBean();
  CredentialValue cred = Mockito.mock(CredentialValue.class);
  Mockito.when(cred.get()).thenReturn("pass2");
  conf.password = cred;

  URI uri = URI.create("sftp://user:pass1@host");
  RemoteConnector connector = createRemoteConnector(conf);
  String resolved = connector.resolvePassword(uri, issues, context, group);
  Assert.assertEquals("pass1", resolved);
  Mockito.verify(connector,
      Mockito.never()).resolveCredential(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
  assertNumIssues(issues, 0);

  uri = URI.create("sftp://user@host");
  connector = createRemoteConnector(conf);
  resolved = connector.resolvePassword(uri, issues, context, group);
  Assert.assertEquals("pass2", resolved);
  Mockito.verify(connector, Mockito.times(1))
      .resolveCredential(cred, "conf.remoteConfig.password", issues, context, group);
  assertNumIssues(issues, 0);

  uri = URI.create("sftp://host");
  connector = createRemoteConnector(conf);
  resolved = connector.resolvePassword(uri, issues, context, group);
  Assert.assertEquals("pass2", resolved);
  Mockito.verify(connector, Mockito.times(1))
      .resolveCredential(cred, "conf.remoteConfig.password", issues, context, group);
  assertNumIssues(issues, 0);
}
 
Example #18
Source File: FTPRemoteConnector.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private KeyStore loadKeyStore(
    String file,
    String fileConfigName,
    String password,
    KeyStoreType keyStoreType,
    boolean isKeystore,
    List<Stage.ConfigIssue> issues,
    ConfigIssueContext context,
    Label group
) {
  KeyStore keyStore;
  if (file != null && !file.isEmpty()) {
    File keystoreFile = new File(file);
    try {
      keyStore = KeyStore.getInstance(keyStoreType.getJavaValue());
      char[] passwordArr = password != null ? password.toCharArray() : null;
      try (FileInputStream fin = new FileInputStream(file)) {
        keyStore.load(fin, passwordArr);
      }
    } catch (IOException | GeneralSecurityException e) {
      keyStore = null;
      issues.add(context.createConfigIssue(
          group.getLabel(),
          fileConfigName,
          Errors.REMOTE_14,
          isKeystore ? "key" : "trust",
          keystoreFile.getAbsolutePath(),
          e.getMessage(),
          e
      ));
    }
  } else {
    keyStore = null;
    if (isKeystore) {
      issues.add(context.createConfigIssue(group.getLabel(), fileConfigName, Errors.REMOTE_12));
    } else {
      issues.add(context.createConfigIssue(group.getLabel(), fileConfigName, Errors.REMOTE_13));
    }
  }
  return keyStore;
}