List of usage examples for org.springframework.util StringUtils hasLength
public static boolean hasLength(@Nullable String str)
From source file:org.codehaus.groovy.grails.plugins.springsecurity.IpAddressFilter.java
private boolean isAllowed(final HttpServletRequest request) { String ip = request.getRemoteAddr(); if (IPV4_LOOPBACK.equals(ip) || IPV6_LOOPBACK.equals(ip)) { // always allow localhost return true; }//from www .ja v a 2s.c o m String uri = (String) request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE); if (!StringUtils.hasLength(uri)) { uri = request.getRequestURI(); if (!request.getContextPath().equals("/") && uri.startsWith(request.getContextPath())) { uri = uri.substring(request.getContextPath().length()); } } Collection<Map.Entry<String, List<String>>> matching = findMatchingRules(uri); if (matching.isEmpty()) { return true; } for (Map.Entry<String, List<String>> entry : matching) { for (String ipPattern : entry.getValue()) { if (new IpAddressMatcher(ipPattern).matches(request)) { return true; } } } _log.warn("disallowed request " + uri + " from " + ip); return false; }
From source file:com.excilys.ebi.bank.util.Asserts.java
public static void doesNotContain(String textToSearch, String substring, String messagePattern, Object arg) { if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.indexOf(substring) != -1) { throw new IllegalArgumentException(MessageFormatter.format(messagePattern, arg).getMessage()); }/* w ww . j av a2 s. c o m*/ }
From source file:org.openmrs.module.sync.scheduler.CleanupSyncTablesTask.java
/** * Get the given property name from the given props and convert it to an array of * {@link SyncRecordState}s//w ww . j av a2 s .c om * * @param propertyName the prop key to look for * @param props the key-value map to look in * @param defaultStates the default array if the value is invalid * @return an array of {@link SyncRecordState}s */ protected static SyncRecordState[] getSyncRecordStateProperty(String propertyName, Map<String, String> props, SyncRecordState[] defaultStates) { if (props != null) { String prop = props.get(propertyName); if (StringUtils.hasLength(prop)) { try { List<SyncRecordState> states = new ArrayList<SyncRecordState>(); for (String stateName : prop.split(",")) { SyncRecordState state = SyncRecordState.valueOf(stateName.trim()); states.add(state); } return states.toArray(new SyncRecordState[] {}); } catch (Exception e) { log.error("Unable to convert property value for " + propertyName + " : '" + prop + "' to an array of states"); } } } return defaultStates; }
From source file:org.springmodules.validation.valang.javascript.AbstractValangJavaScriptTranslator.java
protected String getErrorMessage(String key, String defaultMsg, MessageSourceAccessor messageSource) { if (StringUtils.hasLength(key)) { return messageSource.getMessage(key, defaultMsg); } else {//from w ww . j a v a 2 s . c o m return defaultMsg; } }
From source file:org.browsexml.timesheetjob.web.CustomBooleanEditor.java
public void setAsText(String text) throws IllegalArgumentException { log.debug("HERE"); new Exception("set text = " + text); String input = (text != null ? text.trim() : null); if (this.allowEmpty && !StringUtils.hasLength(input)) { // Treat empty String as null value. setValue(null);/*from w ww . j av a2 s.c o m*/ } else if (this.trueString != null && input.equalsIgnoreCase(this.trueString)) { setValue(Boolean.TRUE); } else if (this.falseString != null && input.equalsIgnoreCase(this.falseString)) { setValue(Boolean.FALSE); } else if (this.trueString == null && (input.equalsIgnoreCase(VALUE_TRUE) || input.equalsIgnoreCase(VALUE_ON) || input.equalsIgnoreCase(VALUE_YES) || input.equals(VALUE_1))) { setValue(Boolean.TRUE); } else if (this.falseString == null && (input.equalsIgnoreCase(VALUE_FALSE) || input.equalsIgnoreCase(VALUE_OFF) || input.equalsIgnoreCase(VALUE_NO) || input.equals(VALUE_0))) { setValue(Boolean.FALSE); } else { throw new IllegalArgumentException("Invalid boolean value [" + text + "]"); } }
From source file:lodsve.core.condition.SpringBootCondition.java
private StringBuilder getLogMessage(String classOrMethodName, ConditionOutcome outcome) { StringBuilder message = new StringBuilder(); message.append("Condition "); message.append(ClassUtils.getShortName(getClass())); message.append(" on "); message.append(classOrMethodName);//from w ww. ja va 2 s.c om message.append(outcome.isMatch() ? " matched" : " did not match"); if (StringUtils.hasLength(outcome.getMessage())) { message.append(" due to "); message.append(outcome.getMessage()); } return message; }
From source file:org.ff4j.spring.autowire.AutowiredFF4JBeanPostProcessor.java
private void autoWiredProperty(Object bean, Field field) { // Find the required and name parameters FF4JProperty annProperty = field.getAnnotation(FF4JProperty.class); String propertyName = StringUtils.hasLength(annProperty.value()) ? annProperty.value() : field.getName(); Property<?> property = readProperty(field, propertyName, annProperty.required()); // if not available in store if (property != null) { if (Property.class.isAssignableFrom(field.getType())) { injectValue(field, bean, propertyName, property); } else if (property.parameterizedType().isAssignableFrom(field.getType())) { injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Integer.class) && field.getType().equals(int.class) && (null != property.getValue())) { // Autoboxing Integer -> Int injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Long.class) && field.getType().equals(long.class) && (null != property.getValue())) { // Autoboxing Long -> long injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Double.class) && field.getType().equals(double.class) && (null != property.getValue())) { // Autoboxing Double -> double injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Byte.class) && field.getType().equals(byte.class) && (null != property.getValue())) { // Autoboxing Byte -> byte injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Boolean.class) && field.getType().equals(boolean.class) && (null != property.getValue())) { // Autoboxing Boolean -> boolean injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Short.class) && field.getType().equals(short.class) && (null != property.getValue())) { // Autoboxing Short -> short injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Character.class) && field.getType().equals(char.class) && (null != property.getValue())) { // Autoboxing Character -> char injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Float.class) && field.getType().equals(float.class) && (null != property.getValue())) { // Autoboxing Float -> float injectValue(field, bean, propertyName, property.getValue()); } else {//from w ww . j a v a2 s .c om throw new IllegalArgumentException("Field annotated with @FF4JProperty" + " must inherit from org.ff4j.property.AbstractProperty or be of type " + property.parameterizedType() + "but is " + field.getType() + " [class=" + bean.getClass().getName() + ", field=" + field.getName() + "]"); } } }
From source file:org.opencredo.couchdb.core.CouchDbDocumentTemplate.java
public void writeDocument(String id, Object document, MessageHeaders headers, Counter counter) throws CouchDbOperationException { Assert.state(defaultDocumentUrl != null, "defaultDatabaseUrl must be set to use this method"); HttpEntity<?> httpEntity = createHttpEntity(document); try {/* w w w .j a v a2s. c o m*/ if (headers != null) { HashMap<String, Object> copiedHeaders = new HashMap<String, Object>(headers); if (StringUtils.hasLength(id)) { copiedHeaders.put(MessageHeaders.ID, id); } restOperations.put(defaultDocumentUrl, httpEntity, copiedHeaders); } else { restOperations.put(defaultDocumentUrl, httpEntity, id); } } catch (RestClientException e) { throw new CouchDbOperationException(document, counter, "Unable to communicate with CouchDB", e); } }
From source file:grails.plugin.springsecurity.web.filter.IpAddressFilter.java
protected boolean isAllowed(final HttpServletRequest request) { String ip = request.getRemoteAddr(); if (allowLocalhost && (IPV4_LOOPBACK.equals(ip) || IPV6_LOOPBACK.equals(ip))) { return true; }/*from www .j a va2 s.c o m*/ String uri = (String) request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE); if (!StringUtils.hasLength(uri)) { uri = request.getRequestURI(); if (!request.getContextPath().equals("/") && uri.startsWith(request.getContextPath())) { uri = uri.substring(request.getContextPath().length()); } } List<InterceptedUrl> matching = findMatchingRules(uri); if (matching.isEmpty()) { return true; } for (InterceptedUrl iu : matching) { for (ConfigAttribute ipPattern : iu.getConfigAttributes()) { if (new IpAddressMatcher(ipPattern.getAttribute()).matches(request)) { return true; } } } log.warn("disallowed request {} from {}", new Object[] { uri, ip }); return false; }
From source file:org.romaz.spring.scripting.ext.ExtScriptBeanDefinitionParser.java
/** * Parses the dynamic object element and returns the resulting bean definition. * Registers a {@link ScriptFactoryPostProcessor} if needed. *//*from www.j ava2 s . c o m*/ @Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { // Resolve the script source. String value = resolveScriptSource(element, parserContext.getReaderContext()); if (value == null) { return null; } // Set up infrastructure. LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry()); // Create script factory bean definition. GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClassName(this.scriptFactoryClassName); bd.setSource(parserContext.extractSource(element)); // Determine bean scope. String scope = element.getAttribute(SCOPE_ATTRIBUTE); if (StringUtils.hasLength(scope)) { bd.setScope(scope); } // Determine autowire mode. String autowire = element.getAttribute(AUTOWIRE_ATTRIBUTE); int autowireMode = parserContext.getDelegate().getAutowireMode(autowire); // Only "byType" and "byName" supported, but maybe other default inherited... if (autowireMode == GenericBeanDefinition.AUTOWIRE_AUTODETECT) { autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE; } else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) { autowireMode = GenericBeanDefinition.AUTOWIRE_NO; } bd.setAutowireMode(autowireMode); // Determine dependency check setting. String dependencyCheck = element.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE); bd.setDependencyCheck(parserContext.getDelegate().getDependencyCheck(dependencyCheck)); // Retrieve the defaults for bean definitions within this parser context BeanDefinitionDefaults beanDefinitionDefaults = parserContext.getDelegate().getBeanDefinitionDefaults(); // Determine init method and destroy method. String initMethod = element.getAttribute(INIT_METHOD_ATTRIBUTE); if (StringUtils.hasLength(initMethod)) { bd.setInitMethodName(initMethod); } else if (beanDefinitionDefaults.getInitMethodName() != null) { bd.setInitMethodName(beanDefinitionDefaults.getInitMethodName()); } String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE); if (StringUtils.hasLength(destroyMethod)) { bd.setDestroyMethodName(destroyMethod); } else if (beanDefinitionDefaults.getDestroyMethodName() != null) { bd.setDestroyMethodName(beanDefinitionDefaults.getDestroyMethodName()); } // Attach any refresh metadata. String refreshCheckDelay = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE); if (StringUtils.hasText(refreshCheckDelay)) { bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, new Long(refreshCheckDelay)); } // Add constructor arguments. ConstructorArgumentValues cav = bd.getConstructorArgumentValues(); int constructorArgNum = 0; cav.addIndexedArgumentValue(constructorArgNum++, value); if (element.hasAttribute(SCRIPT_INTERFACES_ATTRIBUTE)) { cav.addIndexedArgumentValue(constructorArgNum++, element.getAttribute(SCRIPT_INTERFACES_ATTRIBUTE)); } if (element.hasAttribute(SCRIPT_CONFIG_ATTRIBUTE)) { cav.addIndexedArgumentValue(constructorArgNum++, new RuntimeBeanReference(element.getAttribute(SCRIPT_CONFIG_ATTRIBUTE))); } // Add any property definitions that need adding. parserContext.getDelegate().parsePropertyElements(element, bd); return bd; }