@Override public Restlet createInboundRoot() { Router router = new Router(getContext()); router.attachDefault(UserBlackListCollectionResource.class); router.attach("/{userId}", UserBlackListResource.class); Filter filter = new NotifierFilter(getContext()); filter.setNext(router); return filter; }
@Override protected int doHandle(Request request, Response response) { super.doHandle(request, response); if (response.getStatus().isSuccess() && request.getMethod().equals(Method.GET)) { boolean isHtml = false; for (Preference<MediaType> mt : request.getClientInfo().getAcceptedMediaTypes()) { if (mt.getMetadata().includes(MediaType.APPLICATION_XHTML) || mt.getMetadata().includes(MediaType.TEXT_HTML)) { isHtml = true; break; } } if (isHtml) { try { response.setEntity(toHtml(request, response)); } catch (SlipStreamException e) { // ok it failed generating html... do we care? } } } return CONTINUE; }
@Override protected void afterHandle(Request request, Response response) { if (!Method.OPTIONS.equals(request.getMethod())) { Series<Header> requestHeaders = (Series<Header>) request.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS); String origin = requestHeaders.getFirstValue("Origin", false, "*"); String rh = requestHeaders.getFirstValue("Access-Control-Request-Headers", false, "*"); Series<Header> responseHeaders = (Series<Header>) response.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS); if (responseHeaders == null) { responseHeaders = new Series<Header>(Header.class); } responseHeaders.add("Access-Control-Allow-Origin", origin); responseHeaders.add("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); responseHeaders.set("Access-Control-Expose-Headers", "Authorization, Link"); responseHeaders.add("Access-Control-Allow-Headers", rh); responseHeaders.add("Access-Control-Allow-Credentials", "true"); responseHeaders.add("Access-Control-Max-Age", "60"); response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders); } super.afterHandle(request, response); }
@Get @Override public Representation get() { try { // Info JSONObject info = new JSONObject(); info.put("title", "Pinot Controller"); info.put("version", "0.1"); // Paths JSONObject paths = new JSONObject(); Router router = ControllerRestApplication.router; RouteList routeList = router.getRoutes(); for (Route route : routeList) { if (route instanceof TemplateRoute) { TemplateRoute templateRoute = (TemplateRoute) route; JSONObject pathObject = new JSONObject(); String routePath = templateRoute.getTemplate().getPattern(); // Check which methods are present Restlet routeTarget = templateRoute.getNext(); if (routeTarget instanceof Finder) { Finder finder = (Finder) routeTarget; generateSwaggerForFinder(pathObject, routePath, finder); } else if (routeTarget instanceof Filter) { do { Filter filter = (Filter) routeTarget; routeTarget = filter.getNext(); } while (routeTarget instanceof Filter); if (routeTarget instanceof Finder) { Finder finder = (Finder) routeTarget; generateSwaggerForFinder(pathObject, routePath, finder); } } if (pathObject.keys().hasNext()) { paths.put(routePath, pathObject); } } } // Tags JSONArray tags = new JSONArray(); addTag(tags, "tenant", "Tenant-related operations"); addTag(tags, "instance", "Instance-related operations"); addTag(tags, "table", "Table-related operations"); addTag(tags, "segment", "Segment-related operations"); addTag(tags, "schema", "Schema-related operations"); // Swagger JSONObject swagger = new JSONObject(); swagger.put("swagger", "2.0"); swagger.put("info", info); swagger.put("paths", paths); swagger.put("tags", tags); StringRepresentation representation = new StringRepresentation(swagger.toString()); // Set up CORS Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes().get("org.restlet.http.headers"); if (responseHeaders == null) { responseHeaders = new Series(Header.class); getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); } responseHeaders.add(new Header("Access-Control-Allow-Origin", "*")); return representation; } catch (JSONException e) { return new StringRepresentation(e.toString()); } }