/src/org/opencms/gwt/CmsUrlNameValidationService.java

https://github.com/benocms/opencms-core · Java · 80 lines · 33 code · 8 blank · 39 comment · 3 complexity · 27c720a1938e350305a7715b77f384fb MD5 · raw file

  1. /*
  2. * This library is part of OpenCms -
  3. * the Open Source Content Management System
  4. *
  5. * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * For further information about Alkacon Software, please see the
  18. * company website: http://www.alkacon.com
  19. *
  20. * For further information about OpenCms, please see the
  21. * project website: http://www.opencms.org
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. package org.opencms.gwt;
  28. import org.opencms.file.CmsObject;
  29. import org.opencms.file.CmsResource;
  30. import org.opencms.file.CmsVfsResourceNotFoundException;
  31. import org.opencms.gwt.shared.CmsValidationResult;
  32. import org.opencms.main.CmsException;
  33. import org.opencms.main.CmsRuntimeException;
  34. import org.opencms.util.CmsStringUtil;
  35. import org.opencms.util.CmsUUID;
  36. import java.util.Map;
  37. /**
  38. * Validation class which both translates a sitemap URL name and checks whether it already exists in a '|'-separated
  39. * list of URL names which is passed as a configuration parameter.<p>
  40. *
  41. * @since 8.0.0
  42. */
  43. public class CmsUrlNameValidationService implements I_CmsValidationService {
  44. /**
  45. * @see org.opencms.gwt.I_CmsValidationService#validate(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
  46. */
  47. public CmsValidationResult validate(CmsObject cms, String value, String config) {
  48. String name = cms.getRequestContext().getFileTranslator().translateResource(value);
  49. name = name.replace('/', '_');
  50. Map<String, String> configMap = CmsStringUtil.splitAsMap(config, "|", ":");
  51. String parentPath = configMap.get("parent");
  52. String id = configMap.get("id");
  53. try {
  54. CmsResource res = cms.readResource(CmsStringUtil.joinPaths(parentPath, name));
  55. // file already exists
  56. if (!CmsUUID.isValidUUID(id) || res.getStructureId().toString().equals(id)) {
  57. // no problem, it's the same resource
  58. return new CmsValidationResult(null, name);
  59. } else {
  60. // it's a different resource, so we fail
  61. return new CmsValidationResult(org.opencms.gwt.Messages.get().getBundle().key(
  62. org.opencms.gwt.Messages.ERR_URL_NAME_ALREADY_EXISTS_1,
  63. name));
  64. }
  65. } catch (CmsVfsResourceNotFoundException e) {
  66. // ok, the resource was not found
  67. return new CmsValidationResult(null, name);
  68. } catch (CmsException e) {
  69. throw new CmsRuntimeException(e.getMessageContainer());
  70. }
  71. }
  72. }