try {
/* bind services */
URI targetURI = URIUtil.toURI(target.getUrl());
ManifestParseTree manifest = getApplication().getManifest();
ManifestParseTree app = manifest.get("applications").get(0); //$NON-NLS-1$
if (app.has(CFProtocolConstants.V2_KEY_SERVICES)) {
/* fetch all services */
URI servicesURI = targetURI.resolve("/v2/services"); //$NON-NLS-1$
GetMethod getServicesMethod = new GetMethod(servicesURI.toString());
HttpUtil.configureHttpMethod(getServicesMethod, target);
getServicesMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$
/* send request */
ServerStatus jobStatus = HttpUtil.executeMethod(getServicesMethod);
status.add(jobStatus);
if (!jobStatus.isOK())
return status;
JSONObject resp = jobStatus.getJsonData();
JSONArray servicesJSON = resp.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES);
/* check for manifest version */
ManifestParseTree services = app.getOpt(CFProtocolConstants.V2_KEY_SERVICES);
if (services == null)
/* nothing to do */
return status;
int version = services.isList() ? 6 : 2;
if (version == 2) {
String spaceGuid = target.getSpace().getGuid();
URI serviceInstancesURI2 = targetURI.resolve("/v2/spaces/" + spaceGuid + "/service_instances"); //$NON-NLS-1$//$NON-NLS-2$
for (ManifestParseTree service : services.getChildren()) {
String serviceName = service.getLabel();
String nameService = "name:" + serviceName; //$NON-NLS-1$
NameValuePair[] pa = new NameValuePair[] {new NameValuePair("return_user_provided_service_instances", "true"), // //$NON-NLS-1$//$NON-NLS-2$
new NameValuePair("q", nameService), new NameValuePair("inline-relations-depth", "1") // //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
};
GetMethod getServiceMethod = new GetMethod(serviceInstancesURI2.toString());
getServiceMethod.setQueryString(pa);
HttpUtil.configureHttpMethod(getServiceMethod, target);
/* send request */
jobStatus = HttpUtil.executeMethod(getServiceMethod);
status.add(jobStatus);
if (!jobStatus.isOK())
return status;
resp = jobStatus.getJsonData();
String serviceInstanceGUID = null;
JSONArray respArray = resp.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES);
for (int i = 0; i < respArray.length(); ++i) {
JSONObject o = respArray.optJSONObject(i);
if (o != null) {
JSONObject str = o.optJSONObject(CFProtocolConstants.V2_KEY_METADATA);
if (str != null) {
serviceInstanceGUID = str.getString(CFProtocolConstants.V2_KEY_GUID);
break;
}
}
}
if (serviceInstanceGUID == null) {
/* no service instance bound to the application, create one if possible */
/* support both 'type' and 'label' fields as service type */
ManifestParseTree serviceType = service.getOpt(CFProtocolConstants.V2_KEY_TYPE);
if (serviceType == null)
serviceType = service.get(CFProtocolConstants.V2_KEY_LABEL);
ManifestParseTree provider = service.get(CFProtocolConstants.V2_KEY_PROVIDER);
ManifestParseTree plan = service.get(CFProtocolConstants.V2_KEY_PLAN);
String servicePlanGUID = findServicePlanGUID(serviceType.getValue(), provider.getValue(), plan.getValue(), servicesJSON);
if (servicePlanGUID == null) {
String[] bindings = {serviceName, serviceType.getValue(), plan.getValue()};
String msg = NLS.bind("Could not find service instance {0} nor service {1} with plan {2} in target.", bindings);
status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
return status;
}