use of io.atlasmap.v2.DataSource in project syndesis-qe by syndesisio.
the class AtlasMapperGenerator method getAtlasMappingStep.
/**
* This method is used to generate the "AtlasMapping" - the atlasMapping contains list of specifications of
* dataSources and a list of specifications of dataMappings. Both these a must have for a complete and working
* AtlasMapping.
*
* @return step with the mapping defined
*/
public Step getAtlasMappingStep() {
processPrecedingSteps();
processFollowingStep();
AtlasMapping atlasMapping = new AtlasMapping();
atlasMapping.setMappings(new Mappings());
for (DataSource s : processSources()) {
atlasMapping.getDataSource().add(s);
}
atlasMapping.setName("REST." + UUID.randomUUID().toString().replaceAll("-", ""));
atlasMapping.setLookupTables(new LookupTables());
atlasMapping.setProperties(new Properties());
atlasMapping.getDataSource().add(processTarget());
atlasMapping.getMappings().getMapping().addAll(generateBaseMappings());
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
String mapperString = null;
try {
mapperString = mapper.writeValueAsString(atlasMapping);
log.debug(mapperString);
} catch (JsonProcessingException e) {
log.error("Unable to write mapper json as string", e);
}
return new Step.Builder().stepKind(StepKind.mapper).name(mapping.getStep().getName()).configuredProperties(TestUtils.map("atlasmapping", mapperString)).action(getMapperStepAction(followingStep.getStep().getAction().get().getInputDataShape().get())).id(UUID.randomUUID().toString()).build();
}
use of io.atlasmap.v2.DataSource in project syndesis-qe by syndesisio.
the class AtlasMapperGenerator method createDataSource.
/**
* Used to generate data source elements for atlasMapping. There are three types of them: Java, Json, XML.
*
* @param dataShape datashape of the processed step
* @param step step definition
* @param dataSourceType datasource type
* @return datasource
*/
private DataSource createDataSource(DataShape dataShape, StepDefinition step, DataSourceType dataSourceType) {
DataShapeKinds dataShapeKind = dataShape.getKind();
DataSource source = null;
if (dataShapeKind.toString().contains("json")) {
source = new JsonDataSource();
source.setUri("atlas:" + "json:" + step.getStep().getId().get());
} else if (dataShapeKind.toString().contains("java")) {
source = new DataSource();
source.setUri("atlas:" + "java:" + step.getStep().getId().get() + "?className=" + dataShape.getType());
} else if (dataShapeKind.toString().contains("xml")) {
source = new XmlDataSource();
source.setUri("atlas:xml:" + step.getStep().getId().get());
XmlNamespaces xmlNamespaces = new XmlNamespaces();
// Init the array, so that we don't have the null value
xmlNamespaces.getXmlNamespace();
((XmlDataSource) source).setXmlNamespaces(xmlNamespaces);
} else {
fail("Unknown datashape kind " + dataShapeKind.toString());
}
source.setId(step.getStep().getId().get());
source.setDataSourceType(dataSourceType);
return source;
}
use of io.atlasmap.v2.DataSource in project atlasmap by atlasmap.
the class BaseMappingTest method getAtlasMappingFullValid.
protected AtlasMapping getAtlasMappingFullValid() {
AtlasMapping mapping = AtlasModelFactory.createAtlasMapping();
mapping.setName("thisis_a_valid.name");
DataSource src = new DataSource();
src.setDataSourceType(DataSourceType.SOURCE);
src.setUri("atlas:java?2");
DataSource tgt = new DataSource();
tgt.setDataSourceType(DataSourceType.TARGET);
tgt.setUri("atlas:java?3");
mapping.getDataSource().add(src);
mapping.getDataSource().add(tgt);
Mapping mapFieldMapping = AtlasModelFactory.createMapping(MappingType.MAP);
MockField inputField = AtlasModelFactory.createMockField();
inputField.setFieldType(FieldType.STRING);
inputField.setCustom("java.lang.String");
inputField.setName("inputName");
mapFieldMapping.getInputField().add(inputField);
MockField outputField = AtlasModelFactory.createMockField();
outputField.setFieldType(FieldType.STRING);
outputField.setCustom("java.lang.String");
outputField.setName("outputName");
mapFieldMapping.getOutputField().add(outputField);
Mapping separateMapping = AtlasModelFactory.createMapping(MappingType.SEPARATE);
MockField sIJavaField = AtlasModelFactory.createMockField();
sIJavaField.setFieldType(FieldType.STRING);
sIJavaField.setCustom("java.lang.String");
sIJavaField.setName("inputName");
separateMapping.getInputField().add(sIJavaField);
MockField sOJavaField = AtlasModelFactory.createMockField();
sOJavaField.setFieldType(FieldType.STRING);
sOJavaField.setCustom("java.lang.String");
sOJavaField.setName("outputName");
sOJavaField.setIndex(0);
separateMapping.getOutputField().add(sOJavaField);
mapping.getMappings().getMapping().add(mapFieldMapping);
mapping.getMappings().getMapping().add(separateMapping);
return mapping;
}
use of io.atlasmap.v2.DataSource in project atlasmap by atlasmap.
the class AtlasEndpoint method populateTargetDocuments.
private void populateTargetDocuments(AtlasSession session, Exchange exchange) {
Message outMessage = exchange.getOut();
outMessage.setHeaders(exchange.getIn().getHeaders());
outMessage.setAttachments(exchange.getIn().getAttachments());
if (session.getMapping().getDataSource() == null) {
return;
}
DataSource[] targetDataSources = session.getMapping().getDataSource().stream().filter(ds -> ds.getDataSourceType() == DataSourceType.TARGET).toArray(DataSource[]::new);
if (targetDataSources.length == 0) {
Object newBody = session.getDefaultTargetDocument();
outMessage.setBody(newBody);
return;
}
if (targetDataSources.length == 1) {
String docId = targetDataSources[0].getId();
if (docId == null || docId.isEmpty()) {
Object newBody = session.getDefaultTargetDocument();
outMessage.setBody(newBody);
} else {
Object newBody = session.getTargetDocument(docId);
outMessage.setBody(newBody);
}
setContentType(targetDataSources[0], outMessage);
return;
}
Map<String, Object> targetDocuments = new HashMap<>();
for (DataSource ds : targetDataSources) {
String docId = ds.getId();
if (docId == null || docId.isEmpty()) {
targetDocuments.put(io.atlasmap.api.AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID, session.getDefaultTargetDocument());
Object newBody = session.getDefaultTargetDocument();
outMessage.setBody(newBody);
setContentType(ds, outMessage);
} else {
targetDocuments.put(docId, session.getTargetDocument(docId));
}
}
switch(targetMapMode) {
case MAP:
if (targetMapName != null) {
exchange.setProperty(targetMapName, targetDocuments);
} else {
outMessage.setBody(targetDocuments);
}
break;
case MESSAGE_HEADER:
targetDocuments.remove(io.atlasmap.api.AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID);
outMessage.getHeaders().putAll(targetDocuments);
break;
case EXCHANGE_PROPERTY:
targetDocuments.remove(io.atlasmap.api.AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID);
exchange.getProperties().putAll(targetDocuments);
break;
}
}
use of io.atlasmap.v2.DataSource in project atlasmap by atlasmap.
the class AtlasEndpoint method populateSourceDocuments.
private void populateSourceDocuments(Exchange exchange, AtlasSession session) {
if (session.getMapping().getDataSource() == null) {
return;
}
Message inMessage = exchange.getIn();
CamelAtlasPropertyStrategy propertyStrategy = new CamelAtlasPropertyStrategy();
propertyStrategy.setCurrentSourceMessage(inMessage);
propertyStrategy.setTargetMessage(exchange.getMessage());
propertyStrategy.setExchange(exchange);
session.setAtlasPropertyStrategy(propertyStrategy);
DataSource[] sourceDataSources = session.getMapping().getDataSource().stream().filter(ds -> ds.getDataSourceType() == DataSourceType.SOURCE).toArray(DataSource[]::new);
if (sourceDataSources.length == 0) {
session.setDefaultSourceDocument(exchange.getIn().getBody());
return;
}
if (sourceDataSources.length == 1) {
String docId = sourceDataSources[0].getId();
Object payload = extractPayload(sourceDataSources[0], inMessage);
if (docId == null || docId.isEmpty()) {
session.setDefaultSourceDocument(payload);
} else {
session.setSourceDocument(docId, payload);
propertyStrategy.setSourceMessage(docId, inMessage);
}
return;
}
Map<String, Message> sourceMessages = null;
Map<String, Object> sourceDocuments = null;
if (sourceMapName != null) {
sourceMessages = exchange.getProperty(sourceMapName, Map.class);
}
if (sourceMessages == null) {
Object body = exchange.getIn().getBody();
if (body instanceof Map) {
sourceDocuments = (Map<String, Object>) body;
} else {
session.setDefaultSourceDocument(body);
}
}
for (DataSource ds : sourceDataSources) {
String docId = ds.getId();
if (docId == null || docId.isEmpty()) {
Object payload = extractPayload(ds, inMessage);
session.setDefaultSourceDocument(payload);
} else if (sourceMessages != null) {
Object payload = extractPayload(ds, sourceMessages.get(docId));
session.setSourceDocument(docId, payload);
propertyStrategy.setSourceMessage(docId, sourceMessages.get(docId));
} else if (sourceDocuments != null) {
Object payload = sourceDocuments.get(docId);
session.setSourceDocument(docId, payload);
} else if (inMessage.getHeaders().containsKey(docId)) {
Object payload = inMessage.getHeader(docId);
session.setSourceDocument(docId, payload);
} else if (exchange.getProperties().containsKey(docId)) {
Object payload = exchange.getProperty(docId);
session.setSourceDocument(docId, payload);
} else {
LOG.warn("Ignoring missing source document: '{}(ID:{})'", ds.getName(), ds.getId());
}
}
}
Aggregations