Nov
5
2009

Bad Request Error with apache mod_rewrite

If you use apache module mod_alias together with module mod_rewrite, you probably encounter “Bad Request” error if the settings are not correctly configured.


For example, if you want to make the folder “/opt/mysite_v1.2″ have the alias as “/mysite” so it can be accessed as http://www.mydomain.com/mysite. And you want to have clean url “/mysite/page1″ mapped to “/mysite/index.php?p=page1″.

To define the alias, just add the following in the httpd.conf file:

Alias /mysite/ “/opt/mysite_v1.2/”
<Directory “/opt/mysite_v1.2/”>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>

To allow clean URL. you need to have the following in the “.htaccess” file in the folder “/opt/mysite_v1.2″

RewriteEngine On
# Rewrite base
RewriteBase   /mysite

#  now the rewriting rules
RewriteRule   ^([0-9a-zA-Z_]+)$  index.php?p=$1

Please NOTE that “RewriteBase” Directive is very essential. Without it, you will get the well known “Bad Request” Error.

More information can be found in Apache mod_rewrite documentation.

Oct
14
2009

Use OpenJPA with MySQL: Some Tips


OpenJPA is an implementation of the JPA (Java Persistence API) standards. Initially, I used OpenJPA with Derby which is also known as JavaDB. It works well. However, when i tried to use MySQL as the backend database, I encountered a few issues. Here are a few small tips i would like to share when using OpenJPA with MySQL:

Change the default storage engine

By default, OpenJPA uses innodb as the storage engine. While my application does not need to support transactions and other fancy db features, i’d like to use more plain and “traditional” myisam storage engine. To do so, I just need to set the property “openjpa.jdbc.DBDictionary” with the value “TableType=myisam”. This property object is the one passed to the method “Persistence.createEntityManagerFactory(String, Properties)”.

Allow to store bigger objects (blobs)
By default, OpenJPA maps the java object to mysql data type “blob”. However, as you may be aware of, blob type in mysql only can store up to 65536(216) bytes of data (check http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html), which is quite smaller than the blob in JavaDB or Derby (2G).

Mysql does provide other data types which supports larger storage. mediumblob supports up to 16M while longblob up to 4G. So if I want to store objects other than 64K, I need to change the mapping in OpenJPA. To change the mapping, just set the same property “openjpa.jdbc.DBDictionary”, but with value “blobTypeName=mediumblob”.

If I want both myisam as storage engine and mediumblob as mapped blob type, the property should be set as: “TableType=myisam,blobTypeName=mediumblob”.

Avoid the error “Packet too large”
Now i can save objects up to 16M in size to the database. I run my program, only to find another exception popped up. It says something like “Packet too large”.

This requires the change of mysql server setting as indicated in mysql documentation. The setting “max_allowed_packet“, default to 1M bytes, is too small in my case.

Sep
10
2009

Methods/Software to convert RMVB to AVI

Most current DVD players support playback of Divx or Xvid (usually files with .avi extention) encoded files, but few supports RMVB files which are in real media format.

If you have some RMVB files and want to play them on the DVD player, Then you need to convert the RMVB format to AVI.

Currently, I found out two methods to do this.

The first method is from this article: “How to Convert RMVB“. This method requires you install/download several pieces of software to do this. It’s a bit troublesome, but the article provides very detailed step-by-step instructions and it works.

The other method is to use the open source software alltoavi. It’s quite straight forward to use this software to convert RMVB. And it seems that this method takes less time to do the conversion compared with the first method.

The above two methods both use free software to accomplish the task. There are of course other software that can do this task and they may allow you to have better control for the conversion. Well, you have to pay for it.

Sep
2
2009

Use Ant to Generate ChangeLogs From CVS ang Group by Date

Since version 1.6.1, Ant, the java build tool, has a core task called “cvschangelog“. It is quite useful to use this task to grab a list of recent changes committed to the CVS repository. The output of this task is in XML format. Something like the following:

<changelog>
<entry>
<date>2009-09-02</date>
<time>12:00</time>
<author>robin</author>
<file>
<name>test/ant/task/ChangeLog.txt</name>
<revision>1.2</revision>
<prevrevision>1.1</prevrevision>
</file>
<msg><![CDATA[A commit message]]></msg>
</entry>
<entry>

</entry>

</changelog>

To transform the xml formatted changelog into a human friendly html, you can do an XSL transformation with a XSL file. There is a default one in the Ant distribution, so you can use the following task to get a change log in html format.

<style in=”changelog.xml”
out=”changelog.html”
style=”${ant.home}/etc/changelog.xsl”>
<param name=”title” expression=”Ant ChangeLog”/>
<param name=”module” expression=”the_module”/>
<param name=”cvsweb” expression=”the_url”/>
</style>

The default generated html lists each commit change one by one. In some cases, we want to group the commit changes by dates. To do so, we have to change the default xsl file.

It seems that the new XSLT version 2.0 has specified a group functionality. However, it turns out only a few XSLT tools support this new feature. Fortunately, There is a way that allows us to do the grouping using XSLT version 1.0. By applying this method, we have the following looking xsl code for the HTML <BODY> part.

<body>
<h1>
<a name=”top”><xsl:value-of select=”$title”/></a>
</h1>
<p style=”text-align: right”>Designed for use with <a href=”http://ant.apache.org/”>Apache Ant</a>.</p>
<hr/>

<xsl:key name=”log-by-date” match=”entry” use=”date” />

<xsl:template match=”changelog”>
<xsl:for-each select=”entry[count(. | key('log-by-date', date)[1]) = 1]”>
<xsl:sort select=”date” order=”descending” />
<h2><xsl:value-of select=”date”/><xsl:text></xsl:text></h2>
<xsl:for-each select=”key(’log-by-date’, date)”>

<xsl:sort select=”time” order=”descending” />

<table border=”0″ width=”100%” cellspacing=”1″>
<tr>
<td class=”dateAndAuthor”>
<!– <xsl:value-of select=”date”/><xsl:text> </xsl:text> –>
<xsl:value-of select=”time”/><xsl:text> </xsl:text><xsl:value-of select=”author”/>
</td>
</tr>
<tr>
<td>
<pre><xsl:apply-templates select=”msg”/></pre>
<ul>
<xsl:apply-templates select=”file”/>
</ul>
</td>
</tr>
</table>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

</body>

Use the above code to replace the <body> part in the original changelog.xsl file, then it  will allow you to group the change logs by date.

Jul
23
2009

JSP Expression Language in GWT’s embedded Jetty

JSP expression language (EL) is quite handy. but it only works when you indicate your web application is using version 2.4 or higher. That means you have the following in your <Your-Web-App>/WEB-INF/web.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app id=”WebApp_ID” version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>

Google Web Toolkit (GWT) is a handy Ajax Web application development tool. Since version 1.6.4, GWT uses Jetty instead of Tomcat as the embedded Web application container.

However, when I installed GWT plugin (version 1.6.4) for Eclipse, the default generated web application used the version 2.3. So when i used expression language in the jsp pages, it didn’t evaluate. Something like “${title}” as the output in the hosted mode running on jetty. It should also be the same result even i deployed to a tomcat container, as the web app version is still 2.3. This is understandable, as Expression Language is part of JSP version 2.0 standard, which is bound with Servlet (Web App) version 2.4. So if you want to make the expression language work, make sure your web app is using 2.4 or higher.

However, while using version 2.4 works fine, i’ve got the same problem when i use version 2.5, which is hard to understand why. This problem seems only in Jetty, not in Tomcat. I don’t know if anyone else have encountered the same problem as I did, but the following link seems to explain the reason behind: http://docs.codehaus.org/display/JETTY/JSP+expression+do+not+evaluate

Apart from this problem (which can be worked around), the use of GWT plugin seems more convenient than the traditional web app development in eclipse. :)