package pt.estgp.estgweb.web.filters;
import org.apache.log4j.Logger;
import pt.estgp.estgweb.domain.UserRoleConfig;
import pt.estgp.estgweb.web.utils.RequestUtils;
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
/**
*
* @author Jorge Machado
*/
public class UserRoleProxy
implements 1.5.0/docs/api/java/util/logging/Filter.html">Filter
{
private static 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger =
1.5.0/docs/api/java/util/logging/Logger.html">Logger.
getLogger(UserRoleProxy.
class);
static Map
<String,UserRoleConfig
> userRoleConfigs =
new HashMap
<String,UserRoleConfig
>();
public static List
<UserRoleConfig
> getUserRoleConfigs
()
{
List
<UserRoleConfig
> roles =
new ArrayList
<UserRoleConfig
>(userRoleConfigs.
values());
1.5.0/docs/api/java/util/Collections.html">Collections.
sort(roles,
new Comparator
<UserRoleConfig
>() {
@
1.5.0/docs/api/java/lang/Override.html">Override
public int compare
(UserRoleConfig userRoleConfig, UserRoleConfig userRoleConfig2
) {
return userRoleConfig.
getValue().
compareTo(userRoleConfig2.
getValue());
}
});
return roles
;
}
/**
* Olny return valid roles for choose by user in application
* @return a list of strings keys
*/
public static List
<String
> getUserRoles
()
{
List
<UserRoleConfig
> roles =
new ArrayList
<UserRoleConfig
>(userRoleConfigs.
values());
1.5.0/docs/api/java/util/Collections.html">Collections.
sort(roles,
new Comparator
<UserRoleConfig
>() {
@
1.5.0/docs/api/java/lang/Override.html">Override
public int compare
(UserRoleConfig userRoleConfig, UserRoleConfig userRoleConfig2
) {
return userRoleConfig.
getValue().
compareTo(userRoleConfig2.
getValue());
}
});
List
<String
> rolesStr =
new ArrayList
<String
>();
for(UserRoleConfig r: roles
)
if(r.
isValid())
rolesStr.
add(r.
getRole());
return rolesStr
;
}
public UserRoleProxy
()
{
}
public static void setDirty
()
{
try
{
IServiceManager sm = ServiceManager.
getInstance();
5+0%2Fdocs%2Fapi+Object">Object[] args =
new 5+0%2Fdocs%2Fapi+Object">Object[]{};
List
<UserRoleConfig
> userRoleConfigsList =
(List
<UserRoleConfig
>) sm.
execute("system",
"LoadRoleTranslationsIfDirty", args
);
if(userRoleConfigsList
!=
null && userRoleConfigsList.
size() > 0
)
{
userRoleConfigs.
clear();
for(UserRoleConfig c: userRoleConfigsList
)
{
userRoleConfigs.
put(c.
getRole(),c
);
}
}
}
catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e
)
{
logger.
fatal("INIT FILTER SESSION FILTER CALLING FIRST SERVICE TO OPEN CONNECTIONS IN HIBERNATE" + e,e
);
}
}
public void init
(FilterConfig config
)
{
setDirty
();
}
/**
*
* @param request The servlet request we are processing
* @param chain The filter chain we are processing
*
* @exception java.io.IOException if an input/output error occurs
* @exception javax.servlet.ServletException if a servlet error occurs
*/
public void doFilter
(ServletRequest request, ServletResponse response, FilterChain chain
)
throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException
{
try
{
IServiceManager sm = ServiceManager.
getInstance();
5+0%2Fdocs%2Fapi+Object">Object[] args =
new 5+0%2Fdocs%2Fapi+Object">Object[]{};
List
<UserRoleConfig
> userRoleConfigsNow =
(List
<UserRoleConfig
>) sm.
execute(RequestUtils.
getRequester((HttpServletRequest
)request,
(HttpServletResponse
)response
),
"LoadRoleTranslationsIfDirty", args
);
if(userRoleConfigsNow
!=
null)
{
for(UserRoleConfig c: userRoleConfigsNow
)
{
userRoleConfigs.
put(c.
getRole(),c
);
}
}
}
catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e
)
{
logger.
fatal("INIT FILTER USER ROLE FILTER CALLING FIRST SERVICE TO OPEN CONNECTIONS IN HIBERNATE" + e,e
);
}
chain.
doFilter(request,response
);
}
public static 1.5.0/docs/api/java/lang/String.html">String getMessage
(1.5.0/docs/api/java/lang/String.html">String role,
1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
UserRoleConfig c = userRoleConfigs.
get(role
);
if(c ==
null)
{
logger.
error("ATENTION: " + role +
" role does not exist, please create");
return role
;
}
if(locale ==
null)
return c.
getValue();
1.5.0/docs/api/java/lang/String.html">String msg =
null;
if(locale.
getLanguage().
equals("pt"))
msg = c.
getValuePt();
else if(locale.
getLanguage().
equals("en"))
msg = c.
getValueEn();
else if(locale.
getLanguage().
equals("es"))
msg = c.
getValueEs();
else if(locale.
getLanguage().
equals("fr"))
msg = c.
getValueFr();
if(msg ==
null || msg.
trim().
length() == 0
)
msg = c.
getValue();
if(msg ==
null || msg.
trim().
length() == 0
)
return role
;
return msg
;
}
public void destroy
()
{
// Nothing
}
}