This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
/
OslcQueryResult.java
302 lines (257 loc) · 10.4 KB
/
OslcQueryResult.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*******************************************************************************
* Copyright (c) 2012, 2014 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
* Steve Pitschke - added getMembers() method
* Samuel Padgett - support setting member property
* Samuel Padgett - preserve member property on call to getNext()
*******************************************************************************/
package org.eclipse.lyo.client.oslc.resources;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.datatype.DatatypeConfigurationException;
import org.apache.wink.client.ClientResponse;
import org.eclipse.lyo.client.oslc.OSLCConstants;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreApplicationException;
import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
import org.eclipse.lyo.oslc4j.provider.jena.JenaModelHelper;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.ResIterator;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Selector;
import org.apache.jena.rdf.model.SimpleSelector;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.vocabulary.RDFS;
/**
* The results of an OSLC query. If the query was paged, subsequent pages can be retrieved using the Iterator interface.
*
* This class is not currently thread safe.
*/
@Deprecated
public class OslcQueryResult implements Iterator<OslcQueryResult> {
/**
* The default member property to look for in OSLC query results
* (rdfs:member). Can be changed using {@link OslcQueryResult#setMemberProperty(Property)}.
*/
public final static Property DEFAULT_MEMBER_PROPERTY = RDFS.member;
/**
* If system property {@value} is set to true, find any member in the
*/
public final static String SELECT_ANY_MEMBER = "org.eclipse.lyo.client.oslc.query.selectAnyMember";
/**
* Treat any resource in the members resource as a query result (except rdf:type).
*
* @see OslcQueryResult#SELECT_ANY_MEMBER
*/
private final class AnyMemberSelector extends SimpleSelector {
private AnyMemberSelector(Resource subject) {
super(subject, null, (RDFNode) null);
}
public boolean selects(Statement s) {
String fqPredicateName = s.getPredicate().getNameSpace() + s.getPredicate().getLocalName();
if (OSLCConstants.RDF_TYPE_PROP.equals(fqPredicateName)) {
return false;
}
return s.getObject().isResource();
}
}
private final OslcQuery query;
private final ClientResponse response;
private final int pageNumber;
private Property memberProperty = DEFAULT_MEMBER_PROPERTY;
private Model rdfModel;
private Resource infoResource, membersResource;
private String nextPageUrl = "";
private boolean rdfInitialized = false;
public OslcQueryResult(OslcQuery query, ClientResponse response) {
this.query = query;
this.response = response;
this.pageNumber = 1;
}
private OslcQueryResult(OslcQueryResult prev) {
this.query = new OslcQuery(prev);
this.response = this.query.getResponse();
this.membersResource = prev.membersResource;
this.memberProperty = prev.memberProperty;
this.pageNumber = prev.pageNumber + 1;
}
private synchronized void initializeRdf() {
if (!rdfInitialized) {
rdfInitialized = true;
rdfModel = ModelFactory.createDefaultModel();
rdfModel.read(response.getEntity(InputStream.class), query.getCapabilityUrl());
//Find a resource with rdf:type of oslc:ResourceInfo
Property rdfType = rdfModel.createProperty(OslcConstants.RDF_NAMESPACE, "type");
Property responseInfo = rdfModel.createProperty(OslcConstants.OSLC_CORE_NAMESPACE, "ResponseInfo");
ResIterator iter = rdfModel.listResourcesWithProperty(rdfType, responseInfo);
//There should only be one - take the first
infoResource = null;
while (iter.hasNext()) {
infoResource = iter.next();
break;
}
membersResource = rdfModel.getResource(query.getCapabilityUrl());
}
}
String getNextPageUrl() {
initializeRdf();
if ((nextPageUrl == null || nextPageUrl.isEmpty()) && infoResource != 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;
}
/**
* Returns the member property to find query result resources.
*
* @return the member property URI
* @see #setMemberProperty(String)
*/
public String getMemberProperty() {
return this.memberProperty.getURI();
}
/**
* Sets the predicate to use to find query result resources. If unset,
* defaults to {@code http://www.w3.org/2000/01/rdf-schema#member}.
*
* @param memberPredicate
* the RDF predicate for member resources from the provider's
* query shape
* @see <a href="http://open-services.net/bin/view/Main/OSLCCoreSpecRDFXMLExamples?sortcol=table;up=#Specifying_the_shape_of_a_query">Specifying the sahpe of a query</a>
*/
public void setMemberProperty(String memberPredicate) {
this.memberProperty = ModelFactory.createDefaultModel().createProperty(memberPredicate);
}
/**
* Get the raw Wink client response to a query.
*
* NOTE: Using this method and consuming the response will make other methods
* which examine the response unavailable (Examples: getMemberUrls(), next() and hasNext()).
* When this method is invoked, the consumer is responsible for OSLC page processing
*
* @return
*/
public ClientResponse getRawResponse() {
return response;
}
private Selector getMemberSelector() {
if ("true".equalsIgnoreCase(System.getProperty(SELECT_ANY_MEMBER))) {
return new AnyMemberSelector(membersResource);
}
return new SimpleSelector(membersResource, memberProperty, (RDFNode) null);
}
/**
* Return the subject URLs of the query response. The URLs are the location of all artifacts
* which satisfy the query conditions.
*
* NOTE: Using this method consumes the query response and makes other methods
* which examine the response unavailable (Example: getRawResponse().
* @return
*/
public String[] getMembersUrls() {
initializeRdf();
ArrayList<String> membersUrls = new ArrayList<String>();
Selector select = getMemberSelector();
StmtIterator iter = rdfModel.listStatements(select);
while (iter.hasNext()) {
Statement member = iter.next();
membersUrls.add(member.getResource().getURI());
}
return membersUrls.toArray(new String[membersUrls.size()]);
}
/**
* Return the enumeration of queried results from this page
*
* @return member statements from current page.
*/
public <T> Iterable<T> getMembers(final Class<T> clazz) {
initializeRdf();
Selector select = getMemberSelector();
final StmtIterator iter = rdfModel.listStatements(select);
Iterable<T> result = new Iterable<T>() {
public Iterator<T>
iterator() {
return new Iterator<T>() {
public boolean hasNext() {
return iter.hasNext();
}
@SuppressWarnings("unchecked")
public T next() {
Statement member = iter.next();
try {
return (T)JenaModelHelper.fromJenaResource((Resource)member.getObject(), clazz);
} catch (IllegalArgumentException e) {
throw new IllegalStateException(e.getMessage());
} catch (SecurityException e) {
throw new IllegalStateException(e.getMessage());
} catch (DatatypeConfigurationException e) {
throw new IllegalStateException(e.getMessage());
} catch (IllegalAccessException e) {
throw new IllegalStateException(e.getMessage());
} catch (InstantiationException e) {
throw new IllegalStateException(e.getMessage());
} catch (InvocationTargetException e) {
throw new IllegalStateException(e.getMessage());
} catch (OslcCoreApplicationException e) {
throw new IllegalStateException(e.getMessage());
} catch (URISyntaxException e) {
throw new IllegalStateException(e.getMessage());
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e.getMessage());
}
}
public void remove() {
iter.remove();
}
};
}
};
return result;
}
}