This is an AMP version of the article, its original content can be found here.
Java Web App Architecture In Takes Framework
I used to utilize Servlets, JSP, JAX-RS, Spring Framework, Play Framework,
JSF with Facelets, and a bit of Spark Framework. All of these solutions,
in my humble opinion, are very far from being object-oriented and elegant.
They all are full of static methods, un-testable data structures, and
dirty hacks. So about a month ago, I decided to create my own Java web
framework. I put a few basic principles into its foundation: 1) No
2) no public static methods, 3) no
classes, and 4) no class casting, reflection,
operators. These four basic principles should guarantee
clean code and transparent architecture. That's how the
Takes framework was born.
Let's see what was created and how it works.
Java Web Architecture in a Nutshell
This is how I understand a web application architecture and its components, in simple terms.
First, to create a web server, we should create
a new network socket,
that accepts connections on a certain TCP port.
Usually it is 80, but I'm going to use 8080 for testing purposes.
This is done in Java with the
That's enough to start a web server. Now, the socket is ready and listening
on port 8080. When someone opens
http://localhost:8080 in their browser,
the connection will be established and the browser will spin its waiting
wheel forever. Compile this snippet and try. We just built a simple web
server without the use of any frameworks. We're not doing anything with
incoming connections yet, but we're not rejecting them either. All of them
are being lined up inside that
server object. It's being done in a background
thread; that's why we need to put that
while(true) in afterward. Without this endless pause,
the app will finish its execution immediately and the server socket will
The next step is to accept the incoming connections. In Java, that's done through
a blocking call to the
The method is blocking its thread and waiting until a new connection arrives. As
soon as that happens, it returns an instance of
Socket. In order to accept
the next connection, we should call
accept() again. So basically, our
web server should work like this:
It's an endless cycle that accepts a new connection, understands it, creates a response, returns the response, and accepts a new connection again. HTTP protocol is stateless, which means the server should not remember what happened in any previous connection. All it cares about is the incoming HTTP request in this particular connection.
The HTTP request is coming from the input stream of the socket and looks like a multi-line block of text. This is what you would see if you read an input stream of the socket:
You will see something like this:
The client (the Google Chrome browser, for example) passes this text into the
connection established. It connects to port 8080 at
localhost, and as soon
as the connection is ready, it immediately sends this text into it, then waits
for a response.
Our job is to create an HTTP response using the information we get in the
request. If our server is very primitive, we can basically ignore all the
information in the request and just return "Hello, world!" to all requests
That's it. The server is ready. Try to compile and run it. Point your browser
to http://localhost:8080, and you will see
That's all you need to build a web server. Now let's discuss how to make it object-oriented and composable. Let's try to see how the Takes framework was built.
Routing/dispatching is combined with response printing in Takes. All you
need to do to create a working web application is to create a single
class that implements
And now it's time to start a server:
class does the exact same socket manipulations explained
above. It starts a server socket on port 8080 and dispatches all incoming
connections through an instance of
TkFoo that we are giving to its constructor.
It does this dispatching in an endless cycle, checking every second whether
it's time to stop with an instance of
Exit.NEVER always responds with, "Don't stop, please".
Now let's see what's inside the HTTP request arriving at
TkFoo and what
we can get out of it. This is how the
interface is defined in Takes:
The request is divided into two parts: the head and the body. The head
contains all lines that go before the empty line that starts
a body, according to HTTP specification in
RFC 2616. There are many useful
Request in the framework. For example,
help you get the method name from the first line of the header:
RqHref will help extract the query part and parse it. For example,
this is the request:
This code will extract that
RqPrint can get the entire request or its body printed as a
The idea here is to keep the
Request interface simple and provide
this request parsing functionality to its decorators. This approach helps the
framework keep classes small and cohesive. Each decorator is very
small and solid, doing exactly one thing. All of these decorators
are in the
As you already probably understand, the
Rq prefix stands for
First Real Web App
Let's create our first real web application, which will do something
useful. I would recommend starting with an
Entry class, which is
required by Java to start an app from the command line:
This class contains just a single
main() static method that will be
called by JVM when the app starts from the command line. As you see, it
giving it an instance of class
TkApp and command
line arguments. We'll create the
TkApp class in a second.
(translates to "front-end with command line interface") makes an instance
of the same
FtBasic, wrapping it into a few useful decorators and configuring
it according to command line arguments. For example,
be converted into a
8080 port number and passed as a second argument of
The web application itself is called
TkApp and extends
We'll discuss this
TkFork class in a minute.
If you're using Maven, this is the
pom.xml you should start with:
mvn clean package should build a
foo.jar file in
and a collection of all JAR dependencies in
target/deps. Now you
can run the app from the command line:
The application is ready, and you can deploy it to, say, Heroku. Just
Procfile file in the root of the repository and push the repo
to Heroku. This is what
Procfile should look like:
class seems to be one of the core elements of the framework. It
helps route an incoming HTTP request to the right take. Its logic is very
simple, and there are just a few lines of code inside it. It encapsulates
a collection of "forks", which are instances of the
route() method either returns an empty iterator or an iterator
with a single
TkFork goes through all forks, calling their
route() methods until one of them returns a response. Once that happens,
TkFork returns this response to the caller, which is
Let's create a simple fork ourselves now. For example, we want to show
the status of the application when the
/status URL is requested. Here is
I believe the logic here is clear. We either return an empty iterator
or an iterator with an instance of
TkStatus inside. If an empty
iterator is returned,
TkFork will try to find another fork in the
collection that actually gets an instance of
By the way, if nothing is found and all forks return empty iterators,
TkFork will throw a "Page not found" exception.
This exact logic is implemented by an out-of-the-box fork called
which attempts to match a request URI path with the regular expression
We can compose a multi-level structure of
TkFork classes; for example:
Again, I believe it's obvious. The instance of
FkRegex will ask an
encapsulated instance of
TkFork to return a response, and it will try to
fetch it from one that
FkParams encapsulated. If the HTTP query is
/status?f=xml, an instance of
TkStatusXML will be returned.
Now let's discuss the structure of the HTTP response and its object-oriented
This is how the interface looks:
Looks very similar to the
doesn't it? Well, it's identical, mostly because the structure of the HTTP request and
response is almost identical. The only difference is the first line.
There is a collection of useful decorators that help in response building. They are composable, which makes them very convenient. For example, if you want to build a response that contains an HTML page, you compose them like this:
In this example, the decorator
creates a response with a body but with no headers at all. Then,
RsWithType adds the header
Content-Type: text/html to it. Then,
makes sure the first line of the response contains
HTTP/1.1 200 OK.
You can create your own decorators that can reuse existing ones. Take a look
at how it's done in
How About Templates?
Returning simple "Hello, world" pages is not a big problem, as we can see. But
what about more complex output like HTML pages, XML
data sets, etc? There are a few convenient
Response decorators that
enable all of that. Let's start with Velocity,
a simple templating engine. Well, it's not that simple. It's rather powerful,
but I would suggest to use it in simple situations only. Here is how it
accepts a single argument that has to be a Velocity template. Then, you call
with() method, injecting data into the Velocity context. When it's time
to render the HTTP response,
RsVelocity will "evaluate" the template
against the context configured. Again, I would recommend you use
this templating approach only for simple outputs.
For more complex HTML documents, I would recommend you use XML/XSLT
in combination with Xembly. I explained this idea in a few previous posts:
XML+XSLT in a Browser
and RESTful API and a Web Site in the Same URL.
It is simple and powerful—Java generates XML output and the XSLT
processor transforms it into HTML documents. This is how we separate
representation from data. The XSL stylesheet is a "view" and
TkIndex is a "controller",
in terms of MVC.
I'll write a separate article about templating with Xembly and XSL very soon.
In the meantime, we'll create decorators for JSF/Facelets and JSP rendering in Takes. If you're interested in helping, please fork the framework and submit your pull requests.
What About Persistence?
Now, a question that comes up is what to do with persistent entities, like databases,
in-memory structures, network connections, etc. My suggestion is to
initialize them inside the
Entry class and pass them as arguments into
TkApp constructor. Then, the
TkApp will pass them into the
constructors of custom takes.
For example, we have a PostgreSQL database that contains
some table data that we need to render. Here is how I would
initialize a connection to it in the
Entry class (I'm using
a BoneCP connection pool):
Now, the constructor of
TkApp must accept a single argument of type
TkIndex also accepts a single argument of class
Source. I believe
you know what to do with it inside
TkIndex in order to fetch the SQL
table data and convert it into HTML. The point here is that the dependency
must be injected into the application (instance of class
TkApp) at the
moment of its instantiation. This is a pure and clean dependency injection
mechanism, which is absolutely container-free. Read more about it in
"Dependency Injection Containers Are Code Polluters".
Since every class is immutable and all dependencies are injected only
through constructors, unit testing is extremely easy. Let's say we want
TkStatus, which is supposed to return an HTML response
(I'm using JUnit 4 and Hamcrest):
Also, we can start the entire application or any individual take in a test HTTP server and test its behavior via a real TCP socket; for example (I'm using jcabi-http to make an HTTP request and check the output):
starts a test web server at a random TCP port and calls the
exec() method at the provided instance of
FtRemote.Script. The first
argument of this method is a URI of the just-started web server homepage.
The architecture of Takes framework is very modular and composable. Any individual take can be tested as a standalone component, absolutely independent from the framework and other takes.
Why the Name?
That's the question I've been hearing rather often. The idea is simple, and it originates from the movie business. When a movie is made, the crew shoots many takes in order to capture the reality and put it on film. Each capture is called a take.
In other words, a take is like a snapshot of the reality.
The same applies to this framework. Each instance of
a reality at one particular moment in time. This reality is then sent to the
user in the form of a
PS. There are a few words about authentication: How Cookie-Based Authentication Works in the Takes Framework.
PPS. There are a few real web systems, which you may be interested to take a look at. They all are using Takes Framework and their code is open: rultor.com, jare.io, wring.io.