List of usage examples for org.springframework.util StringUtils commaDelimitedListToStringArray
public static String[] commaDelimitedListToStringArray(@Nullable String str)
From source file:org.openmrs.module.personalhr.web.taglib.RequireTag.java
/** * This is where all the magic happens. The privileges are checked and the user is redirected if * need be. <br/>/*from w w w. j a v a 2 s . com*/ * <br/> * Returns SKIP_PAGE if the user doesn't have the privilege and SKIP_BODY if it does. * * @see javax.servlet.jsp.tagext.TagSupport#doStartTag() * @should allow user with the privilege * @should allow user to have any privilege * @should allow user with all privileges * @should reject user without the privilege * @should reject user without any of the privileges * @should reject user without all of the privileges */ @Override public int doStartTag() { this.log.debug("PHR RequireTag started..."); this.errorOccurred = false; final HttpServletResponse httpResponse = (HttpServletResponse) this.pageContext.getResponse(); final HttpSession httpSession = this.pageContext.getSession(); final HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest(); final String request_ip_addr = request.getLocalAddr(); final String session_ip_addr = (String) httpSession .getAttribute(WebConstants.OPENMRS_CLIENT_IP_HTTPSESSION_ATTR); final UserContext userContext = Context.getUserContext(); if ((userContext == null) && (this.privilege != null)) { this.log.error("userContext is null. Did this pass through a filter?"); //httpSession.removeAttribute(WebConstants.OPENMRS_CONTEXT_HTTPSESSION_ATTR); //TODO find correct error to throw throw new APIException("The context is currently null. Please try reloading the site."); } final User user = userContext.getAuthenticatedUser(); Integer patientId = PersonalhrUtil.getInteger(this.pageContext.getAttribute("patientId")); if (patientId == null) { patientId = PersonalhrUtil.getInteger(this.pageContext.getRequest().getAttribute("patientId")); } if (patientId == null) { patientId = PersonalhrUtil.getInteger(this.pageContext.getRequest().getParameter("patientId")); } Integer personId = PersonalhrUtil.getInteger(this.pageContext.getAttribute("personId")); if (personId == null) { personId = PersonalhrUtil.getInteger(this.pageContext.getRequest().getAttribute("personId")); } if (personId == null) { personId = PersonalhrUtil.getInteger(this.pageContext.getRequest().getParameter("personId")); } this.log.debug("Checking user " + user + " for privs " + this.privilege + " on personId|patientId " + personId + "|" + patientId); final Patient pat = patientId == null ? null : Context.getPatientService().getPatient(patientId); final Person per = personId == null ? null : Context.getPersonService().getPerson(personId); if (per != null) { this.log.debug("Checking user " + user + " for privs " + this.privilege + " on person " + per); } if (pat != null) { this.log.debug("Checking user " + user + " for privs " + this.privilege + " on patient " + pat); } if ((per == null) && (pat == null)) { this.log.debug("Checking user " + user + " for privs " + this.privilege); } this.log.debug("Checking user " + user + " for privs|role " + this.privilege + "|" + this.role + " on person|patient " + per + "|" + pat); // Parse comma-separated list of privileges in allPrivileges and anyPrivileges attributes final String[] allPrivilegesArray = StringUtils.commaDelimitedListToStringArray(this.allPrivileges); final String[] anyPrivilegeArray = StringUtils.commaDelimitedListToStringArray(this.anyPrivilege); boolean hasPrivilege = hasPrivileges(user, per, pat, this.privilege, allPrivilegesArray, anyPrivilegeArray); if ((hasPrivilege || this.privilege == null) && (this.role != null && !this.role.trim().isEmpty())) { hasPrivilege = user.hasRole(role); } if (!hasPrivilege) { this.errorOccurred = true; if (userContext.isAuthenticated()) { httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "require.unauthorized"); this.log.warn("The user: '" + Context.getAuthenticatedUser() + "' has attempted to access: " + this.redirect + " which requires privilege: " + this.privilege + " or one of: " + this.allPrivileges + " or any of " + this.anyPrivilege); } else { httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "require.login"); } } else if (hasPrivilege && userContext.isAuthenticated()) { // redirect users to password change form this.log.debug("Login redirect: " + this.redirect); if (new UserProperties(user.getUserProperties()).isSupposedToChangePassword() && !this.redirect.contains("options.form")) { httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "User.password.change"); this.errorOccurred = true; this.redirect = request.getContextPath() + "/options.form#Change Login Info"; this.otherwise = this.redirect; try { httpResponse.sendRedirect(this.redirect); return SKIP_PAGE; } catch (final IOException e) { // oops, cannot redirect this.log.error("Unable to redirect for password change: " + this.redirect, e); throw new APIException(e); } } } if (differentIpAddresses(session_ip_addr, request_ip_addr)) { this.errorOccurred = true; // stops warning message in IE when refreshing repeatedly if ("0.0.0.0".equals(request_ip_addr) == false) { this.log.warn("Invalid ip addr: expected " + session_ip_addr + ", but found: " + request_ip_addr); httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "require.ip_addr"); } } this.log.debug("session ip addr: " + session_ip_addr); if (this.errorOccurred) { String url = ""; if ((this.redirect != null) && !this.redirect.equals("")) { url = request.getContextPath() + this.redirect; } else { url = request.getRequestURI(); } if (request.getQueryString() != null) { url = url + "?" + request.getQueryString(); } httpSession.setAttribute(WebConstants.OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, url); try { httpResponse.sendRedirect(request.getContextPath() + this.otherwise); return SKIP_PAGE; } catch (final IOException e) { // oops, cannot redirect throw new APIException(e); } } return SKIP_BODY; }
From source file:grails.plugin.springsecurity.SpringSecurityUtils.java
/** * Split the role names and create {@link GrantedAuthority}s for each. * @param roleNames comma-delimited role names * @return authorities (possibly empty)/*from ww w . ja v a2 s. c o m*/ */ public static List<GrantedAuthority> parseAuthoritiesString(final String roleNames) { List<GrantedAuthority> requiredAuthorities = new ArrayList<GrantedAuthority>(); for (String auth : StringUtils.commaDelimitedListToStringArray(roleNames)) { auth = auth.trim(); if (auth.length() > 0) { requiredAuthorities.add(new SimpleGrantedAuthority(auth)); } } return requiredAuthorities; }
From source file:org.apache.servicemix.platform.testing.support.AbstractIntegrationTest.java
protected Resource locateBundle(String bundleId) { Assert.hasText(bundleId, "bundleId should not be empty"); // parse the String String[] artifactId = StringUtils.commaDelimitedListToStringArray(bundleId); Assert.isTrue(artifactId.length >= 3, "the CSV string " + bundleId + " contains too few values"); // TODO: add a smarter mechanism which can handle 1 or 2 values CSVs for (int i = 0; i < artifactId.length; i++) { artifactId[i] = StringUtils.trimWhitespace(artifactId[i]); }/* ww w. j a v a2 s . c o m*/ File f; if (artifactId.length == 3) { f = localMavenBundle(artifactId[0], artifactId[1], artifactId[2], null, ArtifactLocator.DEFAULT_ARTIFACT_TYPE); } else { f = localMavenBundle(artifactId[0], artifactId[1], artifactId[2], null, artifactId[3]); } return new FileSystemResource(f); }
From source file:org.atomserver.utils.acegi.RESTfulDefinitionSource.java
/** * this is completely bogus. I am duplicating code in FilterInvocationDefinitionSourceEditor *//*from w w w . ja va 2 s. co m*/ private void processPathList(String pathToRoleList) throws IllegalArgumentException { delegate.setConvertUrlToLowercaseBeforeComparison(true); BufferedReader br = new BufferedReader(new StringReader(pathToRoleList)); int counter = 0; String line; List mappings = new ArrayList(); while (true) { counter++; try { line = br.readLine(); } catch (IOException ioe) { throw new IllegalArgumentException(ioe.getMessage()); } if (line == null) { break; } line = line.trim(); if (line.startsWith("//")) { continue; } // Skip lines that are not directives if (line.lastIndexOf('=') == -1) { continue; } if (line.lastIndexOf("==") != -1) { throw new IllegalArgumentException("Only single equals should be used in line " + line); } // Tokenize the line into its name/value tokens // As per SEC-219, use the LAST equals as the delimiter between LHS and RHS String name = StringSplitUtils.substringBeforeLast(line, "="); String value = StringSplitUtils.substringAfterLast(line, "="); if (!StringUtils.hasText(name) || !StringUtils.hasText(value)) { throw new IllegalArgumentException("Failed to parse a valid name/value pair from " + line); } String antPath = name; String methods = null; int firstColonIndex = name.indexOf(":"); if (firstColonIndex != -1) { antPath = name.substring(0, firstColonIndex); methods = name.substring((firstColonIndex + 1), name.length()); } String[] methodList = null; if (methods != null) { methodList = methods.split(","); // Verify methodList is valid for (int ii = 0; ii < methodList.length; ii++) { boolean matched = false; for (int jj = 0; jj < validMethodNames.length; jj++) { if (methodList[ii].equals(validMethodNames[jj])) { matched = true; break; } } if (!matched) { throw new IllegalArgumentException("The HTTP Method Name (" + methodList[ii] + " does NOT equal a valid name (GET,PUT,POST,DELETE)"); } } } // Should all be lowercase; check each character // We only do this for Ant (regexp have control chars) for (int i = 0; i < antPath.length(); i++) { String character = antPath.substring(i, i + 1); if (!character.toLowerCase().equals(character)) { throw new IllegalArgumentException( "You are using Ant Paths, yet you have specified an uppercase character in line: " + line + " (character '" + character + "')"); } } RESTfulDefinitionSourceMapping mapping = new RESTfulDefinitionSourceMapping(); mapping.setUrl(antPath); mapping.setHttpMethods(methodList); String[] tokens = StringUtils.commaDelimitedListToStringArray(value); for (int i = 0; i < tokens.length; i++) { mapping.addConfigAttribute(tokens[i].trim()); } mappings.add(mapping); } // This will call the addSecureUrl in RESTfulPathBasedFilterInvocationDefinitionMap // which is how this whole convoluted beast gets wired together //source.setMappings(mappings); setMappings(mappings); }
From source file:org.springmodules.validation.bean.conf.loader.annotation.handler.AbstractClassValidationAnnotationHandler.java
/** * Extracts the {@link org.springmodules.validation.bean.rule.resolver.ErrorArgumentsResolver} for the validation rule represented by the given annotation. * Expects a {@link #ARGS_ATTR} to be present. * * @param annotation The property validation annotation. * @return The {@link org.springmodules.validation.bean.rule.resolver.ErrorArgumentsResolver} for the represented validation rule. *//* w w w.j av a 2 s . c o m*/ protected ErrorArgumentsResolver extractArgumentsResolver(Annotation annotation) { String argsAsString = (String) extractAnnotationAttribute(annotation, ARGS_ATTR); argsAsString = (argsAsString == null) ? "" : argsAsString; String[] argsExpressions = StringUtils.commaDelimitedListToStringArray(argsAsString); if (argsExpressions.length == 0) { return null; } return new FunctionErrorArgumentsResolver(argsExpressions, functionExpressionParser); }
From source file:org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils.java
/** * Split the role names and create {@link GrantedAuthority}s for each. * @param roleNames comma-delimited role names * @return authorities (possibly empty)//from w w w . j ava 2s . c o m */ public static List<GrantedAuthority> parseAuthoritiesString(final String roleNames) { List<GrantedAuthority> requiredAuthorities = new ArrayList<GrantedAuthority>(); for (String auth : StringUtils.commaDelimitedListToStringArray(roleNames)) { auth = auth.trim(); if (auth.length() > 0) { requiredAuthorities.add(new GrantedAuthorityImpl(auth)); } } return requiredAuthorities; }
From source file:org.geowebcache.security.RESTfulDefinitionSource.java
/** * this is completely bogus. I am duplicating code in * FilterInvocationDefinitionSourceEditor *///from www . j ava 2 s . c om private void processPathList(String pathToRoleList) throws IllegalArgumentException { delegate.setConvertUrlToLowercaseBeforeComparison(true); BufferedReader br = new BufferedReader(new StringReader(pathToRoleList)); int counter = 0; String line; List mappings = new ArrayList<String>(); while (true) { counter++; try { line = br.readLine(); } catch (IOException ioe) { throw new IllegalArgumentException(ioe.getMessage()); } if (line == null) { break; } line = line.trim(); if (log.isDebugEnabled()) { log.debug("Line " + counter + ": " + line); } if (line.startsWith("//")) { continue; } // Skip lines that are not directives if (line.lastIndexOf('=') == -1) { continue; } if (line.lastIndexOf("==") != -1) { throw new IllegalArgumentException("Only single equals should be used in line " + line); } // Tokenize the line into its name/value tokens // As per SEC-219, use the LAST equals as the delimiter between LHS // and RHS String name = StringSplitUtils.substringBeforeLast(line, "="); String value = StringSplitUtils.substringAfterLast(line, "="); if (!StringUtils.hasText(name) || !StringUtils.hasText(value)) { throw new IllegalArgumentException("Failed to parse a valid name/value pair from " + line); } String antPath = name; String methods = null; int firstColonIndex = name.indexOf(":"); if (log.isDebugEnabled()) log.debug("~~~~~~~~~~ name= " + name + " firstColonIndex= " + firstColonIndex); if (firstColonIndex != -1) { antPath = name.substring(0, firstColonIndex); methods = name.substring((firstColonIndex + 1), name.length()); } if (log.isDebugEnabled()) log.debug("~~~~~~~~~~ name= " + name + " antPath= " + antPath + " methods= " + methods); String[] methodList = null; if (methods != null) { methodList = methods.split(","); // Verify methodList is valid for (int ii = 0; ii < methodList.length; ii++) { boolean matched = false; for (int jj = 0; jj < validMethodNames.length; jj++) { if (methodList[ii].equals(validMethodNames[jj])) { matched = true; break; } } if (!matched) { throw new IllegalArgumentException("The HTTP Method Name (" + methodList[ii] + " does NOT equal a valid name (GET,PUT,POST,DELETE)"); } } } if (log.isDebugEnabled()) { log.debug("methodList = " + Arrays.toString(methodList)); if (methodList != null) { for (int ii = 0; ii < methodList.length; ii++) log.debug("method[" + ii + "]: " + methodList[ii]); } } // Should all be lowercase; check each character // We only do this for Ant (regexp have control chars) for (int i = 0; i < antPath.length(); i++) { String character = antPath.substring(i, i + 1); if (!character.toLowerCase().equals(character)) { throw new IllegalArgumentException( "You are using Ant Paths, yet you have specified an uppercase character in line: " + line + " (character '" + character + "')"); } } RESTfulDefinitionSourceMapping mapping = new RESTfulDefinitionSourceMapping(); mapping.setUrl(antPath); mapping.setHttpMethods(methodList); String[] tokens = StringUtils.commaDelimitedListToStringArray(value); for (int i = 0; i < tokens.length; i++) { mapping.addConfigAttribute(tokens[i].trim()); } mappings.add(mapping); } // This will call the addSecureUrl in // RESTfulPathBasedFilterInvocationDefinitionMap // which is how this whole convoluted beast gets wired together // source.setMappings(mappings); setMappings(mappings); }
From source file:org.cloudfoundry.identity.uaa.config.YamlServletProfileInitializer.java
private void applySpringProfiles(ConfigurableEnvironment environment, ServletContext servletContext) { if (environment.containsProperty("spring_profiles")) { String profiles = (String) environment.getProperty("spring_profiles"); servletContext.log("Setting active profiles: " + profiles); environment.setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles)); }// w w w.j av a 2s . c om }