Search in sources :

Example 1 with ListWithHref

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);
    }
}
Also used : ListWithHref(org.ovirt.api.metamodel.runtime.util.ListWithHref) MalformedURLException(java.net.MalformedURLException) Error(org.ovirt.engine.sdk4.Error) SystemService(org.ovirt.engine.sdk4.services.SystemService) Service(org.ovirt.engine.sdk4.Service) Method(java.lang.reflect.Method) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ListWithHref (org.ovirt.api.metamodel.runtime.util.ListWithHref)1 Error (org.ovirt.engine.sdk4.Error)1 Service (org.ovirt.engine.sdk4.Service)1 SystemService (org.ovirt.engine.sdk4.services.SystemService)1