PFSense wkhtmltopdf WebApp NAT Loopback/Reflection Woes

Please DO NOT use public IP address when generating your pdf using wkhtmltopdf/wkhtmltojpg if you have PFSense running as a firewall.

Use localhost, this will save you headaches.

I’m using WKHTMLTOPDF to convert html to pdf and inside the html contains links to images. Now the address used is the public IP address but wkhtmltopdf is running internally in the local network with its ‘web browser’. See the problem here? Normally, routers allow NAT Reflection/Loopbank… PFSense blocks this by default. So my images were not rendering, for which leaves the wkhtmltopdf process to lag behind due to it waiting for a reply from the server which PFSense is denying, timeout it around 60 sec ( 1 Min ).

Heap Dump and Remote Tomcat 8-OpenJDK 8 Memory Monitoring

Memory problem.. the thorn of web apps… I have to connect to my remote server to monitor its memory usage and produce a heap dump.
Bellow is the steps I had to configure the server to allow me to connect a remote monitor tool.

Requirement: VisualVM app, download here

  1. Access your catalina.sh file.
  2. Look for JAVA_OPTS, if none found, you can add your own.
  3. Add …
    1
    
    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/heap/dump/folder/

    to the line if found, if not, you can do

    1
    
    JAVA_OPTS="JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/heap/dump/folder/"

    The above code will force a heamp dump when the server encounter a out-of-mem exception.

  4. Next, to enable remote memory monitoring, add these commands after the line above, within the quotation mark, add proper spacing after each command…
    1
    2
    3
    4
    
    -Dcom.sun.management.jmxremote.port=<port_num>
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false
    -Djava.rmi.server.hostname=<host_ip_add>
  5. Now, run VisualVM and create a new JMX remote connection, under the CONNECTION field, add host_ip_address:port_num
    ex. 111.111.1.1:5555

Congrats! You should now be able to connect to your server and monitor your memory usage!

Special Character Encoding in XML

Ran into a problem returning an XML data with special characters encoded into it through an AJAX call. The problem was that special characters (i.e. &ntilde; -> ñ) throws an error when being returned through XML and is in the process of being parsed by the browser.

The catch?
XML does not support named entities by default, you have to declare it before hand. Only &lt; &gt; &amp; &quot; and &apos; are predefined.

Something like:

1
2
3
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#160;"> ]>
<xsl:stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">

Got this from here…

The fix…
Instead of using named entities in the XML (I think this also affects JSON, I could be wrong since I didn’t test this is JSON) like &ntilde;, use character references to support special character encoding.

Example:
white space = &nbsp; = &#160;
ñ = &ntilde; = &#241;

they are all the same… #160 is referenced/mapped to ” ” and #241 is referenced/mapped to ñ

So when forming your XML before returning it through an AJAX call, form the special characters via character reference.

Hope this helps you out there having this problem.

Java IO Problem in Tomcat

Encountered this problem today while testing an image upload AJAX script in tomcat. Later on I found out that the folder is actually protected Win7 and tomcat couldn’t write the cache or temporary file into the folder.

So anyone having this problem, check the following…

  • What OS are you running? Vista/Win7?
  • What IDE are you using?
  • Was your IDE elevated to admin status?

So, check your OS, I’m using Win7. I was developing this under WinXP a few months back, and now under Win7, the folder is protected since it was under the “Program Files (x86)” folder group. (Argh!)
I closed my IDE, using IDEA IntelliJ 9.0, ran the IDE under “Administrator Level” and the problem disappeared!

So check those out first.

Java JNI: Unsatisfied Link Error in Library Path

I’m currently playing around with Java Native Interfaces(JNI) and I must say its been too long since I programmed in Java. Its good to be back! Anyway, lets hop to the main topic…

I was following the tutorial from Sun in creating my first JNI and calling my compiled C++ library, I was stuck in the last part where this error popped out whenever I tried to invoke

java HelloWorld

the error was…

java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
          at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
          at java.lang.Runtime.loadLibrary0(Runtime.java:823)
          at java.lang.System.loadLibrary(System.java:1030)
          at HelloWorld.<clinit>(HelloWorld.java:<line number>)
Could not find the main class: HelloWorld. Program will exit.

one way of fixing this is to include the current location of your newly compiled shared library (ex. HelloWorld.so). I’m using Ubuntu linux,so to include the current folder where your “.so” file is located, invoke…

LD_LIBRARY_PATH=`pwd`
export LD_LIBRARY_PATH

or you can also do …
Continue Reading

Java 5 JDK and Jakarta Tomcat 5.5 in Ubuntu Server Installation

These are the steps I took to install Jakarta Tomkat 5.5 in conjunction with Java 2 SDK in Ubuntu server.

  1. Login as root, you can also use “su” though I prefer to login as root.
  2. apt-get install sun-java5-jdk
    • This will install Java 2 SDK (1.5), ubuntu will start downloading packages that are not found in your system.
  3. apt-get install tomcat5.5 tomcat5.5-admin tomcat5.5-webapps
    • Of course jakarta-tomcat 5.5, I’m using 5.5 since this supports java 1.5
  4. Now here’s the tricky part, location for Java and jakarta-tomcat
    • Java’s location is in /usr/lib/jvm/java-1.5.0-sun
    • Jakarta-Tomcat’s location is in /usr/share/tomcat5.5
      • We will need these location for the export configurations to be written in the .bashrc
  5. Now, open .bashrc in your favorite text editor, I used vim
  6. At the end of the file add these:

    export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
    export CLASSPATH="/usr/share/tomcat5.5/common/lib/jsp-api.jar;/usr/share/tomcat5.5/common/lib/servlet-api.jar"
    export CATALINA_HOME=/usr/share/tomcat5.5
  7. Now save the file and exit, then log back in. This is required for the export script to be set.
  8. Before starting the server, the default port the installer set is 8180 instead of the 8080 port that Tomcat uses. To change this, go to /usr/share/tomcat5.5/conf/ and open the file server.xml. Locate the line:

    ...Connector port="8180" maxHttpHeaderSize="8192"...

    and change 8180 to 8080. Save and exit. Now your ready to test the server.
  9. Now try the server if it starts, /usr/share/tomcat5.5/bin/startup.sh You should see something like this:

    Using CATALINA_BASE: /usr/share/tomcat5.5
    Using CATALINA_HOME: /usr/share/tomcat5.5
    Using CATALINA_TMPDIR: /usr/share/tomcat5.5/temp
    Using JRE_HOME: /usr/lib/jvm/java-1.5.0-sun
  10. Shutting down the server is just /usr/share/tomcat5.5/bin/shutdown.sh
  11. Now, during installation of tomcat, Ubuntu automatically adds a startup and shutdown script for ubuntu to use as it starts up or shutsdown in /etc/init.d/ folder. Filename is tomcat5.5. As the server finishes starting up, you will notice tomcat isn’t running at all. One possible problem is that the script didn’t use the correct path for the java 2 sdk. You can check the boot log in /var/log/. Check if during boot up, tomcat5.5 encountered problems. For the problem stated above, you can fix this by going to /etc/default/ and edit the tomcat5.5 file. Locate the line:

    #JAVA_HOME=/usr/lib/j2sdk1.4-sun

    remove the ‘#’ and change it to…

    JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun

    or wherever your java directory is.After that, restart the server and see if tomcat was successfully started.

    /etc/init.d/tomcat5.5 status

Edit: Problem encountered:

  • Tomcat does not load in Ubuntu 8.04
    Found a little problem loading Tomcat 5.5 under Ubuntu 8.04, it either fails or white screen just appears. I don’t know what happened from 6.04 to 8.04 but once I compared the init.d/tomcat5.5 startup script from 6.04 and 8.04, there WERE changes to the scripts. I replaced the new tomcat5.5 script in 8.04 with the old one from 6.04 and the server started properly after a server restart. I don’t know why but this may have something to do with permissions and I have yet to trace the script.

  • java.lang.ClassNotFoundException: org.apache.jasper.tagplugins.jstl.If
    • The problem here is that the above name is incomplete… what it should have been is org.apache.jasper.tagplugins.jstl.core.*
    • Now, to fix the problem, locate /var/lib/tomcat5.5/webapps/jsp-examples/WEB-INF/tagPlugins.xml and open it with your text editor… I used vim. Now edit each line of code that has org.apache.jasper.tagplugins.jstl in it and add “.core” after jstl. What you will see after editing the code will be…
    • org.apache.taglibs.standard.tag.rt.core.IfTag

      org.apache.jasper.tagplugins.jstl.core.If

      org.apache.taglibs.standard.tag.common.core.ChooseTag

      org.apache.jasper.tagplugins.jstl.core.Choose

      org.apache.taglibs.standard.tag.rt.core.WhenTag

      org.apache.jasper.tagplugins.jstl.core.When

      org.apache.taglibs.standard.tag.common.core.OtherwiseTag

      org.apache.jasper.tagplugins.jstl.core.Otherwise

      org.apache.taglibs.standard.tag.rt.core.ForEachTag

      org.apache.jasper.tagplugins.jstl.core.ForEach

  • SEVERE: The scratchDir you specified: [folder_path] is unusable.
    • Sidenote: took me 2 days to figure out why this was happening (X_x)
    • Based from the Catalina logs, the folders under webapps was not accessible by tomcat5.
    • Now looking under all processes running ( ps command ), most bootup services were running under root (mysql, apache, etc ) but tomcat5 was not.
    • If I start tomcat5 when I logged into the console as root, the problem disappears. This lead me to believe this might have been the Tomcat5 user was not given the correct privileges.
    • Work-around 1: Elevate Tomcat5 user to root access level.
    • Work-around 2: Go to /etc/init.d/ and open Tomcat5.5 under any text editor ( I used vim ). Locate the line TOMCAT5_USER=tomcat55 and change it to TOMCAT5_USER=root . Restart your server or you can also execute the restart command for Tomcat5.5 in init.d folder

sources: