java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
net.i2p.imagegen.IdenticonServlet
- All Implemented Interfaces:
Serializable, Servlet, ServletConfig
This servlet generates identicon (visual identifier) images ranging from 16x16 to 1024x1024 in size.
Supported Image Formats
Currently only PNG is supported because javax.imageio package does not come with built-in GIF encoder and PNG is the only remaining reasonable format.
Initialization Parameters:
- version
- bump this to invalidate cached identicons after a change that alters the rendered result. It is part of the ETag. Defaults to 1. (Optional)
- cacheProvider
- full class path to
IdenticonCacheimplementation. Without one, every request is rendered and nothing is cached. (Optional)
Request Parameters
- c
- identicon code to render, as a number, a base32 or base64 hash, or any other string. Required: a request without it is answered with 404 rather than an identicon derived from the requester's IP address. (Required)
- s
- identicon size in pixels. Defaults to 32x32. Values outside 16 to 1024 are clamped to that range, and a non-numeric value is ignored in favour of the default. (Optional)
- See Also:
Field Summary
FieldsModifier and TypeFieldDescriptionprivate IdenticonCacheOptional rendered-image cache; null disables caching.private static final longIdenticons are cached for 24 hours by default.(package private) static final intRequested size when the "s" parameter is missing or unparsable.private static final StringImage format the servlet writes identicons in.private static final StringContent type sent with an identicon.private longHow long a cached image stays fresh, in milliseconds.private static final StringInit parameter naming the cache implementation.private static final StringInit parameter naming the cache version.(package private) static final intLargest size the renderer is asked for; larger requests are clamped down.(package private) static final intSmallest size the renderer is asked for; smaller requests are clamped up.private static final StringRequest parameter naming the identicon seed.private static final StringRequest parameter naming the identicon size.private final IdenticonRendererDraws the PNG for a given code and size.private static final longFixed id, the servlet is never serialized across versions.private intRendering version, folded into the ETag so rendering changes force a refresh.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprotected voiddoGet(HttpServletRequest request, HttpServletResponse response) Handle GET request: generate identicon image.voidinit(ServletConfig cfg) Load configuration from servlet initialization parameters.(package private) static intConvert the "c" request parameter to the int the renderer works with: a number is used as-is, a base32 or base64 hash is reduced to its Java hashCode, and anything else is hashed as a string.(package private) static intResolve the requested pixel size, clamping it to the supported range.Methods inherited from class HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, serviceMethods inherited from class GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
Field Details
serialVersionUID
private static final long serialVersionUIDFixed id, the servlet is never serialized across versions.- See Also:
INIT_PARAM_VERSION
Init parameter naming the cache version.- See Also:
INIT_PARAM_CACHE_PROVIDER
Init parameter naming the cache implementation.- See Also:
PARAM_IDENTICON_SIZE_SHORT
Request parameter naming the identicon size.- See Also:
PARAM_IDENTICON_CODE_SHORT
Request parameter naming the identicon seed.- See Also:
IDENTICON_IMAGE_FORMAT
Image format the servlet writes identicons in.- See Also:
IDENTICON_IMAGE_MIMETYPE
Content type sent with an identicon.- See Also:
DEFAULT_IDENTICON_EXPIRES_IN_MILLIS
private static final long DEFAULT_IDENTICON_EXPIRES_IN_MILLISIdenticons are cached for 24 hours by default.- See Also:
DEFAULT_IDENTICON_SIZE
static final int DEFAULT_IDENTICON_SIZERequested size when the "s" parameter is missing or unparsable.- See Also:
MIN_IDENTICON_SIZE
static final int MIN_IDENTICON_SIZESmallest size the renderer is asked for; smaller requests are clamped up.- See Also:
MAX_IDENTICON_SIZE
static final int MAX_IDENTICON_SIZELargest size the renderer is asked for; larger requests are clamped down.- See Also:
version
private int versionRendering version, folded into the ETag so rendering changes force a refresh.renderer
Draws the PNG for a given code and size.cache
Optional rendered-image cache; null disables caching.identiconExpiresInMillis
private long identiconExpiresInMillisHow long a cached image stays fresh, in milliseconds.
Constructor Details
IdenticonServlet
public IdenticonServlet()Constructor. Jetty instantiates the servlet reflectively with no arguments and configures it through init parameters, so a bare instance is expected.
Method Details
init
Load configuration from servlet initialization parameters.- Specified by:
initin interfaceServlet- Overrides:
initin classGenericServlet- Throws:
ServletException
parseSize
Resolve the requested pixel size, clamping it to the supported range.- Parameters:
sizeParam- the "s" request parameter, may be null- Returns:
MIN_IDENTICON_SIZEtoMAX_IDENTICON_SIZE,DEFAULT_IDENTICON_SIZEif sizeParam is null or not a number
parseCode
Convert the "c" request parameter to the int the renderer works with: a number is used as-is, a base32 or base64 hash is reduced to its Java hashCode, and anything else is hashed as a string.- Parameters:
codeParam- the "c" request parameter, must not be null or empty- Returns:
- the identicon code to render
doGet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException Handle GET request: generate identicon image.- Overrides:
doGetin classHttpServlet- Throws:
ServletExceptionIOException