It's all about the answers!

Ask a question

Oslc - get all project area names and url


John Marten (112) | asked Jul 21 '16, 2:17 a.m.
Hi All,

i have read the "Open Services for Lifecycle Collaboration Workshop" and used the examples for my own project, but now I have one more question. Beside to get all Names of the project areas, i need in addition the url from the project area (e.g. via REST Client: https://localhost:9443/ccm/process/project-areas/_ZOfGcD3yEea3-J04vnQkIQ)

My actual Steps are:
....
1) Request the Root Services document
...
2) Define the XPath evaluation environment

            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            xpath.setNamespaceContext(
                    new NamespaceContextMap(new String[]
                            {    "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                                "oslc_cm","http://open-services.net/xmlns/cm/1.0/"}));

            String catalogXPath = "/rdf:Description/oslc_cm:cmServiceProviders/@rdf:resource";
            String serviceProviderTitleXPath = "//oslc:ServiceProvider/dcterms:title";

3) Parse the response body to retrieve the catalog URI
....
4) Setup the catalog request
...
5) Access to the Service Providers catalog
...
6) Define the XPath evaluation environment
...

7) Parse the response body to retrieve the Service Provider

source = new InputSource(catalogResponse.getEntity().getContent());
NodeList titleNodes = (NodeList) (xpath2.evaluate(serviceProviderTitleXPath, source, XPathConstants.NODESET));
              
     // Print out the title of each Service Provider
                int length = titleNodes.getLength();
                System.out.println(">> Project Areas:");
                for (int i = 0; i < length; i++) {
                    System.out.println(">> \t - "+ titleNodes.item(i).getTextContent());
                    projectAreaNamesList.add(titleNodes.item(i).getTextContent());
                }

How can i get in addition to the names the URLs from the project area (from the "catalogResponse")? Is that possible to add them? i think i have to define it somewhere in Step 2, "String serviceProviderTitleXPath = "//oslc:ServiceProvider/dcterms:title";  "  ??

Best Regards!

One answer



permanent link
Donald Nong (14.5k414) | answered Jul 21 '16, 5:34 a.m.
You just need to step back a bit, as you seem to try to retrieve two "strings" from one "Node" at step 7, which makes it no longer a simple "string". You should parse the node to a predefined class (holding the structure of <oslc:ServiceProvider>), and of course your XPath should point to the <oslc:ServiceProvider>, not the <dcterms:title> within it. Since the <oslc:ServiceProvider> node is well-structured in XML, you can use XPath against it as well, if you don't want to define a class for it.

Comments
John Marten commented Jul 21 '16, 10:04 a.m. | edited Jul 21 '16, 9:19 p.m.

Thank your so much for you fast answer.
What exactly do you mean with "you can use XPath against it as well" ?

i tried the last hours an example from: https://github.com/eclipse/lyo.testsuite/blob/master/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/server/oslcv1tests/ServiceProviderCatalogTests.java

Is that the right Approach?

public void detailsElementsHaveValidResourceAttribute() throws IOException, XPathException
{

//Get all details elements
NodeList detailsElements = (NodeList) OSLCUtils.getXPath().evaluate("//oslc_disc:details", doc, XPathConstants.NODESET);

//Get all resource attributes of the details elements
NodeList resources = (NodeList) OSLCUtils.getXPath().evaluate("//oslc_disc:details/@rdf:resource", doc, XPathConstants.NODESET);

//Make sure they match up 1-to-1
assertTrue(detailsElements.getLength() == resources.getLength());

//Verify that the resource has a url
for (int i = 0; i < resources.getLength(); i++)
{
String url = resources.item(i).getNodeValue();
assertNotNull(url);
}
}

But it doesn't work for me.


Donald Nong commented Jul 21 '16, 10:08 p.m.

I really don't like the line "it does not work for me" in a technical discussion, as it does not give any details of the issue in hand.

You have to be careful of what is the "source" when using XPath, that is, what XML content you're going to parse. You don't blindly construct an XPath. It should be constructed so that you can get what you "expect" to get based on your knowledge about the XML structure. I suggest you use XPath "//oslc:serviceProvider", "//dcterms:title" and "//oslc:details" against the catalog response and see what you get. The result may be different from what you expect.

Bottom line is, if you want to get "dcterms:title" and "oslc:details" at the same time for any particular serviceProvider, you have to get the entire <oslc:serviceProvider> block as a node.

Your answer


Register or to post your answer.