public static Cluster addLinks(UriInfo uriInfo, PowerShellPool pool, PowerShellParser parser, Cluster cluster) { cluster = querySupportedVersions(pool, parser, cluster); Link link = new Link(); link.setRel("networks"); link.setHref(LinkHelper.getUriBuilder(uriInfo, cluster).path("networks").build().toString()); cluster.getLinks().clear(); cluster.getLinks().add(link); return LinkHelper.addLinks(uriInfo, cluster); }
private String getPath(UriInfo uriInfo) { StringBuilder path = new StringBuilder(); // avoid encoding forward slashes to keep URI looking consistent for (String p : uriInfo.getPath().split("/")) { (path.length() == 0 ? path : path.append("/")).append(URLEncoder.encode(p)); } return combine(uriInfo.getBaseUri().getPath(), path.toString()); } }
private static boolean isPluralResourceGetter(String method, String type) { method = method.toLowerCase(); type = type.toLowerCase(); method = chopStart(method, "get"); method = chopEnd(method, "resource"); method = chopEnd(method, "s"); if (type.endsWith("y")) { method = chopEnd(method, "ie"); type = chopEnd(type, "y"); } return method.contains(type); }
public static <R extends BaseResource> R addLinks(UriInfo uriInfo, R model, Class<? extends BaseResource> suggestedParentType) { setHref(uriInfo, model, suggestedParentType); setActions(uriInfo, model, suggestedParentType); for (BaseResource inline : getInlineResources(model)) { if (inline.getId() != null) { setHref(uriInfo, inline); } for (BaseResource grandParent : getInlineResources(inline)) { unsetInlineResource(inline, grandParent.getClass()); } } return model; }
/** * Set the href attribute on the object (and its inline objects) * and construct its set of action links * * @param uriInfo the URI info * @param model the object * @param suggestedParentType the suggested parent type * @return the object, with href attributes and action links */ public static <R extends BaseResource> R addLinks(UriInfo uriInfo, R model) { return addLinks(uriInfo, model, null); }
/** * Create a #UriBuilder which encapsulates the path to an object * * i.e. for a VM tag, return a UriBuilder which encapsulates * '/rhevm-api/vms/{vm_id}/tags/{tag_id}' * * @param uriInfo the URI info * @param model the object * @return the #UriBuilder encapsulating the object's path */ public static <R extends BaseResource> UriBuilder getUriBuilder(UriInfo uriInfo, R model) { return getUriBuilder(uriInfo, model, null); }
Collection collection = getCollection(model, suggestedParentType); if (collection == null) { return null; BaseResource parent = getParentModel(model, collection.getParentType()); Collection parentCollection = getCollection(parent, suggestedParentType); String path = getPath(collection.getCollectionType(), parentCollection.getResourceType(), model.getClass()); uriBuilder = getUriBuilder(uriInfo, parent).path(path); } else { String path = getPath(collection.getCollectionType()); uriBuilder = uriInfo != null ? UriBuilder.fromPath(uriInfo.getBaseUri().getPath()).path(path)
/** * Adds searchable links to resource * * @param url to append to * @param resource to add links to * @param rel link ro add * @param flags used to specify different link options */ public static void addLink(String url, BaseResource resource, String rel, LinkFlags flags) { addLink(url, resource, rel, flags, new HashMap<String, String>()); }
/** * Adds searchable links to resource * * @param url to append to * @param resource to add links to * @param rel link to add * @param flags used to specify different link options * @param params the URL params to append */ public static void addLink(String url, BaseResource resource, String rel, LinkFlags flags, Map<String, String> params) { Link link = new Link(); link.setRel(rel); link.setHref(combine(url, rel)); resource.getLinks().add(link); if (flags == LinkFlags.SEARCHABLE) { addLink(url, resource, rel, params); } }
/** * Construct the set of action links for an object * * @param uriInfo the URI info * @param model the object * @param suggestedParentType the suggested parent type * @return the object, including its set of action links */ private static <R extends BaseResource> void setActions(UriInfo uriInfo, R model, Class<? extends BaseResource> suggestedParentType) { Collection collection = getCollection(model); UriBuilder uriBuilder = getUriBuilder(uriInfo, model, suggestedParentType); if (uriBuilder != null) { ActionsBuilder actionsBuilder = new ActionsBuilder(uriBuilder, collection.getResourceType()); model.setActions(actionsBuilder.build()); } }
/** * Lookup the #Collection instance which represents this object * * i.e. for a VM tag (i.e. a Tag object which its VM property set) * return the #Collection instance which encapsulates AssignedTagResource, * AssignedTagsResource and VM. * * @param model the object to query for * @return the #Collection instance representing the object's collection */ private static Collection getCollection(BaseResource model) { return getCollection(model, null); }
/** * Return any parent object set on @model * * i.e. return the value of any bean property whose type matches @parentType * * @param model object to check * @param parentType the type of the parent * @return the parent object, or null if not set */ private static <R extends BaseResource> BaseResource getParentModel(R model, Class<?> parentType) { for (BaseResource inline : getInlineResources(model)) { if (parentType.isAssignableFrom(inline.getClass())) { return inline; } } return null; }
/** * Lookup the #Collection instance which represents this object * * i.e. for a VM tag (i.e. a Tag object which its VM property set) * return the #Collection instance which encapsulates AssignedTagResource, * AssignedTagsResource and VM. * * @param model the object to query for * @param suggestedParentType the suggested parent type * @return the #Collection instance representing the object's collection */ private static Collection getCollection(BaseResource model, Class<? extends BaseResource> suggestedParentType) { ParentToCollectionMap collections = TYPES.get(model.getClass()); if (suggestedParentType != null) { for (Class<? extends BaseResource> parentType : collections.keySet()) { if (parentType.equals(suggestedParentType)) { return collections.get(parentType); } } } for (Class<? extends BaseResource> parentType : collections.keySet()) { if (parentType != NO_PARENT && getParentModel(model, parentType) != null) { return collections.get(parentType); } } return collections.get(NO_PARENT); }
@Override public CdRom addLinks(CdRom cdrom) { return LinkHelper.addLinks(getUriInfo(), cdrom); }
/** * Set the href attribute on the supplied object * * e.g. set href = '/rhevm-api/vms/{vm_id}/tags/{tag_id}' on a VM tag * * @param uriInfo the URI info * @param model the object * @param suggestedParentType the suggested parent type * @return the model, with the href attribute set */ private static <R extends BaseResource> void setHref(UriInfo uriInfo, R model, Class<? extends BaseResource> suggestedParentType) { UriBuilder uriBuilder = getUriBuilder(uriInfo, model, suggestedParentType); if (uriBuilder != null) { model.setHref(uriBuilder.build().toString()); } }
/** * Appends searchable links to resource's Href * * @param url to append to * @param resource to add links to * @param rel link ro add * @param flags used to specify different link options */ public static void addLink(BaseResource resource, String rel, LinkFlags flags) { addLink(resource.getHref(), resource, rel, flags); }
Event lookupEvent(String id) { StringBuilder buf = new StringBuilder(); buf.append("select-event"); buf.append(" | ? "); buf.append("{ $_.id -eq "); buf.append(PowerShellUtils.escape(id)); buf.append(" }"); return LinkHelper.addLinks(getUriInfo(), runAndParseSingle(buf.toString())); } }
public static Template addLinks(UriInfo uriInfo, PowerShellTemplate template) { Template ret = JAXBHelper.clone("template", Template.class, template); String [] deviceCollections = { "cdroms", "disks", "nics" }; ret.getLinks().clear(); for (String collection : deviceCollections) { Link link = new Link(); link.setRel(collection); link.setHref(LinkHelper.getUriBuilder(uriInfo, ret).path(collection).build().toString()); ret.getLinks().add(link); } return LinkHelper.addLinks(uriInfo, ret); }
private Disk addStatus(UriInfo uriInfo, Disk disk, String taskIds) { if (taskIds != null) { Link link = new Link(); link.setRel(CREATION_STATUS); link.setHref(LinkHelper.getUriBuilder(uriInfo, disk).path(CREATION_STATUS).path(taskIds).build().toString()); disk.getLinks().add(link); } return disk; } }