private UpdateResult handleUpdate()

in src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java [626:730]


    private UpdateResult handleUpdate(final String resourceType,
            final String id,
            final String url,
            final InputStream is,
            final Dictionary<String, Object> dict,
            final Map<String, Object> attributes) {
        // get configuration
        final InstallerConfig cfg = this.getConfiguration();
        if (cfg == null || !cfg.isWriteBack()) {
            return null;
        }

        // we only handle add/update of configs for now
        if (!resourceType.equals(InstallableResource.TYPE_CONFIG)) {
            return null;
        }

        Session session = null;
        try {
            session = repository.loginService(/* subservice name */null, repository.getDefaultWorkspace());

            final String path;
            boolean resourceIsMoved = true;
            if (url != null) {
                // update
                final int pos = url.indexOf(':');
                final String oldPath = url.substring(pos + 1);

                // calculate the new node path
                final String nodePath;
                if (url.startsWith(URL_SCHEME + ':')) {
                    nodePath = getPathWithHighestPrio(cfg, oldPath);
                } else {
                    final int lastSlash = url.lastIndexOf('/');
                    final int lastPos = url.lastIndexOf('.');
                    final String name;
                    if (lastSlash == -1 || lastPos < lastSlash) {
                        name = id;
                    } else {
                        name = url.substring(lastSlash + 1, lastPos);
                    }
                    nodePath = getPathWithHighestPrio(cfg, cfg.getNewConfigPath() + name + CONFIG_FILE_EXTENSION);
                }
                // ensure extension 'config'
                if (!nodePath.endsWith(CONFIG_FILE_EXTENSION)) {
                    if (session.itemExists(nodePath)) {
                        session.getItem(nodePath).remove();
                    }
                    path = nodePath + CONFIG_FILE_EXTENSION;
                } else {
                    path = nodePath;
                }

                resourceIsMoved = nodePath.equals(oldPath);
                logger.debug("Update of {} at {}", resourceType, path);
            } else {
                // add
                final String name;
                if (attributes != null && attributes.get(InstallableResource.RESOURCE_URI_HINT) != null) {
                    name = (String) attributes.get(InstallableResource.RESOURCE_URI_HINT);
                } else {
                    name = id;
                }
                path = cfg.getNewConfigPath() + name + CONFIG_FILE_EXTENSION;
                logger.debug("Add of {} at {}", resourceType, path);
            }

            // write to a byte array stream
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // comments starting with "//"  allowed according to https://osgi.org/specification/osgi.cmpn/7.0.0/service.configurator.html#d0e131566
            baos.write("// Configuration created by Apache Sling JCR Installer\n".getBytes("UTF-8"));
            ConfigurationSerializer serializer = ConfigurationSerializerFactory.create(ConfigurationSerializerFactory.Format.JSON);
            serializer.serialize(dict, baos);
            baos.close();

            // get or create file node
            JcrUtil.createPath(session, path, NT_FILE);
            // get or create resource node
            final Node dataNode = JcrUtil.createPath(session, path + "/jcr:content", NT_RESOURCE);

            dataNode.setProperty(PROP_DATA, new ByteArrayInputStream(baos.toByteArray()));
            dataNode.setProperty(PROP_MODIFIED, Calendar.getInstance());
            dataNode.setProperty(PROP_ENC, ENCODING);
            dataNode.setProperty(PROP_MIME, MIME_TXT);
            session.save();

            final UpdateResult result = new UpdateResult(JcrInstaller.URL_SCHEME + ':' + path);
            // priority
            final int lastSlash = path.lastIndexOf('/');
            final String parentPath = path.substring(0, lastSlash);
            result.setPriority(cfg.getFolderNameFilter().getPriority(parentPath));
            result.setResourceIsMoved(resourceIsMoved);
            return result;
        } catch (final RepositoryException re) {
            logger.error("Unable to add/update resource " + resourceType + ':' + id, re);
            return null;
        } catch (final IOException e) {
            logger.error("Unable to add/update resource " + resourceType + ':' + id, e);
            return null;
        } finally {
            if (session != null) {
                session.logout();
            }
        }
    }