For the rest of Metanotion Software's services and products, please visit our homepage.
This is a server side library for parsing URI Templates and using them to assist dispatching in servlets. There is also the beginnings of work to provide an API to convert templates into URI's for client side purposes as well.
Since URI templates were specified in the context of clients, there is a "little more" to the story for using them server side. Rather than unadorned templates, the templates are specified in a file containing a list of templates:
# this is a comment
[ "admin" ] : http://www.myblog.com/admin/
# this uses a matched variable 'title' in the match list.
[ "article", title ] : http://www.myblog.com/{year}/{month}/{title}
[ "comment" ] : http://www.myblog.com/comments/{uuid}.html
To use these in a servlet, consider the following example code:
import net.metanotion.urlmapper.URITemplateSet;
import net.metanotion.urlmapper.Loader;
public class ExampleServlet extends HttpServlet {
private URITemplateSet uts = null;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String qString = req.getQueryString();
String fullURI = req.getRequestURL().toString() + ((qString.length() > 0) ? ("?" + qString) : "");
Map<String,Object> variables = new HashMap<String,Object>();
List<String> result = new ArrayList<String>();
boolean match = uts.match(fullURI, result, variables);
/* do something specific */
}
public void init(ServletConfig config) {
try {
FileInputStream uriMap = null;
try {
uriMap = new FileInputStream("my/URIMap/file/uritemplates.txt");
uts = net.metanotion.urlmapper.Loader.load(new InputStreamReader(uriMap));
}
catch (Exception e) { }
finally {
if(uriMap != null) {
try {
uriMap.close();
} catch (IOException ioe) { }
}
} catch (Exception e) { }
}
}
Last Update: June 11th, 2008.