Search in sources :

Example 1 with NamespaceAwareExpression

use of org.apache.camel.model.language.NamespaceAwareExpression in project camel by apache.

the class RestContextRefDefinitionHelper method cloneRestDefinition.

private static RestDefinition cloneRestDefinition(JAXBContext jaxbContext, RestDefinition def) throws JAXBException {
    Marshaller marshal = jaxbContext.createMarshaller();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    marshal.marshal(def, bos);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object clone = unmarshaller.unmarshal(bis);
    if (clone != null && clone instanceof RestDefinition) {
        RestDefinition def2 = (RestDefinition) clone;
        Iterator<VerbDefinition> verbit1 = def.getVerbs().iterator();
        Iterator<VerbDefinition> verbit2 = def2.getVerbs().iterator();
        while (verbit1.hasNext() && verbit2.hasNext()) {
            VerbDefinition verb1 = verbit1.next();
            VerbDefinition verb2 = verbit2.next();
            if (verb1.getToOrRoute() instanceof RouteDefinition && verb2.getToOrRoute() instanceof RouteDefinition) {
                RouteDefinition route1 = (RouteDefinition) verb1.getToOrRoute();
                RouteDefinition route2 = (RouteDefinition) verb2.getToOrRoute();
                // need to clone the namespaces also as they are not JAXB marshalled (as they are transient)
                Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(route1.getOutputs(), ExpressionNode.class);
                Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(route2.getOutputs(), ExpressionNode.class);
                while (it.hasNext() && it2.hasNext()) {
                    ExpressionNode node = it.next();
                    ExpressionNode node2 = it2.next();
                    NamespaceAwareExpression name = null;
                    NamespaceAwareExpression name2 = null;
                    if (node.getExpression() instanceof NamespaceAwareExpression) {
                        name = (NamespaceAwareExpression) node.getExpression();
                    }
                    if (node2.getExpression() instanceof NamespaceAwareExpression) {
                        name2 = (NamespaceAwareExpression) node2.getExpression();
                    }
                    if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) {
                        Map<String, String> map = new HashMap<String, String>();
                        map.putAll(name.getNamespaces());
                        name2.setNamespaces(map);
                    }
                }
            }
        }
        return def2;
    }
    return null;
}
Also used : Marshaller(javax.xml.bind.Marshaller) RestDefinition(org.apache.camel.model.rest.RestDefinition) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) NamespaceAwareExpression(org.apache.camel.model.language.NamespaceAwareExpression)

Example 2 with NamespaceAwareExpression

use of org.apache.camel.model.language.NamespaceAwareExpression in project camel by apache.

the class RouteContextRefDefinitionHelper method cloneRouteDefinition.

private static RouteDefinition cloneRouteDefinition(JAXBContext jaxbContext, RouteDefinition def) throws JAXBException {
    Marshaller marshal = jaxbContext.createMarshaller();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    marshal.marshal(def, bos);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object clone = unmarshaller.unmarshal(bis);
    if (clone != null && clone instanceof RouteDefinition) {
        RouteDefinition def2 = (RouteDefinition) clone;
        // need to clone the namespaces also as they are not JAXB marshalled (as they are transient)
        Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(def.getOutputs(), ExpressionNode.class);
        Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(def2.getOutputs(), ExpressionNode.class);
        while (it.hasNext() && it2.hasNext()) {
            ExpressionNode node = it.next();
            ExpressionNode node2 = it2.next();
            NamespaceAwareExpression name = null;
            NamespaceAwareExpression name2 = null;
            if (node.getExpression() instanceof NamespaceAwareExpression) {
                name = (NamespaceAwareExpression) node.getExpression();
            }
            if (node2.getExpression() instanceof NamespaceAwareExpression) {
                name2 = (NamespaceAwareExpression) node2.getExpression();
            }
            if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) {
                Map<String, String> map = new HashMap<String, String>();
                map.putAll(name.getNamespaces());
                name2.setNamespaces(map);
            }
        }
        return def2;
    }
    return null;
}
Also used : Marshaller(javax.xml.bind.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) NamespaceAwareExpression(org.apache.camel.model.language.NamespaceAwareExpression)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Marshaller (javax.xml.bind.Marshaller)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 NamespaceAwareExpression (org.apache.camel.model.language.NamespaceAwareExpression)2 RestDefinition (org.apache.camel.model.rest.RestDefinition)1 VerbDefinition (org.apache.camel.model.rest.VerbDefinition)1