AuthStoreUtils { public void save(AuthStore authStore, OutputStream out) throws IOException { Collection<UserAuthenticationInfo> all = authStore.getAll(); for (UserAuthenticationInfo userAuthenticationInfo : all) { UserAuthenticationData userAuthenticationData = authStore.getUserAuthenticationData(userAuthenticationInfo); } DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = null; try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element root = document.createElement("root"); document.appendChild(root); for (UserAuthenticationInfo userAuthenticationInfo : all) { UserAuthenticationDataWrapper userAuthenticationData = authStore.getUserAuthenticationData(userAuthenticationInfo); Element entry = document.createElement(ENTRY); entry.setAttribute(PROTOCOL, userAuthenticationInfo.getProtocol()); entry.setAttribute(HOST, userAuthenticationInfo.getHost()); entry.setAttribute(USER, userAuthenticationInfo.getUser()); Map<UserAuthenticationData.Type, char[]> addedTypes = userAuthenticationData.getAddedTypes(); for (UserAuthenticationData.Type type : addedTypes.keySet()) { Element elementUserAuthenticationData = document.createElement(USER_AUTHENTICATION_DATA); char[] data = userAuthenticationData.getData(type); String value; value = new String(data); Element elementType = document.createElement(TYPE); elementType.setTextContent(type.toString()); Element elementData = document.createElement(DATA); elementData.setTextContent(value); elementUserAuthenticationData.appendChild(elementType); elementUserAuthenticationData.appendChild(elementData); entry.appendChild(elementUserAuthenticationData); } root.appendChild(entry); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); transformer.setOutputProperty("{http: DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(out); transformer.transform(source, result); } catch (Exception e) { throw new IOException(e); } } AuthStoreUtils(PasswordProvider passwordProvider); void save(AuthStore authStore, OutputStream out); void load(AuthStore authStore, InputStream in); PasswordProvider getPasswordProvider(); void setPasswordProvider(PasswordProvider passwordProvider); static final String USER; static final String HOST; static final String PROTOCOL; static final String ENTRY; static final String USER_AUTHENTICATION_DATA; static final String TYPE; static final String DATA; static final String ALGORITHM_BLOW_FISH; static final int SALT_LENGTH; }
@Test(enabled = false) public void testSave() throws Exception { AuthStore authStore = new MemoryAuthStore(); UserAuthenticationDataWrapper authenticationData1 = new UserAuthenticationDataWrapper(); authenticationData1.setData(UserAuthenticationData.USERNAME, "Stefan".toCharArray()); authenticationData1.setData(UserAuthenticationData.PASSWORD, "Password".toCharArray()); authenticationData1.setData(new UserAuthenticationData.Type("path"), "c:\\file".toCharArray()); UserAuthenticationDataWrapper authenticationData2 = new UserAuthenticationDataWrapper(); authenticationData2.setData(UserAuthenticationData.USERNAME, "Stefaan".toCharArray()); authenticationData2.setData(UserAuthenticationData.PASSWORD, "Passwodrd".toCharArray()); authenticationData2.setData(UserAuthenticationData.DOMAIN, "MS".toCharArray()); UserAuthenticationDataWrapper authenticationData3 = new UserAuthenticationDataWrapper(); authenticationData3.setData(UserAuthenticationData.USERNAME, "Stefan".toCharArray()); authenticationData3.setData(UserAuthenticationData.PASSWORD, "Passwo@rd".toCharArray()); authenticationData3.setData(UserAuthenticationData.DOMAIN, "MSzx!X%a".toCharArray()); authStore.add(new UserAuthenticationInfo("ftp", "host1", "stefan"), authenticationData1); authStore.add(new UserAuthenticationInfo("ftp", "host1", "stefan9"), authenticationData1); authStore.add(new UserAuthenticationInfo("sftp", "host1a", "astefan"), authenticationData1); ByteArrayOutputStream bout = new ByteArrayOutputStream(); String expected = IOUtils.toString(this.getClass().getResourceAsStream("credentials.xml")); expected = expected.replaceAll(">\\s+", ">").replaceAll("\\s+<", "<").replaceAll("<Type>password</Type><Data>.*?<", "<Type>password</Type><Data><").trim(); authStoreUtils.save(authStore, bout); System.out.println(new String(bout.toByteArray())); String result = new String(bout.toByteArray()).replaceAll(">\\s+", ">").replaceAll("\\s+<", "<").replaceAll("<Type>password</Type><Data>.*?<", "<Type>password</Type><Data><").trim(); assertEquals(result, expected); }