use of org.ovirt.api.metamodel.runtime.util.ListWithHref in project ovirt-engine-sdk-java by oVirt.
the class HttpConnection method followLink.
@Override
public <TYPE> TYPE followLink(TYPE object) {
if (!isLink(object)) {
throw new Error("Can't follow link because object don't have any");
}
String href = getHref(object);
if (href == null) {
throw new Error("Can't follow link because the 'href' attribute does't have a value");
}
try {
URL url = new URL(getUrl());
String prefix = url.getPath();
if (!prefix.endsWith("/")) {
prefix += "/";
}
if (!href.startsWith(prefix)) {
throw new Error("The URL '" + href + "' isn't compatible with the base URL of the connection");
}
// Get service based on path
String path = href.substring(prefix.length());
Service service = systemService().service(path);
// Obtain method which provides result object and invoke it:
Method get;
if (object instanceof ListWithHref) {
get = service.getClass().getMethod("list");
} else {
get = service.getClass().getMethod("get");
}
Object getRequest = get.invoke(service);
Method send = getRequest.getClass().getMethod("send");
send.setAccessible(true);
Object getResponse = send.invoke(getRequest);
Method obtainObject = getResponse.getClass().getDeclaredMethods()[0];
obtainObject.setAccessible(true);
return (TYPE) obtainObject.invoke(getResponse);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
throw new Error(String.format("Unexpected error while following link \"%1$s\"", href), ex);
} catch (MalformedURLException ex) {
throw new Error(String.format("Error while creating URL \"%1$s\"", getUrl()), ex);
}
}
Aggregations