/** Creates a root Restlet that will receive all incoming calls. */ @Override public synchronized Restlet createRoot() { // Create a router Restlet that defines routes. Router router = new Router(getContext()); // Defines a route for the resource "list of concepts" router.attach("/{vocabularyName}/?{SPARQLquery}", ConceptListResource.class); return router; }
public Route addPage( String path, Class<? extends Resource> clazz, URLS.Name namedPath, boolean guard) { Route route; path = "/" + adminPage + path; if (guard) { route = root.attach(path, guard(clazz)); } else { route = root.attach(path, clazz); } route.getTemplate().setMatchingMode(Template.MODE_EQUALS); if (namedPath != null) { urls.put(route.getTemplate(), namedPath); } return route; }
public Route add(String path, Class<? extends Resource> clazz, URLS.Name namedPath) { Route route = root.attach(path, clazz); route.getTemplate().setMatchingMode(Template.MODE_EQUALS); if (namedPath != null) { urls.put(route.getTemplate(), namedPath); } return route; }
public void addRoutes(Map m, Router r) { Iterator it = m.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); // LOG.info("Found mapping: " + entry.getKey().toString()); Restlet restlet = (getApplicationContext().getBean(entry.getValue().toString()) instanceof Resource) ? new BeanResourceFinder(getApplicationContext(), entry.getValue().toString()) : new BeanDelegatingRestlet(getApplicationContext(), entry.getValue().toString()); String path = entry.getKey().toString(); r.attach(path, restlet); if (path.indexOf("?") == -1) { r.attach(path + "?{q}", restlet); } else LOG.fine("Query string already listed in restlet mapping: " + path); } }
public synchronized Restlet createRoot() { Router router = new Router(getContext()); router.attach("/", new Redirector(null, "/engine", Redirector.MODE_CLIENT_TEMPORARY)); router.attach("/engine", EngineResource.class).setMatchingMode(Template.MODE_EQUALS); router.attach("/engine/", EngineResource.class).setMatchingMode(Template.MODE_EQUALS); Directory alljobsdir = new Directory(getContext(), engine.getJobsDir().toURI().toString()); alljobsdir.setListingAllowed(true); router.attach("/engine/jobsdir", alljobsdir); EnhDirectory anypath = new EnhDirectory(getContext(), engine.getJobsDir().toURI().toString() /*TODO: changeme*/) { @Override Reference determineRootRef(Request request) { String ref = "file:/"; return new Reference(ref); } }; anypath.setListingAllowed(true); anypath.setModifiable(true); anypath.setEditFilter(JobResource.EDIT_FILTER); router.attach("/engine/anypath/", anypath); EnhDirectory jobdir = new EnhDirectory(getContext(), engine.getJobsDir().toURI().toString() /*TODO: changeme*/) { @Override Reference determineRootRef(Request request) { try { return new Reference( EngineApplication.this .getEngine() .getJob(TextUtils.urlUnescape((String) request.getAttributes().get("job"))) .getJobDir() .getCanonicalFile() .toURI() .toString()); } catch (IOException e) { throw new RuntimeException(e); } } }; jobdir.setListingAllowed(true); jobdir.setModifiable(true); jobdir.setEditFilter(JobResource.EDIT_FILTER); router.attach("/engine/job/{job}/jobdir", jobdir); router.attach("/engine/job/{job}", JobResource.class); router.attach("/engine/job/{job}/report/{reportClass}", ReportGenResource.class); router.attach("/engine/job/{job}/beans", BeanBrowseResource.class); router.attach("/engine/job/{job}/beans/{beanPath}", BeanBrowseResource.class); router.attach("/engine/job/{job}/script", ScriptResource.class); // static files (won't serve directory, but will serve files in it) String resource = "clap://class/org/archive/crawler/restlet"; Directory staticDir = new Directory(getContext(), resource); router.attach("/engine/static/", staticDir); return router; }
@Override public void handle(Request request, Response response) { final StringBuilder sb = new StringBuilder(); final RouteList routers = router.getRoutes(); final String prefix = request.getOriginalRef().getPath().endsWith("/") ? "" : request.getOriginalRef().getPath() + "/"; final boolean detailed = request.getOriginalRef().getQueryAsForm().getNames().contains("describe"); sb.append("<div>"); if (detailed) { String href = request.getOriginalRef().getPath(); sb.append("<a href='" + href + "'>[Less Detail]</a>"); } else { sb.append("<a href='?describe'>[More Detail]</a>"); } sb.append("<ul>"); for (int i = 0; i < routers.size(); i++) { Route r = routers.get(i); if (!detailed && r.getTemplate().getVariableNames().size() > 0) { continue; } String pattern = r.getTemplate().getPattern(); if (pattern.length() > 0) { pattern = pattern.substring(1); if (pattern.length() > 0) { sb.append("<li>"); sb.append("<a href='" + prefix + pattern + "'>" + pattern + "</a>"); if (detailed) { Restlet next = r.getNext(); Class dest = null; if (next instanceof Finder) { dest = ((Finder) next).getTargetClass(); } else if (next instanceof Guard) { Guard g = (Guard) next; if (g.getNext() instanceof Finder) { dest = ((Finder) g.getNext()).getTargetClass(); } else { dest = g.getNext().getClass(); } } if (dest != null) { sb.append("<span> [").append(dest.getName()).append(" ]</span>"); } try { String description = findDescription(dest); if (description != null) { sb.append("<div>").append(description).append("</div>"); } } catch (Exception ex) { getLogger().log(Level.SEVERE, "Error getting description", ex); } } sb.append("</li>"); } } } sb.append("</ul>"); sb.append( "PSCP API, Version " + Version.getVersion() + ", Created " + Version.getVersionDate()); sb.append("</div>"); response.setEntity(sb.toString(), MediaType.TEXT_HTML); }
@Override public Restlet createRoot() { Router root = new Router(getContext()); root.setDefaultMatchingMode(Router.BEST); root.setDefaultMatchQuery(false); RouterBuilder routes = new RouterBuilder(root); RootDirectory rootDirectory = new RootDirectory(root); root.attach("", guard(rootDirectory)).setMatchingMode(Template.MODE_EQUALS); root.attach("/", guard(rootDirectory)).setMatchingMode(Template.MODE_EQUALS); // root.attach("", new RootDirectory(root)).getTemplate().setMatchingMode(Template.MODE_EQUALS); // root.attach("/", new // RootDirectory(root)).getTemplate().setMatchingMode(Template.MODE_EQUALS); routes.addPage("", DirectoryService.class, null, false); routes.addPage("/contacts/form/{id}", ContactForm.class, URLS.Name.CONTACT_FORM); routes.addPage("/contacts/form/", ContactForm.class); routes.addPage("/contacts", ContactService.class, URLS.Name.CONTACT); routes.add("/library", LibraryResource.class); routes.addPage("/library/", LibraryResource.class); routes.addPage("/library/upload", CitationUploadForm.class); routes.addPage("/library/{id}", CitationFileForm.class); routes .add("/productpage/{id}", ProductPageService.class) .getTemplate() .getDefaultVariable() .setType(Variable.TYPE_ALL); routes.addPage( "/products/revisions/form/{id}/{rev}", ProductRevisionForm.class, URLS.Name.PRODUCT_REVISIONS_FORM); routes.addPage("/products/revisions/form/{id}/", ProductRevisionForm.class); routes.addPage( "/products/revisions/{id}/{rev}", ProductRevisionService.class, URLS.Name.PRODUCT_REVISIONS); routes.addPage("/products/revisions/{id}/", ProductRevisionService.class); routes.addPage("/products/form/{id}", ProductForm.class, URLS.Name.PRODUCT_FORM); routes.addPage("/products/form/", ProductForm.class); routes.addPage("/products/upload", ProductUploadForm.class); routes.add("/products", ProductService.class, URLS.Name.PRODUCT); routes.add( "/productnames/ac", AutoCompleteResource.ProductName.class, URLS.Name.AC_PRODUCT_NAME); routes.add("/producttypes", ProductTypeService.class); routes.add( "/producttypes/ac", AutoCompleteResource.ProductType.class, URLS.Name.AC_PRODUCT_TYPE); root.attach("/producttypes/{id}", ProductTypeService.class); routes.add("/stations", StationService.class); routes.add("/stations/ac", AutoCompleteResource.Station.class, URLS.Name.AC_STATION); routes.add( "/stations/types/ac", AutoCompleteResource.StationType.class, URLS.Name.AC_STATION_TYPE); routes.add("/stations/locations", StationLocationService.class); routes.add("/stations/overview", StationOverviewService.class); // @see ext comment in BaseResource routes.add("/stations/locations.{ext}", StationLocationService.class); routes.add("/stations/page/{id}", StationPageService.class, URLS.Name.STATION_PAGE); routes.addPage("/stations/upload", StationUploadForm.class); routes.add("/stations/all/products", ProductLinkService.class); routes.add("/stations/{id}/products", ProductLinkService.class, URLS.Name.PRODUCT_LINK); routes.add("/stations/{id}", StationService.class, URLS.Name.STATION); routes.add("/stations/", StationService.class); routes.addPage("/stations/form/{id}", StationForm.class, URLS.Name.STATION_FORM); routes.addPage("/stations/form/", StationForm.class); routes.addPage("/batches", BatchMonitorResource.class); routes.add("/nations/ac", AutoCompleteResource.Nation.class, URLS.Name.AC_NATIONS); routes.add("/regions/ac", AutoCompleteResource.Region.class); root.attach("/admin/debug", guard(new DebugRestlet())); root.attach( "/admin/refresh", guard( new Restlet() { public static final String PSCP_DESCRIPTION = "Refresh internal DB and Modified-Since caches"; @Override public void handle(Request request, Response response) { getLogger().info("Refresh issued"); lookupCache.refreshAll(); ModifiedCache.clear(); response.setEntity("Refresh Complete", MediaType.TEXT_HTML); } })); root.attach( "/login", guard( new Restlet() { public static final String PSCP_DESCRIPTION = "Provide Login Features"; @Override public void handle(Request request, Response response) { if (request.getChallengeResponse() != null) { Reference referrer = request.getReferrerRef(); String ret = request.getOriginalRef().getQueryAsForm().getFirstValue("ret"); if (ret != null) { response.redirectSeeOther(ret); } else if (referrer != null) { response.redirectSeeOther(referrer); } } } })); urls.put( root.attach( "/logout", new Restlet() { public static final String PSCP_DESCRIPTION = "Provide Logout Features"; @Override public void handle(Request request, Response response) { if (request.getChallengeResponse() != null) { String userName = request.getChallengeResponse().getParameters().getFirstValue("username"); if ("logout".equals(userName)) { response.setStatus(Status.SUCCESS_OK); response.setEntity("Logged Out", MediaType.ALL); CookieSetting cookie = new CookieSetting("u", ""); cookie.setPath("/"); response.getCookieSettings().add(cookie); response.redirectSeeOther(urls.getURL(URLS.Name.STATIC_MEDIA)); } else { response.setStatus(Status.SUCCESS_OK); response.setEntity("Logged Out", MediaType.ALL); CookieSetting cookie = new CookieSetting("u", ""); cookie.setPath("/"); response.getCookieSettings().add(cookie); } } else { ChallengeRequest cr = new ChallengeRequest(ChallengeScheme.HTTP_DIGEST, "PSCP"); response.setChallengeRequest(cr); response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED); } } }) .getTemplate(), URLS.Name.LOGOUT); return new HeaderFilter(getContext(), root); }
public Restlet createRoot() { if (myRouter == null) { myRouter = new Router() { @Override protected synchronized void init(Request request, Response response) { super.init(request, response); // set the page uri's // http://host:port/appName String baseURL = request.getRootRef().getParentRef().toString(); String rootPath = request.getRootRef().toString().substring(baseURL.length()); String pagePath = request.getResourceRef().toString().substring(baseURL.length()); String basePath = null; if (request.getResourceRef().getBaseRef() != null) { basePath = request.getResourceRef().getBaseRef().toString().substring(baseURL.length()); } // strip off the extension String extension = ResponseUtils.getExtension(pagePath); if (extension != null) { pagePath = pagePath.substring(0, pagePath.length() - extension.length() - 1); } // trim leading slash if (pagePath.endsWith("/")) { pagePath = pagePath.substring(0, pagePath.length() - 1); } // create a page info object and put it into a request attribute PageInfo pageInfo = new PageInfo(); pageInfo.setBaseURL(baseURL); pageInfo.setRootPath(rootPath); pageInfo.setBasePath(basePath); pageInfo.setPagePath(pagePath); pageInfo.setExtension(extension); request.getAttributes().put(PageInfo.KEY, pageInfo); for (DispatcherCallback callback : callbacks) { callback.init(request, response); } } @Override public Restlet getNext(Request request, Response response) { Restlet next = super.getNext(request, response); if (next != null) { for (DispatcherCallback callback : callbacks) { callback.dispatched(request, response, next); } } return next; }; @Override public void handle(Request request, Response response) { try { super.handle(request, response); } catch (Exception e) { // execute the exception callback for (DispatcherCallback callback : callbacks) { callback.exception(request, response, e); } if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException(e); } finally { // execute the finished callback for (DispatcherCallback callback : callbacks) { callback.finished(request, response); } } }; }; // load all the rest mappings and register them with the router Iterator i = GeoServerExtensions.extensions(RESTMapping.class).iterator(); while (i.hasNext()) { RESTMapping rm = (RESTMapping) i.next(); addRoutes(rm.getRoutes(), myRouter); } // create a root mapping myRouter.attach("", new IndexRestlet(myRouter)); } return myRouter; }