Restlet Cookbook - How to Create Virtual Hosts in Restlet Applications (Recipe 5)

Posted: 02/24/2008 by Val

Creating virtual hosts using Restlet API consists of several lines of code (see Listing 1 below) and offers flexibility in configuring them. Note that both, setHostDomain and setHostPort methods, take Strings in java.util.regex patterns. Also, VirtualHost extends org.restlet.Router, and because of this you can "attach" to it Restlet's and Resource's

Listing 1
public class Naviquan extends Application {
  ...

  public static void main(String ...args) throws Exception {
    // Create a component with an HTTP server connector
    Component component = new Component();
    component.getServers().add(Protocol.HTTP, 80);
    component.getClients().add(Protocol.FILE);
    // Attach the application to the default host and start it
    component.getDefaultHost().attach("", new Naviquan(component.getContext()));
    
    // ==================================================
    // ============ Create virtual host =================
    // ==================================================
    VirtualHost host = new VirtualHost(component.getContext());
    host.setHostDomain("xyz.naviquan.com|localhost");
    host.setHostPort("80"); 
    host.attach("/xyz", XYZResource.class); 
    component.getHosts().add(host);
    


    ...
    component.start();
  }

  public Naviquan(Context context) {
    super(context);
    try {
       ...
    }
    catch (Exception ex) {
      ...
    }
  }
}

If the above looks a bit confusing here is another listing that, although illustrating same APIs, shows what may be called more consistent approach. In particular, in Listing 2, the main application class is called Camerata. Its main() method is used to start the whole thing. Note, that Camerata extends Restlet's Component class.

When virtual hosts are created, instances of Restlet's Application classes are created and "attached" to VirtualHost's. To clarify the approach, a skeleton coding is shown for Arredocenter, one of the two Application's created in the Camerata's constructor.

Obviously, to make this work, you'll have to fill in this skeleton code with your own that implements logic specific to your application. But this explicit implementation of the Component (Camerata class) and Application's (Naviquan and Arredocenter), and the way you glue together everything inside Camerata's constructor makes it easier to understand in my view the process of creating VirtualHost's in Web sites based and Restlet API.

Listing 2
public class Camerata extends Component {
 .....
 
 public static void main(String ...args) throws Exception {
  new Camerata(); }

 public Camerata() throws Exception {
  getServers().add(Protocol.HTTP, "[your IP]", 80);
  getClients().add(Protocol.FILE);
  ....

  /* arredocenter.com */
  VirtualHost host = new VirtualHost(getContext());
  host.setHostDomain("www.arredocenter.com|arredocenter.com");
  host.setHostPort("80");
  host.attach("", new Arredocenter(getContext()));
  getHosts().add(host);

  /* naviquan.com  */
  host = new VirtualHost(getContext());
  host.setHostDomain("www.naviquan.com|naviquan.com");
  host.setHostPort("80");
  host.attach("", new Naviquan(getContext()));
  getHosts().add(host);
  start();
 }
}

public class Arredocenter extends Application {
 public static Configuration freeMarkerConfig;
 ..... // everything else you need for your application

 public Arredocenter(Context context) {
  super(context);
  try {
   // build home where Home is your custom 'data' model/bean for home
   Home home = buildHome();
   ...
   // configure FreeMarker  FREEMARKER_TEMPLATE_LOCATION
   freeMarkerConfig = new Configuration();
   freeMarkerConfig.setDirectoryForTemplateLoading(new 
File(Constants.ARREDOCENTER_FREEMARKER_TEMPLATE_LOCATION));
   
freeMarkerConfig.setTemplateUpdateDelay(Constants.FREEMARKER_TEMPLATE_UPDATE_DEL
AY);
   
freeMarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG
_HANDLER);
   freeMarkerConfig.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
   freeMarkerConfig.setDefaultEncoding("ISO-8859-1");
   freeMarkerConfig.setOutputEncoding("UTF-8");
   freeMarkerConfig.setLocale(Locale.US);
  }
  catch (Exception ex) {
   ex.printStackTrace();
  }
 }


 private Home buildHome() throws PoolException, NotesException {
  Home home = new Home();
  ....
 }

 /** createRoot */
 public Restlet createRoot() {
  Directory directory = new Directory(getContext(), 
Constants.ARREDOCENTER_STATIC_FILE_LOCATION);
  Router router = new Router(getContext());

  router.attach("/", 
HomeResource.class).getTemplate().setMatchingMode(org.restlet.util.Template.MODE
_EQUALS);
  router.attachDefault(directory);

  // router for reloading home
  router.attach("/home", HomeReloadedResource.class);
  ...
 }

 ..... // everything else you need for your application
}

Recent Blogs

Convergence of Technologies II: Restlets, Highslide Viewer & SlideShowPro for Flash How to use Restlets, Highslide Viewer & Slide Show Pro Flash component to generate slide shows and show them in pop-up iframes 08/14/2009

Convergence of Technologies: Restlets, Highslide Viewer & Google Maps API How to use Restlets, Highslide Viewer & Google Maps API to generate Google maps using links on Web pages and show them in pop-up iframes. 08/13/2009

Ambimorphic, Mobile Configurations for Lamport Clocks After years of technical research into Smalltalk, with close cooperation with MIT Computer Science and Artificial Intelligence Laboratory (CSAIL) and its SCIgen, we have been able to show the construction of link-level acknowledgements. As a result, we propose an analysis of A* search, which we call Volt. 10/02/2008

Restlets and HTTP Sessions (Recipe 9) This recipe discusses some options in available to Web site developers using Restlet API to emulate traditional HttpSession's 07/31/2008

Restlet Cookbook - Delving into handleGet() and handlePost() (Recipe 8) This posting provides related examples that illustrate at least some simple things you may do with several handleX() methods where X may stand for various types of calls. 07/30/2008

Restlet Cookbook - How to enable SSL (Recipe 7) How to enable SSL on Restlet Web sites wihtout knowing too much keys, encryption, SSL and such. 07/29/2008

Restlet Cookbook - Using Filters in Restlet Applications (Recipe 6) This recipe contains code snippets illustrating the use of Filters in Restlet applications 03/09/2008

Restlet Cookbook - How to Create Virtual Hosts in Restlet Applications (Recipe 5) This recipe contains a code snippet for creating virtual hosts 02/24/2008

Popular HTML Escape Codes Here is a table of HTML escape codes for most frequently used symbols. If you use HTML, sooner or later you will find this table useful. 12/29/2007

Restlet Cookbook - Access Log (Recipe 4) This recipe describes access Logging using Restlet framework 11/09/2007

more...