Search in sources :

Example 26 with JAXBElement

use of javax.xml.bind.JAXBElement in project midpoint by Evolveum.

the class JaxbTestUtil method compareElement.

@SuppressWarnings("unchecked")
private boolean compareElement(Object a, Object b) {
    if (a == b) {
        return true;
    }
    if (a == null && b == null) {
        return true;
    }
    if (a == null || b == null) {
        return false;
    }
    Document doc = null;
    Element ae = null;
    Element be = null;
    if (a instanceof Element) {
        ae = (Element) a;
    } else if (a instanceof JAXBElement) {
        if (doc == null) {
            doc = DOMUtil.getDocument();
        }
        try {
            ae = marshalElementToDom((JAXBElement) a, doc);
        } catch (JAXBException e) {
            throw new IllegalStateException("Failed to marshall element " + a, e);
        }
    } else {
        throw new IllegalArgumentException("Got unexpected type " + a.getClass().getName() + ": " + a);
    }
    if (b instanceof Element) {
        be = (Element) b;
    } else if (a instanceof JAXBElement) {
        if (doc == null) {
            doc = DOMUtil.getDocument();
        }
        try {
            be = marshalElementToDom((JAXBElement) a, doc);
        } catch (JAXBException e) {
            throw new IllegalStateException("Failed to marshall element " + b, e);
        }
    } else {
        throw new IllegalArgumentException("Got unexpected type " + b.getClass().getName() + ": " + b);
    }
    return DOMUtil.compareElement(ae, be, true);
}
Also used : JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) Document(org.w3c.dom.Document)

Example 27 with JAXBElement

use of javax.xml.bind.JAXBElement in project midpoint by Evolveum.

the class JaxbTestUtil method unmarshalElement.

public <T> JAXBElement<T> unmarshalElement(File file, Class<T> type) throws SchemaException, FileNotFoundException, JAXBException {
    if (file == null) {
        throw new IllegalArgumentException("File argument must not be null.");
    }
    InputStream is = null;
    try {
        is = new FileInputStream(file);
        JAXBElement<T> element = (JAXBElement<T>) getUnmarshaller().unmarshal(is);
        adopt(element);
        return element;
    } catch (RuntimeException ex) {
        throw new SystemException(ex);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
}
Also used : SystemException(com.evolveum.midpoint.util.exception.SystemException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JAXBElement(javax.xml.bind.JAXBElement) FileInputStream(java.io.FileInputStream)

Example 28 with JAXBElement

use of javax.xml.bind.JAXBElement in project midpoint by Evolveum.

the class JaxbTestUtil method marshalToDom.

public void marshalToDom(Objectable objectable, Node parentNode) throws JAXBException {
    QName elementQName = determineElementQName(objectable);
    JAXBElement<Object> jaxbElement = new JAXBElement<Object>(elementQName, (Class) objectable.getClass(), objectable);
    marshalElementToDom(jaxbElement, parentNode);
}
Also used : QName(javax.xml.namespace.QName) PrismObject(com.evolveum.midpoint.prism.PrismObject) JAXBElement(javax.xml.bind.JAXBElement)

Example 29 with JAXBElement

use of javax.xml.bind.JAXBElement in project midpoint by Evolveum.

the class ProtectedDataType method addContent.

private boolean addContent(Object newObject) {
    if (newObject instanceof String) {
        String s = (String) newObject;
        if (StringUtils.isNotBlank(s)) {
            clearValue = (T) s;
        }
        return true;
    } else if (newObject instanceof JAXBElement<?>) {
        JAXBElement<?> jaxbElement = (JAXBElement<?>) newObject;
        if (QNameUtil.match(F_ENCRYPTED_DATA, jaxbElement.getName())) {
            encryptedDataType = (EncryptedDataType) jaxbElement.getValue();
            return true;
        } else if (QNameUtil.match(F_HASHED_DATA, jaxbElement.getName())) {
            hashedDataType = (HashedDataType) jaxbElement.getValue();
            return true;
        } else {
            throw new IllegalArgumentException("Attempt to add unknown JAXB element " + jaxbElement);
        }
    } else if (newObject instanceof Element) {
        Element element = (Element) newObject;
        QName elementName = DOMUtil.getQName(element);
        if (QNameUtil.match(F_XML_ENC_ENCRYPTED_DATA, elementName)) {
            encryptedDataType = convertXmlEncToEncryptedDate(element);
            return true;
        } else if (QNameUtil.match(F_CLEAR_VALUE, elementName)) {
            clearValue = (T) element.getTextContent();
            return true;
        } else {
            throw new IllegalArgumentException("Attempt to add unknown DOM element " + elementName);
        }
    } else {
        throw new IllegalArgumentException("Attempt to add unknown object " + newObject + " (" + newObject.getClass() + ")");
    }
}
Also used : QName(javax.xml.namespace.QName) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) JAXBElement(javax.xml.bind.JAXBElement)

Example 30 with JAXBElement

use of javax.xml.bind.JAXBElement in project midpoint by Evolveum.

the class SearchEvaluator method evaluate.

public <T extends ObjectType> PipelineData evaluate(SearchExpressionType searchExpression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    Validate.notNull(searchExpression.getType());
    boolean noFetch = expressionHelper.getArgumentAsBoolean(searchExpression.getParameter(), PARAM_NO_FETCH, input, context, false, "search", globalResult);
    @SuppressWarnings({ "unchecked", "raw" }) Class<T> objectClass = (Class<T>) ObjectTypes.getObjectTypeFromTypeQName(searchExpression.getType()).getClassDefinition();
    ObjectQuery objectQuery = null;
    if (searchExpression.getQuery() != null) {
        try {
            objectQuery = QueryJaxbConvertor.createObjectQuery(objectClass, searchExpression.getQuery(), prismContext);
        } catch (SchemaException e) {
            throw new ScriptExecutionException("Couldn't parse object query due to schema exception", e);
        }
    } else if (searchExpression.getSearchFilter() != null) {
        // todo resolve variable references in the filter
        objectQuery = new ObjectQuery();
        try {
            ObjectFilter filter = QueryConvertor.parseFilter(searchExpression.getSearchFilter(), objectClass, prismContext);
            objectQuery.setFilter(filter);
        } catch (SchemaException e) {
            throw new ScriptExecutionException("Couldn't parse object filter due to schema exception", e);
        }
    }
    final String variableName = searchExpression.getVariable();
    final PipelineData oldVariableValue = variableName != null ? context.getVariable(variableName) : null;
    final PipelineData outputData = PipelineData.createEmpty();
    final MutableBoolean atLeastOne = new MutableBoolean(false);
    ResultHandler<T> handler = (object, parentResult) -> {
        context.checkTaskStop();
        atLeastOne.setValue(true);
        if (searchExpression.getScriptingExpression() != null) {
            if (variableName != null) {
                context.setVariable(variableName, PipelineData.create(object.getValue()));
            }
            JAXBElement<?> childExpression = searchExpression.getScriptingExpression();
            try {
                outputData.addAllFrom(scriptingExpressionEvaluator.evaluateExpression((ScriptingExpressionType) childExpression.getValue(), PipelineData.create(object.getValue()), context, globalResult));
                globalResult.setSummarizeSuccesses(true);
                globalResult.summarize();
            } catch (ScriptExecutionException e) {
                // todo think about this
                if (context.isContinueOnAnyError()) {
                    LoggingUtils.logUnexpectedException(LOGGER, "Exception when evaluating item from search result list.", e);
                } else {
                    throw new SystemException(e);
                }
            }
        } else {
            outputData.addValue(object.getValue());
        }
        return true;
    };
    try {
        Collection<SelectorOptions<GetOperationOptions>> options = operationsHelper.createGetOptions(searchExpression.getOptions(), noFetch);
        modelService.searchObjectsIterative(objectClass, objectQuery, handler, options, context.getTask(), globalResult);
    } catch (SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
        // TODO continue on any error?
        throw new ScriptExecutionException("Couldn't execute searchObjects operation: " + e.getMessage(), e);
    }
    if (atLeastOne.isFalse()) {
        String matching = objectQuery != null ? "matching " : "";
        // temporary hack, this will be configurable
        context.println("Warning: no " + matching + searchExpression.getType().getLocalPart() + " object found");
    }
    if (variableName != null) {
        context.setVariable(variableName, oldVariableValue);
    }
    return outputData;
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) OperationsHelper(com.evolveum.midpoint.model.impl.scripting.helpers.OperationsHelper) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Trace(com.evolveum.midpoint.util.logging.Trace) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) ExpressionHelper(com.evolveum.midpoint.model.impl.scripting.helpers.ExpressionHelper) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) JAXBElement(javax.xml.bind.JAXBElement) Collection(java.util.Collection) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) QueryConvertor(com.evolveum.midpoint.prism.marshaller.QueryConvertor) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) QueryJaxbConvertor(com.evolveum.midpoint.prism.query.QueryJaxbConvertor) Component(org.springframework.stereotype.Component) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) SearchExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType) Validate(org.apache.commons.lang.Validate) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) JAXBElement(javax.xml.bind.JAXBElement) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)210 QName (javax.xml.namespace.QName)71 ArrayList (java.util.ArrayList)43 Test (org.junit.Test)42 JAXBException (javax.xml.bind.JAXBException)28 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)28 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)24 JAXBContext (javax.xml.bind.JAXBContext)19 Unmarshaller (javax.xml.bind.Unmarshaller)18 Marshaller (javax.xml.bind.Marshaller)16 LineString (com.vividsolutions.jts.geom.LineString)15 StringWriter (java.io.StringWriter)14 List (java.util.List)14 Element (org.w3c.dom.Element)13 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)12 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)10 PrismObject (com.evolveum.midpoint.prism.PrismObject)9 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)9