PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/php.project/src/org/netbeans/modules/php/project/connections/common/RemoteValidator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 151 lines | 103 code | 4 blank | 44 comment | 0 complexity | 599dcc16696d7c2b2ff28b8d966c959d MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.php.project.connections.common;
  43. import org.netbeans.modules.php.api.util.StringUtils;
  44. import org.netbeans.modules.php.project.connections.transfer.TransferFile;
  45. import org.openide.util.NbBundle;
  46. /**
  47. * Validator for remote properties like port, uypload directory etc.
  48. */
  49. public final class RemoteValidator {
  50. public static final int MINIMUM_PORT = 0;
  51. public static final int MAXIMUM_PORT = 65535;
  52. public static final String INVALID_SEPARATOR = "\\"; // NOI18N
  53. private RemoteValidator() {
  54. }
  55. public static String validateHost(String host) {
  56. if (!StringUtils.hasText(host)) {
  57. return NbBundle.getMessage(RemoteValidator.class, "MSG_NoHostName");
  58. } else if (host.contains(" ")) { // NOI18N
  59. return NbBundle.getMessage(RemoteValidator.class, "MSG_HostNameSpaces");
  60. }
  61. return null;
  62. }
  63. public static String validateUser(String username) {
  64. if (!StringUtils.hasText(username)) {
  65. return NbBundle.getMessage(RemoteValidator.class, "MSG_NoUserName");
  66. }
  67. return null;
  68. }
  69. public static String validatePort(String port) {
  70. String err = null;
  71. try {
  72. int p = Integer.parseInt(port);
  73. if (p < MINIMUM_PORT || p > MAXIMUM_PORT) { // see InetSocketAddress
  74. err = NbBundle.getMessage(RemoteValidator.class, "MSG_PortInvalid", String.valueOf(MINIMUM_PORT), String.valueOf(MAXIMUM_PORT));
  75. }
  76. } catch (NumberFormatException nfe) {
  77. err = NbBundle.getMessage(RemoteValidator.class, "MSG_PortNotNumeric");
  78. }
  79. return err;
  80. }
  81. @NbBundle.Messages({
  82. "MSG_TimeoutNotNumeric=Timeout must be a number.",
  83. "MSG_TimeoutNotPositive=Timeout must be higher than or equal to 0."
  84. })
  85. public static String validateTimeout(String timeout) {
  86. return validatePositiveNumber(timeout, Bundle.MSG_TimeoutNotPositive(), Bundle.MSG_TimeoutNotNumeric());
  87. }
  88. /**
  89. * Validate keep-alive interval.
  90. * @param keepAliveInterval value to be validated
  91. * @return error message or {@code null} if keep-alive interval is correct
  92. */
  93. @NbBundle.Messages({
  94. "MSG_KeepAliveNotNumeric=Keep-alive interval must be a number.",
  95. "MSG_KeepAliveNotPositive=Keep-alive interval must be higher than or equal to 0."
  96. })
  97. public static String validateKeepAliveInterval(String keepAliveInterval) {
  98. return validatePositiveNumber(keepAliveInterval, Bundle.MSG_KeepAliveNotPositive(), Bundle.MSG_KeepAliveNotNumeric());
  99. }
  100. @NbBundle.Messages({
  101. "RemoteValidator.error.uploadDirectory.missing=Upload directory must be specified.",
  102. "# {0} - remote path separator",
  103. "RemoteValidator.error.uploadDirectory.start=Upload directory must start with \"{0}\".",
  104. "# {0} - invalid path separator",
  105. "RemoteValidator.error.uploadDirectory.content=Upload directory cannot contain \"{0}\"."
  106. })
  107. public static String validateUploadDirectory(String uploadDirectory) {
  108. if (!StringUtils.hasText(uploadDirectory)) {
  109. return Bundle.RemoteValidator_error_uploadDirectory_missing();
  110. } else if (!uploadDirectory.startsWith(TransferFile.REMOTE_PATH_SEPARATOR)) {
  111. return Bundle.RemoteValidator_error_uploadDirectory_start(TransferFile.REMOTE_PATH_SEPARATOR);
  112. } else if (uploadDirectory.contains(INVALID_SEPARATOR)) {
  113. return Bundle.RemoteValidator_error_uploadDirectory_content(INVALID_SEPARATOR);
  114. }
  115. return null;
  116. }
  117. /**
  118. * Validate input as a positive number.
  119. * @param number input to be validated
  120. * @param errorNotPositive error used if input is not positive number
  121. * @param errorNotNumeric error used if input is not number
  122. * @return error message or {@code null} if input is positive number
  123. */
  124. static String validatePositiveNumber(String number, String errorNotPositive, String errorNotNumeric) {
  125. String err = null;
  126. try {
  127. int t = Integer.parseInt(number);
  128. if (t < 0) {
  129. err = errorNotPositive;
  130. }
  131. } catch (NumberFormatException nfe) {
  132. err = errorNotNumeric;
  133. }
  134. return err;
  135. }
  136. }