DurboContentBuilder.java 21.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.durbo.DurboNamespaceResolver
 *  com.day.durbo.DurboOutput
 *  javax.jcr.Item
 *  javax.jcr.NamespaceException
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.Workspace
 *  javax.jcr.nodetype.NodeType
 *  javax.jcr.version.Version
 *  javax.jcr.version.VersionException
 *  javax.jcr.version.VersionHistory
 *  javax.jcr.version.VersionManager
 *  org.apache.commons.io.IOUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl.content.durbo;

import com.day.cq.replication.ContentBuilder;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationContentFilterChain;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.impl.content.durbo.DurboUtil;
import com.day.durbo.DurboNamespaceResolver;
import com.day.durbo.DurboOutput;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Item;
import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.jcr.version.Version;
import javax.jcr.version.VersionException;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionManager;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Properties(value={@org.apache.felix.scr.annotations.Property(name="service.description", value={"Adobe Granite Durbo Content Builder"}), @org.apache.felix.scr.annotations.Property(name="name", value={"durbo"})})
@Service(value={ContentBuilder.class})
public class DurboContentBuilder
implements ContentBuilder {
    private static final String PN_CHILDREN_ORDER = "cq:childrenOrder";
    private static final String PN_SIBLING_ORDER = "cq:siblingOrder";
    public static final String NAME = "durbo";
    public static final String TITLE = "Durbo";
    private String name;
    private final Logger log;
    @Reference
    protected SlingRepository repository;

    public DurboContentBuilder() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Activate
    protected void activate(Map<String, Object> props) {
        this.name = PropertiesUtil.toString((Object)props.get("name"), (String)"durbo");
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getTitle() {
        return "Durbo";
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory factory, Map<String, Object> parameters) throws ReplicationException {
        ReplicationContent replicationContent;
        block14 : {
            if (action.getType() != ReplicationActionType.ACTIVATE) {
                return ReplicationContent.VOID;
            }
            ReplicationContentFilterChain filters = parameters == null ? ReplicationContentFilterChain.ALLOW_ALL : (ReplicationContentFilterChain)parameters.get(ContentBuilder.PARAMETER_CONTENT_FILER_CHAIN);
            Node node = (Node)session.getItem(action.getPath());
            if (action.getRevision() == null) {
                return this.create(factory, node, filters, parameters);
            }
            Session admin = null;
            try {
                admin = this.repository.loginService("replicationService", null);
                node = admin.getNode(node.getPath());
                replicationContent = this.create(factory, node, action.getRevision(), filters, parameters);
                if (admin == null) break block14;
            }
            catch (Throwable var10_13) {
                try {
                    if (admin != null) {
                        try {
                            admin.logout();
                        }
                        catch (Exception var11_14) {
                            // empty catch block
                        }
                    }
                    throw var10_13;
                }
                catch (AccessControlException e) {
                    this.log.error("agent cannot access {}: {}", (Object)action.getPath(), (Object)e.getMessage());
                    throw new ReplicationException("Agent is unable to access " + action.getPath(), e);
                }
                catch (IOException io) {
                    this.log.error("IO exception occurred during serialization of content at {}.", (Object)action.getPath());
                    throw new ReplicationException("RepositoryException during serialization", io);
                }
                catch (RepositoryException e) {
                    this.log.error("Repository exception occurred during serialization of content at {}.", (Object)action.getPath());
                    throw new ReplicationException("RepositoryException during serialization", (Exception)e);
                }
            }
            try {
                admin.logout();
            }
            catch (Exception var9_12) {
                // empty catch block
            }
        }
        return replicationContent;
    }

    @Override
    public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory) throws ReplicationException {
        return this.create(session, action, contentFactory, null);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private ReplicationContent create(ReplicationContentFactory factory, Node node, ReplicationContentFilterChain filters, Map<String, Object> parameters) throws IOException, RepositoryException {
        File blob = null;
        FileOutputStream os = null;
        try {
            String childrenOrder;
            Object parentNodeType;
            boolean writeNodeProperties;
            StringBuilder nodePropertiesBuffer;
            String siblingOrder;
            StringBuilder nodeTypesBuffer;
            childrenOrder = DurboUtil.putNodeNames(node);
            nodeTypesBuffer = new StringBuilder();
            nodePropertiesBuffer = new StringBuilder();
            writeNodeProperties = false;
            Session admin = null;
            try {
                admin = this.repository.loginService("replicationService", null);
                Node parentNode = admin.getNode(node.getPath()).getParent();
                siblingOrder = DurboUtil.putNodeNames(parentNode);
                while (parentNode.getDepth() > 0) {
                    if (nodeTypesBuffer.length() > 0) {
                        nodeTypesBuffer.append(',');
                        nodePropertiesBuffer.append(',');
                    }
                    parentNodeType = parentNode.getPrimaryNodeType().getName();
                    String parentProperties = "";
                    if (DurboUtil.isAuthorizableNodeType((String)parentNodeType, 3)) {
                        String principalName = JcrUtils.getStringProperty((Node)parentNode, (String)"rep:principalName", (String)null);
                        String authorizableId = JcrUtils.getStringProperty((Node)parentNode, (String)"rep:authorizableId", (String)null);
                        String uuid = JcrUtils.getStringProperty((Node)parentNode, (String)"jcr:uuid", (String)null);
                        if (principalName != null && uuid != null) {
                            HashMap<String, String> propertiesMap = new HashMap<String, String>();
                            propertiesMap.put("rep:principalName", principalName);
                            if (authorizableId != null) {
                                propertiesMap.put("rep:authorizableId", authorizableId);
                            }
                            propertiesMap.put("jcr:uuid", uuid);
                            parentProperties = DurboUtil.urlEncodeMap(propertiesMap);
                            writeNodeProperties = true;
                        }
                    }
                    nodeTypesBuffer.append((String)parentNodeType);
                    nodePropertiesBuffer.append(parentProperties);
                    parentNode = parentNode.getParent();
                }
            }
            finally {
                if (admin != null) {
                    admin.logout();
                }
            }
            blob = File.createTempFile("cq5", ".durbo");
            os = new FileOutputStream(blob);
            DurboOutput output = new DurboOutput((OutputStream)os, (DurboNamespaceResolver)new DefaultDurboNamespaceResolver(node.getSession()), 2.1);
            output.writeProperty("orderSiblings", siblingOrder);
            output.writeProperty("orderChildren", childrenOrder);
            if (nodeTypesBuffer.length() > 0) {
                output.writeProperty("parentNodeTypes", nodeTypesBuffer.toString());
                if (writeNodeProperties) {
                    output.writeProperty("parentNodeProperties", nodePropertiesBuffer.toString());
                }
            }
            this.writeStartPath(parameters, output, node);
            this.write(output, node, filters, parameters);
            output.close();
            os.close();
            os = null;
            parentNodeType = factory.create("application/cq5-replication-durbo", blob, true);
            return parentNodeType;
        }
        finally {
            if (os != null) {
                IOUtils.closeQuietly((OutputStream)os);
                if (blob != null && !blob.delete()) {
                    throw new IOException("could not delete blob");
                }
            }
        }
    }

    private DurboOutput writeStartPath(Map<String, Object> parameters, DurboOutput output, Node node) throws RepositoryException, IOException {
        String startPathParam = (String)parameters.get("replicationStartPath");
        String startPath = startPathParam != null ? startPathParam : node.getPath();
        output.writeProperty("startPath", startPath);
        return output;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private ReplicationContent create(ReplicationContentFactory factory, Node node, String revision, ReplicationContentFilterChain filters, Map<String, Object> parameters) throws RepositoryException, IOException, ReplicationException {
        File blob = null;
        FileOutputStream os = null;
        try {
            boolean rootIsVersioned;
            VersionHistory history;
            Node content = node.hasNode("{http://www.jcp.org/jcr/1.0}content") ? node.getNode("{http://www.jcp.org/jcr/1.0}content") : null;
            StringBuilder nodeTypesBuffer = new StringBuilder();
            if (node.isNodeType("{http://www.jcp.org/jcr/mix/1.0}versionable")) {
                history = node.getSession().getWorkspace().getVersionManager().getVersionHistory(node.getPath());
                rootIsVersioned = true;
            } else if (content != null && content.isNodeType("{http://www.jcp.org/jcr/mix/1.0}versionable")) {
                history = content.getSession().getWorkspace().getVersionManager().getVersionHistory(content.getPath());
                rootIsVersioned = false;
            } else {
                throw new ReplicationException("Content node is not versionable at " + node.getPath());
            }
            Version version = null;
            try {
                version = history.getVersion(revision);
            }
            catch (VersionException var15_13) {
                // empty catch block
            }
            if (version == null) {
                try {
                    version = history.getVersionByLabel(revision);
                }
                catch (VersionException var15_14) {
                    // empty catch block
                }
            }
            if (version == null) {
                throw new ReplicationException("Version " + revision + " not found for path " + node.getPath());
            }
            Node root = version.getNode("{http://www.jcp.org/jcr/1.0}frozenNode");
            content = rootIsVersioned ? root.getNode("{http://www.jcp.org/jcr/1.0}content") : root;
            String childrenOrder = content.hasProperty("cq:childrenOrder") ? content.getProperty("cq:childrenOrder").getString() : DurboUtil.putNodeNames(node);
            Node parentNode = node.getParent();
            String siblingOrder = content.hasProperty("cq:siblingOrder") ? content.getProperty("cq:siblingOrder").getString() : DurboUtil.putNodeNames(parentNode);
            while (parentNode.getDepth() > 0) {
                if (nodeTypesBuffer.length() > 0) {
                    nodeTypesBuffer.append(',');
                }
                nodeTypesBuffer.append(parentNode.getPrimaryNodeType().getName());
                parentNode = parentNode.getParent();
            }
            blob = File.createTempFile("cq5", ".durbo");
            os = new FileOutputStream(blob);
            DurboOutput output = new DurboOutput((OutputStream)os, (DurboNamespaceResolver)new DefaultDurboNamespaceResolver(node.getSession()), 2.1);
            output.writeProperty("orderSiblings", siblingOrder);
            output.writeProperty("orderChildren", childrenOrder);
            if (nodeTypesBuffer.length() > 0) {
                output.writeProperty("parentNodeTypes", nodeTypesBuffer.toString());
            }
            this.writeStartPath(parameters, output, node);
            if (rootIsVersioned) {
                output.openNode(node.getName());
                this.write(output, root, false, filters, parameters);
                output.openNode("jcr:content");
                this.write(output, content, filters.allowDescent(content), filters, parameters);
                output.closeNode();
                output.closeNode();
            } else {
                output.openNode(node.getName());
                this.write(output, node, false, filters, parameters);
                output.openNode("jcr:content");
                this.write(output, content, filters.allowDescent(content), filters, parameters);
                output.closeNode();
                output.closeNode();
            }
            output.close();
            os.close();
            os = null;
            ReplicationContent replicationContent = factory.create("application/cq5-replication-durbo", blob, true);
            return replicationContent;
        }
        finally {
            if (os != null) {
                IOUtils.closeQuietly((OutputStream)os);
                if (blob != null && !blob.delete()) {
                    throw new IOException("could not delete blob");
                }
            }
        }
    }

    private void write(DurboOutput out, Node root, ReplicationContentFilterChain filters, Map<String, Object> parameters) throws IOException, RepositoryException {
        out.openNode(root.getName());
        this.write(out, root, false, filters, parameters);
        NodeIterator iter = root.getNodes();
        while (iter.hasNext()) {
            Node child = iter.nextNode();
            if (!filters.accept(child)) continue;
            out.openNode(child.getName());
            this.write(out, child, filters.allowDescent(child), filters, parameters);
            out.closeNode();
        }
        out.closeNode();
    }

    void write(DurboOutput out, Node node, boolean recursive, ReplicationContentFilterChain filters, Map<String, Object> parameters) throws IOException, RepositoryException {
        String[] mixinTypes;
        boolean isFrozen = this.isFrozenNode(node);
        out.writeProperty("jcr:primaryType", this.getPrimaryNodeType(node, isFrozen));
        if (node.isNodeType("{http://www.jcp.org/jcr/mix/1.0}referenceable")) {
            out.writeProperty("jcr:uuid", this.getUUID(node, isFrozen));
        }
        if ((mixinTypes = this.getMixinNodeTypes(node, isFrozen)) != null && mixinTypes.length > 0) {
            StringBuilder mixinTypeNames = new StringBuilder();
            for (String mix : mixinTypes) {
                mixinTypeNames.append(mix);
                mixinTypeNames.append(',');
            }
            out.writeProperty("jcr:mixinTypes", mixinTypeNames.toString());
        }
        PropertyIterator iter = node.getProperties();
        while (iter.hasNext()) {
            String propertyName;
            Property property = iter.nextProperty();
            if (!filters.accept(property) || DurboUtil.IGNORED_PROPERTIES.contains(propertyName = property.getName())) continue;
            out.writeProperty(property);
        }
        if (recursive) {
            iter = node.getNodes();
            while (iter.hasNext()) {
                Node child = iter.nextNode();
                if (!filters.accept(child)) continue;
                out.openNode(child.getName());
                this.write(out, child, filters.allowDescent(child), filters, parameters);
                out.closeNode();
            }
        }
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    String[] getMixinNodeTypes(Node node, boolean isFrozen) throws RepositoryException {
        NodeType[] mixins;
        ArrayList<String> mixinList = new ArrayList<String>();
        if (isFrozen) {
            Value[] vals;
            if (!node.hasProperty("{http://www.jcp.org/jcr/1.0}frozenMixinTypes")) return mixinList.toArray(new String[mixinList.size()]);
            Value[] arrvalue = vals = node.getProperty("{http://www.jcp.org/jcr/1.0}frozenMixinTypes").getValues();
            int n = arrvalue.length;
            int n2 = 0;
            while (n2 < n) {
                Value val = arrvalue[n2];
                mixinList.add(val.getString());
                ++n2;
            }
            return mixinList.toArray(new String[mixinList.size()]);
        }
        NodeType[] arrnodeType = mixins = node.getMixinNodeTypes();
        int n = arrnodeType.length;
        int n3 = 0;
        while (n3 < n) {
            NodeType mixin = arrnodeType[n3];
            mixinList.add(mixin.getName());
            ++n3;
        }
        return mixinList.toArray(new String[mixinList.size()]);
    }

    String getUUID(Node node, boolean isFrozen) throws RepositoryException {
        return isFrozen ? node.getProperty("{http://www.jcp.org/jcr/1.0}frozenUuid").getString() : node.getIdentifier();
    }

    String getPrimaryNodeType(Node node, boolean isFrozen) throws RepositoryException {
        return isFrozen ? node.getProperty("{http://www.jcp.org/jcr/1.0}frozenPrimaryType").getString() : node.getPrimaryNodeType().getName();
    }

    boolean isFrozenNode(Node node) throws RepositoryException {
        return node.isNodeType("{http://www.jcp.org/jcr/nt/1.0}frozenNode");
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    private static final class DefaultDurboNamespaceResolver
    implements DurboNamespaceResolver {
        private final Session session;

        private DefaultDurboNamespaceResolver(Session session) {
            this.session = session;
        }

        public String getURI(String prefix) throws NamespaceException {
            try {
                return this.session.getNamespaceURI(prefix);
            }
            catch (RepositoryException e) {
                throw new NamespaceException((Throwable)e);
            }
        }

        public String getPrefix(String uri) throws NamespaceException {
            try {
                return this.session.getNamespacePrefix(uri);
            }
            catch (RepositoryException e) {
                throw new NamespaceException((Throwable)e);
            }
        }
    }

}