List of usage examples for org.apache.commons.httpclient.methods GetMethod setQueryString
public void setQueryString(String queryString)
From source file:org.eclipse.orion.server.cf.commands.DeleteApplicationCommand.java
@Override protected ServerStatus _doIt() { try {/* w w w.ja v a 2 s.c o m*/ /* 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()); HttpUtil.configureHttpMethod(getAppsMethod, target); 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()); HttpUtil.configureHttpMethod(getSummaryMethod, target); 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()); HttpUtil.configureHttpMethod(deleteAppMethod, target); deleteAppMethod.setQueryString("recursive=true"); //$NON-NLS-1$ ServerStatus status = HttpUtil.executeMethod(deleteAppMethod); 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); } }
From source file:org.eclipse.orion.server.cf.commands.DeleteApplicationRoutesCommand.java
@Override protected ServerStatus _doIt() { MultiServerStatus status = new MultiServerStatus(); try {/*from www. jav a2 s . co m*/ URI targetURI = URIUtil.toURI(target.getUrl()); // get app details // TODO: it should be passed along with App object 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()); HttpUtil.configureHttpMethod(getAppsMethod, target); getAppsMethod.setQueryString("q=name:" + appName + "&inline-relations-depth=1"); //$NON-NLS-1$ //$NON-NLS-2$ ServerStatus appsStatus = HttpUtil.executeMethod(getAppsMethod); status.add(appsStatus); if (!status.isOK()) return status; JSONObject jsonData = appsStatus.getJsonData(); if (!jsonData.has("resources") || jsonData.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$ return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found", null); JSONArray apps = jsonData.getJSONArray("resources"); // get app routes String routesUrl = apps.getJSONObject(0).getJSONObject("entity").getString("routes_url"); URI routesURI = targetURI.resolve(routesUrl); GetMethod getRoutesMethod = new GetMethod(routesURI.toString()); HttpUtil.configureHttpMethod(getRoutesMethod, target); ServerStatus routesStatus = HttpUtil.executeMethod(getRoutesMethod); status.add(routesStatus); if (!status.isOK()) return status; jsonData = routesStatus.getJsonData(); if (!jsonData.has("resources") || jsonData.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$ return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, "No routes for the app", null); JSONArray routes = jsonData.getJSONArray("resources"); for (int i = 0; i < routes.length(); ++i) { JSONObject route = routes.getJSONObject(i); // delete route String routeUrl = route.getJSONObject(CFProtocolConstants.V2_KEY_METADATA) .getString(CFProtocolConstants.V2_KEY_URL); URI routeURI = targetURI.resolve(routeUrl); //$NON-NLS-1$ DeleteMethod deleteRouteMethod = new DeleteMethod(routeURI.toString()); HttpUtil.configureHttpMethod(deleteRouteMethod, target); ServerStatus deleteStatus = HttpUtil.executeMethod(deleteRouteMethod); status.add(deleteStatus); if (!status.isOK()) return status; } 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); } }
From source file:org.eclipse.orion.server.cf.commands.FindRouteCommand.java
@Override protected ServerStatus _doIt() { try {// w w w. j a v a 2s. c o m /* create cloud foundry application */ URI targetURI = URIUtil.toURI(target.getUrl()); URI routesURI = targetURI.resolve("/v2/routes"); //$NON-NLS-1$ GetMethod findRouteMethod = new GetMethod(routesURI.toString()); HttpUtil.configureHttpMethod(findRouteMethod, target); findRouteMethod .setQueryString("inline-relations-depth=1&q=host:" + appHost + ";domain_guid:" + domainGUID); //$NON-NLS-1$ //$NON-NLS-2$ ServerStatus status = HttpUtil.executeMethod(findRouteMethod); if (!status.isOK()) return status; JSONObject response = status.getJsonData(); if (response.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Route not found", null); /* retrieve the GUID */ JSONArray resources = response.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES); JSONObject route = resources.getJSONObject(0); return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, route); } 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); } }
From source file:org.eclipse.orion.server.cf.commands.GetAppCommand.java
public ServerStatus _doIt() { try {/*from w ww .ja v a 2s . c o m*/ 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()); HttpUtil.configureHttpMethod(getAppsMethod, target); 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()); HttpUtil.configureHttpMethod(getSummaryMethod, target); 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); 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); } }
From source file:org.eclipse.orion.server.cf.commands.GetDomainsCommand.java
@Override protected ServerStatus _doIt() { try {// w w w . j av a 2s . c o m /* get available domains */ URI targetURI = URIUtil.toURI(target.getUrl()); String domainsURL = target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_ENTITY) .getString(CFProtocolConstants.V2_KEY_DOMAINS_URL); URI domainsURI = targetURI.resolve(domainsURL); GetMethod getDomainsMethod = new GetMethod(domainsURI.toString()); HttpUtil.configureHttpMethod(getDomainsMethod, target); getDomainsMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$ ServerStatus getDomainStatus = HttpUtil.executeMethod(getDomainsMethod); /* extract available domains */ JSONObject domainsJSON = getDomainStatus.getJsonData(); if (domainsJSON.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) { return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, null, null); } JSONObject result = new JSONObject(); domains = new ArrayList<Domain>(); JSONArray resources = domainsJSON.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES); for (int k = 0; k < resources.length(); ++k) { JSONObject domainJSON = resources.getJSONObject(k); Domain domain = new Domain(); domain.setCFJSON(domainJSON); if (domainName == null || domainName.equals(domain.getDomainName())) { domains.add(domain); result.append("Domains", domain.toJSON()); } } if (domains.isEmpty()) return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Domain can not be found", null); 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); } }
From source file:org.eclipse.orion.server.cf.commands.GetOrgsCommand.java
@Override protected ServerStatus _doIt() { try {/*w ww .j av a 2s . c om*/ /* get available orgs */ URI targetURI = URIUtil.toURI(target.getUrl()); URI orgsURI = targetURI.resolve("/v2/organizations"); GetMethod getDomainsMethod = new GetMethod(orgsURI.toString()); HttpUtil.configureHttpMethod(getDomainsMethod, target); getDomainsMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$ ServerStatus status = HttpUtil.executeMethod(getDomainsMethod); if (!status.isOK()) return status; /* extract available orgs */ JSONObject orgs = status.getJsonData(); if (orgs == null || orgs.optInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS, 0) < 1) { return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NO_CONTENT, "Server did not return any organizations.", null); } /* look if the domain is available */ JSONObject result = new JSONObject(); int resources = orgs.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES).length(); for (int k = 0; k < resources; ++k) { JSONObject orgJSON = orgs.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES).getJSONObject(k); List<Space> spaces = new ArrayList<Space>(); status = getSpaces(spaces, orgJSON); if (!status.isOK()) return status; OrgWithSpaces orgWithSpaces = new OrgWithSpaces(); orgWithSpaces.setCFJSON(orgJSON); orgWithSpaces.setSpaces(spaces); result.append("Orgs", orgWithSpaces.toJSON()); } return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result); } catch (Exception e) { String msg = NLS.bind("An error occurred when performing operation {0}", commandName); //$NON-NLS-1$ logger.error(msg, e); return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e); } }
From source file:org.eclipse.orion.server.cf.commands.GetOrgsCommand.java
private ServerStatus getSpaces(List<Space> spaces, JSONObject orgJSON) throws Exception { URI targetURI = URIUtil.toURI(target.getUrl()); URI spaceURI = targetURI.resolve(orgJSON.getJSONObject("entity").getString("spaces_url")); GetMethod getDomainsMethod = new GetMethod(spaceURI.toString()); HttpUtil.configureHttpMethod(getDomainsMethod, target); 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); }// w w w . j a v a 2s .co m /* 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)); } return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK); }
From source file:org.eclipse.orion.server.cf.commands.GetRoutesCommand.java
@Override protected ServerStatus _doIt() { try {/*from w w w. j a v a 2s .co m*/ /* get available routes */ URI targetURI = URIUtil.toURI(target.getUrl()); String routesURL = target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_ENTITY) .getString(CFProtocolConstants.V2_KEY_ROUTES_URL); URI routesURI = targetURI.resolve(routesURL); GetMethod getRoutesMethod = new GetMethod(routesURI.toString()); HttpUtil.configureHttpMethod(getRoutesMethod, target); getRoutesMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$ ServerStatus getRouteStatus = HttpUtil.executeMethod(getRoutesMethod); if (!getRouteStatus.isOK()) return getRouteStatus; /* extract available routes */ JSONObject routesJSON = getRouteStatus.getJsonData(); if (routesJSON.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) { return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, null, null); } JSONObject result = new JSONObject(); routes = new ArrayList<Route>(); JSONArray resources = routesJSON.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES); for (int k = 0; k < resources.length(); ++k) { JSONObject routeJSON = resources.getJSONObject(k); Route route = new Route(); route.setCFJSON(routeJSON); if (domainName == null || (domainName.equals(route.getDomainName()) && (hostName == null || hostName.equals(route.getHost())))) { if (!orphaned || route.getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_ENTITY) .getJSONArray(CFProtocolConstants.V2_KEY_APPS).length() == 0) { routes.add(route); result.append("Routes", route.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); } }
From source file:org.eclipse.orion.server.cf.commands.GetServiceByNameCommand.java
@Override protected ServerStatus _doIt() { try {/*from w w w .java 2s . co m*/ 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); HttpUtil.configureHttpMethod(getServicesMethod, target.getCloud()); 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); } }
From source file:org.eclipse.orion.server.cf.commands.GetServiceInstancesCommand.java
@Override protected ServerStatus _doIt() { /* multi server status */ MultiServerStatus status = new MultiServerStatus(); try {/*from w w w.ja va 2 s. c o m*/ JSONObject response = new JSONObject(); JSONArray services = new JSONArray(); /* get services */ URI targetURI = URIUtil.toURI(target.getUrl()); String spaceGuid = target.getSpace().getGuid(); URI serviceInstancesURI = targetURI.resolve("/v2/spaces/" + spaceGuid + "/service_instances"); //$NON-NLS-1$//$NON-NLS-2$ NameValuePair[] pa = new NameValuePair[] { // new NameValuePair("return_user_provided_service_instances", "true"), // //$NON-NLS-1$//$NON-NLS-2$ new NameValuePair("inline-relations-depth", "1") // //$NON-NLS-1$ //$NON-NLS-2$ }; do { GetMethod getServiceInstancesMethod = new GetMethod(serviceInstancesURI.toString()); getServiceInstancesMethod.setQueryString(pa); HttpUtil.configureHttpMethod(getServiceInstancesMethod, target); /* send request */ ServerStatus jobStatus = HttpUtil.executeMethod(getServiceInstancesMethod); status.add(jobStatus); if (!jobStatus.isOK()) return status; JSONObject resp = jobStatus.getJsonData(); if (resp.has(CFProtocolConstants.V2_KEY_NEXT_URL) && !resp.isNull(CFProtocolConstants.V2_KEY_NEXT_URL)) serviceInstancesURI = targetURI.resolve(resp.getString(CFProtocolConstants.V2_KEY_NEXT_URL)); else serviceInstancesURI = null; JSONArray resources = resp.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES); for (int i = 0; i < resources.length(); ++i) { JSONObject serviceObj = resources.getJSONObject(i); JSONObject serviceInstanceEntity = serviceObj.getJSONObject(CFProtocolConstants.V2_KEY_ENTITY); JSONObject serviceEntity = serviceInstanceEntity .getJSONObject(CFProtocolConstants.V2_KEY_SERVICE_PLAN)// .getJSONObject(CFProtocolConstants.V2_KEY_ENTITY); String serviceGuid = serviceEntity.getString(CFProtocolConstants.V2_KEY_SERVICE_GUID); GetServiceCommand getServiceCommand = new GetServiceCommand(target, serviceGuid); /* get detailed info about the service */ jobStatus = (ServerStatus) getServiceCommand.doIt(); /* FIXME: unsafe type cast */ status.add(jobStatus); if (!jobStatus.isOK()) return status; JSONObject serviceResp = jobStatus.getJsonData(); boolean isBindable = serviceResp.getJSONObject(CFProtocolConstants.V2_KEY_ENTITY) .getBoolean(CFProtocolConstants.V2_KEY_BINDABLE); if (isBindable) { Service s = new Service(serviceInstanceEntity.getString(CFProtocolConstants.V2_KEY_NAME)); services.put(s.toJSON()); } } } while (serviceInstancesURI != null); response.put(ProtocolConstants.KEY_CHILDREN, services); return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, response); } 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; } }