List of usage examples for org.apache.commons.io IOUtils contentEquals
public static boolean contentEquals(Reader input1, Reader input2) throws IOException
From source file:org.voyanttools.trombone.input.expand.XmlExpanderTest.java
@Test public void test() throws IOException { Storage storage = TestHelper.getDefaultTestStorage(); StoredDocumentSourceStorage storedDocumentSourceStorage = storage.getStoredDocumentSourceStorage(); StoredDocumentSourceExpander storedDocumentSourceExpander; InputSource inputSource;/* ww w .ja v a2s . c om*/ StoredDocumentSource storedDocumentSource; List<StoredDocumentSource> expandedSourceDocumentSources; FlexibleParameters parameters; InputStream inputStream; FileInputStream fileInputStream; // make sure we have one document when no xmlDocumentXpath is specified (and input format is XML, not RSS) parameters = new FlexibleParameters(new String[] { "inputFormat=XML" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/rss.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander.expandXml(storedDocumentSource); assertEquals("XML file with no Xpath should contain one document", 1, expandedSourceDocumentSources.size()); // with xmlDocumentXpath we should have two for //item parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//item" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/rss.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander.expandXml(storedDocumentSource); assertEquals("XML file with no Xpath should contain one document", 2, expandedSourceDocumentSources.size()); inputStream = null; fileInputStream = null; try { inputStream = storedDocumentSourceStorage .getStoredDocumentSourceInputStream(expandedSourceDocumentSources.get(1).getId()); fileInputStream = new FileInputStream(TestHelper.getResource("xml/rss.xml.item2_xpath.xml")); assertTrue(IOUtils.contentEquals(fileInputStream, inputStream)); } finally { if (inputStream != null) { inputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } // with no xmlDocumentXpath but splitDocuments we should have two for //item parameters = new FlexibleParameters(new String[] { "inputFormat=RSS", "splitDocuments=true" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/rss.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander.expandXml(storedDocumentSource); assertEquals(2, expandedSourceDocumentSources.size()); inputStream = null; fileInputStream = null; try { inputStream = storedDocumentSourceStorage .getStoredDocumentSourceInputStream(expandedSourceDocumentSources.get(1).getId()); fileInputStream = new FileInputStream(TestHelper.getResource("xml/rss.xml.item2_xpath.xml")); assertTrue(IOUtils.contentEquals(fileInputStream, inputStream)); } finally { if (inputStream != null) { inputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } // with xmlDocumentXpath we should have one for dc:creator parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//dc:creator" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/rss.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander.expandXml(storedDocumentSource); assertEquals("XML file with no Xpath should contain one document", 1, expandedSourceDocumentSources.size()); inputStream = null; fileInputStream = null; try { inputStream = storedDocumentSourceStorage .getStoredDocumentSourceInputStream(expandedSourceDocumentSources.get(0).getId()); fileInputStream = new FileInputStream(TestHelper.getResource("xml/rss.xml.dc_creator_xpath.xml")); assertTrue(IOUtils.contentEquals(fileInputStream, inputStream)); } finally { if (inputStream != null) { inputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } // with xmlDocumentXpath we should have one for local-name()='creator' parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//*[local-name()='creator']" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/rss.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander.expandXml(storedDocumentSource); assertEquals("XML file with local name creator should contain 1 document", 1, expandedSourceDocumentSources.size()); // with xmlDocumentXpath we should have none for creator (without namespace parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//creator" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/rss.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander.expandXml(storedDocumentSource); assertEquals("XML file with creator (no namespace) should contain no documents", 0, expandedSourceDocumentSources.size()); // RSS documents within a zip archive (nested expansion) parameters = new FlexibleParameters(new String[] { "inputFormat=RSS", "splitDocuments=true" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("archive/rss.xml.zip")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander .getExpandedStoredDocumentSources(storedDocumentSource); assertEquals("XML file with no Xpath should contain one document", 2, expandedSourceDocumentSources.size()); inputStream = null; fileInputStream = null; try { inputStream = storedDocumentSourceStorage .getStoredDocumentSourceInputStream(expandedSourceDocumentSources.get(1).getId()); fileInputStream = new FileInputStream(TestHelper.getResource("xml/rss.xml.item2_xpath.xml")); assertTrue(IOUtils.contentEquals(fileInputStream, inputStream)); } finally { if (inputStream != null) { inputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } // make sure a string is recognized as XML parameters = new FlexibleParameters(); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new StringInputSource("<a><b>c</b><b>d</b></a>"); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander .getExpandedStoredDocumentSources(storedDocumentSource); assertEquals("XML string with no Xpath should contain one document", 1, expandedSourceDocumentSources.size()); assertEquals("input string should be recognized as XML", DocumentFormat.XML, expandedSourceDocumentSources.get(0).getMetadata().getDocumentFormat()); // make sure an XML string is expanded parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//b" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new StringInputSource("<a><b>c</b><b>d</b></a>"); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander .getExpandedStoredDocumentSources(storedDocumentSource); assertEquals("XML string with no documents xpath should contain two documents", 2, expandedSourceDocumentSources.size()); assertEquals("input string should be recognized as XML", DocumentFormat.XML, expandedSourceDocumentSources.get(0).getMetadata().getDocumentFormat()); // make sure namespaces works properly parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//*[local-name()='table']" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/namespaces.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander .getExpandedStoredDocumentSources(storedDocumentSource); assertEquals("XML namespaces example should have three documents", 3, expandedSourceDocumentSources.size()); assertTrue("Make sure our first table still has & entity", IOUtils.toString(storedDocumentSourceStorage .getStoredDocumentSourceInputStream(expandedSourceDocumentSources.get(0).getId())) .contains("&")); // test groupby xpath parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//*[local-name()='table']", "xmlGroupByXpath=//*[local-name()='length']" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/namespaces.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander .getExpandedStoredDocumentSources(storedDocumentSource); assertEquals("XML namespaces example should have three documents", 2, expandedSourceDocumentSources.size()); parameters = new FlexibleParameters(new String[] { "xmlDocumentsXpath=//*[local-name()='table']", "xmlGroupByXpath=//*[local-name()='width']" }); storedDocumentSourceExpander = new StoredDocumentSourceExpander(storedDocumentSourceStorage, parameters); inputSource = new FileInputSource(TestHelper.getResource("xml/namespaces.xml")); storedDocumentSource = storedDocumentSourceStorage.getStoredDocumentSource(inputSource); expandedSourceDocumentSources = storedDocumentSourceExpander .getExpandedStoredDocumentSources(storedDocumentSource); assertEquals("XML namespaces example should have three documents", 1, expandedSourceDocumentSources.size()); storage.destroy(); }
From source file:org.xwiki.contrib.ldap.XWikiLDAPUtils.java
/** * Sync user avatar with LDAP// www . j a va2s . co m * * @param ldapUid value of the unique identifier for the user to update. * @param userProfile the XWiki user profile document. * @param context the XWiki context. * @return true if avatar was updated, false otherwise. * @throws XWikiException */ protected boolean updatePhotoFromLdap(String ldapUid, XWikiDocument userProfile, XWikiContext context) throws XWikiException { BaseClass userClass = context.getWiki().getUserClass(context); BaseObject userObj = userProfile.getXObject(userClass.getDocumentReference()); // Get current user avatar String userAvatar = userObj.getStringValue("avatar"); XWikiAttachment currentPhoto = null; if (userAvatar != null) { currentPhoto = userProfile.getAttachment(userAvatar); } // Get properties String photoAttachmentName = this.configuration .getLDAPParam(XWikiLDAPConfig.PREF_LDAP_PHOTO_ATTACHMENT_NAME, "ldapPhoto", context); String ldapPhotoAttribute = this.configuration.getLDAPParam(XWikiLDAPConfig.PREF_LDAP_PHOTO_ATTRIBUTE, XWikiLDAPConfig.DEFAULT_PHOTO_ATTRIBUTE, context); // Proceed only if any of conditions are true: // 1. User do not have avatar currently // 2. User have avatar and avatar file name is equals to PREF_LDAP_PHOTO_ATTACHMENT_NAME if (StringUtils.isEmpty(userAvatar) || photoAttachmentName.equals(FilenameUtils.getBaseName(userAvatar)) || currentPhoto == null) { // Obtain photo from LDAP byte[] ldapPhoto = null; List<XWikiLDAPSearchAttribute> ldapAttributes = searchUserAttributesByUid(ldapUid, new String[] { ldapPhotoAttribute }); if (ldapAttributes != null) { // searchUserAttributesByUid method may return dn as 1st element // Let's iterate over array and search ldapPhotoAttribute for (XWikiLDAPSearchAttribute attribute : ldapAttributes) { if (attribute.name.equals(ldapPhotoAttribute)) { ldapPhoto = attribute.byteValue; } } } if (ldapPhoto != null) { ByteArrayInputStream ldapPhotoInputStream = new ByteArrayInputStream(ldapPhoto); // Try to guess image type String ldapPhotoType = guessImageType(ldapPhotoInputStream); ldapPhotoInputStream.reset(); if (ldapPhotoType != null) { String photoAttachmentFullName = photoAttachmentName + "." + ldapPhotoType.toLowerCase(); if (!StringUtils.isEmpty(userAvatar) && currentPhoto != null) { try { // Compare current xwiki avatar and LDAP photo if (!IOUtils.contentEquals(currentPhoto.getContentInputStream(context), ldapPhotoInputStream)) { ldapPhotoInputStream.reset(); // Store photo return addPhotoToProfile(userProfile, context, ldapPhotoInputStream, ldapPhoto.length, photoAttachmentFullName); } } catch (IOException ex) { LOGGER.error(ex.getMessage()); } } else if (addPhotoToProfile(userProfile, context, ldapPhotoInputStream, ldapPhoto.length, photoAttachmentFullName)) { PropertyClass avatarProperty = (PropertyClass) userClass.getField("avatar"); userObj.safeput("avatar", avatarProperty.fromString(photoAttachmentFullName)); return true; } } else { LOGGER.info("Unable to determine LDAP photo image type."); } } else if (currentPhoto != null) { // Remove current avatar PropertyClass avatarProperty = (PropertyClass) userClass.getField("avatar"); userObj.safeput("avatar", avatarProperty.fromString("")); return true; } } return false; }
From source file:org.xwiki.extension.xar.internal.job.diff.AttachmentUnifiedDiffBuilder.java
private boolean contentEquals(XWikiAttachment previousAttachment, XWikiAttachment nextAttachment) { if (previousAttachment == null || nextAttachment == null) { return previousAttachment == nextAttachment; } else {/* ww w . j av a2s. c o m*/ XWikiContext xcontext = this.xcontextProvider.get(); InputStream previousContent = null; InputStream nextContent = null; try { previousContent = previousAttachment.getContentInputStream(xcontext); nextContent = nextAttachment.getContentInputStream(xcontext); return IOUtils.contentEquals(previousContent, nextContent); } catch (Exception e) { this.logger.warn("Failed to compare the content of attachment [{}]. Root cause: {}", previousAttachment.getFilename(), ExceptionUtils.getRootCauseMessage(e)); return false; } finally { try { if (previousContent != null) { previousContent.close(); } if (nextContent != null) { nextContent.close(); } } catch (IOException e) { // Ignore. } } } }
From source file:org.xwiki.filemanager.internal.DefaultFileTest.java
@Test public void getContentWithoutAttachment() throws Exception { when(file.getDocument().getAttachmentList()).thenReturn(Collections.<XWikiAttachment>emptyList()); assertTrue(IOUtils.contentEquals(file.getContent(), new ByteArrayInputStream(new byte[] {}))); }
From source file:org.xwiki.filemanager.internal.DefaultFileTest.java
@Test public void getContentWithNoAttachmentContent() throws Exception { XWikiAttachment attachment = mock(XWikiAttachment.class); when(attachment.getContentInputStream(any(XWikiContext.class))).thenThrow(XWikiException.class); when(file.getDocument().getAttachmentList()).thenReturn(Collections.singletonList(attachment)); assertTrue(IOUtils.contentEquals(file.getContent(), new ByteArrayInputStream(new byte[] {}))); }
From source file:org.zeroturnaround.zip.ZipUtil.java
private static boolean archiveEqualsInternal(File f1, File f2) throws IOException { ZipFile zf1 = null;//from w w w . j a va 2s.c o m ZipFile zf2 = null; try { zf1 = new ZipFile(f1); zf2 = new ZipFile(f2); // Check the number of entries if (zf1.size() != zf2.size()) { log.debug("Number of entries changed (" + zf1.size() + " vs " + zf2.size() + ")."); return false; } /* * As there are same number of entries in both archives we can traverse * all entries of one of the archives and get the corresponding entries * from the other archive. * * If a corresponding entry is missing from the second archive the * archives are different and we finish the comparison. * * We guarantee that no entry of the second archive is skipped as there * are same number of unique entries in both archives. */ Enumeration en = zf1.entries(); while (en.hasMoreElements()) { ZipEntry e1 = (ZipEntry) en.nextElement(); String path = e1.getName(); ZipEntry e2 = zf2.getEntry(path); // Check meta data if (!metaDataEquals(path, e1, e2)) { return false; } // Check the content InputStream is1 = null; InputStream is2 = null; try { is1 = zf1.getInputStream(e1); is2 = zf2.getInputStream(e2); if (!IOUtils.contentEquals(is1, is2)) { log.debug("Entry '{}' content changed.", path); return false; } } finally { IOUtils.closeQuietly(is1); IOUtils.closeQuietly(is2); } } } finally { closeQuietly(zf1); closeQuietly(zf2); } log.debug("Archives are the same."); return true; }
From source file:org.zeroturnaround.zip.ZipUtil.java
/** * Compares two ZIP entries (byte-by-byte). . * * @param zf1/* w w w . j a v a2 s .c om*/ * first ZIP file. * @param zf2 * second ZIP file. * @param path1 * name of the first entry. * @param path2 * name of the second entry. * @return <code>true</code> if the contents of the entries were same. */ private static boolean doEntryEquals(ZipFile zf1, ZipFile zf2, String path1, String path2) throws IOException { InputStream is1 = null; InputStream is2 = null; try { ZipEntry e1 = zf1.getEntry(path1); ZipEntry e2 = zf2.getEntry(path2); if (e1 == null && e2 == null) { return true; } if (e1 == null || e2 == null) { return false; } is1 = zf1.getInputStream(e1); is2 = zf2.getInputStream(e2); if (is1 == null && is2 == null) { return true; } if (is1 == null || is2 == null) { return false; } return IOUtils.contentEquals(is1, is2); } finally { IOUtils.closeQuietly(is1); IOUtils.closeQuietly(is2); } }