Search in sources :

Example 1 with FileResourceStream

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

the class FolderContentResource method respond.

public void respond(final Attributes attributes) {
    PageParameters parameters = attributes.getParameters();
    String fileName = parameters.get(PARAM_FILE_NAME).toString();
    // we use FilenameUtils to prevent "security tricks", only a file name
    // without path is allowed
    File file = new File(rootFolder, FilenameUtils.getName(fileName));
    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) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) File(java.io.File)

Example 2 with FileResourceStream

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

the class CachingResourceStreamLocatorTest method fileResource.

/**
 * Tests FileResourceStreamReference
 */
@Test
public 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) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) File(java.io.File) Test(org.junit.Test)

Example 3 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
public 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) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) File(java.io.File) Test(org.junit.Test)

Example 4 with FileResourceStream

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

the class DownloadLink method onClick.

@Override
public void onClick() {
    final File file = getModelObject();
    if (file == null) {
        throw new IllegalStateException(getClass().getName() + " failed to retrieve a File object from model");
    }
    String fileName = fileNameModel != null ? fileNameModel.getObject() : null;
    if (Strings.isEmpty(fileName)) {
        fileName = file.getName();
    }
    IResourceStream resourceStream = new FileResourceStream(new org.apache.wicket.util.file.File(file));
    getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {

        @Override
        public void respond(IRequestCycle requestCycle) {
            super.respond(requestCycle);
            if (deleteAfter) {
                Files.remove(file);
            }
        }
    }.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(cacheDuration));
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) IResourceStream(org.apache.wicket.util.resource.IResourceStream) File(java.io.File) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Example 5 with FileResourceStream

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

the class ExportDocumentDialogContent method export.

private FileResourceStream export() {
    File downloadFile = null;
    String username = state.getObject().getMode().equals(Mode.AUTOMATION) && preferences.getObject().documentType.equals(SELECTEXPORT.AUTOMATED.toString()) ? "CORRECTION_USER" : SecurityContextHolder.getContext().getAuthentication().getName();
    try {
        downloadFile = importExportService.exportAnnotationDocument(state.getObject().getDocument(), username, importExportService.getWritableFormats().get(importExportService.getWritableFormatId(preferences.getObject().format)), state.getObject().getDocument().getName(), state.getObject().getMode());
    } catch (Exception e) {
        LOG.error("Export failed", e);
        error("Export failed:" + ExceptionUtils.getRootCauseMessage(e));
        // RestartResponseException the feedback message only flashes.
        throw new NonResettingRestartException(getPage().getPageClass());
    }
    return new FileResourceStream(downloadFile);
}
Also used : NonResettingRestartException(org.apache.wicket.NonResettingRestartException) FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) File(java.io.File) NonResettingRestartException(org.apache.wicket.NonResettingRestartException)

Aggregations

FileResourceStream (org.apache.wicket.util.resource.FileResourceStream)13 File (java.io.File)8 ResourceStreamRequestHandler (org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler)3 ResourceStreamResource (org.apache.wicket.request.resource.ResourceStreamResource)3 IResourceStream (org.apache.wicket.util.resource.IResourceStream)3 Test (org.junit.Test)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)2 CachingResourceStreamLocator (org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator)2 IRequestCycle (org.apache.wicket.request.IRequestCycle)2 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)2 File (org.apache.wicket.util.file.File)2 ExportedTag (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTag)1 ExportedTagSet (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedTagSet)1 Tag (de.tudarmstadt.ukp.clarin.webanno.model.Tag)1 TagSet (de.tudarmstadt.ukp.clarin.webanno.model.TagSet)1 BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1