Skip to content

Commit

Permalink
#4550 - Clean up code
Browse files Browse the repository at this point in the history
- Using var
- Added ability to set content type on InputStreamResourceStream
  • Loading branch information
reckart committed Mar 16, 2024
1 parent fea7951 commit 6d20a5f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class InputStreamResourceStream

private final InputStream inputStream;
private final String name;
private String contentType;

public InputStreamResourceStream(InputStream aInputStream)
{
Expand All @@ -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
{
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6d20a5f

Please sign in to comment.