List of usage examples for org.apache.commons.lang StringUtils containsOnly
public static boolean containsOnly(String str, String validChars)
Checks if the String contains only certain characters.
From source file:ninja.text.TextImpl.java
@Override public boolean containsOnly(CharSequence validChars) { return StringUtils.containsOnly(data.toString(), validChars.toString()); }
From source file:nl.strohalm.cyclos.controls.posweb.ReceivePaymentAction.java
@Override protected void prepareForm(final ActionContext context) throws Exception { super.prepareForm(context); final ReceivePaymentForm form = context.getForm(); final HttpServletRequest request = context.getRequest(); final Channel channel = posWebChannel(); final Credentials credentials = channel.getCredentials(); boolean numericCredentials = false; boolean uppercasedCredentials = false; switch (credentials) { case PIN:/*from w ww . j a v a2 s . c o m*/ case CARD_SECURITY_CODE: numericCredentials = true; break; case LOGIN_PASSWORD: numericCredentials = settingsService.getAccessSettings().isNumericPassword(); break; case TRANSACTION_PASSWORD: numericCredentials = StringUtils .containsOnly(settingsService.getAccessSettings().getTransactionPasswordChars(), "0123456789"); uppercasedCredentials = true; break; } final PrincipalType selectedPrincipalType = channelService.resolvePrincipalType(channel.getInternalName(), form.getPrincipalType()); form.setPrincipalType(selectedPrincipalType.toString()); final Map<String, PrincipalType> principalTypes = new TreeMap<String, PrincipalType>(); for (final PrincipalType principalType : channel.getPrincipalTypes()) { principalTypes.put(principalTypeLabel(principalType), principalType); } request.setAttribute("principalTypes", principalTypes); request.setAttribute("selectedPrincipalType", selectedPrincipalType); request.setAttribute("selectedPrincipalLabel", principalTypeLabel(selectedPrincipalType)); request.setAttribute("credentials", credentials); request.setAttribute("numericCredentials", numericCredentials); request.setAttribute("uppercasedCredentials", uppercasedCredentials); request.setAttribute("credentialsKey", getCredentialsKey()); final LocalSettings localSettings = settingsService.getLocalSettings(); request.setAttribute("today", localSettings.getRawDateConverter().toString(Calendar.getInstance())); RequestHelper.storeEnum(request, SchedulingType.class, "schedulingTypes"); }
From source file:nl.strohalm.cyclos.utils.hibernate.HibernateCustomFieldHandler.java
/** * Appends the custom field values on a query. Should be invoked when building the where part of the query * @param hql The current HQL buffer/*w w w . j a v a 2 s . co m*/ * @param values The custom field values used to filter */ public void appendConditions(final StringBuilder hql, final Map<String, Object> namedParameters, final Collection<? extends CustomFieldValue> values) { if (values == null || values.isEmpty()) { return; } for (final CustomFieldValue fieldValue : values) { CustomField field = fieldValue.getField(); if (field == null) { continue; } field = fetchDao.fetch(field); String value = fieldValue.getValue(); // Remove any manually entered '%' value = StringUtils.trimToNull(StringUtils.replace(value, "%", "")); if (value == null) { continue; } final String alias = alias(field); final String fieldParam = "field_" + alias; final String valueParam = "value_" + alias; hql.append(" and ").append(alias).append(".field = :").append(fieldParam); namedParameters.put(fieldParam, field); if (LoggedUser.hasUser() && !LoggedUser.isAdministrator()) { if (field.getNature() == CustomField.Nature.MEMBER) { // Exclude hidden fields hql.append(" and ").append(alias).append(".hidden <> true"); } } // Check the field type switch (field.getType()) { case STRING: if (StringUtils.isNotEmpty(field.getPattern())) { // Remove the mask and consider the value as matching exactly value = StringHelper.removeMask(field.getPattern(), value, false); hql.append(" and ").append(alias).append(".stringValue like :").append(valueParam); namedParameters.put(valueParam, value); } else { // Use a like expression hql.append(" and ").append(alias).append(".stringValue like :").append(valueParam); namedParameters.put(valueParam, StringUtils.trimToEmpty(value) + "%"); } break; case BOOLEAN: if (Boolean.parseBoolean(value)) { hql.append(" and ").append(alias).append(".stringValue = :" + valueParam); } else { hql.append(" and ").append(alias).append(".stringValue <> :" + valueParam); } namedParameters.put(valueParam, "true"); break; case ENUMERATED: boolean byName = true; if (StringUtils.containsOnly(value, "0123456789,")) { // Try by id try { final Collection<CustomFieldPossibleValue> possibleValues = new ArrayList<CustomFieldPossibleValue>(); final String[] possibleValueIds = StringUtils.split(value, ','); for (final String idAsString : possibleValueIds) { final CustomFieldPossibleValue possibleValue = customFieldPossibleValueDao .load(Long.valueOf(idAsString)); if (!possibleValue.getField().equals(field)) { throw new Exception(); } possibleValues.add(possibleValue); } byName = false; hql.append(" and ").append(alias).append(".possibleValue in (:").append(valueParam) .append(')'); namedParameters.put(valueParam, possibleValues); } catch (final Exception e) { // Possible value not found by id - next try by name } } if (byName) { hql.append(" and ").append(alias).append(".possibleValue.value = :").append(valueParam); namedParameters.put(valueParam, value); } break; case MEMBER: Long memberId = null; if (fieldValue.getMemberValue() != null) { memberId = fieldValue.getMemberValue().getId(); } else { memberId = IdConverter.instance().valueOf(value); } if (memberId != null) { hql.append(" and ").append(alias).append(".memberValue.id = :").append(valueParam); namedParameters.put(valueParam, memberId); } break; default: hql.append(" and ").append(alias).append(".stringValue = :").append(valueParam); namedParameters.put(valueParam, value); break; } } }
From source file:nl.strohalm.cyclos.utils.InternetAddressHelper.java
/** * Checks whether the given address is a valid ip address range, in the form: A.B.C.D-E *//*from w w w . j a v a 2s . co m*/ public static boolean isIpRange(final String address) { if (StringUtils.isEmpty(address)) { return false; } if (StringUtils.containsOnly(address, "0123456789.-")) { final String[] parts = StringUtils.split(address, '.'); if (parts.length != 4) { return false; } for (int i = 0; i < parts.length; i++) { final String part = parts[i]; if (i == 3) { // The last part is the only one that can contain a dash as separator final String[] subParts = StringUtils.split(part, '-'); if (subParts.length != 2) { return false; } final String begin = subParts[0]; final String end = subParts[1]; return isValidIpPart(begin) && isValidIpPart(end); } else { if (!isValidIpPart(part)) { return false; } } } } return false; }
From source file:nl.strohalm.cyclos.utils.InternetAddressHelper.java
/** * Checks whether the given address is a valid simple ip address *///from ww w .jav a 2 s .c o m public static boolean isSimpleIp(final String address) { if (StringUtils.isEmpty(address)) { return false; } if (StringUtils.containsOnly(address, "0123456789.")) { final String[] parts = StringUtils.split(address, '.'); if (parts.length != 4) { return false; } for (final String part : parts) { if (!isValidIpPart(part)) { return false; } } return true; } return false; }
From source file:nl.strohalm.cyclos.utils.validation.CardFormatValidation.java
/** * @see nl.strohalm.cyclos.utils.validation.PropertyValidation#validate(java.lang.Object, java.lang.Object, java.lang.Object) */// w w w. ja v a 2s .co m public ValidationError validate(final Object object, final Object property, final Object value) { if (value == null || "".equals(value)) { return null; } final String format = (String) value; boolean valid = StringUtils.containsOnly(format, ALLOWED_CHARS); if (valid) { valid = ALLOWED_BOUNDARY_CHARS.indexOf(format.charAt(0)) >= 0 && ALLOWED_BOUNDARY_CHARS.indexOf(format.charAt(format.length() - 1)) >= 0; } if (valid) { int cardDigits = 0; boolean wasSeparator = false; for (int i = 0; i < format.length(); i++) { final char c = format.charAt(i); if (ALLOWED_BOUNDARY_CHARS.indexOf(c) >= 0) { cardDigits++; wasSeparator = false; } else if (wasSeparator) { valid = false; break; } else { wasSeparator = true; } } if (valid) { valid = cardDigits >= MIN_CARD_DIGITS; } } if (valid) { return null; } else { return new InvalidError(); } }
From source file:org.apache.atlas.authorize.simple.SimpleAtlasAuthorizer.java
private boolean resourceMatchHelper(List<String> policyResource) { boolean isMatchAny = false; if (isDebugEnabled) { LOG.debug("==> SimpleAtlasAuthorizer resourceMatchHelper"); }/* ww w. ja va 2 s. c o m*/ boolean optWildCard = true; List<String> policyValues = new ArrayList<>(); if (policyResource != null) { boolean isWildCardPresent = !optWildCard; for (String policyValue : policyResource) { if (StringUtils.isEmpty(policyValue)) { continue; } if (StringUtils.containsOnly(policyValue, WILDCARD_ASTERISK)) { isMatchAny = true; } else if (!isWildCardPresent && StringUtils.containsAny(policyValue, WILDCARDS)) { isWildCardPresent = true; } policyValues.add(policyValue); } optWildCard = optWildCard && isWildCardPresent; } else { isMatchAny = false; } if (isDebugEnabled) { LOG.debug("<== SimpleAtlasAuthorizer resourceMatchHelper"); } return isMatchAny; }
From source file:org.apache.atlas.authorize.SimpleAtlasAuthorizer.java
private boolean resourceMatchHelper(List<String> policyResource) { boolean isMatchAny = false; if (isDebugEnabled) { LOG.debug("<== SimpleAtlasAuthorizer resourceMatchHelper"); }/*w ww .ja v a 2 s. c o m*/ boolean optWildCard = true; List<String> policyValues = new ArrayList<String>(); if (policyResource != null) { boolean isWildCardPresent = !optWildCard; for (String policyValue : policyResource) { if (StringUtils.isEmpty(policyValue)) { continue; } if (StringUtils.containsOnly(policyValue, WILDCARD_ASTERISK)) { isMatchAny = true; } else if (!isWildCardPresent && StringUtils.containsAny(policyValue, WILDCARDS)) { isWildCardPresent = true; } policyValues.add(policyValue); } optWildCard = optWildCard && isWildCardPresent; } else { isMatchAny = false; } if (isDebugEnabled) { LOG.debug("==> SimpleAtlasAuthorizer resourceMatchHelper"); } return isMatchAny; }
From source file:org.apache.hadoop.hive.metastore.tools.SchemaToolTaskValidate.java
/** * Check if the location is valid for the given entity. * @param entity the entity to represent a database, partition or table * @param entityLocation the location//from w ww . j av a2s . c o m * @param defaultServers a list of the servers that the location needs to match. * The location host needs to match one of the given servers. * If empty, then no check against such list. * @return true if the location is valid */ private boolean checkLocation(String entity, String entityLocation, URI[] defaultServers) { boolean isValid = true; if (entityLocation == null) { System.err.println(entity + ", Error: empty location"); isValid = false; } else { try { URI currentUri = new Path(entityLocation).toUri(); String scheme = currentUri.getScheme(); String path = currentUri.getPath(); if (StringUtils.isEmpty(scheme)) { System.err.println( entity + ", Location: " + entityLocation + ", Error: missing location scheme."); isValid = false; } else if (StringUtils.isEmpty(path)) { System.err .println(entity + ", Location: " + entityLocation + ", Error: missing location path."); isValid = false; } else if (ArrayUtils.isNotEmpty(defaultServers) && currentUri.getAuthority() != null) { String authority = currentUri.getAuthority(); boolean matchServer = false; for (URI server : defaultServers) { if (StringUtils.equalsIgnoreCase(server.getScheme(), scheme) && StringUtils.equalsIgnoreCase(server.getAuthority(), authority)) { matchServer = true; break; } } if (!matchServer) { System.err .println(entity + ", Location: " + entityLocation + ", Error: mismatched server."); isValid = false; } } // if there is no path element other than "/", report it but not fail if (isValid && StringUtils.containsOnly(path, "/")) { System.err.println(entity + ", Location: " + entityLocation + ", Warn: location set to root, " + "not a recommended config."); } } catch (Exception pe) { System.err.println(entity + ", Error: invalid location - " + pe.getMessage()); isValid = false; } } return isValid; }
From source file:org.apache.hive.beeline.HiveSchemaTool.java
/** * Check if the location is valid for the given entity * @param entity the entity to represent a database, partition or table * @param entityLocation the location//w w w . jav a 2 s. com * @param defaultServers a list of the servers that the location needs to match. * The location host needs to match one of the given servers. * If empty, then no check against such list. * @return true if the location is valid */ private boolean checkLocation(String entity, String entityLocation, URI[] defaultServers) { boolean isValid = true; if (entityLocation == null) { System.err.println(entity + ", Error: empty location"); isValid = false; } else { try { URI currentUri = new Path(entityLocation).toUri(); String scheme = currentUri.getScheme(); String path = currentUri.getPath(); if (StringUtils.isEmpty(scheme)) { System.err.println( entity + ", Location: " + entityLocation + ", Error: missing location scheme."); isValid = false; } else if (StringUtils.isEmpty(path)) { System.err .println(entity + ", Location: " + entityLocation + ", Error: missing location path."); isValid = false; } else if (ArrayUtils.isNotEmpty(defaultServers) && currentUri.getAuthority() != null) { String authority = currentUri.getAuthority(); boolean matchServer = false; for (URI server : defaultServers) { if (StringUtils.equalsIgnoreCase(server.getScheme(), scheme) && StringUtils.equalsIgnoreCase(server.getAuthority(), authority)) { matchServer = true; break; } } if (!matchServer) { System.err .println(entity + ", Location: " + entityLocation + ", Error: mismatched server."); isValid = false; } } // if there is no path element other than "/", report it but not fail if (isValid && StringUtils.containsOnly(path, "/")) { System.err.println(entity + ", Location: " + entityLocation + ", Warn: location set to root, not a recommended config."); } } catch (Exception pe) { System.err.println(entity + ", Error: invalid location - " + pe.getMessage()); isValid = false; } } return isValid; }