Created
April 17, 2014 07:49
-
-
Save DemkaAge/10962102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.brbpm.lecm.utils.server.filenet; | |
import com.filenet.api.constants.ConfigurationParameter; | |
import com.filenet.api.core.Connection; | |
import com.filenet.api.core.Factory; | |
import com.filenet.api.util.ConfigurationParameters; | |
import com.filenet.api.util.UserContext; | |
import com.google.common.base.Preconditions; | |
import ru.brbpm.lecm.shared.constants.ConnectionConstants; | |
import ru.brbpm.lecm.utils.server.LoggerUtils; | |
import javax.security.auth.Subject; | |
import java.util.Properties; | |
/** | |
* User: dshahovkin | |
* Date: 23.05.13 | |
* Time: 16:14 | |
* <p/> | |
* | |
* Только для ТЕСТОВ, УТИЛИТ и ПОДПИСОК. | |
*/ | |
public class CeConnection { | |
private static final LoggerUtils.MyLogger logger = LoggerUtils.getLogger(CeConnection.class); | |
private Properties connectionProperties = new Properties(); | |
private Connection connection; | |
private boolean userSubjectPushed; | |
private boolean adminSubjectPushed; | |
public CeConnection(Properties properties) { | |
connectionProperties = properties; | |
// | |
final String connectionTimeout = connectionProperties.getProperty(ConnectionConstants.CE_CONNECTION_TIMEOUT); | |
final String ceURI = Preconditions.checkNotNull(connectionProperties.getProperty(ConnectionConstants.CE_URI), | |
"Свойство %s не задано.", ConnectionConstants.CE_URI); | |
// | |
ConfigurationParameters configParams = new ConfigurationParameters(); | |
Integer intConnectionTimeout = connectionTimeout == null || connectionTimeout.isEmpty() ? null : Integer | |
.valueOf(connectionTimeout); | |
configParams.setParameter(ConfigurationParameter.WSI_TRANSPORT_CONNECTION_TIMEOUT, intConnectionTimeout); | |
connection = Factory.Connection.getConnection(ceURI, configParams); | |
logger.debug("ceConnection = {0}", connection); | |
} | |
private void logIn() { | |
logger.trace("logIn:\n\tconnectionType = {0} \n\tce.uri= {1} \n\tce.login = {2} \n\tce.password = {3}", | |
connectionProperties.getProperty(ConnectionConstants.CE_AUTHORIZATION_TYPE, "не задано"), | |
connectionProperties.getProperty(ConnectionConstants.CE_URI, "не задано"), | |
connectionProperties.getProperty(ConnectionConstants.CE_LOGIN, "не задано"), | |
connectionProperties.getProperty(ConnectionConstants.CE_PASSWORD, "не задано")); | |
if (userSubjectPushed) { | |
throw new IllegalStateException("Авторизация уже проведена. Повторная авторизация недопустима."); | |
} | |
// | |
// transparent / manual | |
String type = Preconditions.checkNotNull(connectionProperties.getProperty(ConnectionConstants | |
.CE_AUTHORIZATION_TYPE), "Свойство %s не задано. Ожидалось transparent/login.", | |
ConnectionConstants.CE_AUTHORIZATION_TYPE); | |
if (type.equalsIgnoreCase("transparent")) { | |
userSubjectPushed = false; | |
// | |
} else if (type.equalsIgnoreCase("login")) { | |
String user = Preconditions.checkNotNull(connectionProperties.getProperty(ConnectionConstants.CE_LOGIN), | |
"Свойство %s не задано.", ConnectionConstants.CE_LOGIN); | |
String password = Preconditions.checkNotNull(connectionProperties.getProperty(ConnectionConstants | |
.CE_PASSWORD), "Свойство %s не задано.", ConnectionConstants.CE_LOGIN); | |
String jaasStanzaName = connectionProperties.getProperty(ConnectionConstants.CE_JAAS_STANZA); | |
if (jaasStanzaName == null || jaasStanzaName.isEmpty()) { | |
jaasStanzaName = null; | |
} | |
// | |
UserContext uc = UserContext.get(); | |
Subject subject = UserContext.createSubject(connection, user, password, jaasStanzaName); | |
uc.pushSubject(subject); | |
userSubjectPushed = true; | |
} else { | |
throw new IllegalAccessError("Свойство " + ConnectionConstants.CE_AUTHORIZATION_TYPE + " не задано. " + | |
"Ожидалось transparent/login."); | |
} | |
} | |
private void logOff() { | |
logger.trace("logOff {0}", this); | |
if (userSubjectPushed) { | |
UserContext userContext = UserContext.get(); | |
userContext.popSubject(); | |
userSubjectPushed = false; | |
} | |
} | |
private void logInAdmin() { | |
logger.trace("logInAdmin:\n\tconnectionType = {0} \n\tce.uri= {1} \n\tce.login = {2} \n\tce.password = {3}", | |
connectionProperties.getProperty(ConnectionConstants.CE_AUTHORIZATION_TYPE, "не задано"), | |
connectionProperties.getProperty(ConnectionConstants.CE_URI, "не задано"), | |
connectionProperties.getProperty(ConnectionConstants.CE_ADMIN_LOGIN, "не задано"), | |
connectionProperties.getProperty(ConnectionConstants.CE_ADMIN_PASSWORD, "не задано")); | |
String admin = Preconditions.checkNotNull(connectionProperties.getProperty(ConnectionConstants | |
.CE_ADMIN_LOGIN), "Свойство %s не задано.", ConnectionConstants.CE_ADMIN_LOGIN); | |
String password = Preconditions.checkNotNull(connectionProperties.getProperty(ConnectionConstants | |
.CE_ADMIN_PASSWORD), "Свойство %s не задано.", ConnectionConstants.CE_ADMIN_PASSWORD); | |
String jaasStanzaName = connectionProperties.getProperty(ConnectionConstants.CE_JAAS_STANZA); | |
if (jaasStanzaName == null || jaasStanzaName.isEmpty()) { | |
jaasStanzaName = null; | |
} | |
if (adminSubjectPushed) { | |
throw new IllegalStateException("Авторизация администратором уже проведена. Повторная авторизация " + | |
"недопустима."); | |
} | |
// | |
UserContext uc = UserContext.get(); | |
Subject subject = UserContext.createSubject(connection, admin, password, jaasStanzaName); | |
uc.pushSubject(subject); | |
adminSubjectPushed = true; | |
} | |
private void logOffAdmin() { | |
logger.trace("logOffAdmin {0}", this); | |
if (adminSubjectPushed) { | |
UserContext userContext = UserContext.get(); | |
userContext.popSubject(); | |
adminSubjectPushed = false; | |
} | |
} | |
public Connection getFnConnection() { | |
return connection; | |
} | |
/** | |
* Действие, выполняемое от имени пользователя. | |
* </p> | |
* <pre> | |
* {@code String userName = ceConnection.new UserAction<String>() { | |
* @Override public String action() { | |
* User user = Factory.User.fetchCurrent(ceConnection.getFnConnection(), null); | |
* return user.getName(); | |
* } | |
* }.exec(); | |
* } | |
* </pre> | |
* | |
* @param <T> | |
*/ | |
abstract public class UserAction<T> { | |
public abstract T action(); | |
/** | |
* Выполнить | |
* | |
* @return результат {@link #action()} | |
*/ | |
public T exec() { | |
try { | |
CeConnection.this.logIn(); | |
return action(); | |
} finally { | |
CeConnection.this.logOff(); | |
} | |
} | |
} | |
/** | |
* Действие, выполняемое от имени администратора. Может быть вложено в действие пользователя. | |
* </p> | |
* <pre> | |
* {@code String userName = ceConnection.new AdminUserAction<String>() { | |
* @Override public String action() { | |
* User user = Factory.User.fetchCurrent(ceConnection.getFnConnection(), null); | |
* return user.getName(); | |
* } | |
* }.exec(); | |
* } | |
* </pre> | |
* | |
* @param <T> | |
*/ | |
abstract public class AdminUserAction<T> { | |
public abstract T action(); | |
/** | |
* Выполнить | |
* | |
* @return результат {@link #action()} | |
*/ | |
public T exec() { | |
try { | |
CeConnection.this.logInAdmin(); | |
return action(); | |
} finally { | |
CeConnection.this.logOffAdmin(); | |
} | |
} | |
} | |
abstract public class ExceptionThrowableAction<T> { | |
public abstract T action() throws Exception; | |
/** | |
* Выполнить | |
* | |
* @return результат {@link #action()} | |
*/ | |
public T exec() throws Exception { | |
try { | |
CeConnection.this.logInAdmin(); | |
return action(); | |
} finally { | |
CeConnection.this.logOffAdmin(); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment