List of usage examples for org.springframework.util StringUtils delimitedListToStringArray
public static String[] delimitedListToStringArray(@Nullable String str, @Nullable String delimiter)
From source file:org.springframework.batch.test.DataSourceInitializer.java
private void doExecuteScript(final Resource scriptResource) { if (scriptResource == null || !scriptResource.exists()) return;//from w w w .ja v a 2s .c o m TransactionTemplate transactionTemplate = new TransactionTemplate( new DataSourceTransactionManager(dataSource)); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String[] scripts; try { scripts = StringUtils.delimitedListToStringArray( stripComments(IOUtils.readLines(scriptResource.getInputStream())), ";"); } catch (IOException e) { throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e); } for (int i = 0; i < scripts.length; i++) { String script = scripts[i].trim(); if (StringUtils.hasText(script)) { try { jdbcTemplate.execute(script); } catch (DataAccessException e) { if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) { logger.debug("DROP script failed (ignoring): " + script); } else { throw e; } } } } return null; } }); }
From source file:org.springframework.boot.devtools.restart.ChangeableUrls.java
private static List<URL> getUrlsFromManifestClassPathAttribute(URL jarUrl, JarFile jarFile) throws IOException { Manifest manifest = jarFile.getManifest(); if (manifest == null) { return Collections.emptyList(); }/*from w w w. j a v a2 s. c o m*/ String classPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); if (!StringUtils.hasText(classPath)) { return Collections.emptyList(); } String[] entries = StringUtils.delimitedListToStringArray(classPath, " "); List<URL> urls = new ArrayList<>(entries.length); List<URL> nonExistentEntries = new ArrayList<>(); for (String entry : entries) { try { URL referenced = new URL(jarUrl, entry); if (new File(referenced.getFile()).exists()) { urls.add(referenced); } else { nonExistentEntries.add(referenced); } } catch (MalformedURLException ex) { throw new IllegalStateException("Class-Path attribute contains malformed URL", ex); } } if (!nonExistentEntries.isEmpty()) { System.out.println("The Class-Path manifest attribute in " + jarFile.getName() + " referenced one or more files that do not exist: " + StringUtils.collectionToCommaDelimitedString(nonExistentEntries)); } return urls; }
From source file:org.springframework.cloud.dataflow.rest.util.DeploymentPropertiesUtils.java
/** * Parses a String comprised of 0 or more delimited key=value pairs where each key * has the format: {@code app.[appname].[key]} or {@code deployer.[appname].[key]}. Values * may themselves contain commas, since the split points will be based upon the key * pattern./*from w w w.ja v a 2 s .com*/ * <p> * Logic of parsing key/value pairs from a string is based on few rules and assumptions 1. * keys will not have commas or equals. 2. First raw split is done by commas which will * need to be fixed later if value is a comma-delimited list. * * @param s the string to parse * @param delimiter delimiter used to split the string into pairs * @return the List key=value pairs */ public static List<String> parseParamList(String s, String delimiter) { ArrayList<String> pairs = new ArrayList<>(); // get raw candidates as simple comma split String[] candidates = StringUtils.delimitedListToStringArray(s, delimiter); for (int i = 0; i < candidates.length; i++) { if (i > 0 && !candidates[i].contains("=")) { // we don't have '=' so this has to be latter parts of // a comma delimited value, append it to previously added // key/value pair. // we skip first as we would not have anything to append to. this // would happen if dep prop string is malformed and first given // key/value pair is not actually a key/value. pairs.set(pairs.size() - 1, pairs.get(pairs.size() - 1) + delimiter + candidates[i]); } else { // we have a key/value pair having '=', or malformed first pair pairs.add(candidates[i]); } } return pairs; }
From source file:org.springframework.cloud.sleuth.instrument.multiple.DemoApplication.java
@Splitter(inputChannel = "greetings", outputChannel = "words") public List<String> words(String greeting) { this.splitterSpan = this.tracer.currentSpan(); return Arrays.asList(StringUtils.delimitedListToStringArray(greeting, " ")); }
From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java
/** * Determine jar file references from the "java.class.path." manifest property and add them * to the given set of resources in the form of pointers to the root of the jar file content. * @param result the set of resources to add jar roots to * @since 4.3/*w w w . j ava2s.c o m*/ */ protected void addClassPathManifestEntries(Set<Resource> result) { try { String javaClassPathProperty = System.getProperty("java.class.path"); for (String path : StringUtils.delimitedListToStringArray(javaClassPathProperty, System.getProperty("path.separator"))) { try { String filePath = new File(path).getAbsolutePath(); int prefixIndex = filePath.indexOf(':'); if (prefixIndex == 1) { // Possibly "c:" drive prefix on Windows, to be upper-cased for proper duplicate detection filePath = StringUtils.capitalize(filePath); } UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR); // Potentially overlapping with URLClassLoader.getURLs() result above! if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) { result.add(jarResource); } } catch (MalformedURLException ex) { if (logger.isDebugEnabled()) { logger.debug("Cannot search for matching files underneath [" + path + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage()); } } } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to evaluate 'java.class.path' manifest entries: " + ex); } } }
From source file:org.springframework.data.mongodb.config.ServerAddressPropertyEditor.java
/** * Parses the given source into a {@link ServerAddress}. * /*from w w w .j ava 2 s . c om*/ * @param source * @return the */ private ServerAddress parseServerAddress(String source) { String[] hostAndPort = StringUtils.delimitedListToStringArray(source.trim(), ":"); if (!StringUtils.hasText(source) || hostAndPort.length > 2) { LOG.warn(String.format("Could not parse address source '%s'. Check your replica set configuration!", source)); return null; } try { return hostAndPort.length == 1 ? new ServerAddress(hostAndPort[0]) : new ServerAddress(hostAndPort[0], Integer.parseInt(hostAndPort[1])); } catch (UnknownHostException e) { LOG.warn(String.format("Could not parse host '%s'. Check your replica set configuration!", hostAndPort[0])); } catch (NumberFormatException e) { LOG.warn(String.format("Could not parse port '%s'. Check your replica set configuration!", hostAndPort[1])); } return null; }
From source file:org.springframework.js.resource.ResourceServlet.java
private URL[] getRequestResourceURLs(HttpServletRequest request) throws MalformedURLException { String rawResourcePath = request.getPathInfo(); String appendedPaths = request.getParameter("appended"); if (StringUtils.hasText(appendedPaths)) { rawResourcePath = rawResourcePath + "," + appendedPaths; }//from w w w . j av a2 s .c om String[] localResourcePaths = StringUtils.delimitedListToStringArray(rawResourcePath, ","); URL[] resources = new URL[localResourcePaths.length]; for (int i = 0; i < localResourcePaths.length; i++) { String localResourcePath = localResourcePaths[i]; if (!isAllowed(localResourcePath)) { if (log.isWarnEnabled()) { log.warn("An attempt to access a protected resource at " + localResourcePath + " was disallowed."); } return null; } URL resource = getServletContext().getResource(localResourcePath); if (resource == null) { resource = getJarResource(springJsJarPathPrefix, localResourcePath); } if (resource == null) { resource = getJarResource(jarPathPrefix, localResourcePath); } if (resource == null) { if (resources.length > 1) { log.debug("Combined resource not found: " + localResourcePath); } return null; } else { resources[i] = resource; } } return resources; }
From source file:org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices.java
/** * Decodes the cookie and splits it into a set of token strings using the ":" * delimiter.// w w w. j a v a 2s .co m * * @param cookieValue the value obtained from the submitted cookie * @return the array of tokens. * @throws InvalidCookieException if the cookie was not base64 encoded. */ protected String[] decodeCookie(String cookieValue) throws InvalidCookieException { for (int j = 0; j < cookieValue.length() % 4; j++) { cookieValue = cookieValue + "="; } try { Base64.getDecoder().decode(cookieValue.getBytes()); } catch (IllegalArgumentException e) { throw new InvalidCookieException( "Cookie token was not Base64 encoded; value was '" + cookieValue + "'"); } String cookieAsPlainText = new String(Base64.getDecoder().decode(cookieValue.getBytes())); String[] tokens = StringUtils.delimitedListToStringArray(cookieAsPlainText, DELIMITER); for (int i = 0; i < tokens.length; i++) { try { tokens[i] = URLDecoder.decode(tokens[i], StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); } } return tokens; }
From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java
private long determineExpiryTimeFromBased64EncodedToken(String validToken) { String cookieAsPlainText = new String(Base64.decodeBase64(validToken.getBytes())); String[] cookieTokens = StringUtils.delimitedListToStringArray(cookieAsPlainText, ":"); if (cookieTokens.length == 3) { try {//from w w w.j a v a2 s . co m return Long.parseLong(cookieTokens[1]); } catch (NumberFormatException ignored) { } } return -1; }
From source file:org.springframework.security.web.authentication.www.DigestAuthenticationEntryPointTests.java
private void checkNonceValid(String nonce) { // Check the nonce seems to be generated correctly // format of nonce is: // base64(expirationTime + ":" + md5Hex(expirationTime + ":" + key)) assertThat(Base64.isArrayByteBase64(nonce.getBytes())).isTrue(); String decodedNonce = new String(Base64.decodeBase64(nonce.getBytes())); String[] nonceTokens = StringUtils.delimitedListToStringArray(decodedNonce, ":"); assertThat(nonceTokens).hasSize(2);// w w w . j av a 2 s . co m String expectedNonceSignature = DigestUtils.md5Hex(nonceTokens[0] + ":" + "key"); assertThat(nonceTokens[1]).isEqualTo(expectedNonceSignature); }