RE: Doubts with serveResource

Daniel G, modified 6 Years ago. Regular Member Posts: 141 Join Date: 3/14/17 Recent Posts

Hi all.

 

I am using Liferay 7 and I'm facing some issues with ServeResource method.

 

I have a portlet which has a button that calls serveResource method.

The idea is that if one condition is fulfilled, it return a pdf to download, and if that condition is not fulfilled, it shows a message in portal.

 

It works fine when pdf is returned but I can't make it work fine with the other option. If condition is not fulfilled, method takes us to a blank page. I understand why is taking us to a blank page but I don't know how to fix it neither how to show messages.

 

I have this in jsp:

<p>&lt;liferay-portlet:resourceURL var="resourceUrl" /&gt;<br> &lt;a href="${resourceUrl}" &gt;Get Report&lt;/a&gt;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</p>

In java serveResource method:

 

I have this when the condition is fulfilled:

baos = ReportingOffline.getByteArrayOutputStream();<br> res.setContentType("application/octet-stream");<br> res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=1, must-revalidate");<br> res.addProperty(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + Constants.TITLE_PDF + ".pdf\"");<br> res.setContentLength(baos.size());<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> OutputStream out = res.getPortletOutputStream();<br> baos.writeTo(out);<br> out.flush();<br> out.close();

 

Is there any way of make the second part? My idea is that a message is showed with an info, and page remains in the same page that is when the button is showed.

 

Thanks in advance.

 

Kind regards.

 

 

 

thumbnail
David H Nebinger, modified 6 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts

Your premise is already wrong.

A serve resource command is not "optional".

Instead of trying to make it optional, you script the front end to include the button (when the condition is satisfied) or display a message when it is not.

Daniel G, modified 6 Years ago. Regular Member Posts: 141 Join Date: 3/14/17 Recent Posts

First of all thanks for the help.

 

I can't do that because button always has to appear. When the button is pressed, a generation of pdf starts. If it is finished in a number of seconds, a pdf is returned. If not, I want that a message is shown and nothing is returned (the pdf will be sent my email in a second thread: it is already working). Is it possible?

 

I try to handle it in an Ajax function in the jsp, with something like this example:

AUI.use('aui-io-request','aui-base',function(A) {

A.io.request("&lt;%=callMyUrl%&gt;", {

method:'GET',

dataType:'json'

 

 

 

but I don't know how to handle the file neither what kind of dataType is.

 

Thanks again.

 

Kind regards.

 

Daniel G, modified 6 Years ago. Regular Member Posts: 141 Join Date: 3/14/17 Recent Posts

Hi again.

 

I definitely change my mind and I'm trying to do this in other way.

 

If the condition is fulfilled, I will create a file in C:/Temp and return a json with the url in serveResourceMethod. Then I will handle it in jsp with a ajax function.

My problem now is that I am not able to create the file, what am I doing wrong?

 

 

File file = new File("C:\\Temp\\prueba.txt");<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (!file.exists()) {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;try{<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;file.createNewFile();<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;catch(Exception e){<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LOGGER.error(e.getMessage());<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;byte[] contentInBytes = baos.toByteArray();<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;FileOutputStream fop = new FileOutputStream(file);<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fop.write(contentInBytes);<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fop.flush();<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fop.close();

 

Baos is a ByteArrayOutputStream which is correct.

 

Thanks in advance.

 

Kind regards.

thumbnail
David H Nebinger, modified 6 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts

If you're going to write to a file, ditch the BAOS and just write to the file.

Also a hard-coded filename is a bad idea.  What if we both hit the button at once?

Still seems to be a wrong path to me...

I mean, the report would either be one that normally completes in time or it doesn't.

If it will normally complete in time, then the edge case is that it doesn't and the user has to wait a little longer.  So just let them wait.

If it will not normally complete in a reasonable time, then just email it every time.  The edge case, when it doesn't take so long, just means they get the email sooner rather than later.

Seriously, if you make things so complicated it will always be hard to get them right.

Daniel G, modified 6 Years ago. Regular Member Posts: 141 Join Date: 3/14/17 Recent Posts

Thanks again.

 

The hardcoded filename is only for testing that it works, because file is not been created with that code.