Doc4j Struts2 Action OutOfMemoryError - struts2

I am having an Action class in struts2 application and i want to display a docx file to the user.
When i load the file with InputStream the file opens without problem
fileStream = new DataInputStream(
new FileInputStream("d:/deleme/sample.docx"));
The file of docx is about 26Kb and 7 pages.
but when i add the following statement in Action
WordprocessingMLPackage wordMLPackage =
WordprocessingMLPackage.load(new FileInputStream(new java.io.File("d:/deleme/sample.docx")));
with no other modification to the previews code tomcat crashes with
Caused by: java.lang.OutOfMemoryError: PermGen space
at java.lang.ClassLoader.defineClass1(Native Method)
How can i work docx with an action (google did not helped at all)

Related

Youtube API inStream parameter is null

So I am trying to run sample code from the YouTube API. I am trying to run the Search sample code for Java. However, on line 70 and 71 where it says
InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
properties.load(in);
I get an exceptions saying
Exception in thread "main" java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at java.base/java.util.Properties.load(Properties.java:403)
at com.google.api.services.samples.youtube.cmdline.data.Search.main(Search.java:71)
I have not modified the code and don't understand why this exception is occurring. It is acting as if the file does not exist when it is in the place that it was when I downloaded the .zip file.
Not certain as to why this was the case but there was an src folder, which contained a main folder that held a resources folder, which apparently was what .getResourcesStream was reading from rather than the other resources folder. So I updated this new folder and the code worked.
I solved this problem changing the original code provided by the API, and passing a single key inside the properties. Follow the code:
private static final String API_KEY = "XXX";
Properties properties = new Properties();
properties.setProperty(API_KEY, API_KEY);
// System.out.println(properties.getProperty(API_KEY));

Error while uploading .xlsx file using ExcelDataReader

I am working on asp.net MVC project.
I use ExcelDataReader component to read excel file records.
Now when I published my project to server and upload a .xlsx file with uploader I get below mentioned exception message. There are no errors with local deployment but server.
Access to the path '\Microsoft Corporation\Internet Information
Services\7.5.7600.16385' is denied.
and code where I am getting error is:
if (personsFile.FileExtension == ".xls")
{
Stream st = new MemoryStream(personsFile.FileArray);
reader = ExcelReaderFactory.CreateBinaryReader(st);
}
else if (personsFile.FileExtension == ".xlsx")
{
Stream st = new MemoryStream(personsFile.FileArray);
//exception occured on under line
reader = ExcelReaderFactory.CreateOpenXmlReader(st);
}
But when I upload a .xls file, I dont have any error.
How to resolve issue with .xlsx extenstion?
This is most likely due to ExcelDataReader 2.x extracting the xlsx archive to %TEMP% before processing it. The current pre-alpha of 3.0 no longer does this. See https://github.com/ExcelDataReader/ExcelDataReader/releases.

Unicode Issue with Chrome

Here goes the path I follow.
1. User enters text in html editor, tiny mce edtitor.
2. that text is uploaded to Server side MVC controller
3. Controller use following line to write that HTML content (string) to a temp location on server
new MemoryStream(Encoding.Unicode.GetBytes(HtmlContent))
4. That file in temp location (in another step) is then used to get a byte[] of the contents
5. In the last step byte[] is send via stored procedure to database to a column field of type varbinary.
Now the problem is that when I read that varbinary back to byte[] and send it to browser using the following line of MVC with MIME type text/HTML.
File(attachmentFile.BinaryData, mimeType, pFileName);
The file downloads and opens correctly in both IE and Firefox but not in chrome. I don't know what the issue is with chrome

Reading properties file in JSF2.0 which can work in war also

To read a properties file in JSF2.0 with Glassfishv3 webserver, which is located at root directory of my web application, I am using below code-
ServletContext ctx = (ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext();
String deploymentDirectoryPath = ctx.getRealPath("/");
Properties prop = new Properties();
prop.load(new FileInputStream(deploymentDirectoryPath
+ File.separator + "portal-config.properties"));
Below is the screenshot of web portal-
While running the portal I am getting FileNotFound Error, since the file is not present in glassfish domain.
Is there any way to read properties file which can work in both the situations, at development stage and in war file also?
You should never use java.io.File to refer web resources. It knows nothing about the context it is sitting in. You should also never use ServletContext#getRealPath() as it may return null when the server is configured to expand WAR file in memory instead of on disk, which is beyond your control in 3rd party hosts.
Just use ExternalContext#getResourceAsStream() to get the web resource directly in flavor of an InputStream. It takes a path relative to the webcontent root.
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
properties.load(ec.getResourceAsStream("/portal-config.properties"));
See also:
getResourceAsStream() vs FileInputStream
What does servletcontext.getRealPath("/") mean and when should I use it
Where to place and how to read configuration resource files in servlet based application?
Update it does not seem to be a web resource at all. You should move the file into the "WebContent" folder as shown in the screenshot. Or, better, the /WEB-INF folder so that nobody can access it by URL.
properties.load(ec.getResourceAsStream("/WEB-INF/portal-config.properties"));
An alternative would be to put it in the classpath, the "Java source" folder as shown in the screenshot. You don't need to put it in a package, that's optional. Assuming that you didn't put it in a package, then do so:
ClassLoader cl = Thread.currentThread().getContextClassLoader();
properties.load(cl.getResourceAsStream("portal-config.properties"));
(note that the path may not start with a slash!)

Jasper: error opening input stream from url

I'm designing a jasper report using iReport which takes a parameter and fetches an image from a given URL:
The parameter is a user's screen name in twitter, and the url is it's profile image.
<image>
<reportElement x="4" y="51" width="73" height="64"/>
<imageExpression><![CDATA["https://api.twitter.com/1/users/profile_image?screen_name="+$F{user_screen_name}+"&size=bigger"]]></imageExpression>
</image>
It works great when the image exists. If it doesn't the following exception is thrown:
Error filling print... net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
Setting up the file resolver... net.sf.jasperreports.engine.JRRuntimeException:
net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
at net.sf.jasperreports.repo.DefaultRepositoryService.getInputStream(DefaultRepositoryService.java:138)
at net.sf.jasperreports.repo.RepositoryUtil.findInputStream(RepositoryUtil.java:186)
at net.sf.jasperreports.repo.RepositoryUtil.getBytes(RepositoryUtil.java:202)
at net.sf.jasperreports.engine.JRImageRenderer.getInstance(JRImageRenderer.java:141)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:498)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:441)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:468)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2037)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:761)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:291)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:133)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:903)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:813)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:878)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997) Caused by:
net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
at net.sf.jasperreports.engine.util.JRLoader.getInputStream(JRLoader.java:314)
at net.sf.jasperreports.repo.DefaultRepositoryService.getInputStream(DefaultRepositoryService.java:121)
... 19 more Caused by: java.io.FileNotFoundException:
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1029)
at net.sf.jasperreports.engine.util.JRLoader.getInputStream(JRLoader.java:310)... 20 more
Print not filled. Try to use an EmptyDataSource...
How can i handle this situation in the jrxml file?
I would like to simply add a fixed URL address in case it can't find one.
Anyone has any suggestions?
Thank you!
I think you need to add an additional helper class to handle this. You need a static method boolean urlExists(String url) that would allow you to put this in the imageExpression:
MyClass.urlExists($F{image_url}) ? $F{image_url} : $P{fallback_image}
It would be a simple class to write... but clearly there's additional complexity in adding in another .jar file. Without that method, I can't see any way to do the processing in the .jrxml.
in jrxml file,
to show image from url resource, use image expression URL()
like below, for example $F{ADPN_NO} is parameter from report
<imageExpression><![CDATA[new URL("http://anyserver:7001/images/"+$F{ADPN_NO}+".jpg")]]></imageExpression>
when onError occurred, it will show blank item, icon, or error that is set on jrxml.
the other way, outside of jrxml file, you can use java map object which contains img urls that is check by URL() or File() Exception ..
In my case I had a report to which I "injected" the image's url and it was causing an error with the certificate (the url I was "injecting" had the IP and it had to be a name for the certificate). After I changed the url it worked.

Resources