protected void visitSubResources(Resource resource) throws FileSystemException { for(String subResourceName : resource.getSubResources()) { try { Object element = resource.getSubResource(subResourceName); if (element instanceof AbstractResource) { visit((AbstractResource) element); } } catch (Exception e) { logger.error("Could not visit resource " + resource.getLocation().getURL(), e); } } }
public String getPath() { return parent.getPath() + getSegment() + "/"; }
protected Object initSubResource(Object resource, FileObject location, String segment) { if(resource instanceof Resource) { Resource subResource = (Resource) resource; subResource.setParent(this); subResource.setLocation(location); subResource.setSegment(segment); initGenericResource(resource); initSubResource(subResource); return subResource.init(); } else { initGenericResource(resource); return resource; } }
@Override public void afterScan(Reader reader, Swagger swagger) { final SubResourceReader subResourceReader = getSubResourceReader(reader); try { //TODO actions should be put in a special "inspection mode" to avoid checks (e.g. not-in-use-case), //hitting the DB or services, etc. Resource root = rootFactory.createRoot(); root.setResourceContext(getResourceContext()); new DepthFirstVisitor((ResourceVisitor) node -> { try { subResourceReader.readSubResource(node); } catch (Exception e) { logger.error("Could not read node at " + node.getLocation(), e); } }).visit(root); } catch (Exception e) { logger.error(e.getMessage(), e); } }
public Resource getResource(String actionPath) { Resource resource = getRoot(); String[] pathSegments = actionPath.split("/"); for(String segment : pathSegments) { if(resource instanceof WithParameters) { WithParameters withParameters = (WithParameters) resource; if(withParameters.getParameters().size() < withParameters.getMinParameters()) { withParameters.consumeParameter(segment); continue; } } Object subResource = null; try { subResource = resource.getSubResource(segment); } catch (Exception e) { logger.debug("Could not load resource", e); } if(subResource instanceof Resource) { resource = (Resource) subResource; } else if(resource instanceof WithParameters) { WithParameters withParameters = (WithParameters) resource; if(withParameters.getParameters().size() < withParameters.getMaxParameters()) { withParameters.consumeParameter(segment); } } else { return null; } } return resource; }
@Override public ResourceResolver getResourceResolver() { return getParent().getResourceResolver(); }
@Path("") public Object start() throws Exception { Resource root = rootFactory.createRoot(); resourceContext.initResource(root); return root.init(); }
public Swagger readSubResource(Resource resource) { String path = resource.getPath(); ArrayList<Parameter> parameters = new ArrayList<>(); int paramCount = 0; while (path.contains("{requiredPathParameter}")) { String name = "requiredPathParameter_" + paramCount; path = path.replaceFirst("\\{requiredPathParameter}", "{" + name + "}"); PathParameter parameter = new PathParameter(); parameter.setName(name); parameter.setRequired(true); parameters.add(parameter); paramCount++; } paramCount = 0; while (path.contains("{optionalPathParameter}")) { String name = "optionalPathParameter_" + paramCount; path = path.replaceFirst("\\{optionalPathParameter}", "{" + name + "}"); PathParameter parameter = new PathParameter(); parameter.setName(name); parameter.setRequired(false); parameters.add(parameter); paramCount++; } return read(resource.getClass(), path, null, true, new String[0], new String[0], new HashMap<>(), parameters); } }