Search in sources :

Example 11 with FileResourceStream

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

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 12 with FileResourceStream

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

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)

Example 13 with FileResourceStream

use of org.apache.wicket.util.resource.FileResourceStream in project oc-explorer 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 14 with FileResourceStream

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

the class AjaxDownloadBehaviorFromFile method onRequest.

public void onRequest() {
    final File file = initFile();
    IResourceStream resourceStream = new FileResourceStream(new File(file));
    getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {

        @Override
        public void respond(IRequestCycle requestCycle) {
            try {
                super.respond(requestCycle);
            } finally {
                if (removeFile) {
                    LOGGER.debug("Removing file '{}'.", file.getAbsolutePath());
                    Files.remove(file);
                }
            }
        }
    }.setFileName(file.getName()).setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(Duration.ofSeconds(1)));
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) IResourceStream(org.apache.wicket.util.resource.IResourceStream) File(org.apache.wicket.util.file.File) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Example 15 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(ExportedTagSetConstant.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(ExportedTagSetConstant.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