protected void validate(SamlServiceProvider provider) { if (provider == null) { throw new NullPointerException("SAML Service Provider can not be null."); } if (!StringUtils.hasText(provider.getIdentityZoneId())) { throw new DataIntegrityViolationException("Identity zone ID must be set."); } }
public boolean refreshApproval(final Approval approval, final String zoneId) { logger.debug(String.format("refreshing approval: [%s]", approval)); int refreshed = jdbcTemplate.update(REFRESH_AUTHZ_SQL, new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setTimestamp(1, new Timestamp(approval.getLastUpdatedAt().getTime())); ps.setTimestamp(2, new Timestamp(approval.getExpiresAt().getTime())); ps.setString(3, (approval.getStatus() == null ? APPROVED : approval.getStatus()).toString()); ps.setString(4, approval.getUserId()); ps.setString(5, approval.getClientId()); ps.setString(6, approval.getScope()); ps.setString(7, zoneId); } }); if (refreshed != 1) { throw new DataIntegrityViolationException("Attempt to refresh non-existent authorization"); } return true; }
@Override public String process(String item) throws Exception { if (item.equals("1")) { throw new DataIntegrityViolationException("Planned"); } return item; } });
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
/** * Delegate method to execute the insert, generating a single key. */ private Number executeInsertAndReturnKeyInternal(final List<?> values) { KeyHolder kh = executeInsertAndReturnKeyHolderInternal(values); if (kh.getKey() != null) { return kh.getKey(); } else { throw new DataIntegrityViolationException( "Unable to retrieve the generated key for the insert: " + getInsertString()); } }
private Mono<? extends String> translateException(Throwable e, IndexDefinitionHolder indexDefinition) { Mono<IndexInfo> existingIndex = fetchIndexInformation(indexDefinition); Mono<String> defaultError = Mono.error(new DataIntegrityViolationException( String.format("Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.", indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()), e.getCause())); return existingIndex.flatMap(it -> { return Mono.<String> error(new DataIntegrityViolationException( String.format("Index already defined as '%s'.", indexDefinition.getPath()), e.getCause())); }).switchIfEmpty(defaultError); }
return new DataIntegrityViolationException(ex.getMessage(), ex);
@Override public ExpiringCode generateCode(String data, Timestamp expiresAt, String intent, String zoneId) { if (data == null || expiresAt == null) { throw new NullPointerException(); } if (expiresAt.getTime() < timeService.getCurrentTimeMillis()) { throw new IllegalArgumentException(); } String code = generator.generate(); ExpiringCode expiringCode = new ExpiringCode(code, expiresAt, data, intent); ExpiringCode duplicate = store.putIfAbsent(code + IdentityZoneHolder.get().getId(), expiringCode); if (duplicate != null) { throw new DataIntegrityViolationException("Duplicate code: " + code); } return expiringCode; }
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { for (Document dbo : collection.find(dbk)) { String key = (String) dbo.get(ENTITY_FIELD_NAME); if (log.isDebugEnabled()) { log.debug("Processing key: {}", key); } if (!changeSet.getValues().containsKey(key)) { String className = (String) dbo.get(ENTITY_FIELD_CLASS); if (className == null) { throw new DataIntegrityViolationException( "Unble to convert property " + key + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available"); } Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader()); Object value = mongoTemplate.getConverter().read(clazz, dbo); if (log.isDebugEnabled()) { log.debug("Adding to ChangeSet: {}", key); } changeSet.set(key, value); } } return null; } });
return new DataIntegrityViolationException(ex.getMessage(), ex);
return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx);
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex); return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
protected void validate(IdentityProvider provider) { if (provider==null) { throw new NullPointerException("Provider can not be null."); } if (!StringUtils.hasText(provider.getIdentityZoneId())) { throw new DataIntegrityViolationException("Identity zone ID must be set."); } //ensure that SAML IDPs have reduntant fields synchronized if (OriginKeys.SAML.equals(provider.getType()) && provider.getConfig()!=null) { SamlIdentityProviderDefinition saml = ObjectUtils.castInstance(provider.getConfig(), SamlIdentityProviderDefinition.class); saml.setIdpEntityAlias(provider.getOriginKey()); saml.setZoneId(provider.getIdentityZoneId()); provider.setConfig(saml); } }
return new DataIntegrityViolationException(ex.getMessage(), ex);
@Override public String createAuthorizationCode(OAuth2Authentication authentication) { final int max_tries = 3; performExpirationClean(); JdbcTemplate template = new JdbcTemplate(dataSource); int tries = 0; while ((tries++)<=max_tries) { try { String code = generator.generate(); long expiresAt = System.currentTimeMillis()+getExpirationTime(); String userId = authentication.getUserAuthentication()==null ? null : ((UaaPrincipal)authentication.getUserAuthentication().getPrincipal()).getId(); String clientId = authentication.getOAuth2Request().getClientId(); SqlLobValue data = new SqlLobValue(serializeOauth2Authentication(authentication)); int updated = template.update( SQL_INSERT_STATEMENT, new Object[] {code, userId, clientId, expiresAt, data, IdentityZoneHolder.get().getId()}, new int[] {Types.VARCHAR,Types.VARCHAR, Types.VARCHAR, Types.NUMERIC, Types.BLOB, Types.VARCHAR} ); if (updated==0) { throw new DataIntegrityViolationException("[oauth_code] Failed to insert code. Result was 0"); } return code; } catch (DataIntegrityViolationException exists) { if (tries>=max_tries) throw exists; } } return null; }
@Test public void testExceptionHandler() { Map<Class<? extends Exception>, HttpStatus> map = new HashMap<Class<? extends Exception>, HttpStatus>(); map.put(IllegalArgumentException.class, HttpStatus.BAD_REQUEST); map.put(UnsupportedOperationException.class, HttpStatus.BAD_REQUEST); map.put(BadSqlGrammarException.class, HttpStatus.BAD_REQUEST); map.put(DataIntegrityViolationException.class, HttpStatus.BAD_REQUEST); map.put(HttpMessageConversionException.class, HttpStatus.BAD_REQUEST); map.put(HttpMediaTypeException.class, HttpStatus.BAD_REQUEST); endpoints.setStatuses(map); endpoints.setMessageConverters(new HttpMessageConverter<?>[] { new ExceptionReportHttpMessageConverter() }); MockHttpServletRequest request = new MockHttpServletRequest(); validateView(endpoints.handleException(new ScimResourceNotFoundException(""), request), HttpStatus.NOT_FOUND); validateView(endpoints.handleException(new UnsupportedOperationException(""), request), HttpStatus.BAD_REQUEST); validateView(endpoints.handleException(new BadSqlGrammarException("", "", null), request), HttpStatus.BAD_REQUEST); validateView(endpoints.handleException(new IllegalArgumentException(""), request), HttpStatus.BAD_REQUEST); validateView(endpoints.handleException(new DataIntegrityViolationException(""), request), HttpStatus.BAD_REQUEST); }
@Test public void testHandleExceptionWithConstraintViolation() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); endpoints.setMessageConverters(new HttpMessageConverter<?>[]{new ExceptionReportHttpMessageConverter()}); View view = endpoints.handleException(new DataIntegrityViolationException("foo"), request); ConvertingExceptionView converted = (ConvertingExceptionView) view; converted.render(Collections.<String, Object>emptyMap(), request, response); String body = response.getContentAsString(); assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); // System.err.println(body); assertTrue("Wrong body: " + body, body.contains("message\":\"foo")); }
void createIndex(IndexDefinitionHolder indexDefinition) { try { IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection()); indexOperations.ensureIndex(indexDefinition); } catch (UncategorizedMongoDbException ex) { if (ex.getCause() instanceof MongoException && MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) { IndexInfo existingIndex = fetchIndexInformation(indexDefinition); String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'."; if (existingIndex != null) { message += " Index already defined as '%s'."; } throw new DataIntegrityViolationException( String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex.getCause()); } throw ex; } }
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); return new DataIntegrityViolationException(ex.getMessage(), ex);
return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); return new DataIntegrityViolationException(ex.getMessage(), ex);