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

    /* 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);
  }
  @Test
  public void getPortの引数にftpスキームのURIを渡した場合_マイナス1が返されること() {

    // Setup
    URI uri = URIUtil.toURI("ftp://www.ambrosoli.jp/"); // $NON-NLS-1$

    // Exercise
    int actual = URIUtil.getPort(uri);

    // Verify
    assertThat(actual, is(-1));
  }
  @Test
  public void getPortの引数にポート番号を指定しないhttpsスキームのURIを渡した場合_443が返されること() {

    // Setup
    URI uri = URIUtil.toURI("https://www.ambrosoli.jp/"); // $NON-NLS-1$

    // Exercise
    int actual = URIUtil.getPort(uri);

    // Verify
    assertThat(actual, is(443));
  }
  @Test
  public void getPortの引数にポート番号8080を指定したhttpスキームのURIを渡した場合_8080が返されること() {

    // Setup
    URI uri = URIUtil.toURI("http://www.ambrosoli.jp:8080/"); // $NON-NLS-1$

    // Exercise
    int actual = URIUtil.getPort(uri);

    // Verify
    assertThat(actual, is(8080));
  }
  @Test
  public void toURIの引数に空文字を渡した場合_nullが返されること() {

    // Setup
    String uri = ""; // $NON-NLS-1$

    // Exercise
    URI actual = URIUtil.toURI(uri);

    // Verify
    assertThat(actual, is(nullValue()));
  }
  @Test
  public void toURIの引数にhttpsスキームのURI文字列を渡した場合_URIが生成されること() {

    // Setup
    String uri = "https://www.ambrosoli.jp/"; // $NON-NLS-1$

    // Exercise
    URI actual = URIUtil.toURI(uri);

    // Verify
    assertThat(actual.toString(), is(equalTo(uri)));
  }
  public void testVirtualFolderInLinkedFolder() {
    // setup handles
    IFolder topFolder = existingProject.getFolder("topFolder");
    IFolder linkedFolder = topFolder.getFolder("linkedFolder");
    IFolder subFolder = linkedFolder.getFolder("subFolder");
    IFolder virtualFolder = subFolder.getFolder("virtualFolder");

    IPath linkedFolderLocation = getRandomLocation();
    IPath subFolderLocation = linkedFolderLocation.append(subFolder.getName());

    try {
      try {
        // create the structure on disk
        linkedFolderLocation.toFile().mkdir();
        subFolderLocation.toFile().mkdir();

        // create the structure in the workspace
        ensureExistsInWorkspace(topFolder, true);
        linkedFolder.createLink(linkedFolderLocation, IResource.NONE, getMonitor());
        virtualFolder.create(IResource.VIRTUAL, true, getMonitor());
      } catch (CoreException e) {
        fail("1.0", e);
      }

      // assert locations
      assertEquals("2.0", linkedFolderLocation, linkedFolder.getLocation());
      assertEquals(
          "3.0", linkedFolderLocation.append(subFolder.getName()), subFolder.getLocation());
      assertTrue("4.0", virtualFolder.isVirtual());
      assertTrue("5.0", virtualFolder.getLocation() == null);

      // assert URIs
      assertEquals("6.0", URIUtil.toURI(linkedFolderLocation), linkedFolder.getLocationURI());
      assertEquals("7.0", URIUtil.toURI(subFolderLocation), subFolder.getLocationURI());
      // assertTrue("8.0", virtualFolder.getLocationURI() == null);
    } finally {
      Workspace.clear(subFolderLocation.toFile());
      Workspace.clear(linkedFolderLocation.toFile());
    }
  }
  @Test
  public void addQueryStringの引数にURIと空文字を渡すと_クエリストリングが追加されないこと() {

    // Setup
    URI uri = URIUtil.toURI("http://www.ambrosoli.jp/"); // $NON-NLS-1$
    String queryString = ""; // $NON-NLS-1$

    // Exercise
    URI actual = URIUtil.addQueryString(uri, queryString);

    // Verify
    assertThat(actual.toString(), is(equalTo("http://www.ambrosoli.jp/"))); // $NON-NLS-1$
  }
  @Override
  protected ServerStatus _doIt() {
    try {
      /* 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.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) {
        return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, null, 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 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);
    }
  }
  /**
   * Creates a URL describing the location of a included schema. First looks up the plug-in in the
   * schema registry, then tries additional source locations, then looks for a co-located plug-in,
   * then looks in the additional search path locations.
   *
   * @param pluginID ID of the plug-in owning the schema
   * @param path the path to the schema inside the plug-in
   * @param parentURL url of the parent schema file
   * @param additionalSearchPath list of additional locations to search; only used with the <code>
   *     pde.convertSchemaToHTML</code> Ant task
   * @return a url location of the included schema or <code>null</code>
   */
  private static URL getPluginRelativePath(
      String pluginID, IPath path, URL parentURL, List<IPath> additionalSearchPath) {
    // Start by looking in the schema registry
    URL url = SchemaRegistry.getSchemaURL(pluginID, path.toString());

    // Next search source locations
    if (url == null) {
      IPluginModelBase model = PluginRegistry.findModel(pluginID);
      if (model != null)
        url = SchemaRegistry.getSchemaFromSourceExtension(model.getPluginBase(), path);
    }

    File parentFile = null;
    if (url == null && parentURL != null) {
      try {
        parentFile = URIUtil.toFile(URIUtil.toURI(parentURL));
      } catch (URISyntaxException e) {
      }
    }

    // If we are running the ant task, see if another project co-located with the parent contains
    // the schema file
    // The project folder must have the plug-in ID as its file name
    if (url == null && parentFile != null) {
      try {
        // assuming schemas are located in: pluginId/schema/schemaFile.exsd (need to go up to parent
        // of plug-in directory)
        File pluginFile = new File(parentFile + "/../../../" + pluginID); // $NON-NLS-1$
        if (pluginFile.isDirectory()) {
          File schemaFile = new File(pluginFile, path.toOSString());
          if (schemaFile.exists()) {
            url = schemaFile.toURL();
          }
          // This is how we would extract the schema from a jar, but in practice this will never be
          // the case
          // because when a bundle is built, the schema files are moved to the source bundle, not
          // the bundle we are checking
          //					} else if (CoreUtility.jarContainsResource(pluginFile, path.toPortableString(),
          // false)) {
          //						url = new URL("jar:file:" + pluginFile.getAbsolutePath() + "!/" + path);
          // //$NON-NLS-1$ //$NON-NLS-2$
        }
      } catch (MalformedURLException e) {
      }
    }

    // If we are running the ant task, additional search locations may be provided
    // The project folder must have the plug-in ID as its file name
    if (url == null && additionalSearchPath != null) {
      for (IPath searchPath : additionalSearchPath) {
        File pluginFile = null;
        if (searchPath.isAbsolute()) {
          // Append plug-in id directly to absolute paths
          pluginFile = new File(searchPath.toFile(), pluginID);
        } else if (parentFile != null) {
          // Append relative path to parent file location
          File file = new File(parentFile, searchPath.toOSString());
          pluginFile = new File(file, pluginID);
        }

        if (pluginFile != null && pluginFile.isDirectory()) {
          try {
            File schemaFile = new File(pluginFile, path.toOSString());
            if (schemaFile.exists()) {
              url = schemaFile.toURL();
              break;
            }
          } catch (MalformedURLException e) {
          }
        }
      }
    }
    return url;
  }