Search in sources :

Example 6 with Current

use of org.ovirt.engine.api.restapi.invocation.Current in project ovirt-engine by oVirt.

the class UsageFinderTest method init.

@Before
public void init() {
    Current current = new Current();
    current.setRoot("http://localhost:8080");
    current.setPrefix("/ovirt-engine/api");
    CurrentManager.put(current);
    usageFinder = new UsageFinder();
}
Also used : Current(org.ovirt.engine.api.restapi.invocation.Current) Before(org.junit.Before)

Example 7 with Current

use of org.ovirt.engine.api.restapi.invocation.Current in project ovirt-engine by oVirt.

the class LinkHelper method addActions.

/**
 * Adds the set of action links for an object
 *
 * @param model the object to add actions to
 * @param collection the object to get implemented methods from
 */
public static <R extends ActionableResource> void addActions(R model, Object collection) {
    Current current = CurrentManager.get();
    String base = current.getPrefix() + current.getPath();
    if (base != null) {
        ActionsBuilder actionsBuilder = new ActionsBuilder(base, model.getClass(), collection.getClass());
        model.setActions(actionsBuilder.build());
    }
}
Also used : Current(org.ovirt.engine.api.restapi.invocation.Current)

Example 8 with Current

use of org.ovirt.engine.api.restapi.invocation.Current in project ovirt-engine by oVirt.

the class ParametersHelper method setParameter.

/**
 * Set a parameter in the parameters map.
 */
public static void setParameter(String name, String value) {
    Current current = CurrentManager.get();
    if (current != null) {
        Map<String, String> parameters = current.getParameters();
        parameters.put(name, value);
    }
}
Also used : Current(org.ovirt.engine.api.restapi.invocation.Current)

Example 9 with Current

use of org.ovirt.engine.api.restapi.invocation.Current in project ovirt-engine by oVirt.

the class RsdlLoader method loadRsdl.

/**
 * Loads the RSDL object appropriate for the current request and converts it to the corresponding Java object. Note
 * that the RSDL object will be of different classes in V3 and V4 of the API, that is why the class is passed as
 * a parameter, to be able to use this method for both V3 and V4.
 *
 * @param clazz the class of the RSDL object
 * @param <RSDL> the type of the RSDL object
 */
public static <RSDL> RSDL loadRsdl(Class<RSDL> clazz) throws IOException {
    // Decide what version of the RSDL document to load:
    Current current = CurrentManager.get();
    String fileName = current.getApplicationMode() == ApplicationMode.GlusterOnly ? "rsdl_gluster.xml" : "rsdl.xml";
    String resourcePath = String.format("/v%s/%s", current.getVersion(), fileName);
    // Calculate the prefix that will be used in the "href" attributes:
    String prefix = current.getAbsolutePath();
    // Load the RSDL document into a DOM tree and then modify all the "href" attributes to include the prefix that
    // has been previously calculated:
    Document document;
    try {
        DocumentBuilder parser = SecureDocumentBuilderFactory.newDocumentBuilderFactory().newDocumentBuilder();
        try (InputStream in = RsdlIOManager.class.getResourceAsStream(resourcePath)) {
            document = parser.parse(in);
        }
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate("//@href", document, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            String href = node.getNodeValue();
            if (href.startsWith("?")) {
                href = prefix + href;
            } else {
                href = prefix + "/" + href;
            }
            node.setNodeValue(href);
        }
    } catch (Exception exception) {
        throw new IOException(exception);
    }
    // Create the RSDL object:
    return JAXB.unmarshal(new DOMSource(document), clazz);
}
Also used : XPath(javax.xml.xpath.XPath) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) Current(org.ovirt.engine.api.restapi.invocation.Current) IOException(java.io.IOException)

Example 10 with Current

use of org.ovirt.engine.api.restapi.invocation.Current in project ovirt-engine by oVirt.

the class V3UsageFinder method isMatch.

private boolean isMatch(V3DetailedLink link, UriInfo uriInfo, String httpMethod) {
    int baseUriLength = uriInfo.getBaseUri().getPath().length();
    // e.g: [vms, {vm:id}, start]
    Current current = CurrentManager.get();
    int charsToTruncate = current.getVersionSource() == VersionSource.URL ? 0 : current.getVersion().length() + 2;
    String[] linkPathSegments = link.getHref().substring(baseUriLength - charsToTruncate).split("/");
    // e.g: [vms, f26b0918-8e16-4915-b1c2-7f39e568de23, start]
    List<PathSegment> uriPathSegments = uriInfo.getPathSegments();
    return isMatchLength(linkPathSegments, uriPathSegments) && isMatchPath(linkPathSegments, uriPathSegments) && isMatchRel(link.getRel(), httpMethod);
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) Current(org.ovirt.engine.api.restapi.invocation.Current)

Aggregations

Current (org.ovirt.engine.api.restapi.invocation.Current)14 Before (org.junit.Before)4 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DOMSource (javax.xml.transform.dom.DOMSource)2 XPath (javax.xml.xpath.XPath)2 MessageBundle (org.ovirt.engine.api.restapi.logging.MessageBundle)2 DbUser (org.ovirt.engine.core.common.businessentities.aaa.DbUser)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 IOException (java.io.IOException)1 URI (java.net.URI)1 Locale (java.util.Locale)1 GET (javax.ws.rs.GET)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 PathSegment (javax.ws.rs.core.PathSegment)1 UriInfo (javax.ws.rs.core.UriInfo)1