/** * Handle the case where the ResultSet contains multiple rows. * @throws DataAccessException a corresponding exception, * by default an IncorrectResultSizeDataAccessException * @see org.springframework.dao.IncorrectResultSizeDataAccessException */ protected void handleMultipleRowsFound() throws DataAccessException { throw new IncorrectResultSizeDataAccessException( "LobStreamingResultSetExtractor found multiple rows in database", 1); }
if (notFound.getActualSize() == 0) { throw new UsernameNotFoundException("User " + username + " not found in directory.");
@Test @SuppressWarnings("deprecation") // on JDK 9 public void withEquivalentIntegerInstanceTwice() { Collection<Integer> col = new ArrayList<>(2); col.add(new Integer(5)); col.add(new Integer(5)); try { DataAccessUtils.uniqueResult(col); fail("Should have thrown IncorrectResultSizeDataAccessException"); } catch (IncorrectResultSizeDataAccessException ex) { // expected assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize()); } }
throw new ScimException(ex.getMessage(), ex, HttpStatus.CONFLICT); } catch (ScimResourceNotFoundException ex) { logger.error("Error updating group, restoring to previous state: " + existing);
assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize()); assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize()); assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize()); assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize()); assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize());
@Override protected DirContextOperations doAuthentication(UsernamePasswordAuthenticationToken authentication) { try { return super.doAuthentication(authentication); } catch (InternalAuthenticationServiceException e) { // This sometimes happens ... for unknown reasons the underlying libraries cannot // figure out correct exception. Which results to wrong error message (MID-4518) // So, be smart here and try to figure out correct error. throw processInternalAuthenticationException(e, e); } catch (IncorrectResultSizeDataAccessException e) { LOGGER.error("Failed to authenticate user {}. Error: {}", authentication.getName(), e.getMessage(), e); throw new BadCredentialsException("LdapAuthentication.bad.user", e); } catch (RuntimeException e) { LOGGER.error("Failed to authenticate user {}. Error: {}", authentication.getName(), e.getMessage(), e); auditProvider.auditLoginFailure(authentication.getName(), null, ConnectionEnvironment.create(SchemaConstants.CHANNEL_GUI_USER_URI), "bad credentials"); throw e; } }
/** * Return a single result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 element found. * @param results the result Collection (can be {@code null}) * @return the single result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection */ @Nullable public static <T> T singleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
private DirContextOperations searchForUser(DirContext context, String username) throws NamingException { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String bindPrincipal = createBindPrincipal(username); String searchRoot = rootDn != null ? rootDn : searchRootFromPrincipal(bindPrincipal); try { return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context, searchControls, searchRoot, searchFilter, new Object[] { bindPrincipal, username }); } catch (IncorrectResultSizeDataAccessException incorrectResults) { // Search should never return multiple results if properly configured - just // rethrow if (incorrectResults.getActualSize() != 0) { throw incorrectResults; } // If we found no results, then the username/password did not match UsernameNotFoundException userNameNotFoundException = new UsernameNotFoundException( "User " + username + " not found in directory.", incorrectResults); throw badCredentials(userNameNotFoundException); } }
assertEquals(1, ex.getExpectedSize()); assertEquals(0, ex.getActualSize()); assertEquals(1, ex.getExpectedSize()); assertEquals(0, ex.getActualSize()); assertEquals(1, ex.getExpectedSize()); assertEquals(0, ex.getActualSize()); assertEquals(1, ex.getExpectedSize()); assertEquals(0, ex.getActualSize());
/** * Return a single result object from the given Collection. * <p>Throws an exception if 0 or more than 1 element found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the single result object * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection * @throws EmptyResultDataAccessException if no element at all * has been found in the given Collection */ public static <T> T requiredSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** {@inheritDoc} */ @Override public synchronized int getServiceId(String serviceName) throws DataAccessException { Assert.notNull(serviceName, "The serviceName argument must not be null"); if (m_serviceMap.containsKey(serviceName)) { return m_serviceMap.get(serviceName).intValue(); } else { LOG.debug("Could not find entry for '{}' in service name cache. Looking up in database.", serviceName); int serviceId; try { serviceId = new JdbcTemplate(m_dataSource).queryForObject("SELECT serviceID FROM service WHERE serviceName = ?", new Object[] { serviceName }, Integer.class); } catch (IncorrectResultSizeDataAccessException e) { if (e.getActualSize() == 0) { LOG.debug("Did not find entry for '{}' in database.", serviceName); return -1; // not found } else { throw e; // more than one found... WTF?!?! } } m_serviceMap.put(serviceName, serviceId); LOG.debug("Found entry for '{}' (ID {}) in database. Adding to service name cache.", serviceName, serviceId); return serviceId; } }
/** * Return a unique result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 instance found. * @param results the result Collection (can be {@code null}) * @return the unique result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ @Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** {@inheritDoc} */ @Override public synchronized int getServiceId(String serviceName) throws DataAccessException { Assert.notNull(serviceName, "The serviceName argument must not be null"); if (m_serviceMap.containsKey(serviceName)) { return m_serviceMap.get(serviceName).intValue(); } else { LOG.debug("Could not find entry for '{}' in service name cache. Looking up in database.", serviceName); int serviceId; try { serviceId = new JdbcTemplate(m_dataSource).queryForObject("SELECT serviceID FROM service WHERE serviceName = ?", new Object[] { serviceName }, Integer.class); } catch (IncorrectResultSizeDataAccessException e) { if (e.getActualSize() == 0) { LOG.debug("Did not find entry for '{}' in database.", serviceName); return -1; // not found } else { throw e; // more than one found... WTF?!?! } } m_serviceMap.put(serviceName, serviceId); LOG.debug("Found entry for '{}' (ID {}) in database. Adding to service name cache.", serviceName, serviceId); return serviceId; } }
/** * Return a single result object from the given Collection. * <p>Throws an exception if 0 or more than 1 element found. * @param results the result Collection (can be {@code null} * and is also expected to contain {@code null} elements) * @return the single result object * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection * @throws EmptyResultDataAccessException if no element at all * has been found in the given Collection * @since 5.0.2 */ @Nullable public static <T> T nullableSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { // This is identical to the requiredSingleResult implementation but differs in the // semantics of the incoming Collection (which we currently can't formally express) if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
@Override public ScimGroup getByName(String displayName, String zoneId) { if (!hasText(displayName)) { throw new IncorrectResultSizeDataAccessException("group name must contain text", 1, 0); } List<ScimGroup> groups = jdbcTemplate.query(GET_GROUP_BY_NAME_SQL, rowMapper, displayName, zoneId); if (groups.size()==1) { return groups.get(0); } else { throw new IncorrectResultSizeDataAccessException("Invalid result size found for:"+displayName, 1, groups.size()); } }
/** * Return a unique result object from the given Collection. * <p>Throws an exception if 0 or more than 1 instance found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique result object * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object at all * has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a single result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 element found. * @param results the result Collection (can be {@code null}) * @return the single result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection */ @Nullable public static <T> T singleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
public boolean userExists(String username) { List<String> users = getJdbcTemplate().queryForList(userExistsSql, new String[] { username }, String.class); if (users.size() > 1) { throw new IncorrectResultSizeDataAccessException( "More than one user found with name '" + username + "'", 1); } return users.size() == 1; }
/** * Return a single result object from the given Collection. * <p>Throws an exception if 0 or more than 1 element found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the single result object * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection * @throws EmptyResultDataAccessException if no element at all * has been found in the given Collection */ public static <T> T requiredSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 instance found. * @param results the result Collection (can be {@code null}) * @return the unique result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ @Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }