Search in sources :

Example 76 with Document

use of org.jdom2.Document in project gocd by gocd.

the class HgModificationSplitter method modifications.

public List<Modification> modifications() {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(new StringReader(output));
        return parseDOMTree(document);
    } catch (Exception e) {
        throw ExceptionUtils.bomb("Unable to parse hg log output: " + result.replaceSecretInfo(output), result.smudgedException(e));
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) StringReader(java.io.StringReader) Document(org.jdom2.Document) ParseException(java.text.ParseException)

Example 77 with Document

use of org.jdom2.Document in project gocd by gocd.

the class MagicalGoConfigXmlLoader method fromXmlPartial.

public <T> T fromXmlPartial(InputStream inputStream, Class<T> o) throws Exception {
    Document document = new SAXBuilder().build(inputStream);
    Element element = document.getRootElement();
    return classParser(element, o, configCache, new GoCipher(), registry, new ConfigReferenceElements()).parse();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) GoCipher(com.thoughtworks.go.security.GoCipher) Element(org.jdom2.Element) ConfigReferenceElements(com.thoughtworks.go.config.parser.ConfigReferenceElements) Document(org.jdom2.Document) XmlUtils.buildXmlDocument(com.thoughtworks.go.util.XmlUtils.buildXmlDocument)

Example 78 with Document

use of org.jdom2.Document in project gocd by gocd.

the class CommandSnippetXmlParser method parse.

public CommandSnippet parse(String xmlContent, String fileName, String relativeFilePath) {
    try {
        Document document = buildXmlDocument(xmlContent, CommandSnippet.class.getResource("command-snippet.xsd"));
        CommandSnippetComment comment = getComment(document);
        Element execTag = document.getRootElement();
        String commandName = execTag.getAttributeValue("command");
        List<String> arguments = new ArrayList<>();
        for (Object child : execTag.getChildren()) {
            Element element = (Element) child;
            arguments.add(element.getValue());
        }
        return new CommandSnippet(commandName, arguments, comment, fileName, relativeFilePath);
    } catch (Exception e) {
        String errorMessage = String.format("Reason: %s", e.getMessage());
        LOGGER.info(String.format("Could not load command '%s'. %s", fileName, errorMessage));
        return CommandSnippet.invalid(fileName, errorMessage, new EmptySnippetComment());
    }
}
Also used : Element(org.jdom2.Element) ArrayList(java.util.ArrayList) XmlUtils.buildXmlDocument(com.thoughtworks.go.util.XmlUtils.buildXmlDocument) Document(org.jdom2.Document)

Example 79 with Document

use of org.jdom2.Document in project gocd by gocd.

the class SvnLogXmlParser method parseInfoToGetUUID.

public HashMap<String, String> parseInfoToGetUUID(String output, String queryURL, SAXBuilder builder) {
    HashMap<String, String> uidToUrlMap = new HashMap<>();
    try {
        Document document = builder.build(new StringReader(output));
        Element root = document.getRootElement();
        List<Element> entries = root.getChildren("entry");
        for (Element entry : entries) {
            uidToUrlMap.put(queryURL, entry.getChild("repository").getChild("uuid").getValue());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return uidToUrlMap;
}
Also used : HashMap(java.util.HashMap) Element(org.jdom2.Element) StringReader(java.io.StringReader) Document(org.jdom2.Document) ParseException(java.text.ParseException)

Example 80 with Document

use of org.jdom2.Document in project gocd by gocd.

the class ConfigCipherUpdater method migrate.

public void migrate() {
    File cipherFile = systemEnvironment.getCipherFile();
    String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(timeProvider.currentTime());
    File backupCipherFile = new File(systemEnvironment.getConfigDir(), "cipher.original." + timestamp);
    File configFile = new File(systemEnvironment.getCruiseConfigFile());
    File backupConfigFile = new File(configFile.getParentFile(), configFile.getName() + ".original." + timestamp);
    try {
        if (!cipherFile.exists() || !FileUtils.readFileToString(cipherFile).equals(FLAWED_VALUE)) {
            return;
        }
        LOGGER.info("Found unsafe cipher {} on server, Go will make an attempt to rekey", FLAWED_VALUE);
        FileUtils.copyFile(cipherFile, backupCipherFile);
        LOGGER.info("Old cipher was successfully backed up to {}", backupCipherFile.getAbsoluteFile());
        FileUtils.copyFile(configFile, backupConfigFile);
        LOGGER.info("Old config was successfully backed up to {}", backupConfigFile.getAbsoluteFile());
        byte[] oldCipher = FileUtils.readFileToByteArray(backupCipherFile);
        new CipherProvider(systemEnvironment).resetCipher();
        byte[] newCipher = FileUtils.readFileToByteArray(cipherFile);
        if (new String(newCipher).equals(new String(oldCipher))) {
            LOGGER.warn("Unable to generate a new safe cipher. Your cipher is unsafe.");
            FileUtils.deleteQuietly(backupCipherFile);
            FileUtils.deleteQuietly(backupConfigFile);
            return;
        }
        Document document = new SAXBuilder().build(configFile);
        List<String> encryptedAttributes = Arrays.asList("encryptedPassword", "encryptedManagerPassword");
        List<String> encryptedNodes = Arrays.asList("encryptedValue");
        XPathFactory xPathFactory = XPathFactory.instance();
        for (String attributeName : encryptedAttributes) {
            XPathExpression<Element> xpathExpression = xPathFactory.compile(String.format("//*[@%s]", attributeName), Filters.element());
            List<Element> encryptedPasswordElements = xpathExpression.evaluate(document);
            for (Element element : encryptedPasswordElements) {
                Attribute encryptedPassword = element.getAttribute(attributeName);
                encryptedPassword.setValue(reEncryptUsingNewKey(oldCipher, newCipher, encryptedPassword.getValue()));
                LOGGER.debug("Replaced encrypted value at {}", element.toString());
            }
        }
        for (String nodeName : encryptedNodes) {
            XPathExpression<Element> xpathExpression = xPathFactory.compile(String.format("//%s", nodeName), Filters.element());
            List<Element> encryptedNode = xpathExpression.evaluate(document);
            for (Element element : encryptedNode) {
                element.setText(reEncryptUsingNewKey(oldCipher, newCipher, element.getValue()));
                LOGGER.debug("Replaced encrypted value at {}", element.toString());
            }
        }
        try (FileOutputStream fileOutputStream = new FileOutputStream(configFile)) {
            XmlUtils.writeXml(document, fileOutputStream);
        }
        LOGGER.info("Successfully re-encrypted config");
    } catch (Exception e) {
        LOGGER.error("Re-keying of cipher failed with error: [{}]", e.getMessage(), e);
        if (backupCipherFile.exists()) {
            try {
                FileUtils.copyFile(backupCipherFile, cipherFile);
            } catch (IOException e1) {
                LOGGER.error("Could not replace the cipher file [{}] with original one [{}], please do so manually. Error: [{}]", cipherFile.getAbsolutePath(), backupCipherFile.getAbsolutePath(), e.getMessage(), e);
                bomb(e1);
            }
        }
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Attribute(org.jdom2.Attribute) CipherProvider(com.thoughtworks.go.security.CipherProvider) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) IOException(java.io.IOException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) XPathFactory(org.jdom2.xpath.XPathFactory) FileOutputStream(java.io.FileOutputStream) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Document (org.jdom2.Document)86 Element (org.jdom2.Element)76 Test (org.junit.Test)33 File (java.io.File)29 DocType (org.jdom2.DocType)24 SAXBuilder (org.jdom2.input.SAXBuilder)21 IOException (java.io.IOException)16 XMLOutputter (org.jdom2.output.XMLOutputter)15 ProcessingInstruction (org.jdom2.ProcessingInstruction)13 XmlFile (jmri.jmrit.XmlFile)11 Document (com.google.cloud.language.v1beta2.Document)10 ApiException (com.google.api.gax.grpc.ApiException)9 Document (com.google.cloud.language.v1.Document)9 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ArrayList (java.util.ArrayList)9 EncodingType (com.google.cloud.language.v1beta2.EncodingType)8 FileOutputStream (java.io.FileOutputStream)8 JLabel (javax.swing.JLabel)7 EncodingType (com.google.cloud.language.v1.EncodingType)6