use of org.collectionspace.chain.csp.persistence.services.connection.Returned in project application by collectionspace.
the class BlobStorage method retrieveJSON.
@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
JSONObject result = null;
try {
if (r.isType("report") == true) {
Document doc = null;
Map<String, Document> parts = new HashMap<String, Document>();
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
doc = XmlJsonConversion.convertToXml(r, restrictions, section, "POST");
if (doc != null) {
parts.put(record_path[0], doc);
}
}
Returned response = null;
JSONObject out = new JSONObject();
if (filePath.contains("/output") == true) {
//
// <Please document what this request returns>
//
response = conn.getReportDocument(RequestMethod.GET, "reports/" + filePath, null, creds, cache);
} else if (filePath.contains(PUBLISH_URL_SUFFIX) == true) {
//
// If they asked to publish the report then we return a URL to the publicitems service
//
response = conn.getPublishedReportDocumentURL(RequestMethod.POST, "reports/" + filePath, doc, creds, cache);
ReturnedURL returnedURL = (ReturnedURL) response;
out.put("Location", returnedURL.getURL());
} else {
//
// This request returns the contents of the report.
//
response = conn.getReportDocument(RequestMethod.POST, "reports/" + filePath, doc, creds, cache);
ReturnUnknown returnUnknown = (ReturnUnknown) response;
out.put("body", returnUnknown.getBodyAsStream());
out.put("contenttype", returnUnknown.getContentType());
out.put("contentdisposition", returnUnknown.getContentDisposition());
}
int status = response.getStatus();
if (status > 299 || status < 200) {
throw new UnderlyingStorageException("Bad response ", status, r.getServicesURL() + "/");
}
result = out;
} else if (r.isType("batch")) {
Document doc = null;
Map<String, Document> parts = new HashMap<String, Document>();
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
doc = XmlJsonConversion.convertToXml(r, restrictions, section, "POST");
if (doc != null) {
parts.put(record_path[0], doc);
}
}
ReturnedDocument doc2 = null;
if (filePath.contains("/output")) {
doc2 = conn.getBatchDocument(RequestMethod.GET, "batch/" + filePath, null, creds, cache);
} else {
doc2 = conn.getBatchDocument(RequestMethod.POST, "batch/" + filePath, doc, creds, cache);
}
if (doc2.getStatus() > 299 || doc2.getStatus() < 200)
throw new UnderlyingStorageException("Bad response ", doc2.getStatus(), r.getServicesURL() + "/");
JSONObject out = new JSONObject();
this.convertToJson(out, doc2.getDocument(), r.getSpec().getRecord("invocationresults"), "", "invocationResults", filePath);
return out;
} else {
//
// We're being asked for an image or some other attachment.
//
String[] parts = filePath.split("/");
if (parts.length >= 2) {
String imagefilePath = parts[0];
String view = parts[1];
String extra = "";
if (parts.length == 3) {
extra = parts[2];
}
if (view.equalsIgnoreCase(ORIGINAL_CONTENT)) {
result = originalViewRetrieveImg(root, creds, cache, imagefilePath, view, extra, restrictions);
} else {
result = viewRetrieveImg(root, creds, cache, imagefilePath, view, extra, restrictions);
}
} else
result = simpleRetrieveJSON(creds, cache, filePath);
}
} catch (JSONException x) {
throw new UnderlyingStorageException("Error building JSON", x);
} catch (UnsupportedEncodingException x) {
throw new UnderlyingStorageException("Error UnsupportedEncodingException JSON", x);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
}
return result;
}
Aggregations