public JSONObject removeNode(String id) {
    JSONObject result = new JSONObject();
    JSONObject targetElement = this.elementCache.get(id);
    JSONObject parent = this.parentCache.get(id);

    JSONArray children = parent.getJSONArray("children");
    if (children != null && !children.isEmpty()) {
      int targetIndex = -1;
      int targetCount = 0;
      for (int i = 0; i < children.size(); i++) {
        JSONObject child = (JSONObject) children.get(i);
        if (child.getString("id").equals(id)) {
          targetIndex = i;
        }
      }

      if (targetIndex >= 0) {
        children.remove(targetIndex);
        this.elementCache.remove(id);
        this.parentCache.remove(id);
      }

      result = result.element("id", id);
    } else {
      result = result.element("error", "could not find target element");
    }

    this.saveMappings();

    return result;
  }
示例#2
0
 private JSONArray getChildrenOfGroup(Integer groupId, String _site) {
   List<Group> groups =
       dao.listByParams(Group.class, "from Group where parentId = ? and _site=?", groupId, _site);
   JSONArray arr = new JSONArray();
   for (Group g : groups) {
     JSONObject jobj = new JSONObject();
     jobj.put("name", g.name);
     jobj.put("id", g.id);
     jobj.put("type", "group");
     jobj.put("key", "group_" + g.id);
     jobj.put("isParent", true);
     JSONArray children = getChildrenOfGroup(g.id, _site);
     if (!children.isEmpty()) {
       jobj.put("children", children);
     }
     arr.add(jobj);
   }
   List<Map> users =
       dao.listAsMap(
           "select u.name as name , u.id as id from User u , UserGroup ug where u.id = ug.uid and ug.gid=? and u._site=?",
           groupId,
           _site);
   for (Map u : users) {
     JSONObject jobj = new JSONObject();
     jobj.put("name", u.get("name"));
     jobj.put("id", u.get("id"));
     jobj.put("key", "user_" + u.get("id"));
     jobj.put("type", "user");
     arr.add(jobj);
   }
   return arr;
 }
  public void find(String binFile, List<SourceFileResult> results) {
    File bin = new File(binFile);
    String url = null;
    String fileDownloaded = null;

    try {
      if (canceled) return;
      InputStream is2 = null;
      String md5;
      try {
        md5 = new String(Hex.encodeHex(Files.getDigest(bin, MessageDigest.getInstance("MD5"))));
        String serviceUrl = SERVICE + "/rest/libraries?md5=" + md5;
        is2 = new URL(serviceUrl).openStream();
        String str = IOUtils.toString(is2);
        JSONArray json = JSONArray.fromObject(str);

        for (int i = 0; i < json.size(); i++) {
          if (canceled) return;
          JSONObject obj = (JSONObject) json.get(i);
          JSONObject source = obj.getJSONObject("source");
          if (source != null && !source.isNullObject()) {
            JSONArray ar = source.getJSONArray("urls");
            if (ar != null && !ar.isEmpty()) {
              String url1 = ar.getString(0);
              String tmpFile = new UrlDownloader().download(url1);
              if (tmpFile != null && isSourceCodeFor(tmpFile, bin.getAbsolutePath())) {
                fileDownloaded = tmpFile;
                url = url1;
                break;
              }
            }
          }
        }

        if (url != null && fileDownloaded != null) {
          String name = url.substring(url.lastIndexOf('/') + 1);

          SourceFileResult object = new SourceFileResult(binFile, fileDownloaded, name, 90);
          Logger.debug(this.toString() + " FOUND: " + object, null);
          results.add(object);
        }

      } finally {
        IOUtils.closeQuietly(is2);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
    /**
     * Build a new instance from parameters a user input in a configuration page.
     *
     * <p>Usually, it is done by {@link StaplerRequest#bindJSON(Class, JSONObject)}, and {@link
     * DataBoundConstructor} of classes of posted objects.
     *
     * <p>But we have to use {@link Descriptor#newInstance(StaplerRequest, JSONObject)} for classes
     * without {@link DataBoundConstructor} (such as {@link hudson.tasks.Mailer}) and classes with
     * {@link Descriptor#newInstance(StaplerRequest, JSONObject)} doing different from their
     * constructors with {@link DataBoundConstructor} (such as {@link
     * hudson.tasks.junit.JUnitResultArchiver}).
     *
     * @param req
     * @param formData
     * @return
     * @throws hudson.model.Descriptor.FormException
     * @see hudson.model.Descriptor#newInstance(org.kohsuke.stapler.StaplerRequest,
     *     net.sf.json.JSONObject)
     * @see ConditionalPublisher#ConditionalPublisher(RunCondition, BuildStep, BuildStepRunner,
     *     boolean, RunCondition, BuildStepRunner)
     */
    @Override
    public ConditionalPublisher newInstance(StaplerRequest req, JSONObject formData)
        throws hudson.model.Descriptor.FormException {
      RunCondition condition = null;
      BuildStepRunner runner = null;
      List<BuildStep> publisherList = null;
      boolean configuredAggregation = false;
      RunCondition aggregationCondition = null;
      BuildStepRunner aggregationRunner = null;
      ConditionalExecutionStrategy executionStrategy = null;

      if (formData != null) {
        condition = req.bindJSON(RunCondition.class, formData.getJSONObject("condition"));
        runner = req.bindJSON(BuildStepRunner.class, formData.getJSONObject("runner"));
        if (formData.has("configuredAggregation")) {
          configuredAggregation = formData.getBoolean("configuredAggregation");
          aggregationCondition =
              req.bindJSON(RunCondition.class, formData.getJSONObject("aggregationCondition"));
          aggregationRunner =
              req.bindJSON(BuildStepRunner.class, formData.getJSONObject("aggregationRunner"));
        }

        JSONArray a = JSONArray.fromObject(formData.get("publisherList"));
        if (a != null && !a.isEmpty()) {
          publisherList = new ArrayList<BuildStep>(a.size());
          for (int idx = 0; idx < a.size(); ++idx) {
            BuildStep buildstep =
                bindJSONWithDescriptor(req, a.getJSONObject(idx), "publisherList");
            if (buildstep != null) publisherList.add(buildstep);
          }
        }
        executionStrategy =
            req.bindJSON(
                ConditionalExecutionStrategy.class, formData.getJSONObject("executionStrategy"));
      }
      return new ConditionalPublisher(
          condition,
          publisherList,
          runner,
          configuredAggregation,
          aggregationCondition,
          aggregationRunner,
          executionStrategy);
    }
示例#5
0
 @WebMethod
 public ModelAndView getUserTree(String _site) {
   ModelAndView mv = new ModelAndView();
   List<Group> groups =
       dao.listByParams(Group.class, "from Group where parentId is null and _site=?", _site);
   JSONArray arr = new JSONArray();
   for (Group g : groups) {
     JSONObject jobj = new JSONObject();
     jobj.put("name", g.name);
     jobj.put("id", g.id);
     jobj.put("key", "group_" + g.id);
     jobj.put("isParent", true);
     jobj.put("type", "group");
     JSONArray children = getChildrenOfGroup(g.id, _site);
     if (!children.isEmpty()) {
       jobj.put("children", children);
     }
     arr.add(jobj);
   }
   mv.returnText = arr.toString();
   return mv;
 }
示例#6
0
  public static String getJSONuiLabelArray(
      HttpServletRequest request, HttpServletResponse response) {
    String requiredLabels = request.getParameter("requiredLabels");

    JSONObject uiLabelObject = null;
    if (UtilValidate.isNotEmpty(requiredLabels)) {
      // Transform JSON String to Object
      uiLabelObject = (JSONObject) JSONSerializer.toJSON(requiredLabels);
    }

    JSONObject jsonUiLabel = new JSONObject();
    Locale locale = request.getLocale();
    if (!uiLabelObject.isEmpty()) {
      Set<String> resourceSet = UtilGenerics.checkSet(uiLabelObject.keySet());
      // Iterate over the resouce set
      for (String resource : resourceSet) {
        JSONArray labels = uiLabelObject.getJSONArray(resource);
        if (labels.isEmpty() || labels == null) {
          continue;
        }

        // Iterate over the uiLabel List
        Iterator<String> jsonLabelIterator = UtilGenerics.cast(labels.iterator());
        JSONArray resourceLabelList = new JSONArray();
        while (jsonLabelIterator.hasNext()) {
          String label = jsonLabelIterator.next();
          String receivedLabel = UtilProperties.getMessage(resource, label, locale);
          if (UtilValidate.isNotEmpty(receivedLabel)) {
            resourceLabelList.add(receivedLabel);
          }
        }
        jsonUiLabel.element(resource, resourceLabelList);
      }
    }

    writeJSONtoResponse(jsonUiLabel, response);
    return "success";
  }