use of org.apache.wicket.util.resource.AbstractResourceStream in project midpoint by Evolveum.
the class AjaxDownloadBehaviorFromStream method onRequest.
public void onRequest() {
final InputStream byteStream = initStream();
if (byteStream == null) {
return;
}
IResourceStream resourceStream = new AbstractResourceStream() {
@Override
public String getContentType() {
return contentType;
}
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
return byteStream;
}
@Override
public void close() throws IOException {
byteStream.close();
}
};
ResourceStreamRequestHandler reqHandler = new ResourceStreamRequestHandler(resourceStream) {
@Override
public void respond(IRequestCycle requestCycle) {
super.respond(requestCycle);
}
}.setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(Duration.ONE_SECOND);
if (StringUtils.isNotEmpty(getFileName())) {
reqHandler.setFileName(getFileName());
}
getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(reqHandler);
}
use of org.apache.wicket.util.resource.AbstractResourceStream in project wicket by apache.
the class ConcatBundleResource method getResourceStream.
@Override
public IResourceStream getResourceStream() {
List<IResourceStream> resources = collectResourceStreams();
if (resources == null) {
return null;
}
byte[] bytes;
try {
bytes = readAllResources(resources);
} catch (IOException e) {
return null;
} catch (ResourceStreamNotFoundException e) {
return null;
}
final String contentType = findContentType(resources);
final Time lastModified = findLastModified(resources);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
final long length = bytes.length;
AbstractResourceStream ret = new AbstractResourceStream() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
return inputStream;
}
@Override
public Bytes length() {
return Bytes.bytes(length);
}
@Override
public String getContentType() {
return contentType;
}
@Override
public Time lastModifiedTime() {
return lastModified;
}
@Override
public void close() throws IOException {
inputStream.close();
}
};
return ret;
}
use of org.apache.wicket.util.resource.AbstractResourceStream in project webanno by webanno.
the class AgreementTable method makeDownloadBehavior.
private Behavior makeDownloadBehavior(final String aKey1, final String aKey2) {
return new AjaxEventBehavior("click") {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget aTarget) {
AJAXDownload download = new AJAXDownload() {
private static final long serialVersionUID = 1L;
@Override
protected IResourceStream getResourceStream() {
return new AbstractResourceStream() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
try {
AgreementResult result = AgreementTable.this.getModelObject().getStudy(aKey1, aKey2);
switch(settings.getObject().exportFormat) {
case CSV:
return AgreementUtils.generateCsvReport(result);
case DEBUG:
return generateDebugReport(result);
default:
throw new IllegalStateException("Unknown export format [" + settings.getObject().exportFormat + "]");
}
} catch (Exception e) {
// FIXME Is there some better error handling here?
LOG.error("Unable to generate agreement report", e);
throw new ResourceStreamNotFoundException(e);
}
}
@Override
public void close() throws IOException {
// Nothing to do
}
};
}
};
getComponent().add(download);
download.initiate(aTarget, "agreement" + settings.getObject().exportFormat.getExtension());
}
};
}
use of org.apache.wicket.util.resource.AbstractResourceStream in project openmeetings by apache.
the class LangPanel method onInitialize.
@Override
protected void onInitialize() {
// Create feedback panels
add(feedback.setOutputMarkupId(true));
language = new AbstractMap.SimpleEntry<>(1L, Locale.ENGLISH);
final LabelsForm form = new LabelsForm("form", this, new StringLabel(null, null));
form.showNewRecord();
add(form);
final SearchableDataView<StringLabel> dataView = new SearchableDataView<StringLabel>("langList", new SearchableDataProvider<StringLabel>(LabelDao.class) {
private static final long serialVersionUID = 1L;
@Override
public long size() {
return LabelDao.count(language.getValue(), search);
}
@Override
public Iterator<? extends StringLabel> iterator(long first, long count) {
return LabelDao.get(language.getValue(), search, (int) first, (int) count, getSort()).iterator();
}
}) {
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(final Item<StringLabel> item) {
final StringLabel fv = item.getModelObject();
item.add(new Label("key"));
item.add(new Label("value"));
item.add(new AjaxEventBehavior(EVT_CLICK) {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target) {
form.setModelObject(fv);
form.hideNewRecord();
target.add(form, listContainer);
reinitJs(target);
}
});
item.add(AttributeModifier.append(ATTR_CLASS, getRowClass(fv.getId(), form.getModelObject().getId())));
}
};
add(listContainer.add(dataView).setOutputMarkupId(true));
PagedEntityListPanel navigator = new PagedEntityListPanel("navigator", dataView) {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target) {
dataView.modelChanging();
target.add(listContainer);
}
};
DataViewContainer<StringLabel> container = new DataViewContainer<>(listContainer, dataView, navigator);
container.addLink(new OmOrderByBorder<>("orderByName", "key", container)).addLink(new OmOrderByBorder<>("orderByValue", "value", container));
add(container.getLinks());
add(navigator);
langForm = new LangForm("langForm", listContainer, this);
langForm.add(fileUploadField);
langForm.add(new UploadProgressBar("progress", langForm, fileUploadField));
fileUploadField.add(new AjaxFormSubmitBehavior(langForm, "change") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target) {
FileUpload download = fileUploadField.getFileUpload();
try {
if (download == null || download.getInputStream() == null) {
feedback.error("File is empty");
return;
}
LabelDao.upload(language.getValue(), download.getInputStream());
} catch (Exception e) {
log.error("Exception on panel language editor import ", e);
feedback.error(e);
}
// repaint the feedback panel so that it is hidden
target.add(listContainer, feedback);
}
});
// Add a component to download a file without page refresh
final AjaxDownloadBehavior download = new AjaxDownloadBehavior(new ResourceStreamResource() {
private static final long serialVersionUID = 1L;
{
setContentDisposition(ATTACHMENT);
setCacheDuration(NONE);
}
@Override
protected IResourceStream getResourceStream(Attributes attributes) {
final String name = LabelDao.getLabelFileName(language.getValue());
setFileName(name);
return new AbstractResourceStream() {
private static final long serialVersionUID = 1L;
private transient InputStream is;
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
try {
is = Application.class.getResourceAsStream(name);
return is;
} catch (Exception e) {
throw new ResourceStreamNotFoundException(e);
}
}
@Override
public void close() throws IOException {
if (is != null) {
is.close();
is = null;
}
}
};
}
});
langForm.add(download);
langForm.add(new AjaxButton("export") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target) {
download.initiate(target);
// repaint the feedback panel so that it is hidden
target.add(feedback);
}
@Override
protected void onError(AjaxRequestTarget target) {
// repaint the feedback panel so errors are shown
target.add(feedback);
}
});
add(langForm);
final AddLanguageDialog addLang = new AddLanguageDialog("addLang", this);
add(addLang, new AjaxLink<Void>("addLangBtn") {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
addLang.open(target);
}
});
add(BootstrapFileUploadBehavior.INSTANCE);
add(new ConfirmableAjaxBorder("deleteLangBtn", getString("80"), getString("833")) {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target) {
LabelDao.delete(language.getValue());
List<Map.Entry<Long, Locale>> langs = LangForm.getLanguages();
language = langs.isEmpty() ? null : langs.get(0);
langForm.updateLanguages(target);
target.add(listContainer);
}
});
super.onInitialize();
}
use of org.apache.wicket.util.resource.AbstractResourceStream in project webanno by webanno.
the class AJAXDownload method getResourceStream.
/**
* Hook method providing the actual resource stream.
*
* @return the stream.
*/
protected IResourceStream getResourceStream() {
return new AbstractResourceStream() {
private static final long serialVersionUID1 = 1L;
InputStream inStream;
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
try {
inStream = new FileInputStream(fileName);
} catch (IOException e) {
throw new ResourceStreamNotFoundException(e);
}
return inStream;
}
@Override
public void close() throws IOException {
inStream.close();
inStream = null;
FileUtils.forceDelete(new File(fileName));
}
};
}
Aggregations