Пример #1
0
  public String getId(HttpServletRequest request, String namespace) {
    if (_uniqueId) {
      return _id;
    }

    if (Validator.isNotNull(_id)) {
      _id = PortalUtil.getUniqueElementId(request, namespace, _id);
      _uniqueId = true;

      return _id;
    }

    String id = null;

    if (Validator.isNotNull(_className)) {
      String simpleClassName = _className;

      int pos = simpleClassName.lastIndexOf(StringPool.PERIOD);

      if (pos != -1) {
        simpleClassName = simpleClassName.substring(pos + 1);
      }

      String variableCasingSimpleClassName = TextFormatter.format(simpleClassName, TextFormatter.I);

      id = TextFormatter.formatPlural(variableCasingSimpleClassName);

      id = id.concat("SearchContainer");

      _id = PortalUtil.getUniqueElementId(request, namespace, id);
      _uniqueId = true;

      return _id;
    }

    id = DeterminateKeyGenerator.generate("taglib_search_container");

    _id = id.concat("SearchContainer");
    _uniqueId = true;

    return _id;
  }
  protected void checkEmptyCollection(String line, String fileName, int lineCount) {

    // LPS-46028

    Matcher matcher = emptyCollectionPattern.matcher(line);

    if (matcher.find()) {
      String collectionType = TextFormatter.format(matcher.group(1), TextFormatter.J);

      processErrorMessage(
          fileName, "Use Collections.empty" + collectionType + "(): " + fileName + " " + lineCount);
    }
  }
Пример #3
0
  /** TODO: Remove. This should extend from EditFileEntryAction once it is modularized. */
  protected void handleUploadException(
      PortletConfig portletConfig,
      ActionRequest actionRequest,
      ActionResponse actionResponse,
      String cmd,
      Exception e)
      throws Exception {

    if (e instanceof AssetCategoryException || e instanceof AssetTagException) {

      SessionErrors.add(actionRequest, e.getClass(), e);
    } else if (e instanceof AntivirusScannerException
        || e instanceof DuplicateFileEntryException
        || e instanceof DuplicateFolderNameException
        || e instanceof FileExtensionException
        || e instanceof FileMimeTypeException
        || e instanceof FileNameException
        || e instanceof FileSizeException
        || e instanceof LiferayFileItemException
        || e instanceof NoSuchFolderException
        || e instanceof SourceFileNameException
        || e instanceof StorageFieldRequiredException) {

      if (!cmd.equals(Constants.ADD_DYNAMIC)
          && !cmd.equals(Constants.ADD_MULTIPLE)
          && !cmd.equals(Constants.ADD_TEMP)) {

        if (e instanceof AntivirusScannerException) {
          SessionErrors.add(actionRequest, e.getClass(), e);
        } else {
          SessionErrors.add(actionRequest, e.getClass());
        }

        return;
      } else if (cmd.equals(Constants.ADD_TEMP)) {
        hideDefaultErrorMessage(actionRequest);
      }

      if (e instanceof AntivirusScannerException
          || e instanceof DuplicateFileEntryException
          || e instanceof FileExtensionException
          || e instanceof FileNameException
          || e instanceof FileSizeException) {

        HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

        response.setContentType(ContentTypes.TEXT_HTML);
        response.setStatus(HttpServletResponse.SC_OK);

        String errorMessage = StringPool.BLANK;
        int errorType = 0;

        ThemeDisplay themeDisplay =
            (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        if (e instanceof AntivirusScannerException) {
          AntivirusScannerException ase = (AntivirusScannerException) e;

          errorMessage = themeDisplay.translate(ase.getMessageKey());
          errorType = ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
        }

        if (e instanceof DuplicateFileEntryException) {
          errorMessage = themeDisplay.translate("please-enter-a-unique-document-name");
          errorType = ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
        } else if (e instanceof FileExtensionException) {
          errorMessage =
              themeDisplay.translate(
                  "please-enter-a-file-with-a-valid-extension-x",
                  StringUtil.merge(
                      getAllowedFileExtensions(portletConfig, actionRequest, actionResponse)));
          errorType = ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
        } else if (e instanceof FileNameException) {
          errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-name");
          errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
        } else if (e instanceof FileSizeException) {
          long fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);

          if (fileMaxSize == 0) {
            fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
          }

          errorMessage =
              themeDisplay.translate(
                  "please-enter-a-file-with-a-valid-file-size-no-larger" + "-than-x",
                  TextFormatter.formatStorageSize(fileMaxSize, themeDisplay.getLocale()));

          errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
        }

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

        jsonObject.put("message", errorMessage);
        jsonObject.put("status", errorType);

        JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);
      }

      if (e instanceof AntivirusScannerException) {
        SessionErrors.add(actionRequest, e.getClass(), e);
      } else {
        SessionErrors.add(actionRequest, e.getClass());
      }
    } else if (e instanceof DuplicateLockException
        || e instanceof InvalidFileVersionException
        || e instanceof NoSuchFileEntryException
        || e instanceof PrincipalException) {

      if (e instanceof DuplicateLockException) {
        DuplicateLockException dle = (DuplicateLockException) e;

        SessionErrors.add(actionRequest, dle.getClass(), dle.getLock());
      } else {
        SessionErrors.add(actionRequest, e.getClass());
      }

      actionResponse.setRenderParameter("mvcPath", "/html/porltet/document_library/error.jsp");
    } else {
      Throwable cause = e.getCause();

      if (cause instanceof DuplicateFileEntryException) {
        SessionErrors.add(actionRequest, DuplicateFileEntryException.class);
      } else {
        throw e;
      }
    }
  }
Пример #4
0
  /** TODO: Remove. This should extend from EditFileEntryAction once it is modularized. */
  protected String getAddMultipleFileEntriesErrorMessage(
      PortletConfig portletConfig,
      ActionRequest actionRequest,
      ActionResponse actionResponse,
      Exception e)
      throws Exception {

    String errorMessage = null;

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    if (e instanceof AntivirusScannerException) {
      AntivirusScannerException ase = (AntivirusScannerException) e;

      errorMessage = themeDisplay.translate(ase.getMessageKey());
    } else if (e instanceof AssetCategoryException) {
      AssetCategoryException ace = (AssetCategoryException) e;

      AssetVocabulary assetVocabulary = ace.getVocabulary();

      String vocabularyTitle = StringPool.BLANK;

      if (assetVocabulary != null) {
        vocabularyTitle = assetVocabulary.getTitle(themeDisplay.getLocale());
      }

      if (ace.getType() == AssetCategoryException.AT_LEAST_ONE_CATEGORY) {
        errorMessage =
            themeDisplay.translate("please-select-at-least-one-category-for-x", vocabularyTitle);
      } else if (ace.getType() == AssetCategoryException.TOO_MANY_CATEGORIES) {

        errorMessage =
            themeDisplay.translate(
                "you-cannot-select-more-than-one-category-for-x", vocabularyTitle);
      }
    } else if (e instanceof DuplicateFileEntryException) {
      errorMessage =
          themeDisplay.translate(
              "the-folder-you-selected-already-has-an-entry-with-this-name."
                  + "-please-select-a-different-folder");
    } else if (e instanceof FileExtensionException) {
      errorMessage =
          themeDisplay.translate(
              "please-enter-a-file-with-a-valid-extension-x",
              StringUtil.merge(
                  getAllowedFileExtensions(portletConfig, actionRequest, actionResponse)));
    } else if (e instanceof FileNameException) {
      errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-name");
    } else if (e instanceof FileSizeException) {
      long fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);

      if (fileMaxSize == 0) {
        fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
      }

      errorMessage =
          themeDisplay.translate(
              "please-enter-a-file-with-a-valid-file-size-no-larger-than-x",
              TextFormatter.formatStorageSize(fileMaxSize, themeDisplay.getLocale()));
    } else if (e instanceof InvalidFileEntryTypeException) {
      errorMessage =
          themeDisplay.translate("the-document-type-you-selected-is-not-valid-for-this-folder");
    } else {
      errorMessage =
          themeDisplay.translate("an-unexpected-error-occurred-while-saving-your-document");
    }

    return errorMessage;
  }
 public String getVariableName(String name) {
   return TextFormatter.format(name, TextFormatter.I);
 }
  protected String[] getLanguageKeys(Matcher matcher) {
    int groupCount = matcher.groupCount();

    if (groupCount == 1) {
      String languageKey = matcher.group(1);

      if (Validator.isNotNull(languageKey)) {
        return new String[] {languageKey};
      }
    } else if (groupCount == 2) {
      String languageKey = matcher.group(2);

      languageKey = TextFormatter.format(languageKey, TextFormatter.P);

      return new String[] {languageKey};
    }

    StringBundler sb = new StringBundler();

    String match = matcher.group();

    int count = 0;

    for (int i = 0; i < match.length(); i++) {
      char c = match.charAt(i);

      switch (c) {
        case CharPool.CLOSE_PARENTHESIS:
          if (count <= 1) {
            return new String[0];
          }

          count--;

          break;

        case CharPool.OPEN_PARENTHESIS:
          count++;

          break;

        case CharPool.QUOTE:
          if (count > 1) {
            break;
          }

          while (i < match.length()) {
            i++;

            if (match.charAt(i) == CharPool.QUOTE) {
              String languageKey = sb.toString();

              if (match.startsWith("names")) {
                return StringUtil.split(languageKey);
              } else {
                return new String[] {languageKey};
              }
            }

            sb.append(match.charAt(i));
          }
      }
    }

    return new String[0];
  }
  protected String fixSessionKey(String fileName, String content, Pattern pattern) {

    Matcher matcher = pattern.matcher(content);

    if (!matcher.find()) {
      return content;
    }

    String newContent = content;

    do {
      String match = matcher.group();

      String s = null;

      if (pattern.equals(sessionKeyPattern)) {
        s = StringPool.COMMA;
      } else if (pattern.equals(taglibSessionKeyPattern)) {
        s = "key=";
      }

      int x = match.indexOf(s);

      if (x == -1) {
        continue;
      }

      x = x + s.length();

      String substring = match.substring(x).trim();

      String quote = StringPool.BLANK;

      if (substring.startsWith(StringPool.APOSTROPHE)) {
        quote = StringPool.APOSTROPHE;
      } else if (substring.startsWith(StringPool.QUOTE)) {
        quote = StringPool.QUOTE;
      } else {
        continue;
      }

      int y = match.indexOf(quote, x);
      int z = match.indexOf(quote, y + 1);

      if ((y == -1) || (z == -1)) {
        continue;
      }

      String prefix = match.substring(0, y + 1);
      String suffix = match.substring(z);
      String oldKey = match.substring(y + 1, z);

      boolean alphaNumericKey = true;

      for (char c : oldKey.toCharArray()) {
        if (!Validator.isChar(c)
            && !Validator.isDigit(c)
            && (c != CharPool.DASH)
            && (c != CharPool.UNDERLINE)) {

          alphaNumericKey = false;
        }
      }

      if (!alphaNumericKey) {
        continue;
      }

      String newKey = TextFormatter.format(oldKey, TextFormatter.O);

      newKey = TextFormatter.format(newKey, TextFormatter.M);

      if (newKey.equals(oldKey)) {
        continue;
      }

      String oldSub = prefix.concat(oldKey).concat(suffix);
      String newSub = prefix.concat(newKey).concat(suffix);

      newContent = StringUtil.replaceFirst(newContent, oldSub, newSub);
    } while (matcher.find());

    return newContent;
  }
Пример #8
0
  @Override
  protected void setAttributes(HttpServletRequest request) {
    super.setAttributes(request);

    Object bean = getBean();

    if (bean == null) {
      bean = pageContext.getAttribute("aui:model-context:bean");
    }

    Class<?> model = getModel();

    String defaultLanguageId = getDefaultLanguageId();

    if (Validator.isNull(defaultLanguageId)) {
      defaultLanguageId = (String) pageContext.getAttribute("aui:model-context:defaultLanguageId");
    }

    if (Validator.isNull(defaultLanguageId)) {
      if ((model != null) && ModelHintsUtil.hasField(model.getName(), "groupId")) {

        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        defaultLanguageId = LocaleUtil.toLanguageId(themeDisplay.getSiteDefaultLocale());
      }
    }

    if (Validator.isNull(defaultLanguageId)) {
      Locale defaultLocale = LocaleUtil.getDefault();

      defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
    }

    String name = getName();

    int pos = name.indexOf(StringPool.DOUBLE_DASH);

    if (pos != -1) {
      name = name.substring(pos + 2, name.length() - 2);
    }

    String field = getField();

    if (Validator.isNull(field)) {
      field = getName();
    }

    String formName = getFormName();

    if (formName == null) {
      FormTag formTag = (FormTag) findAncestorWithClass(this, FormTag.class);

      if (formTag != null) {
        formName = formTag.getName();
      }
    }

    String id = getId();
    String type = getType();

    if (Validator.isNull(id)) {
      String fieldParam = getFieldParam();

      if ((model != null) && Validator.isNull(type) && Validator.isNotNull(fieldParam)) {

        id = AUIUtil.normalizeId(fieldParam);
      } else if (!Objects.equals(type, "assetTags") && !Objects.equals(type, "radio")) {

        id = AUIUtil.normalizeId(name);
      } else {
        id = PortalUtil.getUniqueElementId(request, StringPool.BLANK, AUIUtil.normalizeId(name));
      }
    }

    String forLabel = id;

    if (Objects.equals(type, "assetTags")) {
      forLabel = forLabel.concat("assetTagNames");
    }

    String languageId = getLanguageId();

    if (Validator.isNotNull(languageId)) {
      forLabel = LocalizationUtil.getLocalizedName(forLabel, languageId);
    }

    String label = getLabel();

    if (label == null) {
      label = TextFormatter.format(name, TextFormatter.P);
    }

    String title = getTitle();

    if ((title == null) && (Validator.isNull(label) || Objects.equals(type, "image"))) {

      title = TextFormatter.format(name, TextFormatter.P);
    }

    boolean wrappedField = getWrappedField();

    FieldWrapperTag fieldWrapper =
        (FieldWrapperTag) findAncestorWithClass(this, FieldWrapperTag.class);

    if (fieldWrapper != null) {
      wrappedField = true;
    }

    setNamespacedAttribute(request, "baseType", getBaseType());
    setNamespacedAttribute(request, "bean", bean);
    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
    setNamespacedAttribute(request, "field", field);
    setNamespacedAttribute(request, "forLabel", forLabel);
    setNamespacedAttribute(request, "formName", formName);
    setNamespacedAttribute(request, "id", id);
    setNamespacedAttribute(request, "label", label);
    setNamespacedAttribute(request, "model", model);
    setNamespacedAttribute(request, "title", String.valueOf(title));
    setNamespacedAttribute(request, "wrappedField", wrappedField);

    request.setAttribute(getAttributeNamespace() + "value", getValue());

    Map<String, ValidatorTag> validatorTags = getValidatorTags();

    if ((validatorTags != null) && (validatorTags.get("required") != null)) {

      setNamespacedAttribute(request, "required", Boolean.TRUE.toString());
    }
  }
  protected void handleUploadException(
      ActionRequest actionRequest,
      ActionResponse actionResponse,
      String cmd,
      long maxFileSize,
      Exception e)
      throws Exception {

    if (e instanceof PrincipalException) {
      SessionErrors.add(actionRequest, e.getClass());

      setForward(actionRequest, "portal.error");
    } else if (e instanceof AntivirusScannerException
        || e instanceof FileExtensionException
        || e instanceof FileSizeException
        || e instanceof ImageTypeException
        || e instanceof NoSuchFileException
        || e instanceof UploadException) {

      if (cmd.equals(Constants.ADD_TEMP)) {
        hideDefaultErrorMessage(actionRequest);

        ThemeDisplay themeDisplay =
            (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        String errorMessage = StringPool.BLANK;

        if (e instanceof AntivirusScannerException) {
          AntivirusScannerException ase = (AntivirusScannerException) e;

          errorMessage = themeDisplay.translate(ase.getMessageKey());
        } else if (e instanceof FileExtensionException) {
          errorMessage =
              themeDisplay.translate(
                  "please-enter-a-file-with-a-valid-extension-x",
                  StringUtil.merge(PropsValues.DL_FILE_EXTENSIONS, StringPool.COMMA));
        } else if (e instanceof FileSizeException) {
          if (maxFileSize == 0) {
            maxFileSize = PrefsPropsUtil.getLong(PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
          }

          errorMessage =
              themeDisplay.translate(
                  "please-enter-a-file-with-a-valid-file-size-no" + "-larger-than-x",
                  TextFormatter.formatStorageSize(maxFileSize, themeDisplay.getLocale()));
        } else if (e instanceof ImageTypeException) {
          errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-type");
        } else if (e instanceof NoSuchFileException || e instanceof UploadException) {

          errorMessage =
              themeDisplay.translate("an-unexpected-error-occurred-while-uploading" + "-your-file");
        }

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

        jsonObject.put("errorMessage", errorMessage);

        writeJSON(actionRequest, actionResponse, jsonObject);
      } else {
        SessionErrors.add(actionRequest, e.getClass(), e);
      }
    } else {
      throw e;
    }
  }
Пример #10
0
  @Override
  protected void setAttributes(HttpServletRequest request) {
    super.setAttributes(request);

    Object bean = getBean();

    if (bean == null) {
      bean = pageContext.getAttribute("aui:model-context:bean");
    }

    String name = getName();

    int pos = name.indexOf(StringPool.DOUBLE_DASH);

    if (pos != -1) {
      name = name.substring(pos + 2, name.length() - 2);
    }

    String field = getField();

    if (Validator.isNull(field)) {
      field = getName();
    }

    String formName = getFormName();

    if (formName == null) {
      FormTag formTag = (FormTag) findAncestorWithClass(this, FormTag.class);

      if (formTag != null) {
        formName = formTag.getName();
      }
    }

    String id = getId();
    String type = getType();

    if (Validator.isNull(id)) {
      if (!Validator.equals(type, "assetTags") && !Validator.equals(type, "radio")) {

        id = name;
      } else {
        id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
      }
    }

    String label = getLabel();

    if (label == null) {
      label = TextFormatter.format(name, TextFormatter.K);
    }

    Class<?> model = getModel();

    if (model == null) {
      model = (Class<?>) pageContext.getAttribute("aui:model-context:model");
    }

    _forLabel = id;

    String baseType = null;

    if ((model != null) && Validator.isNull(type)) {
      baseType = ModelHintsUtil.getType(model.getName(), field);

      String fieldParam = getFieldParam();

      if (Validator.isNotNull(fieldParam)) {
        _forLabel = fieldParam;
      }

      if (ModelHintsUtil.isLocalized(model.getName(), field)) {
        Locale defaultLocale = LocaleUtil.getDefault();
        String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);

        _forLabel += StringPool.UNDERLINE + defaultLanguageId;
      }
    } else if (Validator.isNotNull(type)) {
      if (Validator.equals(type, "checkbox") || Validator.equals(type, "radio")) {

        baseType = type;
      }
    }

    if (Validator.isNull(baseType)) {
      baseType = "text";
    }

    setNamespacedAttribute(request, "baseType", baseType);
    setNamespacedAttribute(request, "bean", bean);
    setNamespacedAttribute(request, "field", field);
    setNamespacedAttribute(request, "forLabel", _forLabel);
    setNamespacedAttribute(request, "formName", formName);
    setNamespacedAttribute(request, "id", id);
    setNamespacedAttribute(request, "label", label);
    setNamespacedAttribute(request, "model", model);

    request.setAttribute(getAttributeNamespace() + "value", getValue());
  }