Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
GTNPORTAL-2928 Added validation at UI level for length of properties …
Browse files Browse the repository at this point in the history
…in user profile. Added method PicketlinkIDMOrganizationServiceImpl.recoverFromIDMError() to handle rollback and restart transaction if DB error occured
  • Loading branch information
mposolda committed May 15, 2013
1 parent 19abebb commit e98c4c7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
import org.gatein.common.transaction.JTAUserTransactionLifecycleService;
import org.picketlink.idm.api.Transaction;

/**
* Abstract superclass for other DAO classes
Expand Down Expand Up @@ -63,6 +64,8 @@ public void handleException(String messageToLog, Exception e) {
} catch (Exception tre) {
log.warn("Unable to set Transaction status to be rollback only", tre);
}
} else {
orgService.recoverFromIDMError();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
import org.gatein.common.transaction.JTAUserTransactionLifecycleService;
import org.picketlink.idm.api.Transaction;
import org.picocontainer.Startable;

/*
Expand Down Expand Up @@ -160,8 +161,13 @@ public void flush() {
idmService_.getIdentitySession().save();
}
} else {
if (idmService_.getIdentitySession().getTransaction().isActive()) {
idmService_.getIdentitySession().save();
try {
if (idmService_.getIdentitySession().getTransaction().isActive()) {
idmService_.getIdentitySession().save();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
recoverFromIDMError();
}
}

Expand All @@ -178,15 +184,35 @@ public void endRequest(ExoContainer container) {
}
jtaTransactionLifecycleService.finishJTATransaction();
} else {
if (idmService_.getIdentitySession().getTransaction().isActive()) {
idmService_.getIdentitySession().getTransaction().commit();
try {
if (idmService_.getIdentitySession().getTransaction().isActive()) {
idmService_.getIdentitySession().getTransaction().commit();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
recoverFromIDMError();
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}

// Should be used only for non-JTA environment
public void recoverFromIDMError() {
try {
// We need to restart Hibernate transaction if it's available. First rollback old one and then start new one
Transaction idmTransaction = idmService_.getIdentitySession().getTransaction();
if (idmTransaction.isActive()) {
idmTransaction.rollback();
idmTransaction.start();
log.info("IDM error recovery finished. Old transaction has been rolled-back and new transaction has been started");
}
} catch (Exception e1) {
log.warn("Error during recovery of old error", e1);
}
}

public Config getConfiguration() {
return configuration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.exoplatform.webui.form.UIFormInputSet;
import org.exoplatform.webui.form.UIFormSelectBox;
import org.exoplatform.webui.form.UIFormStringInput;
import org.exoplatform.webui.form.validator.StringLengthValidator;
import org.gatein.security.oauth.exception.OAuthException;
import org.gatein.security.oauth.exception.OAuthExceptionCode;
import org.gatein.security.oauth.common.OAuthConstants;
Expand All @@ -69,6 +70,9 @@ public class UIUserProfileInputSet extends UIFormInputSet {

public static final String FEMALE = "female";

public static final int DEFAULT_MAX_LENGTH = 255;
private int maxLength = -1;

public UIUserProfileInputSet() {
}

Expand Down Expand Up @@ -108,6 +112,7 @@ public void reset() {
}

private void addInput(UIFormInputSet set, String[] keys) {
int maxLength = getMaxLengthOfTextField();

for (String key : keys) {
if (key.equalsIgnoreCase("user.gender")) {
Expand All @@ -124,7 +129,11 @@ private void addInput(UIFormInputSet set, String[] keys) {
continue;
}

set.addUIFormInput(new UIFormStringInput(key, null, null));
try {
set.addUIFormInput(new UIFormStringInput(key, null, null).addValidator(StringLengthValidator.class, 0, maxLength));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Expand Down Expand Up @@ -317,4 +326,12 @@ private Object[] convertOAuthExceptionAttributes(WebuiRequestContext context, St

return new Object[] { localizedOAuthProviderUsernameAttrName, oauthProviderUsername };
}

private int getMaxLengthOfTextField() {
if (maxLength == -1) {
String property = System.getProperty("gatein.validators.profile.maxlength");
maxLength = property!=null ? Integer.parseInt(property) : DEFAULT_MAX_LENGTH;
}
return maxLength;
}
}

0 comments on commit e98c4c7

Please sign in to comment.