Example usage for org.jdom2 Element getChildText

List of usage examples for org.jdom2 Element getChildText

Introduction

In this page you can find the example usage for org.jdom2 Element getChildText.

Prototype

public String getChildText(final String cname) 

Source Link

Document

Returns the textual content of the named child element, or null if there's no such child.

Usage

From source file:org.artifactory.update.md.MetadataConverterUtils.java

License:Open Source License

public static List<Element> extractExtensionFields(Element rootElement) {
    String modifiedBy = rootElement.getChildText(MODIFIED_BY);
    List<Element> toMove = new ArrayList<>(EXTENSION_FIELDS.length);
    for (String tagName : EXTENSION_FIELDS) {
        Element element = rootElement.getChild(tagName);
        if (element != null) {
            toMove.add(element);/*from   w  w  w  .  j a va  2 s .  co  m*/
            rootElement.removeChild(tagName);
        } else {
            if (CREATED_BY.equals(tagName)) {
                toMove.add(new Element(CREATED_BY).setText(modifiedBy));
            }
        }
    }
    return toMove;
}

From source file:org.artifactory.update.md.MetadataConverterUtils.java

License:Open Source License

public static RepoPath extractRepoPath(Element rootElement) {
    String repoKey = rootElement.getChildText(REPO_KEY);
    String relPath = rootElement.getChildText(REL_PATH);
    RepoPath repoPath = InternalRepoPathFactory.create(repoKey, relPath);
    rootElement.removeChild(REPO_KEY);/*from w ww  . j a va  2  s  . c o m*/
    rootElement.removeChild(REL_PATH);
    return repoPath;
}

From source file:org.artifactory.update.md.v125rc0.MdFolderConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    rootElement.setName(getNewMetadataName());
    RepoPath repoPath = MetadataConverterUtils.extractRepoPath(rootElement);
    // In this version the relPath is the father and name need to be added
    if (rootElement.getChild(ARTIFACTORY_NAME) != null) {
        String name = rootElement.getChildText(ARTIFACTORY_NAME);
        repoPath = InternalRepoPathFactory.create(repoPath, name);
    }/*ww w  . j ava2  s. co m*/
    List<Element> toMove = MetadataConverterUtils.extractExtensionFields(rootElement);
    MetadataConverterUtils.addNewContent(rootElement, repoPath, toMove);
    // Not used anymore
    rootElement.removeChild(ARTIFACTORY_NAME);
}

From source file:org.artifactory.update.security.v1.AclsConverter.java

License:Open Source License

private void addAceToAcl(Element oldAcl, Element newAcl) {
    Element acesListElement = newAcl.getChild("aces").getChild("list");
    String user = oldAcl.getChildText("recipient");
    String mask = oldAcl.getChildText("mask");
    if ("___INHERITENCE_MARKER_ONLY___".equals(user)) {
        user = "anonymous";
        mask = "1";
    }/*from  w  ww.j av  a  2 s .  c  o m*/
    Element ace = new Element("org.artifactory.security.RepoPathAce");
    ace.addContent(new Element("principal").setText(user));
    ace.addContent(new Element("mask").setText(mask));
    acesListElement.addContent(ace);
}

From source file:org.artifactory.update.security.v1.AclsConverter.java

License:Open Source License

private String getNewIdentity(Element objectIdentity) {
    String repoKey = objectIdentity.getChildText("repoKey");
    String path = objectIdentity.getChildText("path");
    String identity = repoKey + "%3a" + path;
    return identity;
}

From source file:org.artifactory.update.security.v1.UserPermissionsConverter.java

License:Open Source License

@Override
@SuppressWarnings({ "unchecked" })
public void convert(Document doc) {
    Element root = doc.getRootElement();

    Element usersElement = root.getChild("users");
    if (usersElement == null) {
        log.warn("No users found");
        return;//ww w. j a v  a 2 s  .c  om
    }

    List<Element> users = usersElement.getChildren("org.artifactory.security.SimpleUser");
    for (Element user : users) {
        Element authoritiesElement = user.getChild("authorities");
        if (authoritiesElement == null) {
            log.warn("No authorities found for {}", user.getChildText("username"));
            continue;
        }
        List<Element> authorities = authoritiesElement.getChildren("org.acegisecurity.GrantedAuthorityImpl");
        for (Element authority : authorities) {
            if ("ADMIN".equals(authority.getChildText("role"))) {
                user.addContent(new Element("admin").setText("true"));
                break;
            }
        }
    }
}

From source file:org.artifactory.update.security.v2.RepoPathAclConverter.java

License:Open Source License

@Override
@SuppressWarnings({ "unchecked" })
public void convert(Document doc) {
    Element aclsTag = doc.getRootElement().getChild("acls");
    List<Element> acls = aclsTag.getChildren();
    for (Element acl : acls) {
        if (acl.getName().contains("RepoPathAcl")) {
            acl.setName("acl");
            convertIdentifierToPermissionTarget(acl);
            Element acesTag = acl.getChild(ACES);
            Element aceListTag = acesTag.getChild("list");
            List<Element> aces = aceListTag.getChildren("org.artifactory.security.RepoPathAce");
            Element newAces = new Element(ACES);
            Element aceTemplate = new Element("ace");
            Element groupEl = new Element("group");
            groupEl.setText("false");
            aceTemplate.addContent(new Element(PRINCIPAL)).addContent(groupEl).addContent(new Element(MASK));
            for (Element ace : aces) {
                Element newAce = (Element) aceTemplate.clone();
                newAce.getChild(PRINCIPAL).setText(ace.getChildText(PRINCIPAL));
                Element maskEl = ace.getChild(MASK);
                int mask = Integer.parseInt(maskEl.getText());
                if ((mask & (ArtifactoryPermission.MANAGE.getMask()
                        | ArtifactoryPermission.DEPLOY.getMask())) > 0) {
                    mask |= ArtifactoryPermission.DELETE.getMask();
                }/*  www. j  a  va 2 s . c  o  m*/
                newAce.getChild(MASK).setText("" + mask);
                newAces.addContent(newAce);
            }
            acl.removeChild(ACES);
            acl.addContent(newAces);
        } else {
            log.warn("Acl tag " + acl + " under acls is not a RepoPAthAcl!");
        }
    }
}

From source file:org.artifactory.update.security.v2.RepoPathAclConverter.java

License:Open Source License

private void convertIdentifierToPermissionTarget(Element acl) {
    String identifier;//from  w  w w.  j  av  a 2s.  com
    try {
        identifier = URLDecoder.decode(acl.getChildText(IDENTIFIER), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Failed to decode identifier", e);
    }
    RepoPath repoPath = InternalRepoPathFactory.fromId(identifier);
    acl.removeChild(IDENTIFIER);
    Element permissionTarget = new Element("permissionTarget");

    Element nameEl = new Element("name");
    if (repoPath.getRepoKey().equalsIgnoreCase(PermissionTargetInfo.ANY_REPO)
            && repoPath.getPath().equalsIgnoreCase(PermissionTargetInfo.ANY_REPO)) {
        nameEl.setText(PermissionTargetInfo.ANY_PERMISSION_TARGET_NAME);
    } else {
        nameEl.setText(repoPath.getId());
    }
    permissionTarget.addContent(nameEl);

    Element repoKeyEl = new Element("repoKey");
    repoKeyEl.setText(repoPath.getRepoKey());
    permissionTarget.addContent(repoKeyEl);

    Element includesEl = new Element("includes");
    Element includeEl = new Element("string");
    if (repoPath.getPath().equalsIgnoreCase(PermissionTargetInfo.ANY_REPO)) {
        includeEl.setText(PermissionTargetInfo.ANY_PATH);
    } else {
        includeEl.setText(repoPath.getPath() + "/" + PermissionTargetInfo.ANY_PATH);
    }
    includesEl.addContent(includeEl);
    permissionTarget.addContent(includesEl);
    permissionTarget.addContent(new Element("excludes"));

    acl.addContent(permissionTarget);
}

From source file:org.artifactory.update.security.v3.AnyRemoteConverter.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
private boolean shouldAddDefaultAnyRemote(Element aclsTag) {
    List<Element> acls = aclsTag.getChildren();
    for (Element acl : acls) {
        Element permissionTarget = acl.getChild("permissionTarget");
        String repoKey = permissionTarget.getChildText("repoKey");
        if ("ANY".equals(repoKey)) {
            Element acesTag = acl.getChild("aces");
            if (acesTag != null) {
                List<Element> aces = acesTag.getChildren();
                for (Element ace : aces) {
                    if ("anonymous".equals(ace.getChildText("principal"))) {
                        return true;
                    }//from www.  j a v a  2s .  c  om
                }
            }
        }
    }
    return false;
}

From source file:org.artifactory.update.security.v4.AnnotatePermissionXmlConverter.java

License:Open Source License

@Override
@SuppressWarnings({ "unchecked" })
public void convert(Document doc) {
    Element aclsTag = doc.getRootElement().getChild("acls");
    List<Element> acls = aclsTag.getChildren();
    for (Element acl : acls) {
        Element acesTag = acl.getChild(ACES);
        List<Element> aces = acesTag.getChildren(ACE);
        Element newAces = new Element(ACES);
        Element aceTemplate = new Element(ACE);
        Element groupEl = new Element(GROUP);
        aceTemplate.addContent(new Element(PRINCIPAL)).addContent(groupEl).addContent(new Element(MASK));
        for (Element ace : aces) {
            Element child = ace.getChild("principal");
            Element newAce = (Element) aceTemplate.clone();
            newAce.getChild(PRINCIPAL).setText(ace.getChildText(PRINCIPAL));
            newAce.getChild(GROUP).setText(ace.getChildText(GROUP));

            Element maskEl = ace.getChild(MASK);
            int mask = Integer.parseInt(maskEl.getText());
            if (!child.getText().equals(UserInfo.ANONYMOUS)) {
                if ((mask & (ArtifactoryPermission.MANAGE.getMask()
                        | ArtifactoryPermission.DEPLOY.getMask())) > 0) {
                    mask |= ArtifactoryPermission.ANNOTATE.getMask();
                }//  ww w.j a va  2  s.  c  om
            }
            newAce.getChild(MASK).setText("" + mask);
            newAces.addContent(newAce);
        }
        acl.removeChild(ACES);
        acl.addContent(newAces);
    }
}