Search in sources :

Example 16 with FileResourceStream

use of org.apache.wicket.util.resource.FileResourceStream in project wicket by apache.

the class CachingResourceStreamLocatorTest method fileResourceDifferentExtensions.

/**
 * Tests two FileResourceStreamReferences with different extensions
 */
@Test
void fileResourceDifferentExtensions() {
    IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
    FileResourceStream frs = new FileResourceStream(new File("."));
    when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, "extension", true)).thenReturn(frs);
    CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator(resourceStreamLocator);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension", true);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension", true);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension2", true);
    // there is a file resource with that Key so expect just one call to the delegate
    verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", null, "extension", true);
    verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", null, "extension2", true);
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 17 with FileResourceStream

use of org.apache.wicket.util.resource.FileResourceStream in project wicket by apache.

the class CachingResourceStreamLocatorTest method fileResource.

/**
 * Tests FileResourceStreamReference
 */
@Test
void fileResource() {
    IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
    FileResourceStream frs = new FileResourceStream(new File("."));
    when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, "extension", true)).thenReturn(frs);
    CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator(resourceStreamLocator);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension", true);
    cachingLocator.locate(String.class, "path", "style", "variation", null, "extension", true);
    // there is a file resource with that Key so expect just one call to the delegate
    verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", null, "extension", true);
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 18 with FileResourceStream

use of org.apache.wicket.util.resource.FileResourceStream in project wicket by apache.

the class ResourceTest method testFileResourceStream.

/**
 * tests a resource that is not cacheable.
 */
@Test
void testFileResourceStream() {
    final File testFile;
    try {
        testFile = File.createTempFile(ResourceTest.class.getName(), null);
        OutputStream out = new FileOutputStream(testFile);
        out.write(TEST_STRING.getBytes());
        out.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    bindToApplicationAsResourceAndRequestIt(new FileResourceStream(new org.apache.wicket.util.file.File(testFile)));
    assertEquals(MockHttpServletResponse.formatDate(testFile.lastModified()), tester.getLastModifiedFromResponseHeader());
    assertEquals(TEST_STRING.length(), tester.getContentLengthFromResponseHeader());
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 19 with FileResourceStream

use of org.apache.wicket.util.resource.FileResourceStream in project webanno by webanno.

the class FileSystemResource method respond.

@Override
public void respond(Attributes attributes) {
    FileResourceStream fileResourceStream = new FileResourceStream(file);
    ResourceStreamResource resource = new ResourceStreamResource(fileResourceStream);
    resource.respond(attributes);
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) ResourceStreamResource(org.apache.wicket.request.resource.ResourceStreamResource)

Example 20 with FileResourceStream

use of org.apache.wicket.util.resource.FileResourceStream in project webanno by webanno.

the class TagSetEditorPanel method export.

private FileResourceStream export() {
    File exportFile = null;
    if (exportFormat.getObject().equals(JSON_FORMAT)) {
        try {
            exportFile = File.createTempFile("exportedtagsets", ".json");
        } catch (IOException e1) {
            error("Unable to create temporary File!!");
            return null;
        }
        if (isNull(selectedTagSet.getObject().getId())) {
            error("Project not yet created. Please save project details first!");
        } else {
            TagSet tagSet = selectedTagSet.getObject();
            ExportedTagSet exTagSet = new ExportedTagSet();
            exTagSet.setDescription(tagSet.getDescription());
            exTagSet.setLanguage(tagSet.getLanguage());
            exTagSet.setName(tagSet.getName());
            List<ExportedTag> exportedTags = new ArrayList<>();
            for (Tag tag : annotationSchemaService.listTags(tagSet)) {
                ExportedTag exportedTag = new ExportedTag();
                exportedTag.setDescription(tag.getDescription());
                exportedTag.setName(tag.getName());
                exportedTags.add(exportedTag);
            }
            exTagSet.setTags(exportedTags);
            try {
                JSONUtil.generatePrettyJson(exTagSet, exportFile);
            } catch (IOException e) {
                error("File Path not found or No permision to save the file!");
            }
            info("TagSets successfully exported to :" + exportFile.getAbsolutePath());
        }
    } else if (exportFormat.getObject().equals(TAB_FORMAT)) {
        TagSet tagSet = selectedTagSet.getObject();
        try {
            exportFile = File.createTempFile("exportedtagsets", ".txt");
        } catch (IOException e1) {
            error("Unable to create temporary File!!");
        }
        OutputStream os;
        OutputStreamWriter osw;
        BufferedWriter bw;
        try {
            String tagSetDescription = tagSet.getDescription() == null ? "" : tagSet.getDescription();
            os = new FileOutputStream(exportFile);
            osw = new OutputStreamWriter(os, "UTF-8");
            bw = new BufferedWriter(osw);
            bw.write(tagSet.getName() + "\t" + tagSetDescription.replace("\n", "\\n") + "\n");
            bw.write(tagSet.getLanguage() + "\t" + " \n");
            for (Tag tag : annotationSchemaService.listTags(tagSet)) {
                String tagDescription = tag.getDescription() == null ? "" : tag.getDescription();
                bw.write(tag.getName() + "\t" + tagDescription.replace("\n", "\\n") + "\n");
            }
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e) {
            error("The file for export not found " + ExceptionUtils.getRootCauseMessage(e));
        } catch (UnsupportedEncodingException e) {
            error("Unsupported encoding " + ExceptionUtils.getRootCauseMessage(e));
        } catch (IOException e) {
            error(ExceptionUtils.getRootCause(e));
        }
    }
    return new FileResourceStream(exportFile);
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ExportedTagSet(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTagSet) TagSet(de.tudarmstadt.ukp.clarin.webanno.model.TagSet) ExportedTagSet(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTagSet) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ExportedTag(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTag) Tag(de.tudarmstadt.ukp.clarin.webanno.model.Tag) File(java.io.File) ExportedTag(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTag)

Aggregations

FileResourceStream (org.apache.wicket.util.resource.FileResourceStream)23 File (java.io.File)14 IOException (java.io.IOException)5 ResourceStreamRequestHandler (org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler)5 ResourceStreamResource (org.apache.wicket.request.resource.ResourceStreamResource)5 IResourceStream (org.apache.wicket.util.resource.IResourceStream)5 FileOutputStream (java.io.FileOutputStream)4 OutputStream (java.io.OutputStream)4 CachingResourceStreamLocator (org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator)4 ExportedTag (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTag)3 ExportedTagSet (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTagSet)3 Tag (de.tudarmstadt.ukp.clarin.webanno.model.Tag)3 TagSet (de.tudarmstadt.ukp.clarin.webanno.model.TagSet)3 BufferedWriter (java.io.BufferedWriter)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 FileSystemResourceStream (org.apache.wicket.util.resource.FileSystemResourceStream)3 Test (org.junit.jupiter.api.Test)3