Archive for December, 2010

f:ajax not working in JSF

Problem is that in JavaServer Faces application ajax request are just not working. o:ajax in openfaces nor f:ajax in plain JSF 2.0 is not sending any requests to server. For testing can be used simple echo application.

<h:form>
<h:inputText value="#{ajaxTestBean.testValue}" >
  <f:ajax event="keyup" render="text"/>
</h:inputText>
<h:outputText id="text" value="#{ajaxTestBean.testValue}" />
</h:form>

Normally this should work without problems but there is little catch. <h:head></h:head> tag must be included in order for ajax to work properly. Definind h:head includes some more needed javascripts to complete everything. Try this code below to get it working:
index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:head>
  <title>AjaxTest</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{ajaxTestBean.testValue}" >
  <f:ajax event="keyup" render="text"/>
</h:inputText>
<h:outputText id="text" value="#{ajaxTestBean.testValue}" />
</h:form>
</h:body>
</f:view>
</html>

AjaxTestBean.java

package com.ajaxtest; 

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped; 

@ManagedBean(name = "ajaxTestBean")
@SessionScoped
public class AjaxTestBean {
private String testValue = "default";

public String getTestValue() {
  return testValue;
} 

public void setTestValue(String testValue) {
   this.testValue = testValue;
 }
}
Share on TumblrSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit

Proxy java web requests for tomcat6 through apache 2.2

Tomcat is missing mod_security and a few other useful features. That is not a problem because httpd eg apache2 has all the missing features and these 2 are designed to work together. What creates problems is getting JK connector to work to display Java pages via apache.

For testing was set up default apache2 and tomcat6 on ubuntu server. Goal was to see tomcat example pages from port 80 when tomcat itself was running on 8080 port.

mod_jk needs configuration of workers.properties, apache conf and tomcat conf. Logs related to errors to why requests are not reaching tomcat are hard to find. One clear error from mod_jk logs was.

[Fri Dec 24 13:51:34.147 2010] [1707:2633885456] [debug] jk_translate::mod_jk.c (3419): missing uri map for localhost:/examples/

Actually biggest problem was that mod_jk is obsolete. It can be made to work but these days mpd_proxy_ajp is used instead. Setting it up just works and can be done in a few minutes.

Confgure tomcat, edit edit /var/lib/tomcat6/conf/server.xml and uncomment this line

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Configure apache enable mod_proxy_ajp

sudo a2enmod proxy_ajp

edit /etc/apache2/sites-available/default and add following to virtual host definition

ProxyPass /examples/ ajp://127.0.0.1:8009/examples/
ProxyPassReverse /examples/ ajp://127.0.0.1:8009/examples/

Restart both servers

sudo service apache2 restart
sudo service tomcat6 restart

Now both links should display same content

http://localhost/examples/
http://localhost:8080/examples/

If you happen to see Forbidden errorpage then proxy-ing is denied. Edit

/etc/apache2/mods-available/proxy.conf

and change Deny to Allow

        <Proxy *>
                AddDefaultCharset off
                Order deny,allow
                Allow from all
        </Proxy>

After getting proxy between apache and tomcat running its possible to start configuring security of tomcat in more finegrained level and performance regarding static pages etc is also improved.

Share on TumblrSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit

Simple way to route all traffic via gateway with OpenVPN

You need VPN when you are connected to unsecured WIFI. Also VPN is needed when this public wifi or your ISP is restricting you. One example of such restrictions is blocking P2P programs and alike.

Good way to overcome those problems is OpenVPN. This can be quite complicated to set up but simple configurations is actually simple.

Firstly is needed server. Server can be your home router or some small server in datacentre that has extra bandwith left over. Your laptop will be called client which sends all(or some) of your traffic through one TCP/IP connection to server and server forwards it so it looks like traffic is originating from server.

Lets have our internal ips 10.66.77.1 for server and 10.66.77.2 for client. Network is selected in the middle of 10.0.0.0/24 network because then it has smaller chance of colliding with your existing network.

Server needs ip forwarding and nat to be enabled. You achieve this with following commands. 10.66.77.0/24 and eth0 needs to be changed to your actual values.

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 10.66.77.0/24 -o eth0 -j MASQUERADE

Next we need static key. Bear in mind that this need to be kept secret. Key generating looks like:

openvpn --genkey --secret static.key
chmod 600 static.key

Now preparation is ready and you can make OpenVPN configuration file. Read the rest of this entry »

Share on TumblrSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit