Java Code Examples for org.eclipse.core.runtime.URIUtil#toURI()

The following examples show how to use org.eclipse.core.runtime.URIUtil#toURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetServiceCommand.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {

		URI targetURI = URIUtil.toURI(target.getUrl());
		URI serviceInstanceURI = targetURI.resolve("/v2/services/" + serviceGuid); //$NON-NLS-1$//$NON-NLS-2$

		GetMethod getServiceMethod = new GetMethod(serviceInstanceURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getServiceMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		ServerStatus getStatus = HttpUtil.executeMethod(getServiceMethod);
		if (!getStatus.isOK())
			return getStatus;

		JSONObject service = getStatus.getJsonData();
		return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, service);

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 2
Source File: MapRouteCommand.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {

		/* attach route to application */
		URI targetURI = URIUtil.toURI(target.getUrl());
		PutMethod attachRouteMethod = new PutMethod(targetURI.resolve("/v2/apps/" + application.getGuid() + "/routes/" + routeGUID).toString()); //$NON-NLS-1$//$NON-NLS-2$
		ServerStatus confStatus = HttpUtil.configureHttpMethod(attachRouteMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		return HttpUtil.executeMethod(attachRouteMethod);

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 3
Source File: DeleteRouteCommand.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {
		URI targetURI = URIUtil.toURI(target.getUrl());

		/* delete the route */
		URI routeURI = targetURI.resolve("/v2/routes/" + route.getGuid()); //$NON-NLS-1$

		DeleteMethod deleteRouteMethod = new DeleteMethod(routeURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(deleteRouteMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		deleteRouteMethod.setQueryString("recursive=true"); //$NON-NLS-1$

		ServerStatus status = HttpUtil.executeMethod(deleteRouteMethod);
		return status;

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName);
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 4
Source File: AttachRouteCommand.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {

		/* attach route to application */
		URI targetURI = URIUtil.toURI(target.getUrl());
		PutMethod attachRouteMethod = new PutMethod(targetURI.resolve("/v2/apps/" + application.getGuid() + "/routes/" + routeGUID).toString()); //$NON-NLS-1$//$NON-NLS-2$
		ServerStatus confStatus = HttpUtil.configureHttpMethod(attachRouteMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		return HttpUtil.executeMethod(attachRouteMethod);

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 5
Source File: GetRouteByGuidCommand.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
public ServerStatus _doIt() {
	try {
		URI targetURI = URIUtil.toURI(getCloud().getUrl());

		// Get the app
		URI appsURI = targetURI.resolve("/v2/routes/" + routeGuid);
		GetMethod getRoutesMethod = new GetMethod(appsURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getRoutesMethod, getCloud());
		if (!confStatus.isOK())
			return confStatus;

		ServerStatus getStatus = HttpUtil.executeMethod(getRoutesMethod);
		if (!getStatus.isOK())
			return getStatus;

		JSONObject routeJSON = getStatus.getJsonData();
		this.route = new Route().setCFJSON(routeJSON);

		return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, this.route.toJSON());
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 6
Source File: GetServiceByNameCommand.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {
		URI targetURI = URIUtil.toURI(target.getUrl());
		URI servicesURI = targetURI.resolve("/v2/spaces/" + target.getSpace().getGuid() + "/services"); //$NON-NLS-0$//$NON-NLS-1$

		GetMethod getServicesMethod = new GetMethod(servicesURI.toString());
		NameValuePair[] params = new NameValuePair[] { //
		new NameValuePair("q", "label:" + serviceName), //$NON-NLS-0$ //$NON-NLS-1$
				new NameValuePair("inline-relations-depth", "1") //$NON-NLS-0$ //$NON-NLS-1$
		};
		getServicesMethod.setQueryString(params);

		ServerStatus confStatus = HttpUtil.configureHttpMethod(getServicesMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		return HttpUtil.executeMethod(getServicesMethod);
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 7
Source File: UnmapRouteCommand.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {

		/* unmap route from application */
		URI targetURI = URIUtil.toURI(target.getUrl());
		DeleteMethod unmapRouteMethod = new DeleteMethod(targetURI.resolve("/v2/apps/" + app.getGuid() + "/routes/" + route.getGuid()).toString()); //$NON-NLS-1$//$NON-NLS-2$
		ServerStatus confStatus = HttpUtil.configureHttpMethod(unmapRouteMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		return HttpUtil.executeMethod(unmapRouteMethod);

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 8
Source File: CheckRouteCommand.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {
		URI targetURI = URIUtil.toURI(target.getUrl());
		ServerStatus getRouteStatus;

		GetDomainsCommand getDomainsCommand = new GetDomainsCommand(target, domainName);
		ServerStatus getDomainsStatus = (ServerStatus) getDomainsCommand.doIt();
		if (!getDomainsStatus.isOK())
			return getDomainsStatus;

		String domainId = getDomainsCommand.getDomains().get(0).getGuid();

		URI routesURI = targetURI.resolve("/v2/routes/reserved/domain/" + domainId + "/host/" + hostName);
		GetMethod getRouteMethod = new GetMethod(routesURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getRouteMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		getRouteStatus = HttpUtil.executeMethod(getRouteMethod);

		if (!getRouteStatus.isOK()){
			if(getRouteStatus.getHttpCode() == 404 && getRouteStatus.getJsonData().getString("error_code").equals("CF-NotFound"))
				return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, null);
			return getRouteStatus;
		}
		JSONObject result = new JSONObject();
		if (getRouteStatus.getMessage().equals("OK")){
			result.append("Route", getRouteStatus.toJSON());
		}

		return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);	

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 9
Source File: JavadocConfigurationBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void validateURL(URL location) throws MalformedURLException, URISyntaxException {
	URI path= URIUtil.toURI(location);
	URI index = URIUtil.append(path, "index.html"); //$NON-NLS-1$
	URI packagelist = URIUtil.append(path, "package-list"); //$NON-NLS-1$
	URL indexURL = URIUtil.toURL(index);
	URL packagelistURL = URIUtil.toURL(packagelist);
	
	boolean suc= checkURLConnection(indexURL) && checkURLConnection(packagelistURL);
	if (suc) {
		showConfirmValidationDialog(indexURL);
	} else {
		MessageDialog.openWarning(fShell, fTitle, fInvalidMessage);
	}
}
 
Example 10
Source File: CreateRouteCommand.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {
		/* create cloud foundry application */
		URI targetURI = URIUtil.toURI(target.getUrl());
		URI routesURI = targetURI.resolve("/v2/routes"); //$NON-NLS-1$

		PostMethod createRouteMethod = new PostMethod(routesURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(createRouteMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		/* set request body */
		JSONObject routeRequest = new JSONObject();
		routeRequest.put(CFProtocolConstants.V2_KEY_SPACE_GUID, target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID));
		routeRequest.put(CFProtocolConstants.V2_KEY_HOST, hostName);
		routeRequest.put(CFProtocolConstants.V2_KEY_DOMAIN_GUID, domain.getGuid());
		createRouteMethod.setRequestEntity(new StringRequestEntity(routeRequest.toString(), "application/json", "utf-8")); //$NON-NLS-1$//$NON-NLS-2$
		createRouteMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$

		ServerStatus createRouteStatus = HttpUtil.executeMethod(createRouteMethod);
		if (!createRouteStatus.isOK())
			return createRouteStatus;

		route = new Route().setCFJSON(createRouteStatus.getJsonData());

		return createRouteStatus;
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 11
Source File: GetOrgsCommand.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private ServerStatus getSpaces(List<Space> spaces, JSONObject orgJSON) throws Exception {
	long time = System.currentTimeMillis();
	URI targetURI = URIUtil.toURI(target.getUrl());
	URI spaceURI = targetURI.resolve(orgJSON.getJSONObject("entity").getString("spaces_url"));

	GetMethod getDomainsMethod = new GetMethod(spaceURI.toString());
	ServerStatus confStatus = HttpUtil.configureHttpMethod(getDomainsMethod, target.getCloud());
	if (!confStatus.isOK())
		return confStatus;

	getDomainsMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$

	ServerStatus status = HttpUtil.executeMethod(getDomainsMethod);
	if (!status.isOK())
		return status;

	/* extract available spaces */
	JSONObject orgs = status.getJsonData();

	if (orgs.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) {
		return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK);
	}

	/* look if the domain is available */
	int resources = orgs.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES).length();
	for (int k = 0; k < resources; ++k) {
		JSONObject spaceJSON = orgs.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES).getJSONObject(k);
		spaces.add(new Space().setCFJSON(spaceJSON));
	}

	logger.debug("GetOrgsCommand: getting spaces using " + spaceURI + " took " + (System.currentTimeMillis() - time));

	return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK);
}
 
Example 12
Source File: StopAppCommand.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public ServerStatus _doIt() {
	try {
		URI targetURI = URIUtil.toURI(target.getUrl());

		String appUrl = this.app.getAppJSON().getString("url");
		URI appURI = targetURI.resolve(appUrl);

		PutMethod stopMethod = new PutMethod(appURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(stopMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		stopMethod.setQueryString("inline-relations-depth=1");

		JSONObject stopComand = new JSONObject();
		stopComand.put("console", true);
		stopComand.put("state", "STOPPED");
		StringRequestEntity requestEntity = new StringRequestEntity(stopComand.toString(), CFProtocolConstants.JSON_CONTENT_TYPE, "UTF-8");
		stopMethod.setRequestEntity(requestEntity);

		GetAppCommand.expire(target, app.getName());
		return HttpUtil.executeMethod(stopMethod);
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 13
Source File: GetIsRouteAvailableCommand.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {

		// CF API
		// http://apidocs.cloudfoundry.org/195/routes/check_a_route_exists.html
		// GET /v2/routes/reserved/domain/:domain_guid/host/:host
		// e.g.
		// GET /v2/routes/reserved/domain/c11da0e2-517a-4d53-98a0-5d340fee4b78/host/sample-java-cloudant

		URI targetURI = URIUtil.toURI(getCloud().getUrl());
		String path = "/v2/routes/reserved/domain/" + this.guid + "/host/" + this.host;
		URI requestURI = targetURI.resolve(path);

		GetMethod getIsRouteAvailableMethod = new GetMethod(requestURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getIsRouteAvailableMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		return HttpUtil.executeMethod(getIsRouteAvailableMethod);

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 14
Source File: DeleteApplicationCommand.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {

		/* read deploy parameters */
		JSONObject appMetadata = null;
		JSONObject appEntity = null;

		URI targetURI = URIUtil.toURI(target.getUrl());

		/* get application details */
		String appsUrl = target.getSpace().getCFJSON().getJSONObject("entity").getString("apps_url"); //$NON-NLS-1$//$NON-NLS-2$
		URI appsURI = targetURI.resolve(appsUrl);
		GetMethod getAppsMethod = new GetMethod(appsURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getAppsMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		getAppsMethod.setQueryString("q=name:" + appName + "&inline-relations-depth=1"); //$NON-NLS-1$ //$NON-NLS-2$

		ServerStatus appsStatus = HttpUtil.executeMethod(getAppsMethod);
		if (!appsStatus.isOK())
			return appsStatus;

		JSONObject apps = appsStatus.getJsonData();
		if (!apps.has("resources") || apps.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$
			return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found", null);

		appMetadata = apps.getJSONArray("resources").getJSONObject(0).getJSONObject("metadata"); //$NON-NLS-1$ //$NON-NLS-2$
		appEntity = apps.getJSONArray("resources").getJSONObject(0).getJSONObject("entity"); //$NON-NLS-1$ //$NON-NLS-2$

		if (application.getGuid() == null) {

			String summaryAppUrl = appMetadata.getString("url") + "/summary"; //$NON-NLS-1$ //$NON-NLS-2$
			URI summaryAppURI = targetURI.resolve(summaryAppUrl);

			GetMethod getSummaryMethod = new GetMethod(summaryAppURI.toString());
			confStatus = HttpUtil.configureHttpMethod(getSummaryMethod, target.getCloud());
			if (!confStatus.isOK())
				return confStatus;

			ServerStatus getStatus = HttpUtil.executeMethod(getSummaryMethod);
			if (!getStatus.isOK())
				return getStatus;

			JSONObject summaryJSON = getStatus.getJsonData();

			/* set known application GUID */
			application.setGuid(summaryJSON.getString(CFProtocolConstants.V2_KEY_GUID));
		}

		/* gather application service bindings */
		ArrayList<String> serviceInstances = new ArrayList<String>();
		JSONArray appServiceBindings = appEntity.getJSONArray("service_bindings"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
		for (int i = 0; i < appServiceBindings.length(); ++i) {
			JSONObject binding = appServiceBindings.getJSONObject(i).getJSONObject("entity"); //$NON-NLS-1$
			serviceInstances.add(binding.getString("service_instance_url")); //$NON-NLS-1$
		}

		/* delete the application */
		URI appURI = targetURI.resolve("/v2/apps/" + application.getGuid()); //$NON-NLS-1$

		DeleteMethod deleteAppMethod = new DeleteMethod(appURI.toString());
		confStatus = HttpUtil.configureHttpMethod(deleteAppMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		deleteAppMethod.setQueryString("recursive=true"); //$NON-NLS-1$

		ServerStatus status = HttpUtil.executeMethod(deleteAppMethod);
		GetAppCommand.expire(target, application.getName());
		return status;

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName);
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 15
Source File: GetAppCommand.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public ServerStatus _doIt() {
	try {
		List<Object> key = Arrays.asList(target, name);
		app = appCache.get(key);
		if (app != null) {
			return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, this.app.toJSON());
		}
		URI targetURI = URIUtil.toURI(target.getUrl());

		// Find the app
		String appsUrl = target.getSpace().getCFJSON().getJSONObject("entity").getString("apps_url");
		URI appsURI = targetURI.resolve(appsUrl);
		GetMethod getAppsMethod = new GetMethod(appsURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getAppsMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		getAppsMethod.setQueryString("q=name:" + URLEncoder.encode(name, "UTF8") + "&inline-relations-depth=1");

		ServerStatus getStatus = HttpUtil.executeMethod(getAppsMethod);
		if (!getStatus.isOK())
			return getStatus;

		JSONObject apps = getStatus.getJsonData();

		if (!apps.has("resources") || apps.getJSONArray("resources").length() == 0)
			return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found", null);

		// Get more details about the app
		JSONObject appJSON = apps.getJSONArray("resources").getJSONObject(0).getJSONObject("metadata");

		String summaryAppUrl = appJSON.getString("url") + "/summary";
		URI summaryAppURI = targetURI.resolve(summaryAppUrl);

		GetMethod getSummaryMethod = new GetMethod(summaryAppURI.toString());
		confStatus = HttpUtil.configureHttpMethod(getSummaryMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		getStatus = HttpUtil.executeMethod(getSummaryMethod);
		if (!getStatus.isOK())
			return getStatus;

		JSONObject summaryJSON = getStatus.getJsonData();
		
		// instances
		String instancesUrl = appJSON.getString("url") + "/instances";
		URI instancesURI = targetURI.resolve(instancesUrl);

		GetMethod getInstancesMethod = new GetMethod(instancesURI.toString());
		confStatus = HttpUtil.configureHttpMethod(getInstancesMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		getStatus = HttpUtil.executeMethod(getInstancesMethod);
		JSONObject instancesJSON = getStatus.getJsonData();

		this.app = new App();
		this.app.setAppJSON(appJSON);
		this.app.setSummaryJSON(summaryJSON);
		this.app.setName(summaryJSON.getString("name"));
		this.app.setGuid(summaryJSON.getString("guid"));
		appCache.put(key, app);
		
		JSONObject result = this.app.toJSON();
		result.put("instances_details", instancesJSON);

		return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, this.app.toJSON());
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 16
Source File: UploadBitsCommand.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	/* multi server status */
	MultiServerStatus status = new MultiServerStatus();

	try {

		/* upload project contents */
		File appPackage = packager.getDeploymentPackage(super.getApplication(), appStore, command);
		deployedAppPackageName = PackageUtils.getApplicationPackageType(appStore);

		if (appPackage == null) {
			status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to read application content", null));
			return status;
		}

		URI targetURI = URIUtil.toURI(target.getUrl());
		PutMethod uploadMethod = new PutMethod(targetURI.resolve("/v2/apps/" + getApplication().getGuid() + "/bits?async=true").toString());
		uploadMethod.addRequestHeader(new Header("Authorization", "bearer " + target.getCloud().getAccessToken().getString("access_token")));

		Part[] parts = {new StringPart(CFProtocolConstants.V2_KEY_RESOURCES, "[]"), new FilePart(CFProtocolConstants.V2_KEY_APPLICATION, appPackage)};
		uploadMethod.setRequestEntity(new MultipartRequestEntity(parts, uploadMethod.getParams()));

		/* send request */
		ServerStatus jobStatus = HttpUtil.executeMethod(uploadMethod);
		status.add(jobStatus);
		if (!jobStatus.isOK())
			return status;

		/* long running task, keep track */
		int attemptsLeft = MAX_ATTEMPTS;
		JSONObject resp = jobStatus.getJsonData();
		String taksStatus = resp.getJSONObject(CFProtocolConstants.V2_KEY_ENTITY).getString(CFProtocolConstants.V2_KEY_STATUS);
		while (!CFProtocolConstants.V2_KEY_FINISHED.equals(taksStatus) && !CFProtocolConstants.V2_KEY_FAILURE.equals(taksStatus)) {
			if (CFProtocolConstants.V2_KEY_FAILED.equals(taksStatus)) {
				/* delete the tmp file */
				appPackage.delete();
				status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Upload failed", null));
				return status;
			}

			if (attemptsLeft == 0) {
				/* delete the tmp file */
				appPackage.delete();
				status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Upload timeout exceeded", null));
				return status;
			}

			/* two seconds */
			Thread.sleep(2000);

			/* check whether job has finished */
			URI jobLocation = targetURI.resolve(resp.getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_URL));
			GetMethod jobRequest = new GetMethod(jobLocation.toString());
			ServerStatus confStatus = HttpUtil.configureHttpMethod(jobRequest, target.getCloud());
			if (!confStatus.isOK())
				return confStatus;

			/* send request */
			jobStatus = HttpUtil.executeMethod(jobRequest);
			status.add(jobStatus);
			if (!jobStatus.isOK())
				return status;

			resp = jobStatus.getJsonData();
			taksStatus = resp.getJSONObject(CFProtocolConstants.V2_KEY_ENTITY).getString(CFProtocolConstants.V2_KEY_STATUS);

			--attemptsLeft;
		}

		if (CFProtocolConstants.V2_KEY_FAILURE.equals(jobStatus)) {
			status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to upload application bits", null));
			return status;
		}

		/* delete the tmp file */
		appPackage.delete();
		return status;

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e));
		return status;
	}
}
 
Example 17
Source File: CreateApplicationCommand.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {
		// get stack object
		Object stackId = JSONObject.NULL;
		if (stack != null){
			GetStackByNameCommand getStackCommand = new GetStackByNameCommand(target, stack);
			ServerStatus getStackStatus = (ServerStatus) getStackCommand.doIt();
			if (!getStackStatus.isOK())
				return getStackStatus;
			if (getStackCommand.getStack() == null)
				return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, NLS.bind("Stack {0} not found", stack), null);
			stackId = getStackCommand.getStack().getGuid();
		}

		/* create cloud foundry application */
		URI targetURI = URIUtil.toURI(target.getUrl());
		URI appsURI = targetURI.resolve("/v2/apps"); //$NON-NLS-1$

		PostMethod createAppMethod = new PostMethod(appsURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(createAppMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		/* set request body */
		JSONObject createAppRequst = new JSONObject();
		createAppRequst.put(CFProtocolConstants.V2_KEY_SPACE_GUID, target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID));
		createAppRequst.put(CFProtocolConstants.V2_KEY_NAME, appName);
		createAppRequst.put(CFProtocolConstants.V2_KEY_INSTANCES, appInstances);
		createAppRequst.put(CFProtocolConstants.V2_KEY_BUILDPACK, buildPack != null ? buildPack : JSONObject.NULL);
		createAppRequst.put(CFProtocolConstants.V2_KEY_COMMAND, appCommand);
		createAppRequst.put(CFProtocolConstants.V2_KEY_MEMORY, appMemory);
		createAppRequst.put(CFProtocolConstants.V2_KEY_STACK_GUID, stackId);
		createAppRequst.put(CFProtocolConstants.V2_KEY_ENVIRONMENT_JSON, env != null ? env : new JSONObject());

		createAppMethod.setRequestEntity(new StringRequestEntity(createAppRequst.toString(), "application/json", "utf-8")); //$NON-NLS-1$ //$NON-NLS-2$

		ServerStatus status = HttpUtil.executeMethod(createAppMethod);
		if (!status.isOK())
			return status;

		/* extract application guid */
		JSONObject appResp = status.getJsonData();
		application.setGuid(appResp.getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID));
		application.setName(appName);

		return status;

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 18
Source File: GetSpaceByNameCommand.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public ServerStatus _doIt() {
	try {
		
		// check the org first
		
		URI targetURI = URIUtil.toURI(target.getUrl());
		URI orgsURI = targetURI.resolve("/v2/organizations"); //$NON-NLS-1$

		GetMethod getOrgsMethod = new GetMethod(orgsURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getOrgsMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;
		
		ServerStatus getOrgsStatus = HttpUtil.executeMethod(getOrgsMethod);
		if (!getOrgsStatus.isOK() && getOrgsStatus.getHttpCode() != HttpServletResponse.SC_PARTIAL_CONTENT)
			return getOrgsStatus;

		JSONObject result = getOrgsStatus.getJsonData();
		JSONArray orgs = result.getJSONArray("resources"); //$NON-NLS-1$

		if (orgs.length() == 0)
			return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Organization not found", null);

		Org organization = getOrganization(orgs, orgName);
		if (organization == null)
			return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Organization not found", null);
		
		// now get the space
		
		String spaceUrl = organization.getCFJSON().getJSONObject("entity").getString("spaces_url"); //$NON-NLS-1$//$NON-NLS-2$
		URI spaceURI = targetURI.resolve(spaceUrl);

		GetMethod getSpaceMethod = new GetMethod(spaceURI.toString());
		confStatus = HttpUtil.configureHttpMethod(getSpaceMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		ServerStatus getSpaceStatus = HttpUtil.executeMethod(getSpaceMethod);
		if (!getSpaceStatus.isOK())
			return getSpaceStatus;

		result = getSpaceStatus.getJsonData();
		JSONArray spaces = result.getJSONArray("resources"); //$NON-NLS-1$

		if (spaces.length() == 0)
			return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Space not found", null);

		Space space = getSpace(spaces, spaceName);
		if (space == null)
			return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Space not found", null);

		return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, space.toJSON());
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 19
Source File: UpdateApplicationCommand.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {
		// get stack object
		Object stackId = JSONObject.NULL;
		if (stack != null) {
			GetStackByNameCommand getStackCommand = new GetStackByNameCommand(target, stack);
			ServerStatus getStackStatus = (ServerStatus) getStackCommand.doIt();
			if (!getStackStatus.isOK())
				return getStackStatus;
			if (getStackCommand.getStack() == null)
				return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, NLS.bind("Stack {0} not found", stack), null);
			stackId = getStackCommand.getStack().getGuid();
		}

		/* get application URL */
		URI targetURI = URIUtil.toURI(target.getUrl());
		URI appURI = targetURI.resolve(application.getAppJSON().getString(CFProtocolConstants.V2_KEY_URL));

		PutMethod updateApplicationMethod = new PutMethod(appURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(updateApplicationMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		updateApplicationMethod.setQueryString("async=true&inline-relations-depth=1"); //$NON-NLS-1$

		/* set request body */
		JSONObject updateAppRequest = new JSONObject();
		updateAppRequest.put(CFProtocolConstants.V2_KEY_NAME, appName);
		updateAppRequest.put(CFProtocolConstants.V2_KEY_INSTANCES, appInstances);
		updateAppRequest.put(CFProtocolConstants.V2_KEY_COMMAND, appCommand);
		updateAppRequest.put(CFProtocolConstants.V2_KEY_MEMORY, appMemory);
		updateAppRequest.put(CFProtocolConstants.V2_KEY_ENVIRONMENT_JSON, env != null ? env : new JSONObject());
		updateAppRequest.put(CFProtocolConstants.V2_KEY_BUILDPACK, buildPack != null ? buildPack : JSONObject.NULL);
		updateAppRequest.put(CFProtocolConstants.V2_KEY_STACK_GUID, stackId);

		updateApplicationMethod.setRequestEntity(new StringRequestEntity(updateAppRequest.toString(), "application/json", "utf-8")); //$NON-NLS-1$ //$NON-NLS-2$

		ServerStatus status = HttpUtil.executeMethod(updateApplicationMethod);
		if (!status.isOK())
			return status;
		GetAppCommand.expire(target, application.getName());
		return status;

	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 20
Source File: GetAppByGuidCommand.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public ServerStatus _doIt() {
	try {
		URI targetURI = URIUtil.toURI(getCloud().getUrl());

		// Get the app
		URI appsURI = targetURI.resolve("/v2/apps/" + appGuid);
		GetMethod getAppsMethod = new GetMethod(appsURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(getAppsMethod, getCloud());
		if (!confStatus.isOK())
			return confStatus;

		ServerStatus getStatus = HttpUtil.executeMethod(getAppsMethod);
		if (!getStatus.isOK())
			return getStatus;

		JSONObject app = getStatus.getJsonData();

		//			if (!apps.has("resources") || apps.getJSONArray("resources").length() == 0)
		//				return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found", null);

		// Get more details about the app
		JSONObject appJSON = app.getJSONObject("metadata");

		String summaryAppUrl = appJSON.getString("url") + "/summary";
		URI summaryAppURI = targetURI.resolve(summaryAppUrl);

		GetMethod getSummaryMethod = new GetMethod(summaryAppURI.toString());
		confStatus = HttpUtil.configureHttpMethod(getSummaryMethod, getCloud());
		if (!confStatus.isOK())
			return confStatus;

		getStatus = HttpUtil.executeMethod(getSummaryMethod);
		if (!getStatus.isOK())
			return getStatus;

		JSONObject summaryJSON = getStatus.getJsonData();

		this.app = new App();
		this.app.setAppJSON(appJSON);
		this.app.setSummaryJSON(summaryJSON);
		this.app.setGuid(appJSON.getString("guid"));
		this.app.setName(summaryJSON.getString("name"));

		return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, this.app.toJSON());
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}