Search in sources :

Example 51 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project gocd by gocd.

the class SvnCommandTest method shouldParsePartlyEncodedUrlAndPath.

@Test
public void shouldParsePartlyEncodedUrlAndPath() {
    String output = "<?xml version=\"1.0\"?>\n" + "<info>\n" + "<entry\n" + "   kind=\"dir\"\n" + "   path=\"unit-reports\"\n" + "   revision=\"3\">\n" + "<url>svn+ssh://hostname/foo%20bar%20baz/end2end</url>\n" + "<repository>\n" + "<root>svn+ssh://hostname/foo%20bar%20baz</root>\n" + "<uuid>f953918e-915c-4459-8d4c-83860cce9d9a</uuid>\n" + "</repository>\n" + "<commit\n" + "   revision=\"1\">\n" + "<author>cceuser</author>\n" + "<date>2008-03-20T04:00:43.976517Z</date>\n" + "</commit>\n" + "</entry>\n" + "</info>";
    SvnCommand.SvnInfo svnInfo = new SvnCommand.SvnInfo();
    svnInfo.parse(output, new SAXBuilder());
    assertThat(svnInfo.getUrl(), is("svn+ssh://hostname/foo%20bar%20baz/end2end"));
    assertThat(svnInfo.getPath(), is("/end2end"));
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 52 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project gocd by gocd.

the class SvnCommandTest method shouldParseEncodedUrlAndPath.

@Test
public void shouldParseEncodedUrlAndPath() {
    String output = "<?xml version=\"1.0\"?>\n" + "<info>\n" + "<entry\n" + "   kind=\"dir\"\n" + "   path=\"unit-reports\"\n" + "   revision=\"3\">\n" + "<url>file:///C:/Documents%20and%20Settings/cceuser/Local%20Settings/Temp/testSvnRepo-1243722556125/end2end/unit-reports</url>\n" + "<repository>\n" + "<root>file:///C:/Documents%20and%20Settings/cceuser/Local%20Settings/Temp/testSvnRepo-1243722556125/end2end</root>\n" + "<uuid>f953918e-915c-4459-8d4c-83860cce9d9a</uuid>\n" + "</repository>\n" + "<commit\n" + "   revision=\"1\">\n" + "<author>cceuser</author>\n" + "<date>2008-03-20T04:00:43.976517Z</date>\n" + "</commit>\n" + "</entry>\n" + "</info>";
    SvnCommand.SvnInfo svnInfo = new SvnCommand.SvnInfo();
    svnInfo.parse(output, new SAXBuilder());
    assertThat(svnInfo.getUrl(), is("file:///C:/Documents%20and%20Settings/cceuser/Local%20Settings/Temp/testSvnRepo-1243722556125/end2end/unit-reports"));
    assertThat(svnInfo.getPath(), is("/unit-reports"));
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 53 with SAXBuilder

use of org.jdom2.input.SAXBuilder 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, UTF_8).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) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) IOException(java.io.IOException) XPathFactory(org.jdom2.xpath.XPathFactory) FileOutputStream(java.io.FileOutputStream) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 54 with SAXBuilder

use of org.jdom2.input.SAXBuilder 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 55 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project gocd by gocd.

the class SvnCommand method parseSvnLog.

private List<Modification> parseSvnLog(String output) {
    SAXBuilder builder = getBuilder();
    SvnInfo svnInfo = remoteInfo(builder);
    return svnLogXmlParser.parse(output, svnInfo.getPath(), builder);
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder)

Aggregations

SAXBuilder (org.jdom2.input.SAXBuilder)60 Document (org.jdom2.Document)35 Element (org.jdom2.Element)21 Test (org.junit.Test)20 IOException (java.io.IOException)14 File (java.io.File)12 JDOMException (org.jdom2.JDOMException)10 Modification (com.thoughtworks.go.domain.materials.Modification)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 BufferedInputStream (java.io.BufferedInputStream)5 ParseException (java.text.ParseException)5 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ModifiedFile (com.thoughtworks.go.domain.materials.ModifiedFile)3 NoProcessSpecifiedException (de.hpi.bpt.scylla.creation.SimulationConfiguration.SimulationConfigurationCreator.NoProcessSpecifiedException)3 NotAuthorizedToOverrideException (de.hpi.bpt.scylla.creation.SimulationConfiguration.SimulationConfigurationCreator.NotAuthorizedToOverrideException)3 FileNotFoundException (java.io.FileNotFoundException)3