From 6d20a5f5cec658bed35b9ebe3169a9bef8128a34 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Sat, 16 Mar 2024 12:55:31 +0100 Subject: [PATCH] #4550 - Clean up code - Using var - Added ability to set content type on InputStreamResourceStream --- .../export/ExportDocumentDialogContent.java | 8 +++---- .../webanno/api/format/FormatSupport.java | 2 +- .../support/wicket/AjaxDownloadLink.java | 6 ++--- .../wicket/InputStreamResourceStream.java | 24 +++++++++++++++++++ .../ui/project/layers/LayerDetailForm.java | 2 +- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/annotation/actionbar/export/ExportDocumentDialogContent.java b/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/annotation/actionbar/export/ExportDocumentDialogContent.java index 2ac573a7b72..2d9750aa762 100644 --- a/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/annotation/actionbar/export/ExportDocumentDialogContent.java +++ b/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/annotation/actionbar/export/ExportDocumentDialogContent.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.Serializable; +import java.lang.invoke.MethodHandles; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -55,7 +56,7 @@ public class ExportDocumentDialogContent { private static final long serialVersionUID = -2102136855109258306L; - private static final Logger LOG = LoggerFactory.getLogger(ExportDocumentDialogContent.class); + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private @SpringBean DocumentImportExportService importExportService; private @SpringBean DocumentService documentService; @@ -100,9 +101,8 @@ private IResourceStream export() { File exportedFile = null; try { - AnnotatorState s = state.getObject(); - FormatSupport format = importExportService - .getFormatByName(preferences.getObject().format).get(); + var s = state.getObject(); + var format = importExportService.getFormatByName(preferences.getObject().format).get(); exportedFile = importExportService.exportAnnotationDocument(s.getDocument(), s.getUser().getUsername(), format, s.getMode()); diff --git a/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java b/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java index 7fa9f24c549..8af8142536d 100644 --- a/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java +++ b/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java @@ -169,7 +169,7 @@ default AnalysisEngineDescription getWriterDescription(Project aProject, default void read(Project aProject, CAS cas, File aFile) throws ResourceInitializationException, IOException, CollectionException { - CollectionReaderDescription readerDescription = getReaderDescription(aProject, null); + var readerDescription = getReaderDescription(aProject, null); addConfigurationParameters(readerDescription, ResourceCollectionReaderBase.PARAM_SOURCE_LOCATION, aFile.getParentFile().getAbsolutePath(), diff --git a/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/AjaxDownloadLink.java b/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/AjaxDownloadLink.java index d2c06cfab28..237f68d9abf 100644 --- a/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/AjaxDownloadLink.java +++ b/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/AjaxDownloadLink.java @@ -83,11 +83,11 @@ void commonInit() @Override public void onRequest() { - IResourceStream is = AjaxDownloadLink.this.getModelObject(); + var is = AjaxDownloadLink.this.getModelObject(); if (is != null) { // If no filename has been set explicitly, try to get it from the resource - String name = filename != null ? filename.getObject() : null; + var name = filename != null ? filename.getObject() : null; if (name == null) { if (is instanceof FileResourceStream) { name = ((FileResourceStream) is).getFile().getName(); @@ -101,7 +101,7 @@ else if (is instanceof InputStreamResourceStream) { } } - ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler( + var handler = new ResourceStreamRequestHandler( AjaxDownloadLink.this.getModelObject(), name); handler.setContentDisposition(ContentDisposition.ATTACHMENT); getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler); diff --git a/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/InputStreamResourceStream.java b/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/InputStreamResourceStream.java index ae5452a77d2..bb5a611edf1 100644 --- a/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/InputStreamResourceStream.java +++ b/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/wicket/InputStreamResourceStream.java @@ -30,6 +30,7 @@ public class InputStreamResourceStream private final InputStream inputStream; private final String name; + private String contentType; public InputStreamResourceStream(InputStream aInputStream) { @@ -43,6 +44,13 @@ public InputStreamResourceStream(InputStream aInputStream, String aName) name = aName; } + public InputStreamResourceStream(InputStream aInputStream, String aName, String aContentType) + { + inputStream = aInputStream; + name = aName; + contentType = aContentType; + } + @Override public InputStream getInputStream() throws ResourceStreamNotFoundException { @@ -59,4 +67,20 @@ public void close() throws IOException { inputStream.close(); } + + public InputStreamResourceStream setContentType(String aContentType) + { + contentType = aContentType; + return this; + } + + @Override + public String getContentType() + { + if (contentType != null) { + return contentType; + } + + return super.getContentType(); + } } diff --git a/inception/inception-ui-project/src/main/java/de/tudarmstadt/ukp/clarin/webanno/ui/project/layers/LayerDetailForm.java b/inception/inception-ui-project/src/main/java/de/tudarmstadt/ukp/clarin/webanno/ui/project/layers/LayerDetailForm.java index 735fa4a5e56..8a66cfbf1ce 100644 --- a/inception/inception-ui-project/src/main/java/de/tudarmstadt/ukp/clarin/webanno/ui/project/layers/LayerDetailForm.java +++ b/inception/inception-ui-project/src/main/java/de/tudarmstadt/ukp/clarin/webanno/ui/project/layers/LayerDetailForm.java @@ -396,7 +396,7 @@ private void actionCancel(AjaxRequestTarget aTarget) private IResourceStream exportFullTypeSystemAsUimaXml() { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + try (var bos = new ByteArrayOutputStream()) { var tsd = annotationService.getFullProjectTypeSystem(getModelObject().getProject(), false); tsd.toXML(bos);