Wicket adding a big Pdf as resource - java

In my app I display pdf's using a ByteArrayResource.
This was working fine untill I started working with bigger files. The conversion to ByteArray keeps giving me an out of memory error.
This is how I do it at the moment:
File myPdf=new File(thePath);
FileInputStream fin = new FileInputStream(myPdf);
final byte fileContent[] = new byte[(int)myPdf.length()];
fin.read(fileContent);
fin.close();
ResourceReference rr = new ResourceReference(dePdf.getName()) {
#Override
public IResource getResource() {
return new ByteArrayResource("Application/pdf", fileContent);
}
};
if (rr.canBeRegistered()) {
getApplication().getResourceReferenceRegistry().registerResourceReference(rr);
}
return wmc;
Is there a better way to display a big file?

Try using ResourceStreamResource and FileResourceStream:
File myPdf=new File(thePath);
FileResourceStream frs = new FileResourceStream(myPdf);
ResourceStreamResource rsr = new ResourceStreamResource(frs);
rsr.setContentDisposition(ContentDisposition.ATTACHMENT);
rsr.setFileName(fileName);
//the same code for resource reference creation and registration
//...

Not entirely sure (never really used them myself) but a ContextRelativeResource may be an option. Perhaps something like:
final File myPdf=new File(thePath);
ResourceReference rr = new ResourceReference(dePdf.getName()) {
#Override
public IResource getResource() {
// You'll need to adjust the path here to be relative to your context
return new ContextRelativeResource(myPdf.getAbsolutePath());
}
};
if (rr.canBeRegistered()) {
getApplication().getResourceReferenceRegistry().registerResourceReference(rr);
}

Related

Spring Boot jar saving to existing file

I want to save data with jackson to existing file (update it) but it won't work when I run my project from jar.
I need to use json as "database" (I know it's pretty stupid but that's for a school project) and to do it I load and save all the data when I do any of CRUD operations. It's working fine when I run it with IDE but when I tried as a jar it had a problem with reading file from ClassPathResource.
So I have this method to save changes to file:
private List<Item> items;
private ObjectMapper mapper;
private ObjectWriter writer;
public void saveData() {
mapper = new ObjectMapper();
writer = mapper.writer(new DefaultPrettyPrinter());
try {
writer.writeValue(new ClassPathResource("items.json").getFile(), items);
} catch (IOException e) {
e.printStackTrace();
}
}
And it works just fine when i run this through IntelliJ but it won't work when I run it as a jar.
I found a solution to loading the data by using InputStream from this question and method looks like this:
public void loadData() {
mapper = new ObjectMapper();
try {
ClassPathResource classPathResource = new ClassPathResource("items.json");
InputStream inputStream = classPathResource.getInputStream();
File tempFile = File.createTempFile("test", ".json");
FileUtils.copyInputStreamToFile(inputStream, tempFile);
System.out.println(tempFile);
System.out.println(ItemDao.class.getProtectionDomain().getCodeSource().getLocation().getPath().toString());
items = mapper.readValue(tempFile, new TypeReference<List<Item>>() {
});
} catch (IOException e) {
items = null;
e.printStackTrace();
}
}
But I still have no idea how to actually save changes. I was thinking about making use of FileOutputStreambut I achieved nothing.
So I want to get this working in jar file and be able to save changes to the same file, thanks for help in advance!
when you want to do read/write operations, it is better keep the file outside of the project. when running the jar, pass file name with path as an argument. like -DfileName=/Users/chappa/Documents/items.json etc. This way, you have absolute path, and you can perform read/write operations on it
if you are using java 1.7 or above, use below approach to write data.
To read data, you can use jackson api to load the json file as is.
Path wipPath = Paths.get("/Users/chappa/Documents/items.json");
try (BufferedWriter writer = Files.newBufferedWriter(wipPath)) {
for (String record : nosRecords) {
writer.write(record);
}
}
Just in case if you want to read json using IO streams, you can use below code
Path wipPath = Paths.get("/Users/chappa/Documents/items.json");
try (BufferedReader reader = Files.newBufferedReader(wipPath)) {
String line=null;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
}

How to watch file for new content and retrieve that content

I have a file with name foo.txt. This file contains some text. I want to achieve following functionality:
I launch program
write something to the file (for example add one row: new string in foo.txt)
I want to get ONLY NEW content of this file.
Can you clarify the best solution of this problem? Also I want resolve related issues: in case if I modify foo.txt I want to see diff.
The closest tool which I found in Java is WatchService but if I understood right this tool can only detect type of event happened on filesystem (create file or delete or modify).
Java Diff Utils is designed for that purpose.
final List<String> originalFileContents = new ArrayList<String>();
final String filePath = "C:/Users/BackSlash/Desktop/asd.txt";
FileListener fileListener = new FileListener() {
#Override
public void fileDeleted(FileChangeEvent paramFileChangeEvent)
throws Exception {
// use this to handle file deletion event
}
#Override
public void fileCreated(FileChangeEvent paramFileChangeEvent)
throws Exception {
// use this to handle file creation event
}
#Override
public void fileChanged(FileChangeEvent paramFileChangeEvent)
throws Exception {
System.out.println("File Changed");
//get new contents
List<String> newFileContents = new ArrayList<String> ();
getFileContents(filePath, newFileContents);
//get the diff between the two files
Patch patch = DiffUtils.diff(originalFileContents, newFileContents);
//get single changes in a list
List<Delta> deltas = patch.getDeltas();
//print the changes
for (Delta delta : deltas) {
System.out.println(delta);
}
}
};
DefaultFileMonitor monitor = new DefaultFileMonitor(fileListener);
try {
FileObject fileObject = VFS.getManager().resolveFile(filePath);
getFileContents(filePath, originalFileContents);
monitor.addFile(fileObject);
monitor.start();
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (FileNotFoundException e) {
//handle
e.printStackTrace();
} catch (IOException e) {
//handle
e.printStackTrace();
}
Where getFileContents is :
void getFileContents(String path, List<String> contents) throws FileNotFoundException, IOException {
contents.clear();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
contents.add(line);
}
}
What I did:
I loaded the original file contents in a List<String>.
I used Apache Commons VFS to listen for file changes, using FileMonitor. You may ask, why? Because WatchService is only available starting from Java 7, while FileMonitor works with at least Java 5 (personal preference, if you prefer WatchService you can use it). note: Apache Commons VFS depends on Apache Commons Logging, you'll have to add both to your build path in order to make it work.
I created a FileListener, then I implemented the fileChanged method.
That method load new contents form the file, and uses Patch.diff to retrieve all differences, then prints them
I created a DefaultFileMonitor, which basically listens for changes to a file, and I added my file to it.
I started the monitor.
After the monitor is started, it will begin listening for file changes.

fop render to pdf one page out of many

I have a code which runs apache fop against xml content and xsl markup and gives me the apache Intermediate Format output:
StreamSource contentSource = new StreamSource(xmlContentStream);
StreamSource transformSource = new StreamSource(xslMarkupStream);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Transformer xslfoTransformer = getTransformer(transformSource);
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
IFDocumentHandler targetHandler = foUserAgent.getRendererFactory().createDocumentHandler(
foUserAgent, MimeConstants.MIME_PDF);
FPSIFSerializer fpsSerializer = new FPSIFSerializer();
fpsSerializer.setContext(new IFContext(foUserAgent));
fpsSerializer.mimicDocumentHandler(targetHandler);
foUserAgent.setDocumentHandlerOverride(fpsSerializer);
Fop fop = fopFactory.newFop("application/X-fop-intermediate-format", foUserAgent, outStream);
DefaultHandler defaultHandler = fop.getDefaultHandler();
Result res = new SAXResult(defaultHandler);
xslfoTransformer.transform(contentSource, res);
Then I use that Intermediate Format file to render pdf and png files out of it.
I'm able to set up my own serilaizer here (FPSIFSerializer()).
I have several pages reports, but I don't need to process all of them. Is there any way to skip some pages or extract them from IntermediateFormat so I will be able e.g. to render only 1st page as png and then 2nd to pdf, etc ?
There
http://svn.apache.org/viewvc/xmlgraphics/fop/branches/archive/fop-1_1/examples/embedding/java/embedding/intermediate/ExampleConcat.java?view=markup
is an example of how to concatenate files via IFConcatenator, so I wonder about the best way to split the multipage file?
Thank_you!
The way I've done it is using custom document handler.
/**
* Custom Apache FOP Intermediate Format document handler which allows page skipping.
* Not thread safe.
*/
public class IFPageFilter extends IFDocumentHandlerProxy {
private static final Logger LOGGER = LoggerFactory.getLogger(IFPageFilter.class);
private int currentPage;
private final int desiredPage;
/**
* #param delegate The real document handler
* #param desiredPage the page you want to render (1-based). Other pages will be skipped.
*/
public IFPageFilter(final IFDocumentHandler delegate, final int desiredPage) {
super(delegate);
this.desiredPage = desiredPage;
}
#Override
public void startPage(final int index, final String name, final String pageMasterName, final Dimension size) throws IFException {
currentPage = index + 1;
if (currentPage == desiredPage) {
super.startPage(index, name, pageMasterName, size);
} else {
// do nothing
LOGGER.debug("Page skipped");
}
}
#Override
public IFPainter startPageContent() throws IFException {
if (currentPage == desiredPage) {
return super.startPageContent();
} else {
return EmptyPainter.getInstance();
}
}
#Override
public void endPageContent() throws IFException {
if (currentPage == desiredPage) {
super.endPageContent();
}
}
}
Then you can attach your handler like that:
final IFDocumentHandler targetHandler = FOP_FACTORY.getRendererFactory().createDocumentHandler(userAgent, mime);
final IFPageFilter documentHandler = new IFPageFilter(targetHandler, page);
final ByteArrayOutputStream mimeOut = new ByteArrayOutputStream(XSL_STREAM_BUFFER_SIZE);
IFUtil.setupFonts(documentHandler);
// Tell the target handler where to write the PDF to
targetHandler.setResult(new StreamResult(mimeOut));
try (final InputStream is = ifStream.toInputStream()) {
final Source src = new StreamSource(is);
new IFParser().parse(src, documentHandler, userAgent);
}
return mimeOut;
and you will get the only page you need in the output stream.
Class EmptyPainter is a dirty hack. It is empty implementation of apache IFPainter, it used here to skip page content and avoid NPE. I'm not happy about it, but that is the only way I was able to make it work.
Please note that I use FOP 1.1, and if you faced with such problems it worth to look at trunk - some of them already solved there. I guess dirty hack with EmptyPainter will not be necessary in trunk.
Please give tips if something could be done better here.
Thanks

DownloadLink with an option to choose the directory where the file should be saved apache wicket

this is my download link I did from the wicket example site. I would like to alter it by letting the user choose the directory the file should be saved. Any way to implement it? Thanks in advance
add(new DownloadLink("generate_report", new AbstractReadOnlyModel<File>()
{
private static final long serialVersionUID = 1L;
#Override
public File getObject()
{
File tempFile;
try
{
tempFile = File.createTempFile("wicket-examples-download-link--", ".tmp");
InputStream data = new ByteArrayInputStream("some data".getBytes());
Files.writeTo(tempFile, data);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
return tempFile;
}
}).setCacheDuration(Duration.NONE).setDeleteAfterDownload(true));
If you have preset paths then the user can choose which one they want to use by using the include_path function; however, the user will not be able to create their own directory/path.
EDIT: Couldn't put code in that comment lol but here is where I was going at.
$path1=set_include_path('file\whateveryouwant');
Then you can use multiple paths and let the user choose from the paths by calling the $path1 (wich would be the extension for the path you want them to use).
More info: http://php.net/manual/en/function.get-include-path.php

Tapestry dynamic generated image

My Tapestry5 application generate dynamically images with jFreeChart every day.
My problem is that i don't know how to show.
I have tried to save them into the webapp folder, but it seems impossible, no file is created inside.
I have tried a solution with StreamResponse without result.
Another one is about IEngineService but it seems to be only available for T4.
So, i would appreciate some help.
Thanks.
Ok i find where was the problem, here the solution, for the other class, please see Tapestry5: How To Stream An Existing Binary File.
public StreamResponse onImage() {
StreamResponse result = null;
if (graphic != null && graphic.getImage() != null) {
try {
InputStream input = new FileInputStream(graphic.getImage());
result = new PngInline(input, "test");
} catch (FileNotFoundException e) {
logger.error("Loading graphic image", e);
}
}
return result;
}
#Inject
private ComponentResources resources;
public Link getLink() {
return resources.createEventLink("image", new Object[]{});
}

Categories

Resources