*/
public void execute() throws Exception {
if (!isExecuted()) {
String targetUri = createTargetUri();
ClientResource resource = service.createResource(new Reference(
targetUri));
Metadata metadata = (Metadata) service.getMetadata();
if (metadata == null) {
throw new Exception(
"Can't execute the query without the service's metadata.");
}
Representation result = null;
try {
result = resource.get(MediaType.APPLICATION_ATOM);
} catch (ResourceException e) {
getLogger().warning(
"Can't execute the query for the following reference: "
+ targetUri + " due to " + e.getMessage());
throw e;
}
if (resource.getStatus().isSuccess()) {
// Guess the type of query based on the URI structure
switch (guessType(targetUri)) {
case TYPE_ENTITY_SET:
FeedContentHandler<T> feedContentHandler = new FeedContentHandler<T>(
entityClass, entityType, metadata, getLogger());
setFeed(new Feed(result, feedContentHandler));
this.count = feedContentHandler.getCount();
this.entities = feedContentHandler.getEntities();
break;
case TYPE_ENTITY:
EntryContentHandler<T> entryContentHandler = new EntryContentHandler<T>(
entityClass, entityType, metadata, getLogger());
Feed feed = new Feed();
feed.getEntries().add(
new Entry(result, entryContentHandler));
setFeed(feed);
entities = new ArrayList<T>();
if (entryContentHandler.getEntity() != null) {
entities.add(entryContentHandler.getEntity());
}
break;
case TYPE_UNKNOWN:
// Guess the type of query based on the returned
// representation
Representation rep = new StringRepresentation(result
.getText());
String string = rep.getText().substring(0,
Math.min(100, rep.getText().length()));
if (string.contains("<feed")) {
feedContentHandler = new FeedContentHandler<T>(
entityClass, entityType, metadata, getLogger());
setFeed(new Feed(rep, feedContentHandler));
this.count = feedContentHandler.getCount();
this.entities = feedContentHandler.getEntities();
} else if (string.contains("<entry")) {
entryContentHandler = new EntryContentHandler<T>(
entityClass, entityType, metadata, getLogger());
feed = new Feed();
feed.getEntries().add(
new Entry(rep, entryContentHandler));
setFeed(feed);
entities = new ArrayList<T>();
if (entryContentHandler.getEntity() != null) {
entities.add(entryContentHandler.getEntity());
}
}
default:
// Can only guess entity and entity set, a priori.
// TODO May we go a step further by analyzing the metadata
// of the data services?
// Do we support only those two types?
// Another way is to guess from the result representation.
// Sometimes, it returns a set, an entity, or a an XML
// representation of a property.
break;
}
}
service.setLatestRequest(resource.getRequest());
service.setLatestResponse(resource.getResponse());
setExecuted(true);
}
}