@@ -0,0 +1,2335 @@
+package com.bradmcevoy.http;%0A%0Aimport com.bradmcevoy.common.Path;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A/**%0A * Used the decorator pattern to add support for .wellknown paths to a custom%0A * resource factory.%0A * %0A * By integrating this into your application it will %22overlay%22 some additional%0A * synthetic resources (ie url's) on your application%0A * %0A * This is required for iCal5+ support for caldav%0A * %0A * http://tools.ietf.org/html/draft-daboo-srv-caldav-10#page-5%0A * http://tools.ietf.org/html/rfc5785%0A * http://hueniverse.com/2010/04/rfc-5785-defining-well-known-uris/%0A *%0A * @author brad%0A */%0Apublic class WellKnownResourceFactory implements ResourceFactory %7B%0A%0A%09public static final String URI_PREFIX = %22/.well-known%22;%0A%09%0A%09private final ResourceFactory wrapped;%0A%09%0A%09private Map%3CString,WellKnownHandler%3E mapOfWellKnownHandlers = new HashMap%3CString, WellKnownHandler%3E();%0A%0A%09public WellKnownResourceFactory(ResourceFactory wrapped, List%3CWellKnownHandler%3E wellKnownHandlers) %7B%0A%09%09this.wrapped = wrapped;%0A%09%09for( WellKnownHandler h : wellKnownHandlers ) %7B%0A%09%09%09addHandler(h);%0A%09%09%7D%0A%09%7D%0A//%09%09%0A//%09public WellKnownResourceFactory(ResourceFactory wrapped) %7B%0A//%09%09this.wrapped = wrapped;%0A//%09%7D%09%0A%09%0A%09@Override%0A%09public Resource getResource(String host, String sPath) %7B%0A%09%09if( sPath.startsWith(URI_PREFIX)) %7B%0A%09%09%09Path path = Path.path(sPath);%0A%09%09%09path = path.getStripFirst();%0A%09%09%09WellKnownHandler wellKnown = mapOfWellKnownHandlers.get(path.getFirst());%0A%09%09%09if( wellKnown != null ) %7B%0A%09%09%09%09Resource hostRes = wrapped.getResource(host, %22/%22);%0A%09%09%09%09if( hostRes != null ) %7B%0A%09%09%09%09%09return wellKnown.locateWellKnownResource(hostRes);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return wrapped.getResource(host, sPath);%0A%09%7D%0A%09%0A%09public final void addHandler(WellKnownHandler handler) %7B%0A%09%09mapOfWellKnownHandlers.put(handler.getWellKnownName(), handler);%0A%09%7D%0A%09%0A%09/**%0A%09 * Locates a resource for a .well-known/XXX path%0A%09 */%0A%09public interface WellKnownHandler %7B%0A%09%09/**%0A%09%09 * Identifies the part of the path following .well-known which will map to this handler%0A%09%09 * %0A%09%09 * @return %0A%09%09 */%0A%09%09String getWellKnownName();%0A%09%09%0A%09%09/**%0A%09%09 * Find a resource which will handle the well-known request. This should%0A%09%09 * generally redirect to the appropriate location. It should not perform%0A%09%09 * the role of the service being looked up.%0A%09%09 */%0A%09%09Resource locateWellKnownResource(Resource hostRes);%0A%09%7D%0A%7D%0A

implement .well-known lookup