Contarini

Java model and renderer for search engine meta tags (e.g. alternative, canonical, description, keywords, robots).

Get Contarini

Most web applications render tags that provide information for web crawlers such as meta-robots, canonical and meta-description. Unfortunately everyone has to write its own code for rendering this stuff. Contarini relieves you from this burden. You create an object with the necessary information and Contarini's well-tested renderer creates the HTML.

The code you write

import com.github.stefanbirkner.contarini.WebCrawlerInfo;
import com.github.stefanbirkner.contarini.render.*;

import static com.github.stefanbirkner.contarini.CommonWebCrawlerAdvice.*;
import static com.github.stefanbirkner.contarini.Alternate.*;
import static com.github.stefanbirkner.contarini.GoogleFeature.*;
import static com.github.stefanbirkner.contarini.render.VoidElementStyle.*;


WebCrawlerInfo info = new WebCrawlerInfo()
    .withCanonical("http://your.domain/canonical")
    .withDescription("dummy description")
    .withKeywords("first keyword, second keyword")
    .withAdvices(NO_ARCHIVE, NO_FOLLOW)
    .withAlternates(
        alternateLanguage("en-gb", "http://your.domain.co.uk/page")
        alternateMedia("only screen and (max-width: 640px)", "http://m.your.domain.com/page"))
    .disableGoogleFeatures(SITELINKS_SEARCH_BOX, TRANSLATION);
Style style = new Style().withVoidElementStyle(HTML_VOID);
WebCrawlerInfoRenderer renderer = new WebCrawlerInfoRenderer(style);
StringWriter html = new StringWriter();
renderer.write(info, html);

The HTML created by Contarini

<link rel="canonical" href="http://your.domain/canonical">
<meta name="description" content="dummy description">
<meta name="keywords" content="first keyword, second keyword">
<meta name="robots" content="noarchive, nofollow">
<link rel="alternate" hreflang="en-gb" href="http://your.domain.co.uk/page">
<link rel="alternate" media="only screen and (max-width: 640px)" href="http://m.your.domain.com/page">
<meta name="google" content="nositelinkssearchbox">
<meta name="google" content="notranslate">

How-to

  1. Add the library to your pom.xml.
    <dependency>
      <groupId>com.github.stefanbirkner</groupId>
      <artifactId>contarini</artifactId>
      <version>1.2.0</version>
    </dependency>
  2. Build a model of the web crawler infos.
    WebCrawlerInfo info = new WebCrawlerInfo().withCanonical("myCanonical.html");
  3. Render the tags.
    new WebCrawlerInfoRenderer().writeMetaTagsForInfoToWriter(info, writer);

JSP Support

There is a JSP tag that renders the WebCrawlerInfo. (It uses the WebCrawlerInfoRenderer internally.) Just add another dependency to your project.

<dependency>
  <groupId>com.github.stefanbirkner</groupId>
  <artifactId>contarini-jsp</artifactId>
  <version>0.1.0</version>
</dependency>

Now you can use the tag to render the info within the JSP.

<contarini:metaTags info="${info}"/>