protected FileObject getChildLocation(String pathSegment) throws FileSystemException { FileObject child = getChildrenLocation().resolveFile(pathSegment); checkChildLocation(pathSegment, child); return child; }
/** * Visits a path segment * @param pathSegment the path segment * @return a sub resource (which can possibly continue dispatch if it is itself an Resource) or null. */ @Path("{pathSegment}") public Object consumePathSegment(@PathParam("pathSegment") String pathSegment) { try { FileObject child = getChildLocation(pathSegment); return consumePathSegment(pathSegment, child, getResourceResolver()); } catch (FileSystemException e) { logger.error("Could not access child: " + pathSegment, e); throw new WebApplicationException(404); } }
public Map<String, Object> describe() { Map<String, Object> description = new HashMap<>(); description.put("superclass", getClass().getSuperclass().getName()); description.put("class", getClass().getName()); description.put("path", getPath()); description.put("children", getSubResources()); describe(description); return description; }
public Object getSubResource(String subResourceName) throws Exception { FileObject subResourceLocation = getChildLocation(subResourceName); return getSubResource(subResourceLocation, subResourceName, getResourceResolver()); }
@Override public ResourcePermissions getPermissions() { try { Map permissions = getResourceResolver().resolve(location, "permissions", Map.class); if(permissions != null) { Map<String, List<String>> allow = (Map<String, List<String>>) permissions.get("allow"); Map<String, List<String>> deny = (Map<String, List<String>>) permissions.get("deny"); return new ResourcePermissions(this, translatePermissions(allow), translatePermissions(deny)); } else { return getEmptyResourcePermissions(); } } catch (Exception e) { logger.error("Could not load permissions for " + location, e); return getEmptyResourcePermissions(); } }
protected void checkChildLocation(String pathSegment, FileObject child) throws FileSystemException { if(child == null) { return; } if(!child.getParent().equals(getChildrenLocation())) { throw new IllegalArgumentException( "Path segment " + pathSegment + " results in a path that is not a child of the current node and " + "for security reasons this is forbidden."); } }
@Path(":description") @Produces(MediaType.APPLICATION_JSON) @GET public Map<String, Object> getJSONDescription() { return describe(); }
@Override public String getPath() { StringBuilder path = new StringBuilder(super.getPath()); for(String param : parameters) { path = path.append(param).append("/"); } return path.toString(); }
public Object getSubResource(FileObject resourceLocation, String segment, ResourceResolver resourceResolver) throws Exception { Class<?> subResourceClass = resourceResolver.resolve(resourceLocation, Class.class); if(subResourceClass == null) { logger.debug("Subresource could not be resolved"); return null; } else { return createSubResource(subResourceClass, resourceLocation, segment); } }
@Override public ResourceResolver getResourceResolver() { return getParent().getResourceResolver(); }
@Override @Path("{pathSegment}") public Object consumePathSegment(@PathParam("pathSegment") String pathSegment) { if(parameters.size() < minParameters) { consumeParameter(pathSegment); return this; } try { Object element = super.consumePathSegment(pathSegment); parametersAcquired(); return element; } catch (WebApplicationException e) { logger.debug("Invalid subresource: " + pathSegment, e); if(parameters.size() < maxParameters) { consumeParameter(pathSegment); if(parameters.size() == maxParameters) { parametersAcquired(); } return this; } else { throw new WebApplicationException("Too many path parameters", 404); } } }
@Override public Collection<String> getSubResources() { try { List<String> subResources = new ArrayList<>(); FileObject childrenLocation = getChildrenLocation(); if(childrenLocation == null || !childrenLocation.exists() || childrenLocation.getType() != FileType.FOLDER) { return subResources; } FileObject[] children = childrenLocation.getChildren(); for (FileObject child : children) { if (child.getType() == FileType.FOLDER) { subResources.add(child.getName().getBaseName()); } } return subResources; } catch (FileSystemException e) { throw new RuntimeException(e); } }