Implementing security in Wicket with visural-wicket

Wicket has a nice mechanism for plugging in security whereby all components (including pages) have three cross-cutting hooks which can be validated by a security rule –

  • Instantiation
  • Render
  • Enable

You can build your own security framework using the IAuthorizationStrategy interface and setting an appropriate implementation in your Wicket Application class.

Alternatively, you can leverage a simple security framework that is included in my open source library visural-wicket.

Visural-wicket’s security framework really is just the “glue” between Wicket’s IAuthorizationStrategy and something that you can start using immediately to control component level security.

There are two key interfaces – IClient and IPrivilege. The former represents some client of the application, generally a logged in user. The latter represents some security rule which is applied on rendering or enabling of a component.

They’re really simple interfaces -

/**
 * A client is any user of your system, i.e. in most cases an authenticated User.
 */
public interface IClient < T extends Serializable> {
    /**
     * Return any unique identifier.
     * @return
     */
    T getId();
}

/**
 * A privilege is any condition that can be evaluated with respect to a {@link IClient}.
 */
public interface IPrivilege < T extends IClient> {

    /**
     * Determine whether the privilege applies to the client.
     * @param client
     * @return
     */
    boolean isGrantedToClient(T client);
}

In your application class you provide some glue to define how to retrieve the current IClient (e.g. from the session) –

public class MyApp extends Application {

    public void init() {
        // ...
        getSecuritySettings().setAuthorizationStrategy(new com.visural.wicket.security.AuthorizationStrategy(new IClientProvider() {
            public IClient getCurrentClient() {
                return userService.getCurrentUser(); // for example
            }
        }));
        // ...
    }
}

Then any component which implements ISecureRenderInstance or ISecureEnableInstance can define the privilege required to display or enable it –

public class MyPage extends WebPage implements ISecureRenderInstance {
    // ...

    public IPrivilege getRenderPrivilege() {
       return new IPrivilege < User>() {
           public boolean isGrantedToClient(User client) {
               return client != null && client.isAdmin();
           }
       };
       // instead of returning a anonymous class like this, you could also
       // package up common privileges into a singleton instance,
       // e.g. return Privilege.ADMIN;
    }
}

Wicket and visural-wicket then work in conjunction to ensure that the privileges get evaluated automatically at run-time. Unlike manually coded security which is mixed with other visibility logic, you can guarantee that no component that fails its security check can get rendered to a user that shouldn’t see it. You can also define common security rules as privileges in your code base and refer to them from many pages and components rather than duplicating similar logic across the application.

It’s a simple security mechanism which is designed to fit in with pretty much any app. I hope others find it useful too.

Posted in Java, Software Engineering, Wicket | Tagged guides, open-source, visural-wicket, wicket | Leave a comment

Demystifying HttpServletRequest properties…

I always find myself writing little test cases to figure out which methods to call on HttpServletRequest to get path / parameter information when doing low level Servlet and Filter programming.

The method naming and Javadoc is pretty poor for this part of the Java API and it’s always confusing as to how to interrogate the request to figure out which bits of information you want from the request URL and parameters.

I thought I save myself (and you) the effort of doing this again, once and for all, by writing a quick post which summarises all the interesting “getters” on the HttpServletRequest:

Say I have a request going to a Java web application deployed at context path “WebApplication1″ on my localhost sever, running on port 8080 –

URL = “http://localhost:8080/WebApplication1/test?q=123&r=456″

Method Example Return Value Description
getContextPath /WebApplication1 Returns the deployed context path without the host/port, but including the preceding “/”
getQueryString q=123&r=456 Returns query parameters as provided in the url, everything to the right of the “?”
getRequestURI /WebApplication1/test Returns the request path, without query parameters or host
getRequestURL http://localhost:8080/WebApplication1/test Returns the full request URL, without query parameters
getServletPath /test Returns the relative path within your web application, not including query parameters
getServerName localhost Returns the server hostname as per URL
getServerPort 8080 Returns server port as per URL
Posted in Java, Software Engineering | Tagged Java, tips, web | Leave a comment

Book Review: Racing the Beam – The Atari Video Computer System

I just finished reading Racing the Beam: The Atari Video Computer System (Platform Studies) an interesting read about the Atari 2600 (or Atari VCS as it was orginally known).

You might think that the Atari VCS was one of the early 2D home console systems, which had a number of 2D sprites to move around a two dimensional plane, but you’d be wrong.

The original Atari was more like a “one dimensional” console. That is, its graphics processor had no notion of a two dimensional surface whatsoever, no frame buffer, and not even the concept of two dimensional sprites. The TIA (the Atari VCS’s graphics processing chip) supported -

  • Two one-line 8 pixel wide player sprites, of a single colour
  • A one-line ”ball” up to 8 pixel wide single colour blob of pixels
  • Two one-line ”missiles” up to 8 pixel wide single colour blob of pixels, tied to the colour of the player sprites
  • A one-line 20-bit (20 pixel stretched) single colour “playfield” which can be mirrored or copied across the two halves of the screen

Oh, and the system had 128 bytes of RAM. That’s right BYTES.

The above graphic elements are one dimensional in the sense, for each scanline down the display, the programmer had to dynamically reprogram the objects if they needed to change appearance. If you didn’t do anything between scanlines the same row of pixels would get repeated down the screen.

This is what the book’s title “Racing the Beam” references, since for each scanline the Atari VCS developers were racing the CPU’s clock to setup the next scanline during the horizontal blank of the screen.

The names of the various graphic elements were named after Combat and Pong, the two games the system was actually designed to play. The company figured that if they could create other games for the system, then it was a bonus, but it was designed around Combat and Pong, which were popular arcade games at the time. It’s incredible to think that the VCS had one of the longest production runs in home console history with over 10 years of service!

Atari VCS ROMs were also 2K-4K for the most part (with 8K and 16K appearing late in the life of the system using bank switching). This small size meant developers would go to huge lengths to save space in the programs, often using graphic data as code, or code as graphics data.

A simply astonishing number of games were created for the Atari VCS which found innovative ways to overcome the limitations of the system (even when it probably wasn’t a good idea to try). The book covers a number of landmark games in the system’s history, such as Pacman (which is sometimes blamed for the North American game crash of 1983), Pitfall, Star Wars – The Empire Strikes Back (the first licenced Star Wars game) and others.

Pacman was interesting since the game requires Pacman and four ghost characters to be somehow fit into the “two player sprites” Atari VCS. This was accomplished by only drawing each ghost on every fourth frame, which makes the game pretty much unplayable in an emulator -

But as you can see, on a traditional CRT television, it looks fine (thanks to the eye’s persistence of vision) -

That said, it’s still a pretty poor rendition of Pacman.

Pitfall was also interesting as it’s pretty much the first “platform game” that has many recognisable elements of the genre by contemporary standards. The way the game was fit into the cartridge and how it came about from a design perspective is also fascinating and there’s an excellent talk on it by David Crane given at this year’s GDC.

Anyhow, I’d highly recommend picking up this book, if you were a kid in the 80′s, if you have an interest in early computing hardware, or just if you’re generally interested in computing, it’s a great read.

I’m actually tempted to write an Atari VCS game in assembler as a programming challenge. I used the 6502 quite a bit during my university degree, and the simplicity and purity of coding in against an 8-bit CPU like that is a lot of fun.

Amazon.com - Racing the Beam: The Atari Video Computer System (Platform Studies)

Posted in IT, Software Engineering | Tagged books, history, soap-box | Leave a comment

Netbeans 7 + Git plugin on Windows Issues?

I just started checking out Netbeans 7 which has a nice Git plugin available now in the plugin manager.

I was horrified though when I opened a project to find every single source file was being shown as “modified” by Netbeans, although there were no diffs shown inside the files!

However, it turns out that it’s not Netbean’s fault.

If you’re using msysgit on Windows to check out your projects, you probably installed it and did the typical “next->next->next” through the installer. Including through the following screen….

Well by default msysgit does line ending mutation so that if you checkout a source file to a Windows box, it will add Windows line ending (“\n\r”) to all source files, and when you push it back to the server, change the line ending to Unix style (“\n”). Unfortunately this causes Netbean’s Git plugin to (rightly) say, “Hey! You changed all these files!”.

Really the auto line ending change stuff is a really bad idea, and you should disable it.

The command to do so is as follows -

 git config --global core.autocrlf false

After disabling, the Git support seems to work really well.

Posted in Java, Software Engineering | Tagged git, Java, netbeans, tips | 4 Comments