Esempio n. 1
0
  public Object execute(Map properties) {
    WorkflowAssignment wfAssignment = (WorkflowAssignment) properties.get("workflowAssignment");

    String jsonUrl = (String) properties.get("jsonUrl");
    GetMethod get = null;
    try {
      HttpClient client = new HttpClient();

      jsonUrl = WorkflowUtil.processVariable(jsonUrl, "", wfAssignment);

      jsonUrl = StringUtil.encodeUrlParam(jsonUrl);

      get = new GetMethod(jsonUrl);
      client.executeMethod(get);
      InputStream in = get.getResponseBodyAsStream();
      String jsonResponse = streamToString(in);

      Map object = PropertyUtil.getPropertiesValueFromJson(jsonResponse);

      storeToForm(wfAssignment, properties, object);
      storeToWorkflowVariable(wfAssignment, properties, object);

    } catch (Exception ex) {
      LogUtil.error(getClass().getName(), ex, "");
    } finally {
      if (get != null) {
        get.releaseConnection();
      }
    }

    return null;
  }
  protected DataList parseFromJsonParameter(
      ModelMap map, DataList dataList, String id, HttpServletRequest request) {
    // get parameters

    String jsonParam = new ParamEncoder(id).encodeParameterName("json");
    String json = request.getParameter(jsonParam);

    // use preview json if available
    if (json != null && json.trim().length() > 0) {
      try {
        String tempJson = json;
        if (tempJson.contains(SecurityUtil.ENVELOPE)
            || tempJson.contains(PropertyUtil.PASSWORD_PROTECTED_VALUE)) {
          AppDefinition appDef = AppUtil.getCurrentAppDefinition();
          DatalistDefinition datalist = datalistDefinitionDao.loadById(id, appDef);

          if (datalist != null) {
            tempJson = PropertyUtil.propertiesJsonStoreProcessing(datalist.getJson(), tempJson);
          }
        }

        dataList =
            dataListService.fromJson(AppUtil.processHashVariable(tempJson, null, null, null));
        dataList.setId(id);
      } catch (Exception ex) {
        map.addAttribute("dataListError", ex.toString());
      }
    } /* else {
      json = dataListService.toJson(dataList);
      }*/

    String jsonEncoded = null;
    try {
      if (json != null) {
        jsonEncoded = URLEncoder.encode(json, "UTF-8");
      }
    } catch (Exception ex) {
      LogUtil.error(this.getClass().getName(), ex, "parseFromJsonParameter Error!");
    }

    // set for view
    map.addAttribute("json", json);
    map.addAttribute("jsonEncoded", jsonEncoded);
    map.addAttribute("jsonParam", jsonParam);
    return dataList;
  }
Esempio n. 3
0
  protected String streamToString(InputStream in) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        in.close();
      } catch (IOException e) {
        LogUtil.error(getClass().getName(), e, "");
      }
    }

    return sb.toString();
  }
Esempio n. 4
0
  public static String processHashVariable(
      String content,
      WorkflowAssignment wfAssignment,
      String escapeFormat,
      Map<String, String> replaceMap,
      AppDefinition appDef) {
    // check for hash # to avoid unnecessary processing
    if (!containsHashVariable(content)) {
      return content;
    }

    // parse content
    if (content != null) {
      Pattern pattern = Pattern.compile("\\#([^#^\"^ ])*\\.([^#^\"])*\\#");
      Matcher matcher = pattern.matcher(content);
      List<String> varList = new ArrayList<String>();
      while (matcher.find()) {
        varList.add(matcher.group());
      }

      try {
        if (!varList.isEmpty()) {
          PluginManager pluginManager = (PluginManager) appContext.getBean("pluginManager");
          PluginDefaultPropertiesDao pluginDefaultPropertiesDao =
              (PluginDefaultPropertiesDao) appContext.getBean("pluginDefaultPropertiesDao");
          Collection<Plugin> pluginList = pluginManager.list(HashVariablePlugin.class);
          Map<String, HashVariablePlugin> hashVariablePluginCache =
              new HashMap<String, HashVariablePlugin>();

          for (String var : varList) {
            String tempVar = var.replaceAll("#", "");

            for (Plugin p : pluginList) {
              HashVariablePlugin hashVariablePlugin = (HashVariablePlugin) p;
              if (tempVar.startsWith(hashVariablePlugin.getPrefix() + ".")) {
                tempVar = tempVar.replaceFirst(hashVariablePlugin.getPrefix() + ".", "");

                HashVariablePlugin cachedPlugin =
                    hashVariablePluginCache.get(hashVariablePlugin.getClassName());
                if (cachedPlugin == null) {
                  cachedPlugin =
                      (HashVariablePlugin)
                          pluginManager.getPlugin(hashVariablePlugin.getClassName());
                  // get default plugin properties

                  if (appDef == null) {
                    appDef = AppUtil.getCurrentAppDefinition();
                  }
                  PluginDefaultProperties pluginDefaultProperties =
                      pluginDefaultPropertiesDao.loadById(cachedPlugin.getClassName(), appDef);
                  if (pluginDefaultProperties != null
                      && pluginDefaultProperties.getPluginProperties() != null
                      && pluginDefaultProperties.getPluginProperties().trim().length() > 0) {
                    cachedPlugin.setProperties(
                        PropertyUtil.getPropertiesValueFromJson(
                            pluginDefaultProperties.getPluginProperties()));
                  }

                  // put appDef & wfAssignment to properties
                  cachedPlugin.setProperty("appDefinition", appDef);
                  cachedPlugin.setProperty("workflowAssignment", wfAssignment);
                  hashVariablePluginCache.put(hashVariablePlugin.getClassName(), cachedPlugin);
                }

                // process nested hash
                while (tempVar.contains("{") && tempVar.contains("}")) {
                  Pattern nestedPattern = Pattern.compile("\\{([^\\{^\\}])*\\}");
                  Matcher nestedMatcher = nestedPattern.matcher(tempVar);
                  while (nestedMatcher.find()) {
                    String nestedHash = nestedMatcher.group();
                    String nestedHashString = nestedHash.replace("{", "#");
                    nestedHashString = nestedHashString.replace("}", "#");

                    String processedNestedHashValue =
                        processHashVariable(
                            nestedHashString, wfAssignment, escapeFormat, replaceMap, appDef);
                    tempVar =
                        tempVar.replaceAll(
                            StringUtil.escapeString(nestedHash, StringUtil.TYPE_REGEX, null),
                            StringUtil.escapeString(
                                processedNestedHashValue, escapeFormat, replaceMap));
                  }
                }

                // get result from plugin
                String value = cachedPlugin.processHashVariable(tempVar);

                if (value != null && !StringUtil.TYPE_REGEX.equals(escapeFormat)) {
                  value = StringUtil.escapeRegex(value);
                }

                // escape special char in HashVariable
                var = cachedPlugin.escapeHashVariable(var);

                // replace
                if (value != null) {
                  content =
                      content.replaceAll(
                          var, StringUtil.escapeString(value, escapeFormat, replaceMap));
                }
              }
            }
          }
        }
      } catch (Exception ex) {
        LogUtil.error(AppUtil.class.getName(), ex, "");
      }
    }
    return content;
  }