Package org.eclipse.orion.server.cf.utils

Examples of org.eclipse.orion.server.cf.utils.MultiServerStatus


  }

  @Override
  protected ServerStatus _doIt() {
    /* multi server status */
    MultiServerStatus status = new MultiServerStatus();

    try {

      if (noRoute)
        /* nothing to do */
        return status;

      /* get available domains */
      GetDomainsCommand getDomainsCommand = new GetDomainsCommand(target);
      ServerStatus jobStatus = (ServerStatus) getDomainsCommand.doIt(); /* FIXME: unsafe type cast */
      status.add(jobStatus);

      if (!jobStatus.isOK())
        return status;

      List<Domain> domains = getDomainsCommand.getDomains();
      if (domains == null || domains.size() == 0) {
        status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Failed to find available domains in target", null));
        return status;
      }

      if (!appDomain.isEmpty()) {
        /* look if the domain is available */
        for (Iterator<Domain> iterator = domains.iterator(); iterator.hasNext();) {
          Domain domain = iterator.next();
          if (appDomain.equals(domain.getDomainName())) {
            this.domain = domain;
            break;
          }
        }

        /* client requested an unavailable domain, fail */
        if (domain == null) {
          String msg = NLS.bind("Failed to find domain {0} in target", appDomain);
          status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
          return status;
        }
      } else {
        /* client has not requested a specific domain, get the first available */
        this.domain = domains.get(0);
      }

      /* find out whether the declared host can be reused */
      String routeGUID = null;
      FindRouteCommand findRouteCommand = new FindRouteCommand(target, getApplication(), domain.getGuid());
      jobStatus = (ServerStatus) findRouteCommand.doIt(); /* FIXME: unsafe type cast */
      status.add(jobStatus);

      if (jobStatus.isOK()) {
        /* extract route guid */
        route = jobStatus.getJsonData();
        routeGUID = route.getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID);

        /* attach route to application */
        AttachRouteCommand attachRoute = new AttachRouteCommand(target, getApplication(), routeGUID);
        jobStatus = (ServerStatus) attachRoute.doIt(); /* FIXME: unsafe type cast */
        status.add(jobStatus);

        if (jobStatus.isOK())
          return status;

        /* the route is bound to another space */
        String msg = NLS.bind("The host {0} is already used in another space.", findRouteCommand.getAppHost());
        status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_CONFLICT, msg, null));
        return status;
      }

      /* create a new route */
      CreateRouteCommand createRoute = new CreateRouteCommand(target, domain, getApplication());
      jobStatus = (ServerStatus) createRoute.doIt(); /* FIXME: unsafe type cast */
      status.add(jobStatus);

      if (!jobStatus.isOK())
        return status;

      /* extract route guid */
      route = jobStatus.getJsonData();
      routeGUID = route.getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID);

      /* attach route to application */
      AttachRouteCommand attachRoute = new AttachRouteCommand(target, getApplication(), routeGUID);
      jobStatus = (ServerStatus) attachRoute.doIt(); /* FIXME: unsafe type cast */
      status.add(jobStatus);

      if (!jobStatus.isOK())
        return status;

      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;
    }
  }
View Full Code Here


      noRoute = (noRouteNode != null) ? Boolean.parseBoolean(noRouteNode.getValue()) : false;

      return Status.OK_STATUS;

    } catch (InvalidAccessException e) {
      return new MultiServerStatus(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, e.getMessage(), null));
    }
  }
View Full Code Here

  }

  @Override
  protected ServerStatus _doIt() {
    /* multi server status */
    MultiServerStatus status = new MultiServerStatus();

    try {

      /* stop the application */
      StopAppCommand stopApp = new StopAppCommand(target, application);
      ServerStatus jobStatus = (ServerStatus) stopApp.doIt();
      status.add(jobStatus);
      if (!jobStatus.isOK())
        return status;

      /* start again */
      StartAppCommand startApp = new StartAppCommand(target, application);
      jobStatus = (ServerStatus) startApp.doIt();
      status.add(jobStatus);
      if (!jobStatus.isOK())
        return status;

      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;
    }
  }
View Full Code Here

  @Override
  protected ServerStatus _doIt() {

    /* multi server status */
    MultiServerStatus status = new MultiServerStatus();

    try {

      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;
    }

  }
View Full Code Here

    this.timeout = -1;
  }

  public ServerStatus _doIt() {
    /* multi server status */
    MultiServerStatus result = new MultiServerStatus();

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

      String appUrl = this.app.getAppJSON().getString("url"); //$NON-NLS-1$
      URI appURI = targetURI.resolve(appUrl);

      PutMethod startMethod = new PutMethod(appURI.toString());
      HttpUtil.configureHttpMethod(startMethod, target);
      startMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$

      JSONObject startCommand = new JSONObject();
      startCommand.put("console", true); //$NON-NLS-1$
      startCommand.put("state", "STARTED"); //$NON-NLS-1$ //$NON-NLS-2$
      StringRequestEntity requestEntity = new StringRequestEntity(startCommand.toString(), CFProtocolConstants.JSON_CONTENT_TYPE, "UTF-8"); //$NON-NLS-1$
      startMethod.setRequestEntity(requestEntity);

      ServerStatus startStatus = HttpUtil.executeMethod(startMethod);
      result.add(startStatus);
      if (!result.isOK())
        return result;

      if (timeout < 0) {
        /* extract user defined timeout if present */
        ManifestParseTree manifest = app.getManifest();
        ManifestParseTree timeoutNode = manifest.get(CFProtocolConstants.V2_KEY_APPLICATIONS).get(0).getOpt(CFProtocolConstants.V2_KEY_TIMEOUT);
        timeout = (timeoutNode != null) ? Integer.parseInt(timeoutNode.getValue()) : ManifestConstants.DEFAULT_TIMEOUT;
      }

      /* long running task, keep track */
      timeout = Math.min(timeout, ManifestConstants.MAX_TIMEOUT);
      int attemptsLeft = timeout / 2;

      String msg = NLS.bind("Can not start the application", commandName); //$NON-NLS-1$
      ServerStatus getInstancesStatus = new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null);

      while (attemptsLeft > 0) {

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

        // check instances
        String appInstancesUrl = appUrl + "/instances"; //$NON-NLS-1$
        URI appInstancesURI = targetURI.resolve(appInstancesUrl);

        GetMethod getInstancesMethod = new GetMethod(appInstancesURI.toString());
        HttpUtil.configureHttpMethod(getInstancesMethod, target);

        getInstancesStatus = HttpUtil.executeMethod(getInstancesMethod);
        if (!getInstancesStatus.isOK()) {
          --attemptsLeft;
          continue;
        }

        JSONObject appInstancesJSON = getInstancesStatus.getJsonData();

        int instancesNo = appInstancesJSON.length();
        int runningInstanceNo = 0;
        int flappingInstanceNo = 0;

        @SuppressWarnings("unchecked")
        Iterator<String> instanceIt = appInstancesJSON.keys();
        while (instanceIt.hasNext()) {
          JSONObject instanceJSON = appInstancesJSON.getJSONObject(instanceIt.next());
          if ("RUNNING".equals(instanceJSON.optString("state"))) //$NON-NLS-1$ //$NON-NLS-2$
            runningInstanceNo++;
          else if ("FLAPPING".equals(instanceJSON.optString("state"))) //$NON-NLS-1$ //$NON-NLS-2$
            flappingInstanceNo++;
        };

        if (runningInstanceNo == instancesNo)
          break;

        if (flappingInstanceNo > 0)
          break;

        --attemptsLeft;
      }

      result.add(getInstancesStatus);
      return 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);
View Full Code Here

  @Override
  protected ServerStatus _doIt() {

    /* multi server status */
    MultiServerStatus status = new MultiServerStatus();

    try {

      /* set up the application */
      AbstractCFCommand setUpApplication = null;
      if (app.getSummaryJSON() != null) {

        /* set known application guid */
        app.setGuid(app.getSummaryJSON().getString(CFProtocolConstants.V2_KEY_GUID));
        setUpApplication = new UpdateApplicationCommand(target, app);

      } else
        setUpApplication = new CreateApplicationCommand(target, app);

      ServerStatus jobStatus = (ServerStatus) setUpApplication.doIt(); /* FIXME: unsafe type cast */
      status.add(jobStatus);
      if (!jobStatus.isOK())
        return status;

      JSONObject respAppJSON = jobStatus.getJsonData();

      /* set up the application route */
      BindRouteCommand bindRoute = new BindRouteCommand(target, app);
      ServerStatus multijobStatus = (ServerStatus) bindRoute.doIt(); /* FIXME: unsafe type cast */
      status.add(multijobStatus);
      if (!multijobStatus.isOK())
        return status;

      /* upload application contents */
      UploadBitsCommand uploadBits = new UploadBitsCommand(target, app, appStore, packager);
      multijobStatus = (ServerStatus) uploadBits.doIt(); /* FIXME: unsafe type cast */
      status.add(multijobStatus);
      if (!multijobStatus.isOK())
        return status;

      /* bind application specific services */
      BindServicesCommand bindServices = new BindServicesCommand(target, app);
      multijobStatus = (ServerStatus) bindServices.doIt(); /* FIXME: unsafe type cast */
      status.add(multijobStatus);
      if (!multijobStatus.isOK())
        return status;

      /* extract user defined timeout if present */
      ManifestParseTree manifest = app.getManifest();
      ManifestParseTree timeoutNode = manifest.get(CFProtocolConstants.V2_KEY_APPLICATIONS).get(0).getOpt(CFProtocolConstants.V2_KEY_TIMEOUT);
      int timeout = (timeoutNode != null) ? Integer.parseInt(timeoutNode.getValue()) : ManifestConstants.DEFAULT_TIMEOUT;

      /* craft command result */
      JSONObject result = new JSONObject();
      result.put("Target", target.toJSON()); //$NON-NLS-1$
      if (target.getManageUrl() != null)
        result.put("ManageUrl", target.getManageUrl().toString() + "#/resources/appGuid=" + app.getGuid() + "&orgGuid=" + target.getOrg().getGuid() + "&spaceGuid=" + target.getSpace().getGuid()); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
      result.put("App", respAppJSON); //$NON-NLS-1$
      result.put("Domain", bindRoute.getDomainName()); //$NON-NLS-1$
      result.put("Route", bindRoute.getRoute()); //$NON-NLS-1$
      result.put("Timeout", timeout); //$NON-NLS-1$
      result.put("DeployedPackage", uploadBits.getDeployedAppPackageName()); //$NON-NLS-1$

      status.add(new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result));
      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;
    }
  }
View Full Code Here

  }

  @Override
  protected ServerStatus _doIt() {
    /* multi server status */
    MultiServerStatus status = new MultiServerStatus();

    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;
              }

              /* create service instance */
              URI serviceInstancesURI = targetURI.resolve("/v2/service_instances"); //$NON-NLS-1$
              PostMethod createServiceMethod = new PostMethod(serviceInstancesURI.toString());
              HttpUtil.configureHttpMethod(createServiceMethod, target);

              /* set request body */
              JSONObject createServiceRequest = new JSONObject();
              createServiceRequest.put(CFProtocolConstants.V2_KEY_SPACE_GUID, target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID));
              createServiceRequest.put(CFProtocolConstants.V2_KEY_NAME, serviceName);
              createServiceRequest.put(CFProtocolConstants.V2_KEY_SERVICE_PLAN_GUID, servicePlanGUID);
              createServiceMethod.setRequestEntity(new StringRequestEntity(createServiceRequest.toString(), "application/json", "utf-8")); //$NON-NLS-1$ //$NON-NLS-2$

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

              resp = jobStatus.getJsonData();
              serviceInstanceGUID = resp.getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID);
            }

            /* bind service to the application */
            URI serviceBindingsURI = targetURI.resolve("/v2/service_bindings"); //$NON-NLS-1$
            PostMethod bindServiceMethod = new PostMethod(serviceBindingsURI.toString());
            HttpUtil.configureHttpMethod(bindServiceMethod, target);

            /* set request body */
            JSONObject bindServiceRequest = new JSONObject();
            bindServiceRequest.put(CFProtocolConstants.V2_KEY_APP_GUID, getApplication().getGuid());
            bindServiceRequest.put(CFProtocolConstants.V2_KEY_SERVICE_INSTANCE_GUID, serviceInstanceGUID);
            bindServiceMethod.setRequestEntity(new StringRequestEntity(bindServiceRequest.toString(), "application/json", "utf-8")); //$NON-NLS-1$ //$NON-NLS-2$

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

        if (version == 6) {

          String spaceGuid = target.getSpace().getGuid();
          URI serviceInstancesURI = targetURI.resolve("/v2/spaces/" + spaceGuid + "/service_instances"); //$NON-NLS-1$//$NON-NLS-2$

          for (ManifestParseTree service : services.getChildren()) {
            String nameService = service.getValue();

            NameValuePair[] pa = new NameValuePair[] {new NameValuePair("return_user_provided_service_instances", "true"), // //$NON-NLS-1$ //$NON-NLS-2$
                new NameValuePair("q", "name:" + nameService), new NameValuePair("inline-relations-depth", "1")}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

            GetMethod getServiceMethod = new GetMethod(serviceInstancesURI.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);
                }
              }
            }

            if (serviceInstanceGUID == null) {
              status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Service instance " + nameService + " can not be found in target space", null));
              return status;
            }

            /* bind service to the application */
            URI serviceBindingsURI = targetURI.resolve("/v2/service_bindings"); //$NON-NLS-1$
            PostMethod bindServiceMethod = new PostMethod(serviceBindingsURI.toString());
            HttpUtil.configureHttpMethod(bindServiceMethod, target);

            /* set request body */
            JSONObject bindServiceRequest = new JSONObject();
            bindServiceRequest.put(CFProtocolConstants.V2_KEY_APP_GUID, getApplication().getGuid());
            bindServiceRequest.put(CFProtocolConstants.V2_KEY_SERVICE_INSTANCE_GUID, serviceInstanceGUID);
            bindServiceMethod.setRequestEntity(new StringRequestEntity(bindServiceRequest.toString(), "application/json", "utf-8")); //$NON-NLS-1$ //$NON-NLS-2$

            /* send request */
            jobStatus = HttpUtil.executeMethod(bindServiceMethod);

            if (!jobStatus.isOK()) {

              /* the binding might be already present - detect it by checking the error code type */
              if (!jobStatus.getJsonData().has("error_code") || !"CF-ServiceBindingAppServiceTaken".equals(jobStatus.getJsonData().getString("error_code"))) { //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
                status.add(jobStatus);
                return status;
              }
            } else
              status.add(jobStatus);
          }
        }
      }

      return status;

    } catch (InvalidAccessException e) {
      status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, e.getMessage(), null));
      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;
    }
  }
View Full Code Here

    this.app = app;
  }

  @Override
  protected ServerStatus _doIt() {
    MultiServerStatus status = new MultiServerStatus();

    try {
      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;
View Full Code Here

TOP

Related Classes of org.eclipse.orion.server.cf.utils.MultiServerStatus

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.