package org.xins.server.frontend;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.Utils;
import org.xins.common.collections.InvalidPropertyValueException;
import org.xins.common.collections.MissingRequiredPropertyException;
import org.xins.common.manageable.BootstrapException;
import org.xins.common.manageable.Manageable;
import org.xins.common.spec.FunctionSpec;
import org.xins.common.text.TextUtils;
import org.xins.server.API;
import org.xins.server.Log;
public class SessionManager extends Manageable {
private API _api;
private ThreadLocal _currentSession = new ThreadLocal();
private ArrayList _unrestrictedPages = new ArrayList();
private String _defaultCommand;
public SessionManager(API api) {
_api = api;
}
protected void bootstrapImpl(Map<String, String> bootstrapProperties)
throws MissingRequiredPropertyException,
InvalidPropertyValueException,
BootstrapException {
_defaultCommand = bootstrapProperties.get("xinsff.default.command");
if (_defaultCommand == null) {
_defaultCommand = "DefaultCommand";
}
String loginPage = bootstrapProperties.get("xinsff.login.page");
if (loginPage != null) {
_unrestrictedPages.add(loginPage);
_unrestrictedPages.add("Control");
_unrestrictedPages.add("Logout");
String unrestrictedPages = bootstrapProperties.get("xinsff.unrestricted.pages");
if (unrestrictedPages != null && !unrestrictedPages.equals("")) {
StringTokenizer stUnrestricted = new StringTokenizer(unrestrictedPages, ",", false);
while (stUnrestricted.hasMoreTokens()) {
String nextPage = stUnrestricted.nextToken();
_unrestrictedPages.add(nextPage);
}
}
} else {
_unrestrictedPages.add("*");
}
}
protected void request(HttpServletRequest request) {
String sessionId = null;
Cookie[] cookies = request.getCookies();
int cookieCount = (cookies == null) ? 0 : cookies.length;
for (int i = 0; i < cookieCount && sessionId == null; i++) {
Cookie cookie = cookies[i];
String name = cookie.getName();
if ("SessionID".equals(name)) {
sessionId = cookie.getValue();
}
}
HttpSession session = request.getSession(true);
_currentSession.set(session);
if (sessionId == null || sessionId.equals("") || sessionId.equals("null")) {
sessionId = session.getId();
setProperty(sessionId, Boolean.FALSE);
}
Map inputParameters = new LinkedHashMap();
Enumeration params = request.getParameterNames();
while (params.hasMoreElements()) {
String name = (String) params.nextElement();
String value = request.getParameter(name);
if ("".equals(value) || name.equals(getSessionId())) {
value = null;
}
inputParameters.put(name, value);
}
setProperty("_inputs", inputParameters);
setProperty("_remoteIP", request.getRemoteAddr());
setProperty("_propertiesSet", new HashSet());
setProperty("_userAgent", request.getHeader("User-Agent"));
}
protected void result(boolean successful) {
if (successful) {
Map inputParameters = (Map) getProperty("_inputs");
Set propertiesSet = (Set) getProperty("_propertiesSet");
if (propertiesSet.contains("*")) {
return;
}
String command = (String) inputParameters.get("command");
String action = (String) inputParameters.get("action");
String functionName = command;
if (action != null && !action.equals("") && !action.equalsIgnoreCase("show")) {
functionName += TextUtils.firstCharUpper(action);
}
try {
Map specInputParameters = _api.getAPISpecification().getFunction(functionName).getInputParameters();
Map clonedInputParameters = new LinkedHashMap();
clonedInputParameters.putAll(inputParameters);
Iterator itInputParameters = clonedInputParameters.entrySet().iterator();
while (itInputParameters.hasNext()) {
Map.Entry nextInput = (Map.Entry) itInputParameters.next();
String parameterName = (String) nextInput.getKey();
parameterName = getRealParameter(parameterName, functionName);
if (specInputParameters.containsKey(parameterName) && !propertiesSet.contains(parameterName)
&& !propertiesSet.contains(parameterName.toLowerCase())) {
String value = (String) nextInput.getValue();
if ("".equals(value) || parameterName.equals(getSessionId())) {
value = null;
}
setProperty(parameterName.toLowerCase(), value);
}
}
} catch (Exception ex) {
Utils.logIgnoredException(ex);
}
}
}
public boolean shouldLogIn() {
Map inputParameters = (Map) getProperty("_inputs");
String command = (String) inputParameters.get("command");
if (command == null || command.equals("")) {
command = _defaultCommand;
}
if (_unrestrictedPages.contains("*") ||
_unrestrictedPages.contains(command) ||
(command != null && command.startsWith("_"))) {
return false;
}
boolean shouldLogIn = !getBoolProperty(getSessionId());
return shouldLogIn;
}
public String getSessionId() {
HttpSession session = (HttpSession) _currentSession.get();
if (session == null) {
return null;
}
String sessionId = session.getId();
return sessionId;
}
public Map getProperties() {
HttpSession session = (HttpSession) _currentSession.get();
if (session == null) {
return new LinkedHashMap();
}
Map properties = new LinkedHashMap();
Enumeration enuAttributes = session.getAttributeNames();
while (enuAttributes.hasMoreElements()) {
String nextAttribute = (String) enuAttributes.nextElement();
Object value = session.getAttribute(nextAttribute);
properties.put(nextAttribute, value);
}
return properties;
}
public void setProperty(String name, Object value) throws IllegalArgumentException {
MandatoryArgumentChecker.check("name", name);
HttpSession session = (HttpSession) _currentSession.get();
if (session != null) {
if (value == null) {
removeProperty(name);
} else {
try {
session.setAttribute(name, value);
} catch (Throwable t) {
Utils.logProgrammingError(t);
}
}
}
if (!name.startsWith("_")) {
registerProperty(name);
}
}
public void setProperty(String name, boolean value) throws IllegalArgumentException {
MandatoryArgumentChecker.check("name", name);
setProperty(name, value ? Boolean.TRUE : Boolean.FALSE);
}
public Object getProperty(String name) throws IllegalArgumentException {
MandatoryArgumentChecker.check("name", name);
HttpSession session = (HttpSession) _currentSession.get();
if (session == null) {
return null;
}
Object propertyValue = session.getAttribute(name);
return propertyValue;
}
public boolean getBoolProperty(String name) throws IllegalArgumentException {
MandatoryArgumentChecker.check("name", name);
HttpSession session = (HttpSession) _currentSession.get();
if (session == null) {
return false;
}
Object value = session.getAttribute(name);
boolean isTrue = "true".equals(value) || Boolean.TRUE.equals(value);
return isTrue;
}
public void removeProperty(String name) throws IllegalArgumentException {
MandatoryArgumentChecker.check("name", name);
HttpSession session = (HttpSession) _currentSession.get();
if (session != null) {
session.removeAttribute(name);
Map inputParameters = (Map) session.getAttribute("_inputs");
if (inputParameters != null) {
inputParameters.remove(name);
}
registerProperty(name);
}
}
public void removeProperties() {
HttpSession session = (HttpSession) _currentSession.get();
if (session != null) {
ArrayList attributeNames = new ArrayList();
Enumeration enuAttributes = session.getAttributeNames();
while (enuAttributes.hasMoreElements()) {
String nextAttribute = (String) enuAttributes.nextElement();
if (!nextAttribute.startsWith("_")) {
attributeNames.add(nextAttribute);
}
}
Iterator itAttributes = attributeNames.iterator();
while (itAttributes.hasNext()) {
String nextAttribute = (String) itAttributes.next();
session.removeAttribute(nextAttribute);
}
registerProperty("*");
}
}
private void registerProperty(String name) {
Set propertiesSet = (Set) getProperty("_propertiesSet");
if (propertiesSet != null) {
propertiesSet.add(name);
} else {
propertiesSet = new HashSet();
propertiesSet.add(name);
setProperty("_propertiesSet", propertiesSet);
}
}
private String getRealParameter(String receivedParameter, String functionName) {
String flatParameter = receivedParameter;
if (receivedParameter.indexOf("_") != -1) {
flatParameter = TextUtils.removeCharacter('_', receivedParameter);
}
try {
FunctionSpec function = _api.getAPISpecification().getFunction(functionName);
Set parametersSet = function.getInputParameters().keySet();
if (parametersSet.contains(receivedParameter)) {
return receivedParameter;
}
Iterator itParameters = parametersSet.iterator();
while (itParameters.hasNext()) {
String nextParameterName = (String) itParameters.next();
if (nextParameterName.equalsIgnoreCase(flatParameter)) {
return nextParameterName;
}
}
} catch (Exception ex) {
Log.log_3705(ex.getMessage());
}
return receivedParameter;
}
}