/**
   * Extracts the username from the specified DN. If the username cannot be extracted because the CN
   * is in an unrecognized format, the entire CN is returned. If the CN cannot be extracted because
   * the DN is in an unrecognized format, the entire DN is returned.
   *
   * @param dn the dn to extract the username from
   * @return the exatracted username
   */
  public static String extractUsername(String dn) {
    String username = dn;
    String cn = "";

    // ensure the dn is specified
    if (StringUtils.isNotBlank(dn)) {

      // attempt to locate the cn
      if (dn.startsWith("CN=")) {
        cn = StringUtils.substringBetween(dn, "CN=", ",");
      } else if (dn.startsWith("/CN=")) {
        cn = StringUtils.substringBetween(dn, "CN=", "/");
      } else if (dn.startsWith("C=") || dn.startsWith("/C=")) {
        cn = StringUtils.substringAfter(dn, "CN=");
      } else if (dn.startsWith("/") && StringUtils.contains(dn, "CN=")) {
        cn = StringUtils.substringAfter(dn, "CN=");
      }

      // attempt to get the username from the cn
      if (StringUtils.isNotBlank(cn)) {
        if (cn.endsWith(")")) {
          username = StringUtils.substringBetween(cn, "(", ")");
        } else if (cn.contains(" ")) {
          username = StringUtils.substringAfterLast(cn, " ");
        } else {
          username = cn;
        }
      }
    }

    return username;
  }
  static RabbitConnectionFactoryConfig uriToConnectionConfig(String uri) {
    RabbitConnectionFactoryConfig properties = new RabbitConnectionFactoryConfig();
    if (isNotEmpty(uri)) {
      String username = StringUtils.substringBetween(uri, "amqp://", ":");
      String password = StringUtils.substringBetween(uri, username + ":", "@");
      String hostWithPort = StringUtils.substringBetween(uri, "@", "/");

      // If no virtual host is specified
      if (isEmpty(hostWithPort)) {
        hostWithPort = StringUtils.substringAfter(uri, "@");
      }

      int port = properties.getPort();
      String host = hostWithPort;
      boolean hasPort = StringUtils.contains(hostWithPort, ":");
      if (hasPort) {
        host = StringUtils.substringBefore(hostWithPort, ":");
        port = NumberUtils.toInt(StringUtils.substringAfter(hostWithPort, ":"));
      }
      String virtualHost = StringUtils.substringAfter(uri, hostWithPort + "/");

      properties.setUsername(username);
      properties.setPassword(password);
      properties.setHost(host);
      properties.setPort(port);

      if (isNotEmpty(virtualHost)) {
        properties.setVirtualHost(virtualHost);
      }
    }
    return properties;
  }
 private Set<String> getConversationsWithMessage() {
   Set<String> result = new HashSet<String>();
   for (String k : template.keys("conversation:*:messages")) {
     result.add(StringUtils.substringBetween(k, ":"));
   }
   return result;
 }
  public ActionForward printQuestionnaireAnswer(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    // TODO : this is only available after questionnaire is saved ?
    ActionForward forward = mapping.findForward(Constants.MAPPING_BASIC);
    Map<String, Object> reportParameters = new HashMap<String, Object>();
    ProtocolFormBase protocolForm = (ProtocolFormBase) form;
    ProtocolBase protocol = protocolForm.getActionHelper().getProtocol();
    final int answerHeaderIndex = this.getSelectedLine(request);
    String methodToCall = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
    String formProperty =
        StringUtils.substringBetween(methodToCall, ".printQuestionnaireAnswer.", ".line");
    QuestionnaireHelperBase helper =
        (QuestionnaireHelperBase)
            BeanUtilsBean.getInstance().getPropertyUtils().getProperty(form, formProperty);
    AnswerHeader answerHeader = helper.getAnswerHeaders().get(answerHeaderIndex);
    // TODO : a flag to check whether to print answer or not
    // for release 3 : if questionnaire questions has answer, then print answer.
    reportParameters.put(
        QuestionnaireConstants.QUESTIONNAIRE_SEQUENCE_ID_PARAMETER_NAME,
        answerHeader.getQuestionnaire().getQuestionnaireSeqIdAsInteger());
    reportParameters.put("template", answerHeader.getQuestionnaire().getTemplate());
    reportParameters.put("coeusModuleSubItemCode", answerHeader.getModuleSubItemCode());

    AttachmentDataSource dataStream =
        getQuestionnairePrintingService().printQuestionnaireAnswer(protocol, reportParameters);
    if (dataStream.getData() != null) {
      streamToResponse(dataStream, response);
      forward = null;
    }
    return forward;
  }
 private String parseLineSafe(List<String> lines, int n, int i, int j) {
   if (i + j < n) {
     return StringUtils.substringBetween(lines.get(i + j), "\"");
   }
   log.debug("Out of index == {}:{}", i + j, n);
   return null;
 }
Example #6
0
 private String getListOfGamesForTournament() {
   String result = telnet.readUntil(PROMPT + " ");
   result = StringUtils.remove(result, StringUtils.substringBetween(result, "There are", "\n"));
   result = StringUtils.remove(result, "There are");
   result = StringUtils.remove(result, "\n\r");
   result = StringUtils.remove(result, "fics" + PROMPT);
   return result;
 }
Example #7
0
 public BKBook getBKBook(String url) {
   String content = hc.getAsString(url);
   Document document = Jsoup.parse(content);
   BKBook bb = new BKBook();
   bb.title = url;
   bb.dxid = StringUtils.substringBetween(url, "dxNumber=", "&");
   bb.title = BaokuDownload.normalize(document.select("#topsw").val());
   bb.status = 0;
   return bb;
 }
 /**
  * *
  * <li><b>Substring/Left/Right/Mid</b> - null-safe substring extractions
  * <li><b>SubstringBefore/SubstringAfter/SubstringBetween</b> - substring extraction relative to
  *     other strings
  */
 @Test
 public void testSubstringLeftStringUtils() {
   System.out.println(strOne + ":" + strOne.length());
   System.out.println(StringUtils.substring(strOne, 3));
   System.out.println(StringUtils.substring(strOne, 3, 7));
   System.out.println(StringUtils.substringAfter(strOne, " "));
   System.out.println(StringUtils.substringAfterLast(strOne, " "));
   System.out.println(StringUtils.substringBefore(strOne, " "));
   System.out.println(StringUtils.substringBeforeLast(strOne, " "));
   System.out.println(StringUtils.substringBetween(strOne, "the", "not"));
 }
  /**
   * Every overload of {@link Reader#read()} method delegates to this one so it is enough to
   * override only this one. <br>
   * To skip invalid characters this method shifts only valid chars to left and returns decreased
   * value of the original read method. So after last valid character there will be some unused
   * chars in the buffer.
   *
   * @return Number of read valid characters or <code>-1</code> if end of the underling reader was
   *     reached.
   */
  @Override
  public int read(char[] cbuf, int off, int len) throws IOException {
    int read = super.read(cbuf, off, len);
    // check for end
    if (read == -1) {
      return -1;
    }
    // target position
    int pos = off - 1;

    int entityStart = -1;
    for (int readPos = off; readPos < off + read; readPos++) {
      boolean useChar = true;
      switch (cbuf[readPos]) {
        case '&':
          pos++;
          entityStart = readPos;
          break;
        case ';':
          pos++;
          if (entityStart >= 0) {
            int entityLength = readPos - entityStart + 1;
            if (entityLength <= 5) {
              String entity = new String(cbuf, entityStart, entityLength);
              if (StringUtils.startsWith(entity, "&#")) {
                String numberString = StringUtils.substringBetween(entity, "&#", ";");
                final int value;
                if (StringUtils.startsWith(numberString, "x")) {
                  value = Integer.parseInt(numberString.substring(1), 16);
                } else {
                  value = Integer.parseInt(numberString);
                }
                if (!isValidXMLChar((char) value)) {
                  pos -= entityLength;
                  useChar = false;
                }
              }
            }
          }
          break;
        default:
          if (isValidXMLChar(cbuf[readPos])) {
            pos++;
          } else {
            continue;
          }
      }
      // copy, and skip unwanted characters
      if (pos < readPos && useChar) {
        cbuf[pos] = cbuf[readPos];
      }
    }
    return pos - off + 1;
  }
 /**
  * Parses the method to call attribute to pick off the line number which should have an action
  * performed on it.
  *
  * @param request
  * @return
  */
 protected String getCoiDisclosureDocumentNumber(HttpServletRequest request) {
   String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
   if (StringUtils.isNotBlank(parameterName)) {
     String documentNumber = StringUtils.substringBetween(parameterName, ".disclosureDocNbr", ".");
     if (StringUtils.isEmpty(documentNumber)) {
       return null;
     } else {
       return documentNumber;
     }
   }
   return null;
 }
  public static WebElement BtnMostExpesiveProd() throws Exception {
    try {
      elements = driver.findElement(By.id("ondemand")).findElements(By.className("priceBox"));

      priceStrList = new ArrayList<String>();
      priceList = new ArrayList<Integer>();
      //			get text from the elements

      int i = 0;
      for (WebElement element : elements) {
        priceStrList.add(element.getText());
        i++;
      }

      //			Retrieve number from all strings
      for (i = 0; i < priceStrList.size(); i++) {
        int pos = priceStrList.get(i).indexOf("/");
        String str = priceStrList.get(i).substring(0, pos);
        pos = priceStrList.get(i).indexOf("$");
        str = str.substring(pos + 1, str.length());
        priceList.add(Integer.parseInt(str));
      }

      //			Compare the price and get the most expensive
      for (i = 0; i < priceList.size() - 1; i++) {
        priceTopProdPage = priceList.get(0);
        if (priceList.get(i + 1) > priceList.get(i)) {
          priceTopProdPage = priceList.get(i + 1);
        }
      }

      //			get the product bundle class containing the product of top price
      //			get the product bundle elements by looking for partial class name
      elements = driver.findElement(By.id("ondemand")).findElements(By.className("pricingBox "));
      for (WebElement element : elements) {
        String str = element.getText();
        str = StringUtils.substringBetween(str, "$", "/mo");
        strPriceProdPage = "$" + str + "/mo";
        int price = Integer.parseInt(str);
        if (price == priceTopProdPage) {
          BtnTrialTopPrice = element.findElement(By.className("buttonTxt"));
          break;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return BtnTrialTopPrice;
  }
  // Add wordnet test ?
  public static String getSecondaryEntity(String title) {
    String potentialEntity = null;

    // e.g. National_Party_(South_Africa)
    if (title.contains("(") && title.endsWith(")")) {
      potentialEntity = StringUtils.substringBetween(title, "(", ")");
      String[] parts = potentialEntity.split(",");

      // e.g. National_Party_(UK,_1976)
      if (parts.length > 1 && StringUtils.containsOnly(parts[1], "_0123456789")) {
        potentialEntity = parts[0];
      }
    }

    // e.g. Chicago,_Illinois
    else if (title.contains(",")) {
      potentialEntity = StringUtils.substringAfterLast(title, ",");
    }

    // e.g. New_Progressive_Party_of_Puerto_Rico
    // Might need to be careful
    else if (title.contains("of")) {
      potentialEntity = StringUtils.substringAfterLast(title, "of");
      if (title.startsWith("List_of")) {
        String[] tokens = potentialEntity.split("_");
        int capPos = 0;
        StringBuilder sb = new StringBuilder();
        while (capPos < tokens.length
            && (StringUtils.isEmpty(tokens[capPos])
                || WordFeatures.isCapitalized(tokens[capPos]))) {
          sb.append(tokens[capPos]).append('_');
          capPos++;
        }
        potentialEntity = sb.toString();
      }
    }

    // Removes extra chars
    if (potentialEntity != null)
      potentialEntity = potentialEntity.replace('_', ' ').trim().replace(' ', '_');

    try {
      if (wiki == null || wiki.getTitleIdOf(potentialEntity) >= 0) return potentialEntity;
    } catch (Exception e) {
    }

    return null;
  }
Example #13
0
 public String replaceMVSTagWithValue(String text) {
   String res = text;
   int n = 0;
   while (text.indexOf("[pattern_dictonary:") != -1 && n < 20) {
     n++;
     String pattern = StringUtils.substringBetween(text, "[pattern_dictonary:", "]");
     LOG.info("Found pattern in the text: " + pattern);
     String[] params = pattern.split(":");
     if (params.length > 2) {
       LOG.info("Have to replace pattern with ID:" + params[1] + " and column:" + params[2]);
       Map<String, String> patternValues = VALUES.get(params[0]);
       if (patternValues == null) {
         synchronized (VALUES) {
           loadDictionary(params[0]);
           patternValues = VALUES.get(params[0]);
         }
       }
       if (patternValues == null) {
         LOG.error("Unable to find dictionary value from the path: " + params[0]);
         return res;
       }
       LOG.info("Pattern value for the specified ID: " + patternValues);
       if (!patternValues.isEmpty()) {
         String patternValue = patternValues.get(params[1]);
         String[] patternColumns = patternValue.split(";");
         String valueToReplace =
             patternColumns[
                 Integer.valueOf(params[2])
                     - 1]; // value in the map starts from second column in csv file
         LOG.info("Replacing pattern with the value " + valueToReplace);
         res = StringUtils.replace(text, "[pattern_dictonary:" + pattern + "]", valueToReplace);
       }
     }
     text = res;
   }
   return res;
 }
 public List<Conversation> findByVisitor(String visitor, int pageFrom, int pageSize) {
   List<Conversation> result = new ArrayList<Conversation>();
   template.delete(KeyUtils.conversationVisitor(visitor));
   for (String k : template.keys(KeyUtils.conversationProductIdAndVisitorKey(visitor))) {
     template
         .opsForSet()
         .add(KeyUtils.conversationVisitor(visitor), template.opsForValue().get(k));
   }
   template.delete(KeyUtils.conversationWithMessageKey());
   for (String k : template.keys("conversation:*:messages")) {
     template
         .opsForSet()
         .add(KeyUtils.conversationWithMessageKey(), StringUtils.substringBetween(k, ":"));
   }
   template
       .opsForSet()
       .intersectAndStore(
           KeyUtils.conversationVisitor(visitor),
           KeyUtils.conversationWithMessageKey(),
           KeyUtils.conversationVisitor(visitor));
   List<String> sort =
       template.sort(
           SortQueryBuilder.sort(KeyUtils.conversationVisitor(visitor))
               .order(SortParameters.Order.DESC)
               .limit(pageFrom, pageSize)
               .build());
   for (String covId : sort) {
     Conversation c = findBy(Long.valueOf(covId));
     String messageQueue = KeyUtils.conversationMessage(Long.valueOf(covId));
     for (String msgId : template.opsForList().range(messageQueue, 0, 0)) {
       c.addMessage(messageDao.findBy(msgId));
       result.add(c);
     }
   }
   return result;
 }
Example #15
0
 private static String body(String value) {
   return StringUtils.substringBetween(value, "[", "]");
 }
  @SuppressWarnings({"resource", "rawtypes", "unchecked"})
  public static void main(String[] args) throws Exception {
    Configuration cfg = new Configuration();
    // 设置FreeMarker的模版文件位置
    cfg.setClassForTemplateLoading(
        SourceCodeFrameworkBuilder.class, "/lab/s2jh/tool/builder/freemarker");
    cfg.setDefaultEncoding("UTF-8");
    String rootPath = args[0];

    Set<String> entityNames = new HashSet<String>();

    String entityListFile = rootPath + "entity_list.properties";
    BufferedReader reader = new BufferedReader(new FileReader(entityListFile));
    String line;
    while ((line = reader.readLine()) != null) {
      if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
        entityNames.add(line);
      }
    }

    new File(rootPath + "\\codes").mkdir();
    new File(rootPath + "\\codes\\integrate").mkdir();
    new File(rootPath + "\\codes\\standalone").mkdir();

    for (String entityName : entityNames) {

      String integrateRootPath = rootPath + "\\codes\\integrate\\";
      String standaloneRootPath = rootPath + "\\codes\\standalone\\";

      String rootPackage = StringUtils.substringBetween(entityName, "[", "]");
      String rootPackagePath = StringUtils.replace(rootPackage, ".", "\\");

      String className = StringUtils.substringAfterLast(entityName, ".");
      String classFullName =
          StringUtils.replaceEach(entityName, new String[] {"[", "]"}, new String[] {"", ""});

      String modelName = StringUtils.substringBetween(entityName, "].", ".entity");
      String modelPath = StringUtils.replace(modelName, ".", "/");
      modelPath = "/" + modelPath;
      String modelPackagePath = StringUtils.replace(modelName, ".", "\\");
      modelPackagePath = "\\" + modelPackagePath;

      Map<String, Object> root = new HashMap<String, Object>();
      String nameField = propertyToField(StringUtils.uncapitalize(className)).toLowerCase();
      root.put("model_name", modelName);
      root.put("model_path", modelPath);
      root.put("entity_name", className);
      root.put("entity_name_uncapitalize", StringUtils.uncapitalize(className));
      root.put("entity_name_field", nameField);
      root.put("root_package", rootPackage + "." + modelName);
      root.put("action_package", rootPackage);
      root.put("table_name", "T_TODO_" + className.toUpperCase());
      root.put("base", "${base}");
      Class entityClass = Class.forName(classFullName);
      root.put("id_type", entityClass.getMethod("getId").getReturnType().getSimpleName());
      MetaData classEntityComment = (MetaData) entityClass.getAnnotation(MetaData.class);
      if (classEntityComment != null) {
        root.put("model_title", classEntityComment.value());
      } else {
        root.put("model_title", entityName);
      }
      debug("Entity Data Map=" + root);

      Set<Field> fields = new HashSet<Field>();

      Field[] curfields = entityClass.getDeclaredFields();
      for (Field field : curfields) {
        fields.add(field);
      }

      Class superClass = entityClass.getSuperclass();
      while (superClass != null && !superClass.equals(BaseEntity.class)) {
        Field[] superfields = superClass.getDeclaredFields();
        for (Field field : superfields) {
          fields.add(field);
        }
        superClass = superClass.getSuperclass();
      }

      // 定义用于OneToOne关联对象的Fetch参数
      Map<String, String> fetchJoinFields = Maps.newHashMap();
      List<EntityCodeField> entityFields = new ArrayList<EntityCodeField>();
      int cnt = 1;
      for (Field field : fields) {
        if ((field.getModifiers() & Modifier.FINAL) != 0 || "id".equals(field.getName())) {
          continue;
        }
        debug(" - Field=" + field);
        Class fieldType = field.getType();

        EntityCodeField entityCodeField = null;
        if (fieldType.isEnum()) {
          entityCodeField = new EntityCodeField();
          entityCodeField.setListFixed(true);
          entityCodeField.setListWidth(80);
          entityCodeField.setListAlign("center");
        } else if (fieldType == Boolean.class) {
          entityCodeField = new EntityCodeField();
          entityCodeField.setListFixed(true);
          entityCodeField.setListWidth(60);
          entityCodeField.setListAlign("center");
        } else if (PersistableEntity.class.isAssignableFrom(fieldType)) {
          entityCodeField = new EntityCodeField();
          entityCodeField.setFieldType("Entity");

        } else if (Number.class.isAssignableFrom(fieldType)) {
          entityCodeField = new EntityCodeField();
          entityCodeField.setListFixed(true);
          entityCodeField.setListWidth(60);
          entityCodeField.setListAlign("right");
        } else if (fieldType == String.class) {
          entityCodeField = new EntityCodeField();

          // 根据Hibernate注解的字符串类型和长度设定是否列表显示
          Method getMethod = entityClass.getMethod("get" + StringUtils.capitalize(field.getName()));
          Column fieldColumn = getMethod.getAnnotation(Column.class);
          if (fieldColumn != null) {
            int length = fieldColumn.length();
            if (length > 255) {
              entityCodeField.setList(false);
              entityCodeField.setListWidth(length);
            }
          }
          Lob fieldLob = getMethod.getAnnotation(Lob.class);
          if (fieldLob != null) {
            entityCodeField.setList(false);
            entityCodeField.setListWidth(Integer.MAX_VALUE);
          }
        } else if (fieldType == Date.class) {
          entityCodeField = new EntityCodeField();
          entityCodeField.setListFixed(true);

          // 根据Json注解设定合理的列宽
          entityCodeField.setListWidth(120);
          Method getMethod = entityClass.getMethod("get" + StringUtils.capitalize(field.getName()));
          JsonSerialize fieldJsonSerialize = getMethod.getAnnotation(JsonSerialize.class);
          if (fieldJsonSerialize != null) {
            if (DateJsonSerializer.class.equals(fieldJsonSerialize.using())) {
              entityCodeField.setListWidth(80);
            }
          }
          entityCodeField.setListAlign("center");
        }

        if (entityCodeField != null) {
          if (fieldType.isEnum()) {
            entityCodeField.setEnumField(true);
          }
          if (StringUtils.isBlank(entityCodeField.getFieldType())) {
            entityCodeField.setFieldType(fieldType.getSimpleName());
          }
          entityCodeField.setFieldName(field.getName());
          EntityAutoCode entityAutoCode = field.getAnnotation(EntityAutoCode.class);
          if (entityAutoCode != null) {
            entityCodeField.setListHidden(entityAutoCode.listHidden());
            entityCodeField.setEdit(entityAutoCode.edit());
            entityCodeField.setList(entityAutoCode.listHidden() || entityAutoCode.listShow());
            entityCodeField.setOrder(entityAutoCode.order());
          } else {
            entityCodeField.setTitle(field.getName());
            entityCodeField.setOrder(cnt++);
          }

          MetaData entityMetaData = field.getAnnotation(MetaData.class);
          if (entityMetaData != null) {
            entityCodeField.setTitle(entityMetaData.value());
          }

          Method getMethod = entityClass.getMethod("get" + StringUtils.capitalize(field.getName()));
          JsonProperty fieldJsonProperty = getMethod.getAnnotation(JsonProperty.class);
          if (fieldJsonProperty != null) {
            entityCodeField.setList(true);
          }

          if (entityCodeField.getList() || entityCodeField.getListHidden()) {
            JoinColumn fieldJoinColumn = getMethod.getAnnotation(JoinColumn.class);
            if (fieldJoinColumn != null) {
              if (fieldJoinColumn.nullable() == false) {
                fetchJoinFields.put(field.getName(), "INNER");
              } else {
                fetchJoinFields.put(field.getName(), "LEFT");
              }
            }
          }

          entityFields.add(entityCodeField);
        }
      }
      Collections.sort(entityFields);
      root.put("entityFields", entityFields);
      if (fetchJoinFields.size() > 0) {
        root.put("fetchJoinFields", fetchJoinFields);
      }

      integrateRootPath = integrateRootPath + rootPackagePath + modelPackagePath;
      // process(cfg.getTemplate("Entity.ftl"), root, integrateRootPath + "\\entity\\", className +
      // ".java");
      process(
          cfg.getTemplate("Dao.ftl"), root, integrateRootPath + "\\dao\\", className + "Dao.java");
      process(
          cfg.getTemplate("Service.ftl"),
          root,
          integrateRootPath + "\\service\\",
          className + "Service.java");
      process(
          cfg.getTemplate("Controller.ftl"),
          root,
          integrateRootPath + "\\web\\action\\",
          className + "Controller.java");
      process(
          cfg.getTemplate("Test.ftl"),
          root,
          integrateRootPath + "\\test\\service\\",
          className + "ServiceTest.java");
      process(
          cfg.getTemplate("JSP_Index.ftl"),
          root,
          integrateRootPath + "\\jsp\\",
          nameField + "-index.jsp");
      process(
          cfg.getTemplate("JSP_Input_Tabs.ftl"),
          root,
          integrateRootPath + "\\jsp\\",
          nameField + "-inputTabs.jsp");
      process(
          cfg.getTemplate("JSP_Input_Basic.ftl"),
          root,
          integrateRootPath + "\\jsp\\",
          nameField + "-inputBasic.jsp");
      process(
          cfg.getTemplate("JSP_View_Tabs.ftl"),
          root,
          integrateRootPath + "\\jsp\\",
          nameField + "-viewTabs.jsp");
      process(
          cfg.getTemplate("JSP_View_Basic.ftl"),
          root,
          integrateRootPath + "\\jsp\\",
          nameField + "-viewBasic.jsp");

      standaloneRootPath =
          standaloneRootPath + rootPackagePath + modelPackagePath + "\\" + className;
      // process(cfg.getTemplate("Entity.ftl"), root, standaloneRootPath + "\\entity\\", className +
      // ".java");
      process(
          cfg.getTemplate("Dao.ftl"), root, standaloneRootPath + "\\dao\\", className + "Dao.java");
      process(
          cfg.getTemplate("Service.ftl"),
          root,
          standaloneRootPath + "\\service\\",
          className + "Service.java");
      process(
          cfg.getTemplate("Controller.ftl"),
          root,
          standaloneRootPath + "\\web\\action\\",
          className + "Controller.java");
      process(
          cfg.getTemplate("Test.ftl"),
          root,
          standaloneRootPath + "\\test\\service\\",
          className + "ServiceTest.java");
      process(
          cfg.getTemplate("JSP_Index.ftl"),
          root,
          standaloneRootPath + "\\jsp\\",
          nameField + "-index.jsp");
      process(
          cfg.getTemplate("JSP_Input_Tabs.ftl"),
          root,
          standaloneRootPath + "\\jsp\\",
          nameField + "-inputTabs.jsp");
      process(
          cfg.getTemplate("JSP_Input_Basic.ftl"),
          root,
          standaloneRootPath + "\\jsp\\",
          nameField + "-inputBasic.jsp");
      process(
          cfg.getTemplate("JSP_View_Tabs.ftl"),
          root,
          standaloneRootPath + "\\jsp\\",
          nameField + "-viewTabs.jsp");
      process(
          cfg.getTemplate("JSP_View_Basic.ftl"),
          root,
          standaloneRootPath + "\\jsp\\",
          nameField + "-viewBasic.jsp");
    }
  }
Example #17
0
  @Override
  public List<CuriosityFare> getFare(CuriositySearch search)
      throws IOException, InterruptedException, ExecutionException {
    try {
      List<CuriosityFare> fareDepartures = Lists.newArrayList();
      List<CuriosityFare> fareArrvials = Lists.newArrayList();
      List<CuriosityFare> fares = Lists.newArrayList();
      AirfranceFlightResponseFirst responseFirst = new AirfranceFlightResponseFirst();
      Response resultFirst = responseFirst.getResponse(search);
      Document document = Jsoup.parse(resultFirst.getResponseBody());

      FluentStringsMap requestParams = new FluentStringsMap();
      for (Element element : document.select("input[type=hidden]")) {
        requestParams.add(element.attr("name"), element.val());
      }

      search.createParam("requestParam", requestParams);

      String sId = StringUtils.substringBetween(document.toString(), "var Session='", "';");
      search.createParam("sid", "JSESSIONID=" + sId);
      AirfranceFlightResponseSecond responseSecond = new AirfranceFlightResponseSecond();

      Response resultSecond = responseSecond.getResponse(search);
      String contentSecond = resultSecond.getResponseBody();

      AirfranceFlightResponseThird responseThird = new AirfranceFlightResponseThird();
      String contentThird = responseThird.getResponse(search).getResponseBody();
      Document documentThird = Jsoup.parse(contentThird);
      String currency =
          StringUtils.substringBetween(contentThird, "tc_vars_miniRecap[\"currency\"] = \"", "\";");
      Float pricePerChild = 0F;
      Float basePriceDep = 0F;
      Float basePriceArr = 0F;
      Float basePricePerAdult = 0F;
      // System.err.print(documentThird);
      if (search.getChildrenCount() > 0) {
        String[] basePricePerAdultArr =
            documentThird.select("div#idUpsellTotalPrice").text().split(" ");

        if (basePricePerAdultArr.length == 3) {
          basePricePerAdult = Float.parseFloat(basePricePerAdultArr[0] + basePricePerAdultArr[1]);
        }
        String[] totalBaseArr =
            documentThird.select("div#idUpsellTotalPriceMultiPax").text().split(" ");
        Float totalBase = 0F;
        if (totalBaseArr.length == 3) {
          totalBase = Float.parseFloat(totalBaseArr[0] + totalBaseArr[1]);
        }
        pricePerChild =
            (totalBase - (basePricePerAdult * search.getAdultsCount())) / search.getChildrenCount();
      }
      String depContent =
          StringUtils.substringBetween(
              contentThird,
              "</script> <dl> <dt class=\"etape\"> <div class=\"titre\" id=\"idUpsellTitle0\">",
              "tc_product_css = new Array();");
      if (depContent != null && !depContent.equals("")) {
        Document depDocument = Jsoup.parse(depContent);
        Elements depElements = depDocument.select("table.upsellRow");
        for (Element element : depElements) {
          Elements segmentElements = element.select("tbody>tr");
          Float pricePerAdult = 0F;
          if (segmentElements.size() > 0) {
            pricePerAdult =
                Float.parseFloat(segmentElements.get(0).select("input[name=price]").val());
          }
          Float priceTotal =
              Float.parseFloat(segmentElements.get(0).select("input[name=priceMultiPax]").val());
          List<CuriositySegment> outboundSegments = parseSegments(element);
          CuriosityFare fare = new CuriosityFare();

          // fare.setTotalPrice(pricePerAdult*search.getAdultsCount(),search);
          fare.setOutboundSegments(outboundSegments);
          fare.setCurrencyCode(currency);
          fare.setAirlineCode("AF");
          fare.setPrice(priceTotal);
          fare.setPricePerAdult(priceTotal / search.getPassengersCount());
          if (search.getChildrenCount() > 0) {
            fare.setPricePerChild(priceTotal / search.getPassengersCount());
          }
          fareDepartures.add(fare);
        }

        if (search.isRoundtrip()) {
          // System.err.print(contentThird);
          int i = 0;
          for (CuriosityFare depFare : fareDepartures) {
            search.getParams().clear();
            search.createParam("sid", "JSESSIONID=" + sId);
            search.createParam("selected", i);
            AirfranceFlightResponseFour responseFour = new AirfranceFlightResponseFour();
            String contentFour = responseFour.getResponse(search).getResponseBody();
            // System.err.print(contentFour);
            //					AirfranceFlightResponseThird newResponseThird = new
            // AirfranceFlightResponseThird();
            //					String newContentThird = newResponseThird.getResponse(search).getResponseBody();
            //	System.err.print(newContentThird);
            String arrContent =
                StringUtils.substringBetween(
                    contentThird,
                    "</script> <dl> <dt class=\"etape\"> <div class=\"titre\" id=\"idUpsellTitle1\">",
                    "tc_product_css = new Array();");
            Document arrpDocument = Jsoup.parse(contentFour);
            Elements arrElements = arrpDocument.select("table.upsellRow");
            // System.err.print(arrElements);
            for (Element element : arrElements) {
              String isCheap =
                  element
                      .select("tbody>tr")
                      .get(0)
                      .select("td.upsellRadio>span.upsellFareCheap")
                      .text();
              if (isCheap != null && !isCheap.equalsIgnoreCase("")) {
                Elements segmentElements = element.select("tbody>tr");
                Float pricePerAdult = 0F;
                if (segmentElements.size() > 0) {
                  pricePerAdult =
                      Float.parseFloat(segmentElements.get(0).select("input[name=price]").val());
                }
                List<CuriositySegment> inboundSegments = parseSegments(element);
                CuriosityFare fare = new CuriosityFare();
                fare.setOutboundSegments(depFare.getOutboundSegments());
                fare.setInboundSegments(inboundSegments);
                Float basePricePerAdultRT = depFare.getPricePerAdult();
                fare.setPricePerAdult(depFare.getPrice() / search.getPassengersCount());
                if (search.getChildrenCount() > 0) {
                  fare.setPricePerChild(depFare.getPrice() / search.getPassengersCount());
                }
                fare.setPrice(depFare.getPrice());
                fare.setCurrencyCode(currency);
                fare.setAirlineCode("AF");
                fares.add(fare);
              }
            }
            i++;
          }

        } else {
          //				if (search.getChildrenCount()>0) {
          //					for (CuriosityFare fareDep : fareDepartures) {
          //						Float basePricePerAdultOW = fareDep.getBasePricePerAdult();
          //						Float price = fareDep.getPrice();
          //						price +=
          // ((basePricePerAdultOW/basePricePerAdult)*pricePerChild)*search.getChildrenCount();
          //						fareDep.setPrice(price);
          //						fares.add(fareDep);
          //					}
          //				}else{
          fares.addAll(fareDepartures);
          //				}
        }
      }
      return fares;
    } catch (Exception e) {
      return null;
    }
  }
  @Override
  public DownloadableContent getContent(final ContentRequestContext request) {
    // if clustered, send request to cluster manager
    if (properties.isClustered()
        && clusterCoordinator != null
        && clusterCoordinator.isConnected()) {
      // get the URI
      URI dataUri;
      try {
        dataUri = new URI(request.getDataUri());
      } catch (final URISyntaxException use) {
        throw new ClusterRequestException(use);
      }

      // set the request parameters
      final MultivaluedMap<String, String> parameters = new MultivaluedMapImpl();
      parameters.add(CLIENT_ID_PARAM, request.getClientId());

      // set the headers
      final Map<String, String> headers = new HashMap<>();
      if (StringUtils.isNotBlank(request.getProxiedEntitiesChain())) {
        headers.put("X-ProxiedEntitiesChain", request.getProxiedEntitiesChain());
      }

      // add the user's authorities (if any) to the headers
      final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
      if (authentication != null) {
        final Object userDetailsObj = authentication.getPrincipal();
        if (userDetailsObj instanceof NiFiUserDetails) {
          // serialize user details object
          final String hexEncodedUserDetails =
              WebUtils.serializeObjectToHex((Serializable) userDetailsObj);

          // put serialized user details in header
          headers.put("X-ProxiedEntityUserDetails", hexEncodedUserDetails);
        }
      }

      // ensure we were able to detect the cluster node id
      if (request.getClusterNodeId() == null) {
        throw new IllegalArgumentException("Unable to determine the which node has the content.");
      }

      // get the target node and ensure it exists
      final NodeIdentifier nodeId =
          clusterCoordinator.getNodeIdentifier(request.getClusterNodeId());
      final Set<NodeIdentifier> targetNodes = Collections.singleton(nodeId);

      // replicate the request to the specific node
      NodeResponse nodeResponse;
      try {
        nodeResponse =
            requestReplicator
                .replicate(targetNodes, HttpMethod.GET, dataUri, parameters, headers)
                .awaitMergedResponse();
      } catch (InterruptedException e) {
        throw new IllegalClusterStateException(
            "Interrupted while waiting for a response from node");
      }

      final ClientResponse clientResponse = nodeResponse.getClientResponse();
      final MultivaluedMap<String, String> responseHeaders = clientResponse.getHeaders();

      // ensure an appropriate response
      if (Status.NOT_FOUND.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()) {
        throw new ResourceNotFoundException(clientResponse.getEntity(String.class));
      } else if (Status.FORBIDDEN.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()
          || Status.UNAUTHORIZED.getStatusCode()
              == clientResponse.getStatusInfo().getStatusCode()) {
        throw new AccessDeniedException(clientResponse.getEntity(String.class));
      } else if (Status.OK.getStatusCode() != clientResponse.getStatusInfo().getStatusCode()) {
        throw new IllegalStateException(clientResponse.getEntity(String.class));
      }

      // get the file name
      final String contentDisposition = responseHeaders.getFirst("Content-Disposition");
      final String filename = StringUtils.substringBetween(contentDisposition, "filename=\"", "\"");

      // get the content type
      final String contentType = responseHeaders.getFirst("Content-Type");

      // create the downloadable content
      return new DownloadableContent(filename, contentType, clientResponse.getEntityInputStream());
    } else {
      // example URIs:
      // http://localhost:8080/nifi-api/provenance/events/{id}/content/{input|output}
      // http://localhost:8080/nifi-api/flowfile-queues/{uuid}/flowfiles/{uuid}/content

      // get just the context path for comparison
      final String dataUri = StringUtils.substringAfter(request.getDataUri(), "/nifi-api");
      if (StringUtils.isBlank(dataUri)) {
        throw new IllegalArgumentException("The specified data reference URI is not valid.");
      }

      // flowfile listing content
      final Matcher flowFileMatcher = FLOWFILE_CONTENT_URI_PATTERN.matcher(dataUri);
      if (flowFileMatcher.matches()) {
        final String connectionId = flowFileMatcher.group(1);
        final String flowfileId = flowFileMatcher.group(2);

        return getFlowFileContent(connectionId, flowfileId, dataUri);
      }

      // provenance event content
      final Matcher provenanceMatcher = PROVENANCE_CONTENT_URI_PATTERN.matcher(dataUri);
      if (provenanceMatcher.matches()) {
        try {
          final Long eventId = Long.parseLong(provenanceMatcher.group(1));
          final ContentDirection direction =
              ContentDirection.valueOf(provenanceMatcher.group(2).toUpperCase());

          return getProvenanceEventContent(eventId, dataUri, direction);
        } catch (final IllegalArgumentException iae) {
          throw new IllegalArgumentException("The specified data reference URI is not valid.");
        }
      }

      // invalid uri
      throw new IllegalArgumentException("The specified data reference URI is not valid.");
    }
  }
  @Override
  @PreAuthorize("hasRole('ROLE_PROVENANCE')")
  public DownloadableContent getContent(final ContentRequestContext request) {
    // if clustered, send request to cluster manager
    if (properties.isClusterManager()) {
      // get the URI
      URI dataUri;
      try {
        dataUri = new URI(request.getDataUri());
      } catch (final URISyntaxException use) {
        throw new ClusterRequestException(use);
      }

      // set the request parameters
      final MultivaluedMap<String, String> parameters = new MultivaluedMapImpl();
      parameters.add(CLIENT_ID_PARAM, request.getClientId());

      // set the headers
      final Map<String, String> headers = new HashMap<>();
      if (StringUtils.isNotBlank(request.getProxiedEntitiesChain())) {
        headers.put("X-ProxiedEntitiesChain", request.getProxiedEntitiesChain());
      }

      // add the user's authorities (if any) to the headers
      final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
      if (authentication != null) {
        final Object userDetailsObj = authentication.getPrincipal();
        if (userDetailsObj instanceof NiFiUserDetails) {
          // serialize user details object
          final String hexEncodedUserDetails =
              WebUtils.serializeObjectToHex((Serializable) userDetailsObj);

          // put serialized user details in header
          headers.put("X-ProxiedEntityUserDetails", hexEncodedUserDetails);
        }
      }

      // get the target node and ensure it exists
      final Node targetNode = clusterManager.getNode(request.getClusterNodeId());
      if (targetNode == null) {
        throw new UnknownNodeException("The specified cluster node does not exist.");
      }

      final Set<NodeIdentifier> targetNodes = new HashSet<>();
      targetNodes.add(targetNode.getNodeId());

      // replicate the request to the specific node
      final NodeResponse nodeResponse =
          clusterManager.applyRequest(HttpMethod.GET, dataUri, parameters, headers, targetNodes);
      final ClientResponse clientResponse = nodeResponse.getClientResponse();
      final MultivaluedMap<String, String> responseHeaders = clientResponse.getHeaders();

      // get the file name
      final String contentDisposition = responseHeaders.getFirst("Content-Disposition");
      final String filename = StringUtils.substringBetween(contentDisposition, "filename=\"", "\"");

      // get the content type
      final String contentType = responseHeaders.getFirst("Content-Type");

      // create the downloadable content
      return new DownloadableContent(filename, contentType, clientResponse.getEntityInputStream());
    } else {
      // example URI: http://localhost:8080/nifi-api/controller/provenance/events/1/content/input
      final String eventDetails = StringUtils.substringAfterLast(request.getDataUri(), "events/");
      final String rawEventId = StringUtils.substringBefore(eventDetails, "/content/");
      final String rawDirection = StringUtils.substringAfterLast(eventDetails, "/content/");

      // get the content type
      final Long eventId;
      final ContentDirection direction;
      try {
        eventId = Long.parseLong(rawEventId);
        direction = ContentDirection.valueOf(rawDirection.toUpperCase());
      } catch (final IllegalArgumentException iae) {
        throw new IllegalArgumentException("The specified data reference URI is not valid.");
      }
      return serviceFacade.getContent(eventId, request.getDataUri(), direction);
    }
  }
Example #20
0
  private String markdownToHtml(
      RootNode rootNode,
      String projectName,
      String branchName,
      String path,
      Authentication authentication,
      Locale locale,
      boolean nonCacheableMacros,
      String contextPath) {

    HtmlSerializerContext context =
        new HtmlSerializerContext(
            projectName,
            branchName,
            path,
            this,
            authentication,
            locale,
            pageStore,
            systemSettingsStore,
            contextPath);
    HtmlSerializer serializer = new HtmlSerializer(context);
    String html = serializer.toHtml(rootNode);

    List<MacroInvocation> macroInvocations = Lists.newArrayList(context.getMacroInvocations());
    // reverse order so that inner invocations will be processed before outer
    Collections.reverse(macroInvocations);
    int nonCacheableMacroIdx = 1;
    for (MacroInvocation invocation : macroInvocations) {
      IMacro macro = macroFactory.get(invocation.getMacroName());
      if (macro == null) {
        macro = new UnknownMacroMacro();
      }
      IMacroDescriptor macroDescriptor = macro.getDescriptor();
      String startMarker = invocation.getStartMarker();
      String endMarker = invocation.getEndMarker();
      String body = StringUtils.substringBetween(html, startMarker, endMarker);
      if (macroDescriptor.isCacheable()) {
        MacroContext macroContext =
            MacroContext.create(
                invocation.getMacroName(),
                invocation.getParameters(),
                body,
                context,
                locale,
                beanFactory);
        IMacroRunnable macroRunnable = macro.createRunnable();
        String macroHtml = StringUtils.defaultString(macroRunnable.getHtml(macroContext));
        html = StringUtils.replace(html, startMarker + body + endMarker, macroHtml);
      } else if (nonCacheableMacros) {
        String macroName = invocation.getMacroName();
        String params = invocation.getParameters();
        String idx = String.valueOf(nonCacheableMacroIdx++);
        html =
            StringUtils.replace(
                html,
                startMarker + body + endMarker,
                "__"
                    + NON_CACHEABLE_MACRO_MARKER
                    + "_"
                    + idx
                    + "__"
                    + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    macroName
                    + " "
                    + StringUtils.defaultString(params)
                    + //$NON-NLS-1$
                    "__"
                    + NON_CACHEABLE_MACRO_BODY_MARKER
                    + "__"
                    + //$NON-NLS-1$ //$NON-NLS-2$
                    body
                    + "__/"
                    + NON_CACHEABLE_MACRO_MARKER
                    + "_"
                    + idx
                    + "__"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      } else {
        html = StringUtils.replace(html, startMarker + body + endMarker, StringUtils.EMPTY);
      }
    }
    html = cleanupHtml(html, macroInvocations, true);
    return html;
  }