Example usage for org.apache.commons.io FileUtils toFile

List of usage examples for org.apache.commons.io FileUtils toFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils toFile.

Prototype

public static File toFile(URL url) 

Source Link

Document

Convert from a URL to a File.

Usage

From source file:nl.strohalm.cyclos.utils.lucene.IndexHandler.java

/**
 * Returns the root directory where indexes are stored
 *///from  w w  w. j  a va  2  s. c  o m
public static File resolveIndexRoot() {
    // Setup the Lucene index directory to WEB-INF/indexes directory
    final File bin = FileUtils.toFile(IndexHandler.class.getResource("/")); // WEB-INF/classes
    File root = bin.getParentFile(); // WEB-INF
    // When running on the standalone server, the bin is root/bin, not root/web/WEB-INF/classes
    if (!bin.getAbsolutePath().contains("WEB-INF") && new File(root, "web").exists()) {
        root = new File(root, "web/WEB-INF");
    }
    return new File(root, "indexes"); // WEB-INF/indexes
}

From source file:org.ambraproject.rhino.RhinoTestHelper.java

/**
 * Parses an XML file.//w w  w.ja va  2 s.c o m
 * 
 * @param xmlDataResource The XML URL data resource
 *
 * @return The {@link org.w3c.dom.Document Document}
 *
 * @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies the
 *         configuration requested
 * @throws IOException if cannot process file
 * @throws SAXException if any parse errors occur
 */
public static Document loadXMLFromString(URL xmlDataResource)
        throws ParserConfigurationException, IOException, SAXException {
    final File xmlFile = FileUtils.toFile(xmlDataResource);
    final Document document = loadXMLFromString(xmlFile);
    return document;
}

From source file:org.ambraproject.rhino.service.impl.IngestionServiceTest.java

/**
 * Test successful processing of <b>manifest.xml</b> file.
 *
 * @param entryNames The list of entry names.
 *
 * @throws IOException if manifest file cannot be processed
 *///from  ww w  .  jav a 2  s  . c o m
@Test(dataProvider = "getPackageEntryNames")
public void testGetManifestXmlShouldSucceed(Collection<String> entryNames) throws IOException {
    final URL xmlDataResource = Resources.getResource(IngestionServiceTest.class, MANIFEST_XML);
    final File manifestFile = FileUtils.toFile(xmlDataResource);
    final byte[] manifestData = FileUtils.readFileToByteArray(manifestFile);
    final Archive testArchive = createStubArchive(manifestData, entryNames);

    final ManifestXml actualManifest = ingestionService.getManifestXml(testArchive);

    final Asset articleAsset = actualManifest.getArticleAsset();
    assertThat(articleAsset.getRepresentations()).hasSize(2);

    final Optional<Representation> printable = articleAsset.getRepresentation("printable");
    assertThat(printable.isPresent()).isTrue();
    final Representation printablePresentation = printable.get();
    assertThat(printablePresentation.getType()).isEqualTo("printable");
    final ManifestFile presentationManifest = printablePresentation.getFile();
    assertThat(presentationManifest.getEntry()).isEqualTo("dupp.0000001.pdf");
    assertThat(presentationManifest.getCrepoKey()).isEqualTo("10.1111/dupp.0000001.pdf");

    final ImmutableList<Asset> assets = actualManifest.getAssets();
    assertThat(assets).hasSize(8);
    final MutableInt supplementary_count = new MutableInt();
    assets.forEach(asset -> {
        final AssetTagName assetTag = asset.getAssetTagName();
        final AssetType assetType = asset.getType();
        if (assetTag.equals(ManifestXml.AssetTagName.ARTICLE)) {
            assertThat(assetType).isEqualTo(AssetType.ARTICLE);
            assertThat(asset).isEqualTo(articleAsset);
        } else if (assetTag.equals(ManifestXml.AssetTagName.OBJECT)) {
            assertThat(asset.getRepresentations()).hasSize(1);
            assertThat(assetType).isEqualTo(AssetType.SUPPLEMENTARY_MATERIAL);

            supplementary_count.increment();
            final String expectedUri = String.format("info:doi/10.1111/dupp.0000001.s%03d",
                    supplementary_count.intValue());
            assertThat(asset.getUri()).isEqualTo(expectedUri);

            if (supplementary_count.intValue() == 1) {
                // First asset will have "strkImage" set to True.
                assertThat(asset.isStrikingImage()).isTrue();
            } else {
                assertThat(asset.isStrikingImage()).isFalse();
            }
        } else {
            assert false : String.format("Unexpected test case for asset tag: '%s'", assetTag);
        }
    });

    final ImmutableList<String> expectedEntries = ImmutableList.of(MANIFEST_XML, MANIFEST_DTD);
    final ImmutableList<ManifestFile> ancillaryFiles = actualManifest.getAncillaryFiles();
    assertThat(ancillaryFiles).hasSize(2);
    ancillaryFiles.forEach(ancillary -> {
        final String entry = ancillary.getEntry();
        assertThat(expectedEntries).contains(entry);
        if (entry.equals(MANIFEST_XML)) {
            assertThat(ancillary.getCrepoKey()).isEqualTo("10.1111/dupp.0000001.manifest.xml");
            assertThat(ancillary.getMimetype()).isEqualTo("application/xml");
        } else if (entry.equals(MANIFEST_DTD)) {
            assertThat(ancillary.getCrepoKey()).isEqualTo("10.1111/dupp.0000001.manifest.dtd");
            assertThat(ancillary.getMimetype()).isEqualTo("application/xml-dtd");
        } else {
            assert false : String.format("Unexpected test case for entry: '%s'", entry);
        }
    });
}

From source file:org.ambraproject.rhino.service.impl.IngestionServiceTest.java

/**
 * Test exception handling with an <b>empty</b> manifest.xml file.
 *
 * @param entryNames The list of entries in the test archive.
 *
 * @throws IOException if manifest file cannot be processed
 *///  w  w w .  ja v a2  s.c om
@Test(dataProvider = "getPackageEntryNames")
public void testEmptyManifestXmlShouldFail(Collection<String> entryNames) throws IOException {
    final URL xmlDataResource = Resources.getResource(IngestionServiceTest.class, "empty_manifest.xml");
    final File manifestFile = FileUtils.toFile(xmlDataResource);
    final byte[] manifestData = FileUtils.readFileToByteArray(manifestFile);
    final Archive testArchive = createStubArchive(manifestData, entryNames);

    final ManifestXml actualManifest = ingestionService.getManifestXml(testArchive);

    assertThat(actualManifest).isNotNull();

    try {
        actualManifest.getArticleAsset();
        fail("Expecting exception, but nothing was thrown.");
    } catch (Exception exception) {
        assertThat(exception).isInstanceOf(ManifestDataException.class);
        assertThat(exception).hasMessageThat().isEqualTo("'article' node must have 'uri' attribute");
    }
}

From source file:org.ambraproject.rhino.service.impl.IngestionServiceTest.java

/**
 * Test exception handling with an <b>duplicate URI</b> in manifest.xml file.
 *
 * @param entryNames The list of entries in the test archive.
 *
 * @throws IOException if manifest file cannot be processed
 *///from  w  w w.  j a v  a  2s  .  c o  m
@Test(dataProvider = "getPackageEntryNames")
public void testDuplicateUriManifestXmlShouldFail(Collection<String> entryNames) throws IOException {
    final URL xmlDataResource = Resources.getResource(IngestionServiceTest.class,
            "duplicate_assets_manifest.xml");
    final File manifestFile = FileUtils.toFile(xmlDataResource);
    final byte[] manifestData = FileUtils.readFileToByteArray(manifestFile);
    final Archive testArchive = createStubArchive(manifestData, entryNames);

    final ManifestXml actualManifest = ingestionService.getManifestXml(testArchive);

    assertThat(actualManifest).isNotNull();

    try {
        actualManifest.getArticleAsset();
        fail("Expecting exception, but nothing was thrown.");
    } catch (Exception exception) {
        assertThat(exception).isInstanceOf(ManifestDataException.class);
        assertThat(exception).hasMessageThat()
                .isEqualTo("Manifest has assets with duplicate uri: info:doi/10.1111/dupp.0000001.s001");
    }
}

From source file:org.ambraproject.rhino.service.impl.IngestionServiceTest.java

/**
 * Test successful article ingestion./*from w ww. j  a va 2  s.  c  om*/
 * 
 * @throws IOException if archive file cannot be processed
 * @throws SAXException if any XML parsing errors
 * @throws ParserConfigurationException if a DocumentBuilder cannot be created
 */
@Test
@DirtiesContext()
public void testArticleIngestShouldSucceed() throws IOException, ParserConfigurationException, SAXException {
    final URL xmlDataResource = Resources.getResource(IngestionServiceTest.class, MANIFEST_XML);
    final File manifestFile = FileUtils.toFile(xmlDataResource);
    final byte[] manifestData = FileUtils.readFileToByteArray(manifestFile);
    final Archive testArchive = createStubArchive(manifestData, ARTICLE_INGEST_ENTRIES);

    final Optional<String> bucketName = Optional.empty();
    final String manuscriptEntry = "dupp.0000001.xml";
    final URL manuscriptResource = Resources.getResource(IngestionServiceTest.class, manuscriptEntry);
    final Document manuscript = RhinoTestHelper.loadXMLFromString(manuscriptResource);

    final Article expectedArticle = new Article();
    expectedArticle.setDoi("10.1111/dupp.0000001");

    final ArticleIngestion expectedIngestion = new ArticleIngestion();
    expectedIngestion.setArticle(expectedArticle);
    expectedIngestion.setIngestionNumber(1);

    final Doi expectedArticleDoi = Doi.create(INGESTED_DOI_URI);

    final HibernatePersistenceService mockPersistenceService = applicationContext
            .getBean(HibernatePersistenceService.class);
    when(mockPersistenceService.persistArticle(expectedArticleDoi)).thenReturn(expectedArticle);
    when(mockPersistenceService.persistIngestion(any(Article.class), any(IngestPackage.class)))
            .thenReturn(expectedIngestion);

    final IngestionService mockIngestionService = applicationContext.getBean(IngestionService.class);
    doReturn(manuscript).when(mockIngestionService).getDocument(testArchive, manuscriptEntry);

    final ArticleIngestion actualIngestion = mockIngestionService.ingest(testArchive, bucketName);

    assertThat(actualIngestion).isNotNull();
    assertThat(actualIngestion).isEqualTo(expectedIngestion);
    assertThat(actualIngestion.getArticle()).isEqualTo(expectedArticle);

    final CustomMetadataExtractor.Factory mockMetadataExtractorFactory = applicationContext
            .getBean(CustomMetadataExtractor.Factory.class);
    final ArticleCrudService mockArticleCrudService = applicationContext.getBean(ArticleCrudService.class);

    verify(mockIngestionService).getDocument(testArchive, manuscriptEntry);
    verify(mockMetadataExtractorFactory).parse(manuscript);

    final ImmutableList<String> expectedDois = ImmutableList.of(INGESTED_DOI_URI,
            "info:doi/10.1111/dupp.0000001.s001", "info:doi/10.1111/dupp.0000001.s002",
            "info:doi/10.1111/dupp.0000001.s003", "info:doi/10.1111/dupp.0000001.s004",
            "info:doi/10.1111/dupp.0000001.s005", "info:doi/10.1111/dupp.0000001.s006",
            "info:doi/10.1111/dupp.0000001.s007");
    final int callCount = mockingDetails(mockArticleCrudService).getInvocations().size();
    assertThat(callCount).isEqualTo(expectedDois.size());
    expectedDois.forEach(doiUri -> {
        final Doi assetDoi = Doi.create(doiUri);
        verify(mockArticleCrudService).getAllArticleItems(assetDoi);
    });

    verify(mockPersistenceService).persistArticle(expectedArticleDoi);
    verify(mockPersistenceService).persistIngestion(any(Article.class), any(IngestPackage.class));
}

From source file:org.ambraproject.rhino.service.impl.IngestionServiceTest.java

/**
 * Test successful article ingestion into a bucket.
 * /*ww  w  .java  2  s  .  co  m*/
 * @throws IOException if archive file cannot be processed
 * @throws SAXException if any XML parsing errors
 * @throws ParserConfigurationException if a DocumentBuilder cannot be created
 */
@Test
@DirtiesContext
public void testIngestIntoBucketShouldSucceed() throws IOException, ParserConfigurationException, SAXException {
    final URL xmlDataResource = Resources.getResource(IngestionServiceTest.class, MANIFEST_XML);
    final File manifestFile = FileUtils.toFile(xmlDataResource);
    final byte[] manifestData = FileUtils.readFileToByteArray(manifestFile);
    final Archive testArchive = createStubArchive(manifestData, ARTICLE_INGEST_ENTRIES);

    final Optional<String> bucketName = Optional.of("corpus");
    final RuntimeConfiguration.MultiBucketContentRepoEndpoint mockRepoEndpoint = new RhinoTestHelper.TestMultiBucketContentRepoEndpoint(
            bucketName.get());

    final RuntimeConfiguration mockRuntimeConfiguration = applicationContext
            .getBean(RuntimeConfiguration.class);
    doReturn(mockRepoEndpoint).when(mockRuntimeConfiguration).getCorpusStorage();

    final String manuscriptEntry = "dupp.0000001.xml";
    final URL manuscriptResource = Resources.getResource(IngestionServiceTest.class, manuscriptEntry);
    final Document manuscript = RhinoTestHelper.loadXMLFromString(manuscriptResource);

    final IngestionService mockIngestionService = applicationContext.getBean(IngestionService.class);

    doReturn(manuscript).when(mockIngestionService).getDocument(testArchive, manuscriptEntry);

    final ArticleIngestion expectedIngestion = new ArticleIngestion();

    doReturn(expectedIngestion).when(mockIngestionService).processIngestPackage(any(IngestPackage.class));

    mockIngestionService.ingest(testArchive, bucketName); // Ingest the archive to the bucket.

    verify(mockIngestionService).processIngestPackage(any(IngestPackage.class));
}

From source file:org.ambraproject.rhino.service.impl.IngestionServiceTest.java

/**
 * Test exception handling for article ingestion into an unknown bucket.
 * //w  w w.ja va  2 s  .c  o m
 * @throws IOException if archive file cannot be processed
 * @throws SAXException if any XML parsing errors
 * @throws ParserConfigurationException if a DocumentBuilder cannot be created
 */
@Test
@DirtiesContext
public void testIngestIntoUnknownBucketShouldFail()
        throws IOException, ParserConfigurationException, SAXException {
    final URL xmlDataResource = Resources.getResource(IngestionServiceTest.class, MANIFEST_XML);
    final File manifestFile = FileUtils.toFile(xmlDataResource);
    final byte[] manifestData = FileUtils.readFileToByteArray(manifestFile);
    final Archive testArchive = createStubArchive(manifestData, ARTICLE_INGEST_ENTRIES);

    final Optional<String> bucketName = Optional.of("unknown");
    final String manuscriptEntry = "dupp.0000001.xml";
    final URL manuscriptResource = Resources.getResource(IngestionServiceTest.class, manuscriptEntry);
    final Document manuscript = RhinoTestHelper.loadXMLFromString(manuscriptResource);

    final IngestionService mockIngestionService = applicationContext.getBean(IngestionService.class);

    doReturn(manuscript).when(mockIngestionService).getDocument(testArchive, manuscriptEntry);

    try {
        mockIngestionService.ingest(testArchive, bucketName);
        fail("Expecting exception, but nothing was thrown.");
    } catch (Exception exception) {
        assertThat(exception).isInstanceOf(RestClientException.class);
        final String message = NO_SPACE_JOINER.join("Invalid bucket name: ", bucketName.get(),
                ". Allowed values are: ", "[bucket_name, secondary_bucket]. ",
                "(Allowed values are specified by rhino.yaml.)");
        assertThat(exception).hasMessageThat().isEqualTo(message);
    }

    verify(mockIngestionService, times(0)).processIngestPackage(any(IngestPackage.class));
}

From source file:org.anc.gate.GrafDocument.java

@Override
public Resource init() throws ResourceInstantiationException {
    //      Out.prln("Initializing GrafDocument.");
    super.init();

    IGraph graph;/* ww w  .  j a  v a2  s.  c  o  m*/
    File fullPath;
    //from the gate gui
    URL url = super.getSourceUrl();
    if (url != null) {
        fullPath = FileUtils.toFile(url);
    } else {
        Out.println("No file found: sourceUrl = " + url);
        return null;
    }

    // Load a DocumentHeader object to query the data it contains.
    DocumentHeader docHeader = null;
    try {
        docHeader = new DocumentHeader(fullPath);
    } catch (FileNotFoundException e) {
        throw new ResourceInstantiationException("Error loading document header.", e);
    }

    // Discard the XML from the header
    AnnotationSet originalMarkups = getAnnotations("Original markups");
    if (originalMarkups != null) {
        originalMarkups.clear();
    }

    // Get the text for the document
    String filename = null;
    try {
        filename = docHeader.getContentLocation();
    } catch (GrafException e) {
        throw new ResourceInstantiationException("Unable to locate the primary data.", e);
    }
    //file should be there...
    if (filename == null) {
        throw new ResourceInstantiationException("Document content not specified in the headern.");
    }

    //get the original text, see getContent below
    File txtFile = new File(fullPath.getParentFile(), filename);
    if (!txtFile.exists()) {
        throw new ResourceInstantiationException("Primary data not found: " + txtFile.getPath());
    }
    //      Out.prln("Loading text from " + txtFile.getPath());
    String theContent = null;
    try {
        theContent = getContent(new File(fullPath.getParentFile(), filename));
    } catch (IOException e) {
        throw new ResourceInstantiationException("Unable to load text content.", e);
    }

    endOfContent = theContent.length();
    if (endOfContent == 0) {
        throw new ResourceInstantiationException("Text file is empty");
    }
    // Replace the DocumentContent.  The current DocumentContent contains the
    // ANC header file, which not what we want the user to see.
    //make a new gate Document Content object with the original text
    DocumentContent docContent = new DocumentContentImpl(theContent);
    //set it to this object, and move on, nothing to see here...
    this.setContent(docContent);

    // Get a parser for the standoff annotations
    //AnnotationParser parser = new AnnotationParser();
    try {
        ResourceHeader header = new ResourceHeader(resourceHeader.openStream());
        GrafParser graphParser = new GrafParser(header);
        //This is a gate Annotation set made using standoffASName, that comes from the
        //gate GUI, Gate uses the setStandoffASName above to fill it, getAnnotations is a gate method, not ours
        gateAnnotations = this.getAnnotations(standoffASName);
        //get an iterator from the standoffAnnotations; ie iterate through the stand off file names
        if (standoffAnnotations == null || standoffAnnotations.size() == 0) {
            standoffAnnotations = docHeader.getAnnotationTypes();
        }
        for (String type : standoffAnnotations) {
            //ok get the file name for each standoff file type, remember we are working with a *.anc file here..
            filename = docHeader.getAnnotationLocation(type);
            Out.prln("Loading annotation " + type + " from " + filename);
            // System.out.println("filename is " + filename);
            if (filename != null) {
                try {
                    //get the file name from this iteration, call the parser for that file now...
                    //and pull out the annotations from this iterations standoff file.
                    //  newAnnotations = parser.parse(basePath + System.getProperty("file.separator") + filename);
                    graph = graphParser.parse(new File(fullPath.getParentFile(), filename));
                } catch (Exception e) {
                    Out.println("Could not load graph from " + filename);
                    throw new ResourceInstantiationException(e);
                }
                //cycle through the nodes only here, making it look easy
                for (INode node : graph.nodes()) {
                    //this is where the magic happens...see comments in addAnnotations
                    addAnnotation(node);
                }
            }
        }
    } catch (SAXException e) {
        Out.println("Could not create GraphParser");
    } catch (Exception e) {
        Out.prln("Unable to load GrafDocument");
        e.printStackTrace();
    }
    return this;
}

From source file:org.anc.gate.LoadGrafStandoff.java

@Override
public Resource init() throws ResourceInstantiationException {
    if (resourceHeader == null) {
        throw new ResourceInstantiationException("The resource header has not been set.");
    }/*  w w  w .  ja  v  a  2  s.  c o m*/
    try {
        super.init();
        //         parser = new GrafParser();
        File headerFile = FileUtils.toFile(resourceHeader);
        header = new ResourceHeader(headerFile);
        //         for (IAnnotationSpace aspace : header.getAnnotationSpaces())
        //         {
        //            parser.addAnnotationSpace(aspace);
        //         }
    } catch (Exception ex) {
        throw new ResourceInstantiationException("Unable to initialize the GraphParser", ex);
    }
    return this;
}