Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

When Framework is Overkill

DZone's Guide to

When Framework is Overkill

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

Recently there have been some hot articles about frameworks: Better Java Web Frameworks and Better Java Web Frameworks: ItsNat. Sometimes I've found that frameworks just make life harder. Recently I was assigned a job to create a company's site from scratch. The requirement is relatively simple, less than 10 static HTML pages, literally no server side processing.

I decided to write a simple mini "framework" to aid my client's designer, instead of adpoting an off-the-shelf web framework.

The idea is pretty much the same as most CMS: to keep a big template out and to only let the user (the maintanence designer) touch the content of the page. Yet, I want to provide flexibility to some degree. For example, at least I wanted to do some simple SEO to every page.

Template:

<html>
<head>
<title>{{title}}</title>
<meta name="description" content="{{description}}" />
</head>
<body>
<div id="head">
... ignored for simplicity...
</div>
<div id="content">{{body}}</div>
<div id="footer">... </div>
</body>
</html>

And with the template, the actual page needs only to provide the information like TITLE, BODY, DESCRIPTION:

e.g., "about-us.html":

{{title}} About Us

{{description}} what ever information that you want search engine know

{{body}} ... here is the real content, ignored for simplicity... 

So I need a ServletFilter to intercept the request and to wrap the response from "about-us.html" and to make any necessary subsititutions.  I search the default servlet's output for anything like {{ABC}} and use "String.replace" to try to replace/insert into the template string. Once all is done, I dump the result to the original response. 

I have included my source code below. The BufferedServletOutputStream class is copy-and-pasted from a Google search result, sorry I couldn't find the author's name:

public class StaticSiteTemplate implements Filter {
private Map defaultParameters = new HashMap();
private String template = "";

@Override
public void init(FilterConfig filterConfig) throws ServletException {
...
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
CapturedHttpResponseWrapper wrapper = new CapturedHttpResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
// find tagString originalContent = wrapper.toString();
Pattern pattern = Pattern.compile( < a
href = "file://%7b//%7B//w+//%7D//%7D"
mce_href = "file://%7b//%7B//w+//%7D//%7D" >\\{\\{\\w +\\}\\}</a >);
Matcher matcher = pattern.matcher(originalContent);
String result = template;
if (matcher.find()) {
ArrayList breakpointList = new ArrayList();
breakpointList.add(new TemplateBreakupPoint(matcher.group(), matcher.end(), originalContent.length()));
while (matcher.find()) {
breakpointList.get(breakpointList.size() - 1).end = matcher.start();
breakpointList.add(new TemplateBreakupPoint(matcher.group(), matcher.end(), originalContent.length()));
}
for (TemplateBreakupPoint tbp : breakpointList) {
result = result.replace(tbp.key, originalContent.substring(tbp.start, tbp.end).trim());
}
} else {// assume all belong to bodyresult = template.replace("{{body}}", originalContent);}
// replace default params
for (String key : defaultParameters.keySet()) {
result = result.replace(key, defaultParameters.get(key));
}
// dump outputbyte[] buffer = result.getBytes(response.getCharacterEncoding());
response.setContentLength(buffer.length);
response.getOutputStream().write(buffer);
}

public void loadTemplate() throws IOException {...}

...
}

You can view my site as a live demo of this approach here: http://www.nwhholdings.com 

Source code.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}