Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent inserting affiliations and subscriptions to db if allready exists #2043

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,14 @@ private void process( final NodeOperation operation ) {
break;

case CREATE_AFFILIATION:
delegate.createAffiliation( operation.node, operation.affiliate );
if (!operation.affiliate.isSavedToDB())
{
delegate.createAffiliation( operation.node, operation.affiliate );
}
else
{
delegate.updateAffiliation( operation.node, operation.affiliate );
}
break;

case UPDATE_AFFILIATION:
Expand All @@ -408,7 +415,14 @@ private void process( final NodeOperation operation ) {
break;

case CREATE_SUBSCRIPTION:
delegate.createSubscription( operation.node, operation.subscription );
if (!operation.subscription.isSavedToDB())
{
delegate.createSubscription( operation.node, operation.subscription );
}
else
{
delegate.updateSubscription( operation.node, operation.subscription );
}
break;

case UPDATE_SUBSCRIPTION:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ private void loadAffiliations(Map<Node.UniqueIdentifier, Node> nodes, ResultSet
}
NodeAffiliate affiliate = new NodeAffiliate(node, new JID(rs.getString(2)));
affiliate.setAffiliation(NodeAffiliate.Affiliation.valueOf(rs.getString(3)));
affiliate.setSavedToDB(true);
node.addAffiliate(affiliate);
}
catch (SQLException sqle) {
Expand Down Expand Up @@ -996,12 +997,14 @@ public void createAffiliation(Node node, NodeAffiliate affiliate)
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();

pstmt = con.prepareStatement(ADD_AFFILIATION);
pstmt.setString(1, node.getUniqueIdentifier().getServiceIdentifier().getServiceId());
pstmt.setString(2, encodeNodeID(node.getNodeID()));
pstmt.setString(3, affiliate.getJID().toString());
pstmt.setString(4, affiliate.getAffiliation().name());
pstmt.executeUpdate();
affiliate.setSavedToDB(true);
}
catch (SQLException sqle) {
log.error("An exception occurred while creating an affiliation ({}) to a node ({}) in the database.", affiliate, node.getUniqueIdentifier(), sqle);
Expand Down Expand Up @@ -1046,6 +1049,7 @@ public void removeAffiliation(Node node, NodeAffiliate affiliate)
pstmt.setString(2, encodeNodeID(node.getNodeID()));
pstmt.setString(3, affiliate.getJID().toString());
pstmt.executeUpdate();
affiliate.setSavedToDB(false);
}
catch (SQLException sqle) {
log.error("An exception occurred while removing an affiliation ({}) to a node ({}) in the database.", affiliate, node.getUniqueIdentifier(), sqle);
Expand Down Expand Up @@ -1163,6 +1167,7 @@ public void removeSubscription(NodeSubscription subscription)
pstmt.setString(2, encodeNodeID(subscription.getNode().getNodeID()));
pstmt.setString(3, subscription.getID());
pstmt.executeUpdate();
subscription.setSavedToDB(false);
}
catch (SQLException sqle) {
log.error("An exception occurred while removing a subscription ({}) in the database.", subscription, sqle);
Expand All @@ -1176,7 +1181,7 @@ public void removeSubscription(NodeSubscription subscription)
public void savePublishedItem(PublishedItem item) {
// When an item with the given itemId exists, it must be overwritten (says the XEP)
final boolean create = getPublishedItem( item.getNode(), item.getUniqueIdentifier() ) == null;
if ( create ) {
if ( create && !item.isSavedToDB()) {
createPublishedItem( item );
} else {
updatePublishedItem( item );
Expand All @@ -1199,6 +1204,7 @@ public void createPublishedItem(PublishedItem item)
pstmt.setString(5, StringUtils.dateToMillis( item.getCreationDate()));
pstmt.setString(6, item.getPayloadXML());
pstmt.execute();
item.setSavedToDB(true);
} catch (SQLException ex) {
log.error("Published item could not be created in database: {}\n{}", item.getUniqueIdentifier(), item.getPayloadXML(), ex);
} finally {
Expand Down Expand Up @@ -1253,10 +1259,15 @@ public void savePublishedItems(Connection con, List<PublishedItem> addList, bool
pstmt.addBatch();
} else {
pstmt.execute();
item.setSavedToDB(true);
}
}
if (hasBatchItems) {
pstmt.executeBatch();
for ( final PublishedItem item : addList)
{
item.setSavedToDB(true);
}
}
} finally {
DbConnectionManager.closeStatement(pstmt);
Expand All @@ -1274,6 +1285,7 @@ public void removePublishedItem(PublishedItem item) {
pstmt.setString(2, encodeNodeID(item.getNode().getNodeID()));
pstmt.setString(3, item.getID());
pstmt.execute();
item.setSavedToDB(false);
} catch (SQLException ex) {
log.error("Failed to delete published item from DB: {}", item.getUniqueIdentifier(), ex);
} finally {
Expand Down Expand Up @@ -1302,10 +1314,15 @@ protected void removePublishedItems(Connection con, List<PublishedItem> delList,
pstmt.addBatch();
} else {
pstmt.execute();
item.setSavedToDB(false);
}
}
if (hasBatchItems) {
pstmt.executeBatch();
for ( final PublishedItem item : delList)
{
item.setSavedToDB(false);
}
}
} finally {
DbConnectionManager.closeStatement(pstmt);
Expand Down Expand Up @@ -1628,6 +1645,8 @@ else if (maxPublished != -1)
results.add(item);
else
results.addFirst(item);

item.setSavedToDB(true);
counter++;
}
}
Expand Down Expand Up @@ -1668,6 +1687,7 @@ public PublishedItem getLastPublishedItem(LeafNode node) {
if (rs.getString(4) != null) {
item.setPayloadXML(rs.getString(4));
}
item.setSavedToDB(true);
}
}
catch (Exception sqle) {
Expand Down Expand Up @@ -1704,6 +1724,7 @@ public PublishedItem getPublishedItem(LeafNode node, PublishedItem.UniqueIdentif
if (rs.getString(3) != null) {
result.setPayloadXML(rs.getString(3));
}
result.setSavedToDB(true);
log.debug("Loaded item from DB");
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1811,11 +1811,25 @@ public void saveToDB() {
setSavedToDB(true);
// Save the existing node affiliates to the DB
for (NodeAffiliate affiliate : affiliates) {
persistenceProvider.createAffiliation(this, affiliate);
if (!affiliate.isSavedToDB())
{
persistenceProvider.createAffiliation(this, affiliate);
}
else
{
persistenceProvider.updateAffiliation(this, affiliate);
}
}
// Add new subscriptions to the database
for (NodeSubscription subscription : subscriptionsByID.values()) {
persistenceProvider.createSubscription(this, subscription);
if (!subscription.isSavedToDB())
{
persistenceProvider.createSubscription(this, subscription);
}
else
{
persistenceProvider.updateSubscription(this, subscription);
}
}
// Add the new node to the list of available nodes
getService().addNode(this);
Expand Down Expand Up @@ -2162,7 +2176,14 @@ else if (authorizationRequired && !isAdmin(owner)) {

if (savedToDB) {
// Add the new subscription to the database
XMPPServer.getInstance().getPubSubModule().getPersistenceProvider().createSubscription(this, subscription);
if (!subscription.isSavedToDB())
{
XMPPServer.getInstance().getPubSubModule().getPersistenceProvider().createSubscription(this, subscription);
}
else
{
XMPPServer.getInstance().getPubSubModule().getPersistenceProvider().updateSubscription(this, subscription);
}
}

if (originalIQ != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ public void setAffiliation(Affiliation affiliation) {
this.affiliation = affiliation;
}

/**
* Indicates if the affiliation is present in the database.
*/
private boolean savedToDB = false;

public void setSavedToDB(boolean savedToDB) {
this.savedToDB = savedToDB;
}

public boolean isSavedToDB() {
return this.savedToDB;
}

/**
* Returns the list of subscriptions of the affiliate in the node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ void setSavedToDB(boolean savedToDB) {
this.savedToDB = savedToDB;
}

public boolean isSavedToDB() {
return this.savedToDB;
}

/**
* Configures the subscription based on the sent {@link DataForm} included in the IQ
* packet sent by the subscriber. If the subscription was pending of configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public class PublishedItem implements Serializable {
* XML representation of the payload (for serialization)
*/
private String payloadXML;


private boolean savedToDB = false;

/**
* Creates a published item
* @param node The node the published item is created in
Expand Down Expand Up @@ -287,6 +289,14 @@ public static UniqueIdentifier getUniqueIdentifier(String serviceId, String node
return new UniqueIdentifier( serviceId, nodeId, itemId );
}

void setSavedToDB(boolean savedToDB) {
this.savedToDB = savedToDB;
}

public boolean isSavedToDB() {
return this.savedToDB;
}

/**
* A unique identifier for an item, in context of all nodes of all services in the system.
*
Expand Down