use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JsonFieldWriter method write.
@Override
public void write(AtlasInternalSession session) throws AtlasException {
Field targetField = session.head().getTargetField();
if (targetField == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'jsonField' cannot be null"));
}
if (LOG.isDebugEnabled()) {
LOG.debug("Field: " + AtlasModelFactory.toString(targetField));
LOG.debug("Field type=" + targetField.getFieldType() + " path=" + targetField.getPath() + " v=" + targetField.getValue());
}
AtlasPath path = new AtlasPath(targetField.getPath());
String lastSegment = path.getLastSegment();
ObjectNode parentNode = this.rootNode;
String parentSegment = null;
for (String segment : path.getSegments()) {
if (!segment.equals(lastSegment)) {
// this is a parent node.
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing parent segment: " + segment);
}
JsonNode childNode = getChildNode(parentNode, parentSegment, segment);
if (childNode == null) {
childNode = createParentNode(parentNode, parentSegment, segment);
} else if (childNode instanceof ArrayNode) {
int index = AtlasPath.indexOfSegment(segment);
ArrayNode arrayChild = (ArrayNode) childNode;
if (arrayChild.size() < (index + 1)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Object Array is too small, resizing to accomodate index: " + index + ", current array: " + arrayChild);
}
// index available
while (arrayChild.size() < (index + 1)) {
arrayChild.addObject();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Object Array after resizing: " + arrayChild);
}
}
childNode = arrayChild.get(index);
}
parentNode = (ObjectNode) childNode;
parentSegment = segment;
} else {
// this is the last segment of the path, write the value
if (targetField.getFieldType() == FieldType.COMPLEX) {
createParentNode(parentNode, parentSegment, segment);
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing field value segment: " + segment);
}
writeValue(parentNode, parentSegment, segment, targetField);
}
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class XmlModule method getCollectionSize.
@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
// TODO could this use FieldReader?
try {
Object sourceObject = session.getSourceDocument(getDocId());
Document document = getDocument((String) sourceObject, false);
Element parentNode = document.getDocumentElement();
for (SegmentContext sc : new XmlPath(field.getPath()).getSegmentContexts(false)) {
if (sc.getPrev() == null) {
// "/XOA/contact<>/firstName", skip.
continue;
}
String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
if (namespaceAlias != null && !"".equals(namespaceAlias)) {
childrenElementName = namespaceAlias + ":" + childrenElementName;
}
List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
if (children == null || children.isEmpty()) {
return 0;
}
if (XmlPath.isCollectionSegment(sc.getSegment())) {
return children.size();
}
parentNode = children.get(0);
}
return 0;
} catch (Exception e) {
throw new AtlasException(e);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class AtlasEndpoint method onExchange.
@Override
protected void onExchange(Exchange exchange) throws Exception {
Message incomingMessage = exchange.getIn();
String newResourceUri = incomingMessage.getHeader(AtlasConstants.ATLAS_RESOURCE_URI, String.class);
if (newResourceUri != null) {
incomingMessage.removeHeader(AtlasConstants.ATLAS_RESOURCE_URI);
log.debug("{} set to {} creating new endpoint to handle exchange", AtlasConstants.ATLAS_RESOURCE_URI, newResourceUri);
AtlasEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
newEndpoint.onExchange(exchange);
return;
}
AtlasSession atlasSession = getOrCreateAtlasContext(incomingMessage).createSession();
populateSourceDocuments(exchange, atlasSession);
getAtlasContext().process(atlasSession);
List<Audit> errors = new ArrayList<>();
for (Audit audit : atlasSession.getAudits().getAudit()) {
switch(audit.getStatus()) {
case ERROR:
errors.add(audit);
break;
case WARN:
LOG.warn("{}: docId='{}', path='{}'", audit.getMessage(), audit.getDocId(), audit.getPath());
break;
default:
LOG.info("{}: docId='{}', path='{}'", audit.getMessage(), audit.getDocId(), audit.getPath());
}
}
if (!errors.isEmpty()) {
StringBuilder buf = new StringBuilder("Errors: ");
errors.stream().forEach(a -> buf.append(String.format("[%s: docId='%s', path='%s'], ", a.getMessage(), a.getDocId(), a.getPath())));
throw new AtlasException(buf.toString());
}
populateTargetDocuments(atlasSession, exchange);
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DefaultAtlasContext method processValidation.
@Override
public void processValidation(AtlasSession userSession) throws AtlasException {
if (!(userSession instanceof DefaultAtlasSession)) {
throw new AtlasException(String.format("Unsupported session class '%s'", userSession.getClass().getName()));
}
if (!this.equals(userSession.getAtlasContext())) {
throw new AtlasException("Cannot execute AtlasSession created by the other AtlasContext");
}
DefaultAtlasSession session = (DefaultAtlasSession) userSession;
if (LOG.isDebugEnabled()) {
LOG.debug("Begin processValidation {}", session == null ? null : session.toString());
}
List<Validation> validations = getContextFactory().getValidationService().validateMapping(session.getMapping());
if (validations != null && !validations.isEmpty()) {
session.getValidations().getValidation().addAll(validations);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Detected {} core validation notices", validations.size());
}
for (AtlasModule module : getSourceModules().values()) {
module.processPreValidation(session);
}
for (AtlasModule module : getTargetModules().values()) {
module.processPreValidation(session);
}
if (LOG.isDebugEnabled()) {
LOG.debug("End processValidation {}", session == null ? null : session.toString());
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DefaultAtlasFieldActionService method processAction.
protected Object processAction(Action action, ActionDetail actionDetail, Object sourceObject) throws AtlasException {
Object targetObject = null;
if (actionDetail != null) {
Object actionObject = null;
try {
Class<?> actionClazz = Class.forName(actionDetail.getClassName());
actionObject = actionClazz.newInstance();
Method method = null;
if (actionDetail.getSourceType() != null) {
switch(actionDetail.getSourceType()) {
case ANY:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Object.class);
break;
case BIG_INTEGER:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigInteger.class);
break;
case BOOLEAN:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Boolean.class);
break;
case BYTE:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte.class);
break;
case BYTE_ARRAY:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte[].class);
break;
case CHAR:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Character.class);
break;
case DATE:
case DATE_TIME:
case DATE_TZ:
case TIME_TZ:
case DATE_TIME_TZ:
case ANY_DATE:
if (sourceObject instanceof Calendar) {
sourceObject = DateTimeHelper.toZonedDateTime((Calendar) sourceObject);
} else if (sourceObject instanceof Date) {
sourceObject = DateTimeHelper.toZonedDateTime((Date) sourceObject, null);
} else if (sourceObject instanceof LocalDate) {
sourceObject = DateTimeHelper.toZonedDateTime((LocalDate) sourceObject, null);
} else if (sourceObject instanceof LocalTime) {
sourceObject = DateTimeHelper.toZonedDateTime((LocalTime) sourceObject, null);
} else if (sourceObject instanceof LocalDateTime) {
sourceObject = DateTimeHelper.toZonedDateTime((LocalDateTime) sourceObject, null);
} else if (!(sourceObject instanceof ZonedDateTime)) {
LOG.warn(String.format("Unsupported sourceObject type=%s in actionClass=%s", sourceObject.getClass(), actionDetail.getClassName()));
break;
}
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, ZonedDateTime.class);
break;
case DECIMAL:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigDecimal.class);
break;
case DOUBLE:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Double.class);
break;
case FLOAT:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Float.class);
break;
case INTEGER:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Integer.class);
break;
case LONG:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Long.class);
break;
case NUMBER:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Number.class);
break;
case SHORT:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Short.class);
break;
case STRING:
method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, String.class);
break;
default:
LOG.warn(String.format("Unsupported sourceType=%s in actionClass=%s", actionDetail.getSourceType().value(), actionDetail.getClassName()));
break;
}
}
if (method == null) {
throw new AtlasException(String.format("Unable to locate field action className=%s method=%s sourceType=%s", actionDetail.getClassName(), actionDetail.getMethod(), actionDetail.getSourceType()));
}
if (Modifier.isStatic(method.getModifiers())) {
targetObject = method.invoke(null, action, sourceObject);
} else {
targetObject = method.invoke(actionObject, action, sourceObject);
}
} catch (Throwable e) {
throw new AtlasException(String.format("Error processing action %s", actionDetail.getName()), e);
}
return targetObject;
}
return sourceObject;
}
Aggregations