List of usage examples for org.springframework.util StringUtils commaDelimitedListToStringArray
public static String[] commaDelimitedListToStringArray(@Nullable String str)
From source file:com.oolong.platform.web.error.DefaultRestErrorResolver.java
protected RestError toRestError(String exceptionConfig) { String[] values = StringUtils.commaDelimitedListToStringArray(exceptionConfig); if (values == null || values.length == 0) { throw new IllegalStateException( "Invalid config mapping. Exception names must map to a string configuration."); }//from w w w . jav a2 s. com RestError.Builder builder = new RestError.Builder(); boolean statusSet = false; boolean codeSet = false; boolean msgSet = false; boolean devMsgSet = false; boolean moreInfoSet = false; for (String value : values) { String trimmedVal = StringUtils.trimWhitespace(value); // check to see if the value is an explicitly named key/value pair: String[] pair = StringUtils.split(trimmedVal, "="); if (pair != null) { // explicit attribute set: String pairKey = StringUtils.trimWhitespace(pair[0]); if (!StringUtils.hasText(pairKey)) { pairKey = null; } String pairValue = StringUtils.trimWhitespace(pair[1]); if (!StringUtils.hasText(pairValue)) { pairValue = null; } if ("status".equalsIgnoreCase(pairKey)) { int statusCode = getRequiredInt(pairKey, pairValue); builder.setStatus(statusCode); statusSet = true; } else if ("code".equalsIgnoreCase(pairKey)) { int code = getRequiredInt(pairKey, pairValue); builder.setCode(code); codeSet = true; } else if ("msg".equalsIgnoreCase(pairKey)) { builder.setMessage(pairValue); msgSet = true; } else if ("devMsg".equalsIgnoreCase(pairKey)) { builder.setDeveloperMessage(pairValue); devMsgSet = true; } else if ("infoUrl".equalsIgnoreCase(pairKey)) { builder.setMoreInfoUrl(pairValue); moreInfoSet = true; } } else { // not a key/value pair - use heuristics to determine what value // is being set: int val; if (!statusSet) { val = getInt("status", trimmedVal); if (val > 0) { builder.setStatus(val); statusSet = true; continue; } } if (!codeSet) { val = getInt("code", trimmedVal); if (val > 0) { builder.setCode(val); codeSet = true; continue; } } if (!moreInfoSet && trimmedVal.toLowerCase().startsWith("http")) { builder.setMoreInfoUrl(trimmedVal); moreInfoSet = true; continue; } if (!msgSet) { builder.setMessage(trimmedVal); msgSet = true; continue; } if (!devMsgSet) { builder.setDeveloperMessage(trimmedVal); devMsgSet = true; continue; } if (!moreInfoSet) { builder.setMoreInfoUrl(trimmedVal); moreInfoSet = true; // noinspection UnnecessaryContinue continue; } } } return builder.build(); }
From source file:com.yang.oa.commons.exception.DefaultRestErrorResolver.java
protected RestError toRestError(String exceptionConfig) { String[] values = StringUtils.commaDelimitedListToStringArray(exceptionConfig); if (values == null || values.length == 0) { throw new IllegalStateException( "Invalid config mapping. Exception names must map to a string configuration."); }//from w w w . j ava 2s . co m System.out.println("??" + exceptionConfig); RestError.Builder builder = new RestError.Builder(); boolean statusSet = false; boolean codeSet = false; boolean msgSet = false; boolean devMsgSet = false; boolean moreInfoSet = false; for (String value : values) { String trimmedVal = StringUtils.trimWhitespace(value); //check to see if the value is an explicitly named key/value pair: String[] pair = StringUtils.split(trimmedVal, "="); if (pair != null) { //explicit attribute set: String pairKey = StringUtils.trimWhitespace(pair[0]); if (!StringUtils.hasText(pairKey)) { pairKey = null; } String pairValue = StringUtils.trimWhitespace(pair[1]); if (!StringUtils.hasText(pairValue)) { pairValue = null; } if ("status".equalsIgnoreCase(pairKey)) { int statusCode = getRequiredInt(pairKey, pairValue); builder.setStatus(statusCode); statusSet = true; } else if ("code".equalsIgnoreCase(pairKey)) { int code = getRequiredInt(pairKey, pairValue); builder.setCode(code); codeSet = true; } else if ("msg".equalsIgnoreCase(pairKey)) { builder.setMessage(pairValue); msgSet = true; } else if ("devMsg".equalsIgnoreCase(pairKey)) { builder.setDeveloperMessage(pairValue); devMsgSet = true; } else if ("infoUrl".equalsIgnoreCase(pairKey)) { builder.setMoreInfoUrl(pairValue); moreInfoSet = true; } } else { //not a key/value pair - use heuristics to determine what value is being set: int val; if (!statusSet) { val = getInt("status", trimmedVal); if (val > 0) { builder.setStatus(val); statusSet = true; continue; } } if (!codeSet) { val = getInt("code", trimmedVal); if (val > 0) { builder.setCode(val); codeSet = true; continue; } } if (!moreInfoSet && trimmedVal.toLowerCase().startsWith("http")) { builder.setMoreInfoUrl(trimmedVal); moreInfoSet = true; continue; } if (!msgSet) { builder.setMessage(trimmedVal); msgSet = true; continue; } if (!devMsgSet) { builder.setDeveloperMessage(trimmedVal); devMsgSet = true; continue; } if (!moreInfoSet) { builder.setMoreInfoUrl(trimmedVal); moreInfoSet = true; //noinspection UnnecessaryContinue continue; } } } return builder.build(); }
From source file:com.beyondjservlet.gateway.velocity.VelocityEngineFactory.java
/** * Initialize a Velocity resource loader for the given VelocityEngine: * either a standard Velocity FileResourceLoader or a SpringResourceLoader. * <p>Called by {@code createVelocityEngine()}. * * @param velocityEngine the VelocityEngine to configure * @param resourceLoaderPath the path to load Velocity resources from * @see org.apache.velocity.runtime.resource.loader.FileResourceLoader * @see SpringResourceLoader//w w w.j ava 2 s. c o m * @see #initSpringResourceLoader * @see #createVelocityEngine() */ protected void initVelocityResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) { if (isPreferFileSystemAccess()) { // Try to load via the file system, fall back to SpringResourceLoader // (for hot detection of template changes, if possible). try { StringBuilder resolvedPath = new StringBuilder(); String[] paths = StringUtils.commaDelimitedListToStringArray(resourceLoaderPath); for (int i = 0; i < paths.length; i++) { String path = paths[i]; Resource resource = getResourceLoader().getResource(path); File file = resource.getFile(); // will fail if not resolvable in the file system if (logger.isDebugEnabled()) { logger.debug("Resource loader path [" + path + "] resolved to file [" + file.getAbsolutePath() + "]"); } resolvedPath.append(file.getAbsolutePath()); if (i < paths.length - 1) { resolvedPath.append(','); } } velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true"); velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, resolvedPath.toString()); } catch (IOException ex) { if (logger.isDebugEnabled()) { logger.debug("Cannot resolve resource loader path [" + resourceLoaderPath + "] to [java.io.File]: using SpringResourceLoader", ex); } initSpringResourceLoader(velocityEngine, resourceLoaderPath); } } else { // Always load via SpringResourceLoader // (without hot detection of template changes). if (logger.isDebugEnabled()) { logger.debug("File system access not preferred: using SpringResourceLoader"); } initSpringResourceLoader(velocityEngine, resourceLoaderPath); } }
From source file:org.entitypedia.games.common.api.handlers.DefaultExceptionDetailsResolver.java
private ExceptionDetails toExceptionDetails(String exceptionConfig) { String[] values = StringUtils.commaDelimitedListToStringArray(exceptionConfig); if (values == null || values.length == 0) { throw new IllegalStateException( "Invalid config mapping. Exception names must map to a string configuration."); }/*from w w w . ja v a 2s . c o m*/ ExceptionDetails result = new ExceptionDetails(); for (String value : values) { String trimmedVal = StringUtils.trimWhitespace(value); //check to see if the value is an explicitly named key/value pair: String[] pair = StringUtils.split(trimmedVal, "="); if (pair != null) { //explicit attribute set: String pairKey = StringUtils.trimWhitespace(pair[0]); if (!StringUtils.hasText(pairKey)) { pairKey = null; } String pairValue = StringUtils.trimWhitespace(pair[1]); if (!StringUtils.hasText(pairValue)) { pairValue = null; } if ("status".equalsIgnoreCase(pairKey)) { result.setStatus(getRequiredInt(pairKey, pairValue)); } else if ("msg".equalsIgnoreCase(pairKey)) { result.setErrorMessage(pairValue); } else if ("emsg".equalsIgnoreCase(pairKey)) { result.setExplanationMessage(pairValue); } else if ("wmsg".equalsIgnoreCase(pairKey)) { result.setWhatToDoMessage(pairValue); } else if ("infoUrl".equalsIgnoreCase(pairKey)) { result.setMoreInfoUrl(pairValue); } else if ("target".equalsIgnoreCase(pairKey)) { result.setExceptionClass(pairValue); } } } return result; }
From source file:org.wallride.service.UserService.java
@CacheEvict(value = WallRideCacheConfiguration.USER_CACHE, allEntries = true) public List<UserInvitation> inviteUsers(UserInvitationCreateRequest form, BindingResult result, AuthorizedUser authorizedUser) throws MessagingException { String[] recipients = StringUtils.commaDelimitedListToStringArray(form.getInvitees()); LocalDateTime now = LocalDateTime.now(); List<UserInvitation> invitations = new ArrayList<>(); for (String recipient : recipients) { UserInvitation invitation = new UserInvitation(); invitation.setEmail(recipient);// w w w. ja v a 2 s .co m invitation.setMessage(form.getMessage()); invitation.setExpiredAt(now.plusHours(72)); invitation.setCreatedAt(now); invitation.setCreatedBy(authorizedUser.toString()); invitation.setUpdatedAt(now); invitation.setUpdatedBy(authorizedUser.toString()); invitation = userInvitationRepository.saveAndFlush(invitation); invitations.add(invitation); } Blog blog = blogService.getBlogById(Blog.DEFAULT_ID); for (UserInvitation invitation : invitations) { String websiteTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage()); String signupLink = ServletUriComponentsBuilder.fromCurrentContextPath().path("/_admin/signup") .queryParam("token", invitation.getToken()).buildAndExpand().toString(); final Context ctx = new Context(LocaleContextHolder.getLocale()); ctx.setVariable("websiteTitle", websiteTitle); ctx.setVariable("authorizedUser", authorizedUser); ctx.setVariable("signupLink", signupLink); ctx.setVariable("invitation", invitation); final MimeMessage mimeMessage = mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart message.setSubject(MessageFormat.format( messageSourceAccessor.getMessage("InvitationMessageTitle", LocaleContextHolder.getLocale()), authorizedUser.toString(), websiteTitle)); message.setFrom(authorizedUser.getEmail()); message.setTo(invitation.getEmail()); final String htmlContent = templateEngine.process("user-invite", ctx); message.setText(htmlContent, true); // true = isHtml mailSender.send(mimeMessage); } return invitations; }
From source file:com.github.hateoas.forms.spring.AffordanceBuilder.java
/** * Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with the host tweaked in case the request contains * an {@code X-Forwarded-Host} header and the scheme tweaked in case the request contains an {@code X-Forwarded-Ssl} header * * @return builder//from w ww.j a v a2 s. c o m */ static UriComponentsBuilder getBuilder() { HttpServletRequest request = getCurrentRequest(); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request); String forwardedSsl = request.getHeader("X-Forwarded-Ssl"); if (StringUtils.hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) { builder.scheme("https"); } String host = request.getHeader("X-Forwarded-Host"); if (!StringUtils.hasText(host)) { return builder; } String[] hosts = StringUtils.commaDelimitedListToStringArray(host); String hostToUse = hosts[0]; if (hostToUse.contains(":")) { String[] hostAndPort = StringUtils.split(hostToUse, ":"); builder.host(hostAndPort[0]); builder.port(Integer.parseInt(hostAndPort[1])); } else { builder.host(hostToUse); builder.port(-1); // reset port if it was forwarded from default port } String port = request.getHeader("X-Forwarded-Port"); if (StringUtils.hasText(port)) { builder.port(Integer.parseInt(port)); } return builder; }
From source file:org.smf4j.spring.RegistryNodeTemplateDefinitionParser.java
protected ManagedList<RuntimeBeanReference> createGroupings(ParserContext context, Element element, String ranges, String suffixes) { ManagedList<RuntimeBeanReference> groupings = new ManagedList<RuntimeBeanReference>(); String[] rangeArray = StringUtils.commaDelimitedListToStringArray(ranges); String[] suffixArray = StringUtils.commaDelimitedListToStringArray(suffixes); if (rangeArray.length != suffixArray.length) { context.getReaderContext().error("'ranges' and 'suffixes' must have the same number of " + "elements", context.extractSource(element)); return null; }//from w w w. j ava 2 s . c o m for (int i = 0; i < rangeArray.length; i++) { String range = rangeArray[i]; String suffix = suffixArray[i]; BeanDefinitionBuilder bdb = getBdb(RANGEGROUP_GROUPING_CLASS); bdb.addPropertyValue(RANGE_ATTR, range); bdb.addPropertyValue(SUFFIX_ATTR, suffix); String groupId = context.getReaderContext().registerWithGeneratedName(bdb.getBeanDefinition()); groupings.add(new RuntimeBeanReference(groupId)); } return groupings; }
From source file:org.jruby.rack.mock.WebUtils.java
/** * Parse the given string with matrix variables. An example string would look * like this {@code "q1=a;q1=b;q2=a,b,c"}. The resulting map would contain * keys {@code "q1"} and {@code "q2"} with values {@code ["a","b"]} and * {@code ["a","b","c"]} respectively./*from ww w.j a va 2 s. co m*/ * * @param matrixVariables the unparsed matrix variables string * @return a map with matrix variable names and values, never {@code null} */ public static MultiValueMap<String, String> parseMatrixVariables(String matrixVariables) { MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(); if (!StringUtils.hasText(matrixVariables)) { return result; } StringTokenizer pairs = new StringTokenizer(matrixVariables, ";"); while (pairs.hasMoreTokens()) { String pair = pairs.nextToken(); int index = pair.indexOf('='); if (index != -1) { String name = pair.substring(0, index); String rawValue = pair.substring(index + 1); for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) { result.add(name, value); } } else { result.add(pair, ""); } } return result; }
From source file:com.lm.lic.manager.util.GenUtil.java
/** * When multiple forms are present in a jsp, spring tends to supply values separated by comas ",". Clean them. * //w w w. ja va 2 s. co m * @param prodDefKey * @return */ public static final String cleanUnwantedDuplicateComas(String prodDefKey) { String clean = GenConst.EMPTY; if (StringUtils.hasText(prodDefKey)) { String[] sl = StringUtils.commaDelimitedListToStringArray(prodDefKey); clean = sl[0]; } return clean; }