Lembas Backend

This document will describe how you setup your Java backend to use Lembas.

Lembas-Backend is the service part of the Lembas

Lembas-core allows you to define data types(artifacts) and service interfaces(endpoints) that are capable of generating self descriptions.

Lembas-Backend on the other hand helps you build a service using Lembas-core objects (artifacts and endpoints), builds service description file, generate source code to build mobile clients and process their endpoint calls.

1. Maven

Add maven dependencies to your project

<dependency>
          <groupId>com.happyblueduck.lembas</groupId>
          <artifactId>lembas-core</artifactId>
          <version>2.1</version>
    </dependency>
    <dependency>
          <groupId>com.happyblueduck.lembas</groupId>
          <artifactId>lembas-backend</artifactId>
          <version>2.1</version>
    </dependency>

Lembas needs two servlet entry, one for generating source code and another one for processing and serving generated calls from source code:

2. Code Generator

Add code generation servlet com.happyblueduck.lembas.servlets.CodeGen to your web.xml. It is responsible from generating a description file of your project according to Configuration , sending that description file to code generator and zipping the result.

<servlet>
        <servlet-name>Codegen</servlet-name>
        <servlet-class>com.happyblueduck.lembas.servlets.CodeGen</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Codegen</servlet-name>
        <url-pattern>/lembas</url-pattern>
    </servlet-mapping>

3. Dispatcher

For serving LembasRequests, you need to extend com.happyblueduck.lembas.servlets.Dispatcher (thoght you can also directly use it, extending will give you more control) . Create a new java file named MobileDispatcher :

package com.happyblueduck.application; 

import com.happyblueduck.lembas.servlets.Dispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MobileDispatcher extends Dispatcher {

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
            // for now, our dispatcher only calls the original implementation..
            // ..and time the execution
            double start =     System.currentTimeMillis(); 
            super.doPost(req, res);
            double duration = System.currentTimeMillis() - start; 
            System.out.println("LembasRequest executed in :" + duration + "ms");
        }

 @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().println("Hello you have reached the winter of our discontent");
	}
}

You can now add your dispatcher to web.xml

<servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>com.happyblueduck.application.MobileDispatcher</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Dispatcher</servlet-name>
        <url-pattern>/Arwen/*</url-pattern>
    </servlet-mapping>

4. Configuration

You can now configure your MobileDispatcher by overriding init() method

@Override
public void init() throws ServletException {
	//service name and servlet url-mapping *should* match
  Config.serviceName  = "Arwen"; 
  Config.version      = "1.0";
  Config.HOST_URL     = http://localhost";
  Config.HOST_PORT    = "8080"
}

If you start your server and go to http://localhost:8080/lembas?target=objc, you should be able to download zipped source files iOS, which you can use with Lembas-ios.