use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class JdbcTransactionalStorage method storeMessageInDatabase.
protected String storeMessageInDatabase(Connection conn, String messageId, String correlationId, Timestamp receivedDateTime, String comments, String label, Serializable message) throws IOException, SQLException, JdbcException, SenderException {
PreparedStatement stmt = null;
try {
IDbmsSupport dbmsSupport = getDbmsSupport();
if (log.isDebugEnabled()) {
log.debug("preparing insert statement [" + insertQuery + "]");
}
if (!dbmsSupport.mustInsertEmptyBlobBeforeData()) {
stmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
} else {
stmt = conn.prepareStatement(insertQuery);
}
stmt.clearParameters();
int parPos = 0;
if (StringUtils.isNotEmpty(getTypeField())) {
stmt.setString(++parPos, type);
}
if (StringUtils.isNotEmpty(getSlotId())) {
stmt.setString(++parPos, getSlotId());
}
if (StringUtils.isNotEmpty(getHostField())) {
stmt.setString(++parPos, host);
}
if (StringUtils.isNotEmpty(getLabelField())) {
stmt.setString(++parPos, label);
}
stmt.setString(++parPos, messageId);
stmt.setString(++parPos, correlationId);
stmt.setTimestamp(++parPos, receivedDateTime);
stmt.setString(++parPos, comments);
if (type.equalsIgnoreCase(TYPE_MESSAGELOG_PIPE) || type.equalsIgnoreCase(TYPE_MESSAGELOG_RECEIVER)) {
if (getRetention() < 0) {
stmt.setTimestamp(++parPos, null);
} else {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, getRetention());
stmt.setTimestamp(++parPos, new Timestamp(cal.getTime().getTime()));
}
} else {
stmt.setTimestamp(++parPos, null);
}
if (!isStoreFullMessage()) {
if (isOnlyStoreWhenMessageIdUnique()) {
stmt.setString(++parPos, messageId);
stmt.setString(++parPos, slotId);
}
stmt.execute();
return null;
}
if (!dbmsSupport.mustInsertEmptyBlobBeforeData()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (isBlobsCompressed()) {
DeflaterOutputStream dos = new DeflaterOutputStream(out);
ObjectOutputStream oos = new ObjectOutputStream(dos);
oos.writeObject(message);
dos.close();
} else {
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(message);
}
stmt.setBytes(++parPos, out.toByteArray());
if (isOnlyStoreWhenMessageIdUnique()) {
stmt.setString(++parPos, messageId);
stmt.setString(++parPos, slotId);
}
stmt.execute();
ResultSet rs = stmt.getGeneratedKeys();
boolean messageIdExists = false;
if (rs.next() && rs.getString(1) != null) {
return "<results><result>" + rs.getString(1) + "</result></results>";
} else {
messageIdExists = true;
}
if (messageIdExists) {
boolean isMessageDifferent = isMessageDifferent(conn, messageId, message);
String resultString = createResultString(isMessageDifferent);
log.warn("MessageID [" + messageId + "] already exists");
if (isMessageDifferent) {
log.warn("Message with MessageID [" + messageId + "] is not equal");
}
return resultString;
}
}
if (isOnlyStoreWhenMessageIdUnique()) {
stmt.setString(++parPos, messageId);
stmt.setString(++parPos, slotId);
}
stmt.execute();
int updateCount = stmt.getUpdateCount();
if (log.isDebugEnabled()) {
log.debug("update count for insert statement: " + updateCount);
}
if (updateCount > 0) {
if (log.isDebugEnabled()) {
log.debug("preparing select statement [" + selectKeyQuery + "]");
}
stmt = conn.prepareStatement(selectKeyQuery);
ResultSet rs = null;
try {
// retrieve the key
rs = stmt.executeQuery();
if (!rs.next()) {
throw new SenderException("could not retrieve key of stored message");
}
String newKey = rs.getString(1);
rs.close();
// and update the blob
if (log.isDebugEnabled()) {
log.debug("preparing update statement [" + updateBlobQuery + "]");
}
stmt = conn.prepareStatement(updateBlobQuery);
stmt.clearParameters();
stmt.setString(1, newKey);
rs = stmt.executeQuery();
if (!rs.next()) {
throw new SenderException("could not retrieve row for stored message [" + messageId + "]");
}
Object blobHandle = dbmsSupport.getBlobUpdateHandle(rs, 1);
OutputStream out = dbmsSupport.getBlobOutputStream(rs, 1, blobHandle);
if (isBlobsCompressed()) {
DeflaterOutputStream dos = new DeflaterOutputStream(out);
ObjectOutputStream oos = new ObjectOutputStream(dos);
oos.writeObject(message);
oos.close();
dos.close();
} else {
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(message);
oos.close();
}
out.close();
dbmsSupport.updateBlob(rs, 1, blobHandle);
return newKey;
} finally {
if (rs != null) {
rs.close();
}
}
} else {
if (isOnlyStoreWhenMessageIdUnique()) {
return "already there";
} else {
throw new SenderException("update count for update statement not greater than 0 [" + updateCount + "]");
}
}
} finally {
if (stmt != null) {
stmt.close();
}
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class JdbcTransactionalStorage method storeMessage.
public String storeMessage(Connection conn, String messageId, String correlationId, Date receivedDate, String comments, String label, Serializable message) throws SenderException {
String result;
try {
Timestamp receivedDateTime = new Timestamp(receivedDate.getTime());
if (messageId.length() > MAXIDLEN) {
messageId = messageId.substring(0, MAXIDLEN);
}
if (correlationId.length() > MAXCIDLEN) {
correlationId = correlationId.substring(0, MAXCIDLEN);
}
if (comments != null && comments.length() > MAXCOMMENTLEN) {
comments = comments.substring(0, MAXCOMMENTLEN);
}
if (label != null && label.length() > MAXLABELLEN) {
label = label.substring(0, MAXLABELLEN);
}
result = storeMessageInDatabase(conn, messageId, correlationId, receivedDateTime, comments, label, message);
if (result == null) {
result = retrieveKey(conn, messageId, correlationId, receivedDateTime);
}
return result;
} catch (Exception e) {
throw new SenderException("cannot serialize message", e);
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class LdapSender method performOperationUpdate.
private String performOperationUpdate(String entryName, ParameterResolutionContext prc, Map paramValueMap, Attributes attrs) throws SenderException, ParameterException {
String entryNameAfter = entryName;
if (paramValueMap != null) {
String newEntryName = (String) paramValueMap.get("newEntryName");
if (newEntryName != null && StringUtils.isNotEmpty(newEntryName)) {
if (log.isDebugEnabled())
log.debug("newEntryName=[" + newEntryName + "]");
DirContext dirContext = null;
try {
dirContext = getDirContext(paramValueMap);
dirContext.rename(entryName, newEntryName);
entryNameAfter = newEntryName;
} catch (NamingException e) {
String msg;
// [LDAP: error code 32 - No Such Object...
if (e.getMessage().startsWith("[LDAP: error code 32 - ")) {
msg = "Operation [" + getOperation() + "] failed - wrong entryName [" + entryName + "]";
} else {
msg = "Exception in operation [" + getOperation() + "] entryName [" + entryName + "]";
}
storeLdapException(e, prc);
throw new SenderException(msg, e);
} finally {
closeDirContext(dirContext);
}
}
}
if (manipulationSubject.equals(MANIPULATION_ATTRIBUTE)) {
if (attrs == null && !entryNameAfter.equals(entryName)) {
// it should be possible to only 'rename' the entry (without attribute change)
return DEFAULT_RESULT;
}
NamingEnumeration na = attrs.getAll();
while (na.hasMoreElements()) {
Attribute a = (Attribute) na.nextElement();
log.debug("Update attribute: " + a.getID());
NamingEnumeration values;
try {
values = a.getAll();
} catch (NamingException e1) {
storeLdapException(e1, prc);
throw new SenderException("cannot obtain values of Attribute [" + a.getID() + "]", e1);
}
while (values.hasMoreElements()) {
Attributes partialAttrs = new BasicAttributes();
Attribute singleValuedAttribute;
String id = a.getID();
Object value = values.nextElement();
if (log.isDebugEnabled()) {
if (id.toLowerCase().contains("password") || id.toLowerCase().contains("pwd")) {
log.debug("Update value: ***");
} else {
log.debug("Update value: " + value);
}
}
if (unicodePwd && "unicodePwd".equalsIgnoreCase(id)) {
singleValuedAttribute = new BasicAttribute(id, encodeUnicodePwd(value));
} else {
singleValuedAttribute = new BasicAttribute(id, value);
}
partialAttrs.put(singleValuedAttribute);
DirContext dirContext = null;
try {
dirContext = getDirContext(paramValueMap);
dirContext.modifyAttributes(entryNameAfter, DirContext.REPLACE_ATTRIBUTE, partialAttrs);
} catch (NamingException e) {
String msg;
// [LDAP: error code 32 - No Such Object...
if (e.getMessage().startsWith("[LDAP: error code 32 - ")) {
msg = "Operation [" + getOperation() + "] failed - wrong entryName [" + entryNameAfter + "]";
} else {
msg = "Exception in operation [" + getOperation() + "] entryName [" + entryNameAfter + "]";
}
// result = DEFAULT_RESULT_UPDATE_NOK;
storeLdapException(e, prc);
throw new SenderException(msg, e);
} finally {
closeDirContext(dirContext);
}
}
}
return DEFAULT_RESULT;
} else {
DirContext dirContext = null;
try {
dirContext = getDirContext(paramValueMap);
// dirContext.rename(newEntryName, oldEntryName);
// result = DEFAULT_RESULT;
dirContext.rename(entryName, entryName);
return "<LdapResult>Deze functionaliteit is nog niet beschikbaar - naam niet veranderd.</LdapResult>";
} catch (NamingException e) {
// [LDAP: error code 68 - Entry Already Exists]
if (!e.getMessage().startsWith("[LDAP: error code 68 - ")) {
storeLdapException(e, prc);
throw new SenderException(e);
}
return DEFAULT_RESULT_CREATE_NOK;
} finally {
closeDirContext(dirContext);
}
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class LdapSender method performOperationChallenge.
private String performOperationChallenge(String principal, ParameterResolutionContext prc, Map paramValueMap) throws SenderException, ParameterException {
DirContext dirContext = null;
try {
// Use loopkupDirContext instead of getDirContext to prevent
// NamingException (with error code 49) being converted to
// SenderException.
dirContext = loopkupDirContext(paramValueMap);
attributesToXml(dirContext.getAttributes(principal, getAttributesReturnedParameter())).toXML();
return DEFAULT_RESULT_CHALLENGE_OK;
} catch (NamingException e) {
// 49 LDAP_INVALID_CREDENTIALS Indicates that during a bind operation one of the following occurred: The client passed either an incorrect DN or password, or the password is incorrect because it has expired, intruder detection has locked the account, or another similar reason. This is equivalent to AD error code 52e.
if (e.getMessage().startsWith("[LDAP: error code 49 - ")) {
if (log.isDebugEnabled())
log.debug("Operation [" + getOperation() + "] invalid credentials for: " + principal);
return DEFAULT_RESULT_CHALLENGE_NOK;
} else {
storeLdapException(e, prc);
throw new SenderException("Exception in operation [" + getOperation() + "] principal=[" + principal + "]", e);
}
} finally {
closeDirContext(dirContext);
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class LdapSender method performOperation.
/**
* Performs the specified operation and returns the results.
*
* @return - Depending on operation, DEFAULT_RESULT or read/search result (always XML)
*/
public String performOperation(String message, ParameterResolutionContext prc) throws SenderException, ParameterException {
Map paramValueMap = null;
String entryName = null;
if (paramList != null && prc != null) {
paramValueMap = prc.getValueMap(paramList);
entryName = (String) paramValueMap.get("entryName");
if (log.isDebugEnabled())
log.debug("entryName=[" + entryName + "]");
}
if ((entryName == null || StringUtils.isEmpty(entryName)) && !getOperation().equals(OPERATION_CHALLENGE)) {
throw new SenderException("entryName must be defined through params, operation [" + getOperation() + "]");
}
if (getOperation().equals(OPERATION_READ)) {
return performOperationRead(entryName, prc, paramValueMap);
} else if (getOperation().equals(OPERATION_UPDATE)) {
return performOperationUpdate(entryName, prc, paramValueMap, parseAttributesFromMessage(message));
} else if (getOperation().equals(OPERATION_CREATE)) {
return performOperationCreate(entryName, prc, paramValueMap, parseAttributesFromMessage(message));
} else if (getOperation().equals(OPERATION_DELETE)) {
return performOperationDelete(entryName, prc, paramValueMap, parseAttributesFromMessage(message));
} else if (getOperation().equals(OPERATION_SEARCH)) {
return performOperationSearch(entryName, prc, paramValueMap, (String) paramValueMap.get(FILTER), SearchControls.ONELEVEL_SCOPE);
} else if (getOperation().equals(OPERATION_DEEP_SEARCH)) {
return performOperationSearch(entryName, prc, paramValueMap, (String) paramValueMap.get(FILTER), SearchControls.SUBTREE_SCOPE);
} else if (getOperation().equals(OPERATION_SUB_CONTEXTS)) {
return performOperationGetSubContexts(entryName, prc, paramValueMap);
} else if (getOperation().equals(OPERATION_GET_TREE)) {
return performOperationGetTree(entryName, prc, paramValueMap);
} else if (getOperation().equals(OPERATION_CHALLENGE)) {
return performOperationChallenge((String) paramValueMap.get("principal"), prc, paramValueMap);
} else if (getOperation().equals(OPERATION_CHANGE_UNICODE_PWD)) {
return performOperationChangeUnicodePwd(entryName, prc, paramValueMap);
} else {
throw new SenderException("unknown operation [" + getOperation() + "]");
}
}
Aggregations