Donnerstag, 17. September 2009

Favicon with Wicket and HeaderContributer

Based on Michael's example, I did a complete example for a Wicket header contributer generating a favicon entry:

public class FavIconHeaderContributor extends AbstractHeaderContributor {

private static final long serialVersionUID = 1L;

private final FavIconContributor contributor;

private static final class FavIconContributor implements IHeaderContributor {

private static final long serialVersionUID = 1L;

ResourceReference _resourceReference;

public FavIconContributor(ResourceReference resourceReference) {
_resourceReference = resourceReference;
}

public void renderHead(IHeaderResponse response) {
CharSequence url = RequestCycle.get().urlFor(_resourceReference);
response.renderString(getFavIconReference(url));
}

private CharSequence getFavIconReference(CharSequence url) {
StringBuilder sb = new StringBuilder();
sb.append("<link rel=\"shortcut icon\" href=\"");
sb.append(url);
sb.append("\" type=\"image/x-icon\">\n");
return sb.toString();
}

}

public FavIconHeaderContributor(ResourceReference resourceReference) {
contributor = new FavIconContributor(resourceReference);
}

@Override
public final IHeaderContributor[] getHeaderContributors() {
return new IHeaderContributor[] { contributor };
}

@Override
public String toString() {
return contributor._resourceReference.getName();
}

}

Mittwoch, 6. Mai 2009

Some PostgreSQL stuff

Just some bits that might be useful when working with PostgreSQL databases. Everything is working with version 8.x, I can't say anything about the compatibility with older versions.

Display the sizes all of your databases running on a particular server:
SELECT pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size FROM pg_database;

Show running PostgreSQL thread on Linux:
ps auxww | grep ^postgres

When upgrading to a new version (usually major or secondary), you have to do a full dump and restore. This one works well for me for some time now:
    * pg_dumpall -o -U postgres > [dump file]
* stop postgresql server
* you may want to save your configuration files (e.g. postgresql.conf, pg_hba.conf)
* now you may safely remove your old postgresql installation
* rm -rf /var/lib/postgresql/data
* install new postgresql
* merge changes from postgresql.conf and pg_hba.conf
* start new postgresql server
* psql -f [dumpfile] -U postgres

Montag, 27. April 2009

script based phpBB user add

For a customer I had to implement a script based user management for phpBB (add/delete) called from within a Java web app using existing login data. After evaluating and merging a lot of non-working suggestions/examples I managed to build a working script (tested with phpBB 3.0.4). Maybe it saves you some time.


<?php
if (isset($_REQUEST['forum_username']) && isset($_REQUEST['forum_password']) && isset($_REQUEST['forum_email'])) {

define('IN_PHPBB', true);

$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../forum/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/ucp/ucp_register.php');
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);

$username = $_REQUEST['forum_username'];
$username_clean = strtolower($username_clean);
$password = $_REQUEST['forum_password'];
$email = $_REQUEST['forum_email'];

$user_row = array(
'username' => $username,
'username_clean' => $username_clean,
'user_password' => phpbb_hash($password),
'user_pass_convert' => 0,
'user_email' => $email,
'user_email_hash' => crc32(strtolower($email)) . strlen($email),
'group_id' => 7,
'user_type' => 0,
);

user_add($user_row);
}
?>

Montag, 13. April 2009

Wicket base webapp

After having some trouble finding a slim and small base layout in the web, serving as template for any Wicket application I might develop (without using Jetty or providing any "quickstart" framework), I'd like to post a solution using filters instead of the Wicket servlet (recommended as of Wicket 1.4+):

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<display-name>Wicket Base</display-name>

<context-param>
<param-name>configuration</param-name>
<param-value>deployment</param-value>
</context-param>

<filter>
<filter-name>wicketbase</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.wicketbase.WebApplicationBase</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>wicketbase</filter-name>
<url-pattern>/wbase/*</url-pattern>
</filter-mapping>

</web-app>


WebApplicationBase.java:

public class WebApplicationBase extends WebApplication {

@Override
protected void init() {
this.getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
this.getRequestCycleSettings().setResponseRequestEncoding("UTF-8");
}

@Override
public Class getHomePage() {
return WebPageBase.class;
}

public static WebadminApplication get() {
return (WebadminApplication) Application.get();
}

@Override
public Session newSession(Request request, Response response) {
return new WebSessionBase(request);
}

}


WebSessionBase.java:

public class WebSessionBase extends WebSession {

public static WebSessionBase get() {
return (WebSessionBase) Session.get();
}

protected WebSessionBase(Request request) {
super(request);
setLocale(new Locale("de", "DE_EURO"));
}


}


WebPageBase.java:

public class WebPageBase extends WebPage {

public WebPageBase() {
add(HeaderContributor.forCss(WebadminPage.class, "res/style.css"));
}

public WebSessionBase getWebSessionBase() {
return (WebSessionBase)getSession();
}
}


WebPageBase.html (just for completing this example):

<?xml version="1.0" ?>
<!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:wicket="http://wicket.apache.org/" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
...
</body>
</html>


Ant build script:

<?xml version="1.0" encoding="UTF-8"?>
<project default="war" name="wicketbase" basedir=".">
<property name="final.name" value="wicketbase" />
<property name="src.main.dir" value="src/com/wicketbase" />
<property name="src.web.dir" value="webapp" />
<property name="lib.dir" value="lib" />
<property name="build.dir" value="target" />
<property name="build.main.classes" value="bin" />
<property name="build.reports.dir" value="${build.dir}/reports" />
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" failonerror="false" />
<delete file="${final.name}.war" failonerror="false" />
</target>
<target name="init">
<mkdir dir="${build.dir}" />
</target>
<target name="compile" depends="init">
<mkdir dir="${build.main.classes}" />
<javac destdir="${build.main.classes}" target="1.5" source="1.5"
srcdir="${src.main.dir}" classpathref="build.classpath" />
<copy todir="${build.main.classes}">
<fileset dir="${src.main.dir}">
<include name="**/*.*" />
<exclude name="**/*.java" />
</fileset>
</copy>
</target>
<target name="war" depends="compile">
<war destfile="${build.dir}/${final.name}.war" webxml="${src.web.dir}/WEB-INF/web.xml">
<lib dir="lib">
<include name="*.jar" />
</lib>
<classes dir="${build.main.classes}" />
<fileset dir="${src.web.dir}">
<include name="**/*" />
<exclude name="**/web.xml" />
</fileset>
</war>
</target>
</project>


The folder layout of the webapp is as you would probably expect it. Wicket jar's go to "WEB-INF/lib", your classes and the HTML go to "WEB-INF/classes/[package]".

Labels:

Mittwoch, 4. März 2009

Regular expression for email validation

After unsuccessfully searching the web for a working common POSIX conform regular expression for validating email addresses, I took the time and built one. It should cover almost almost any possible format: