| author | Sean Kennedy | 2012-08-21 14:09:08 (EDT) |
|---|---|---|
| committer | Sean Kennedy | 2012-08-21 14:09:08 (EDT) |
| commit | e908ce8b8bc5124c29960474387b23e9327b5274 (patch) (side-by-side diff) | |
| tree | c0f87179ffc3d15a0f402d48ddf0d4d40a9758d0 | |
| parent | 14fff6516a59ef564e18f574d46411902887a3d5 (diff) | |
| download | org.eclipse.lyo.client-e908ce8b8bc5124c29960474387b23e9327b5274.zip org.eclipse.lyo.client-e908ce8b8bc5124c29960474387b23e9327b5274.tar.gz org.eclipse.lyo.client-e908ce8b8bc5124c29960474387b23e9327b5274.tar.bz2 | |
Bug 387414 - adding paging supportrefs/changes/29/7329/1
Created OslcQuery and OslcQueryResult classes.
Change-Id: Ic85b268649da5ee10eb10e35d2add6d20131f70a
6 files changed, 315 insertions, 19 deletions
diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcClient.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcClient.java index 95e513f..8b30636 100644 --- a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcClient.java +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcClient.java @@ -44,6 +44,7 @@ import org.apache.wink.client.ApacheHttpClientConfig; import org.apache.wink.client.ClientConfig; import org.apache.wink.client.ClientResponse; import org.apache.wink.client.RestClient; +import org.eclipse.lyo.client.oslc.resources.OslcQuery; import org.eclipse.lyo.oslc4j.provider.jena.JenaProvidersRegistry; import org.eclipse.lyo.oslc4j.provider.json4j.Json4JProvidersRegistry; @@ -51,6 +52,7 @@ import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.Selector; import com.hp.hpl.jena.rdf.model.SimpleSelector; import com.hp.hpl.jena.rdf.model.Statement; @@ -113,13 +115,19 @@ public class OslcClient { * @throws URISyntaxException */ - public ClientResponse getResource(String url, String mediaType) throws IOException, OAuthException, URISyntaxException { + public ClientResponse getResponse(String url, String mediaType) throws IOException, OAuthException, URISyntaxException { RestClient restClient = new RestClient(clientConfig); return restClient.resource(url).accept(mediaType).header("Oslc-Core-Version","2.0").get(); } + public org.apache.wink.client.Resource getRemoteResource(OslcQuery query) { + RestClient restClient = new RestClient(clientConfig); + org.apache.wink.client.Resource resource = restClient.resource(query.getCapabilityUrl()); + return resource; + } + public class OAuthHttpPool implements HttpClientPool { public HttpClient getHttpClient(URL url) { return httpClient; @@ -139,7 +147,7 @@ public class OslcClient { public String lookupServiceProviderUrl(String catalogUrl, String serviceProviderTitle) throws IOException, OAuthException, URISyntaxException { String retval = null; - ClientResponse response = getResource(catalogUrl,"application/rdf+xml"); + ClientResponse response = getResponse(catalogUrl,"application/rdf+xml"); Model rdfModel = ModelFactory.createDefaultModel(); rdfModel.read(response.getEntity(InputStream.class),catalogUrl); @@ -176,7 +184,7 @@ public class OslcClient { public String lookupQueryCapability(String serviceProviderUrl, String oslcDomain) throws IOException, OAuthException, URISyntaxException { String retval = null; - ClientResponse response = getResource(serviceProviderUrl,"application/rdf+xml"); + ClientResponse response = getResponse(serviceProviderUrl,"application/rdf+xml"); Model rdfModel = ModelFactory.createDefaultModel(); rdfModel.read(response.getEntity(InputStream.class),serviceProviderUrl); diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcOAuthClient.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcOAuthClient.java index 10612a9..4f9c7ab 100644 --- a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcOAuthClient.java +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/OslcOAuthClient.java @@ -67,7 +67,7 @@ public class OslcOAuthClient extends OslcClient { * Get an OAuth protected OSLC resource */ @Override - public ClientResponse getResource(String url, String mediaType) throws IOException, OAuthException, URISyntaxException + public ClientResponse getResponse(String url, String mediaType) throws IOException, OAuthException, URISyntaxException { OAuthMessage message = getResourceInternal(url, HttpMethod.GET, false); diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/jazz/JazzRootServicesHelper.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/jazz/JazzRootServicesHelper.java index a90dc73..299fbe5 100644 --- a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/jazz/JazzRootServicesHelper.java +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/jazz/JazzRootServicesHelper.java @@ -85,7 +85,7 @@ public class JazzRootServicesHelper { { try { OslcClient rootServicesClient = new OslcClient(); - ClientResponse response = rootServicesClient.getResource(rootServicesUrl, "application/rdf+xml"); + ClientResponse response = rootServicesClient.getResponse(rootServicesUrl, "application/rdf+xml"); InputStream is = response.getEntity(InputStream.class); Model rdfModel = ModelFactory.createDefaultModel(); rdfModel.read(is,rootServicesUrl); diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQuery.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQuery.java new file mode 100644 index 0000000..e89720f --- a/dev/null +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQuery.java @@ -0,0 +1,130 @@ +/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Sean Kennedy - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.client.oslc.resources;
+
+import org.apache.wink.client.ClientResponse;
+import org.apache.wink.client.Resource;
+import org.eclipse.lyo.client.oslc.OslcClient;
+
+/**
+ * Represents an OSLC query (HTTP GET) request to be made of a remote system.
+ *
+ * Immutable.
+ */
+//TODO: Support additional (possibly arbitrary) query parameters
+// if we need to allow arbitrary query parameters,
+// or have too many possible parameters for ease of use in constructors,
+// decide on some way to allow users to build the parameter list with
+// multiple statements.
+// Possible solution: contained static class that has all available parameters
+// (perhaps with support for arbitrary parameters) that is passed on the
+// constructor and held as a final variable.
+public class OslcQuery {
+
+ private final OslcClient oslcClient;
+
+ private final String capabilityUrl;
+
+ private String queryUrl;
+
+ private final int pageSize;
+
+ private final Resource queryResource;
+
+ /**
+ * Create an OSLC query that uses the remote system's default page size.
+ *
+ * @param oslcClient the authenticated OSLC client
+ * @param capabilityUrl the URL that is the base
+ */
+ public OslcQuery(OslcClient oslcClient, String capabilityUrl) {
+ this(oslcClient, capabilityUrl, 0);
+ }
+
+ /**
+ * Create an OSLC query that uses the given page size
+ *
+ * @param oslcClient the authenticated OSLC client
+ * @param capabilityUrl the URL that is the base
+ * @param pageSize the number of results to include on each page (OslcQueryResult)
+ */
+ public OslcQuery(OslcClient oslcClient, String capabilityUrl, int pageSize) {
+ this.oslcClient = oslcClient;
+ this.capabilityUrl = capabilityUrl;
+ this.pageSize = (pageSize < 1) ? 0 : pageSize;
+ this.queryResource = createQueryResource();
+ this.queryUrl = null;
+ }
+
+ OslcQuery(OslcQueryResult previousResult) {
+ this(previousResult.getQuery(), previousResult.getNextPageUrl());
+ }
+
+ private OslcQuery(OslcQuery previousQuery, String nextPageUrl) {
+ this(previousQuery.oslcClient, previousQuery.capabilityUrl, previousQuery.pageSize);
+ this.queryUrl = nextPageUrl;
+ this.queryResource.uri(nextPageUrl);
+ }
+
+ private Resource createQueryResource() {
+ Resource resource = oslcClient.getRemoteResource(this);
+ resource.accept("application/rdf+xml");
+ resource.header("Oslc-Core-Version","2.0");
+ applyPagination(resource);
+ return resource;
+ }
+
+ private void applyPagination(Resource resource) {
+ if (pageSize > 0) {
+ resource.queryParam("oslc.paging", "true");
+ resource.queryParam("oslc.pageSize", pageSize);
+ }
+ }
+
+ /**
+ * @return the number of entries to return for each page,
+ * if zero, the remote system's (or full query's) default is used
+ */
+ public int getPageSize() {
+ return pageSize;
+ }
+
+ /**
+ * @return the base query capability URL
+ */
+ public String getCapabilityUrl() {
+ return capabilityUrl;
+ }
+
+ /**
+ * @return the complete query URL
+ */
+ public String getQueryUrl() {
+ if (queryUrl == null) {
+ queryUrl = queryResource.getUriBuilder().build(new Object[0]).toString();
+ }
+ return queryUrl;
+ }
+
+ public OslcQueryResult submit() {
+ return new OslcQueryResult(this, getResponse());
+ }
+
+ ClientResponse getResponse() {
+ return queryResource.get();
+ }
+
+}
diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java new file mode 100644 index 0000000..c34f8b6 --- a/dev/null +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java @@ -0,0 +1,137 @@ +/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Sean Kennedy - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.client.oslc.resources;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import org.apache.wink.client.ClientResponse;
+import org.eclipse.lyo.client.oslc.OSLCConstants;
+import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Selector;
+import com.hp.hpl.jena.rdf.model.SimpleSelector;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.StmtIterator;
+
+
+/**
+ * The results of an OSLC query. If the query was paged, subsequent pages can be retrieved using the Iterator interface.
+ */
+public class OslcQueryResult implements Iterator<OslcQueryResult> {
+
+ private final OslcQuery query;
+
+ private final ClientResponse response;
+
+ private final int pageNumber;
+
+ private Model rdfModel;
+
+ private Resource infoResource, membersResource;
+
+ private String nextPageUrl;
+
+ public OslcQueryResult(OslcQuery query, ClientResponse response) {
+ this.query = query;
+ this.response = response;
+
+ this.pageNumber = 1;
+
+ initializeRdf();
+ }
+
+ private OslcQueryResult(OslcQueryResult prev) {
+ this.query = new OslcQuery(prev);
+ this.response = this.query.getResponse();
+
+ this.pageNumber = prev.pageNumber + 1;
+
+ initializeRdf();
+ }
+
+ private void initializeRdf() {
+ rdfModel = ModelFactory.createDefaultModel();
+ rdfModel.read(response.getEntity(InputStream.class), query.getCapabilityUrl());
+
+ infoResource = rdfModel.getResource(query.getQueryUrl());
+ membersResource = rdfModel.getResource(query.getCapabilityUrl());
+ }
+
+ String getNextPageUrl() {
+ if (nextPageUrl == null) {
+ Property predicate = rdfModel.getProperty(OslcConstants.OSLC_CORE_NAMESPACE, "nextPage");
+ Selector select = new SimpleSelector(infoResource, predicate, (RDFNode) null);
+ StmtIterator iter = rdfModel.listStatements(select);
+ if (iter.hasNext()) {
+ Statement nextPage = iter.next();
+ nextPageUrl = nextPage.getResource().getURI();
+ } else {
+ nextPageUrl = "";
+ }
+ }
+ return nextPageUrl;
+ }
+
+ /**
+ * @return whether there is another page of results after this
+ */
+ public boolean hasNext() {
+ return (!"".equals(getNextPageUrl()));
+ }
+
+ /**
+ * @return the next page of results
+ * @throws NoSuchElementException if there is no next page
+ */
+ public OslcQueryResult next() {
+ return new OslcQueryResult(this);
+ }
+
+ /**
+ * @throws UnsupportedOperationException always
+ */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ public OslcQuery getQuery() {
+ return query;
+ }
+
+ public String[] getMembersUrls() {
+ ArrayList<String> membersUrls = new ArrayList<String>();
+ Property predicate = rdfModel.getProperty(OSLCConstants.RDFS, "member");
+ Selector select = new SimpleSelector(membersResource, predicate, (RDFNode) null);
+ StmtIterator iter = rdfModel.listStatements(select);
+ while (iter.hasNext()) {
+ Statement member = iter.next();
+ try {
+ membersUrls.add(member.getResource().getURI());
+ } catch (Throwable t) {
+ //FIXME
+ System.err.println("Member was not a resource");
+ }
+ }
+ return membersUrls.toArray(new String[membersUrls.size()]);
+ }
+}
diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/JazzFormSample.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/JazzFormSample.java index 00a6e73..d8fb442 100644 --- a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/JazzFormSample.java +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/JazzFormSample.java @@ -30,6 +30,8 @@ import org.eclipse.lyo.client.oslc.OSLCConstants; import org.eclipse.lyo.client.oslc.jazz.JazzFormAuthClient; import org.eclipse.lyo.client.oslc.jazz.JazzRootServicesHelper; import org.eclipse.lyo.client.oslc.resources.ChangeRequest; +import org.eclipse.lyo.client.oslc.resources.OslcQuery; +import org.eclipse.lyo.client.oslc.resources.OslcQueryResult; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.GnuParser; @@ -82,13 +84,33 @@ public class JazzFormSample { //Get the Query Capabilities URL so that we can run some OSLC queries String queryCapability = client.lookupQueryCapability(serviceProviderUrl,OSLCConstants.OSLC_CM_V2); + OslcQuery query = new OslcQuery(client, queryCapability, 5); + System.out.println("base query = " + query.getCapabilityUrl()); + System.out.println("full query = " + query.getQueryUrl()); + OslcQueryResult result = query.submit(); + int page = 1; + do { + System.out.println("Page " + page); + for (String resultsUrl : result.getMembersUrls()) { + System.out.println(resultsUrl); + } + if (result.hasNext()) { + result = result.next(); + page++; + } else { + break; + } + } while(true); + + /* //First let's get run a query to get references to 50 workitems (default page size) in the project area. //TODO: Add paging support //To do this, just do a GET on the Query Capability URL - String queryUrl = queryCapability; - ClientResponse response = client.getResource(queryUrl,"application/rdf+xml"); - Collection<String> changeRequestRefs = client.getQueryResponseMembers(queryCapability,response); +// String queryUrl = queryCapability; +// ClientResponse response = client.getResponse(queryUrl,"application/rdf+xml"); + + Collection<String> changeRequestRefs = client.getQueryResponseMembers(query.getCapabilityUrl(), response); //Now, loop through each of the references in the query response and get the actual ChangeRequest //Note: If we could use oslc.select=* with RTC, we would not have to do these followup requests. @@ -96,7 +118,7 @@ public class JazzFormSample { System.out.println("Getting ready to retrieve " + changeRequestRefs.size() + " change requests"); for (String changeRequestRef : changeRequestRefs ) { - response = client.getResource(changeRequestRef, "application/rdf+xml"); + response = client.getResponse(changeRequestRef, "application/rdf+xml"); //Marshal as an OSLC4J ChangeRequest. If you'd prefer the raw XML for the workitem, comment the next 2 lines and //uncomment the code below. @@ -104,17 +126,16 @@ public class JazzFormSample { printChangeRequestInfo(cr); //print a few attributes //Uncomment following code if you prefer to work with the raw XML - /* - InputStream is = response.getEntity(InputStream.class); - BufferedReader in = new BufferedReader(new InputStreamReader(is)); - String line = null; - while((line = in.readLine()) != null) { - System.out.println(line); - } - System.out.println(); - */ +// InputStream is = response.getEntity(InputStream.class); +// BufferedReader in = new BufferedReader(new InputStreamReader(is)); +// String line = null; +// while((line = in.readLine()) != null) { +// System.out.println(line); +// } +// System.out.println(); + } - + */ } |

