Skip to content

Commit

Permalink
Add implementation for schema guessing from the example file in the HTTP
Browse files Browse the repository at this point in the history
resolves #1637
fixes #1518 for streampipes-extensions
  • Loading branch information
obermeier committed Jun 1, 2023
1 parent 5e3f367 commit 1cb22eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Expand Up @@ -257,6 +257,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<propertyExpansion>
checkstyle.config.base.path=${project.parent.parent.basedir}/tools/maven
</propertyExpansion>
</configuration>
</plugin>
</plugins>
<finalName>streampipes-connect-adapters-iiot</finalName>
Expand Down
Expand Up @@ -18,6 +18,8 @@
package org.apache.streampipes.connect.iiot.protocol.stream;

import org.apache.streampipes.commons.exceptions.connect.AdapterException;
import org.apache.streampipes.commons.exceptions.connect.ParseException;
import org.apache.streampipes.connect.iiot.utils.FileProtocolUtils;
import org.apache.streampipes.extensions.api.connect.IAdapterConfiguration;
import org.apache.streampipes.extensions.api.connect.IEventCollector;
import org.apache.streampipes.extensions.api.connect.StreamPipesAdapter;
Expand Down Expand Up @@ -46,6 +48,8 @@
import org.apache.streampipes.sdk.utils.Assets;
import org.apache.streampipes.sdk.utils.Datatypes;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -134,22 +138,36 @@ public void onAdapterStopped(IAdapterParameterExtractor extractor,
@Override
public GuessSchema onSchemaRequested(IAdapterParameterExtractor parameterExtractor,
IAdapterGuessSchemaContext adapterGuessSchemaContext) throws AdapterException {
IStaticPropertyExtractor extractor = parameterExtractor.getStaticPropertyExtractor();
applyConfiguration(extractor);
IStaticPropertyExtractor propertyExtractor = parameterExtractor.getStaticPropertyExtractor();
applyConfiguration(propertyExtractor);
GuessSchemaBuilder schemaBuilder = GuessSchemaBuilder.create();

String selectedImportMode = extractor.selectedAlternativeInternalId(CONFIGURE);

String selectedImportMode = propertyExtractor.selectedAlternativeInternalId(CONFIGURE);
if (selectedImportMode.equals(MANUALLY)) {
CollectionStaticProperty sp = (CollectionStaticProperty) extractor.getStaticPropertyByName(EP_CONFIG);
CollectionStaticProperty sp = (CollectionStaticProperty) propertyExtractor.getStaticPropertyByName(EP_CONFIG);

for (StaticProperty member : sp.getMembers()) {
StaticPropertyExtractor memberExtractor =
StaticPropertyExtractor.from(((StaticPropertyGroup) member).getStaticProperties(), new ArrayList<>());
schemaBuilder.property(makeProperty(memberExtractor));
}
return schemaBuilder.build();
} else if (selectedImportMode.equals(FILE_IMPORT)){
String fileName = propertyExtractor.selectedFilename(FILE);
InputStream dataInputStream = this.getDataFromEndpoint(fileName);

return parameterExtractor.selectedParser().getGuessSchema(dataInputStream);
} else {
throw new AdapterException("Unknown import mode selected: " + selectedImportMode);
}
}

return schemaBuilder.build();

private InputStream getDataFromEndpoint(String fileName) throws ParseException {
try {
return FileProtocolUtils.getFileInputStream(fileName);
} catch (IOException e) {
throw new ParseException("Could not find file: " + fileName);
}
}
}

0 comments on commit 1cb22eb

Please sign in to comment.