📄 coreutil.java
字号:
}
return msgs;
}
return null;
}
/**
* @param request
*/
public static ActionMessages checkForVPNErrors(Action action, HttpServletRequest request) {
/*
* The VPN client may supply error and message request parameters after
* a redirect. This allows thoses messages to be displaed
*/
if (request.getParameter("vpnError") != null) {
String[] val = request.getParameterValues("vpnError");
ActionMessages msgs = new ActionMessages();
for (int i = 0; i < val.length; i++) {
msgs.add(Globals.ERROR_KEY, new BundleActionMessage("tunnels", "vpnClient.error.text", val[i]));
}
return msgs;
}
return null;
}
/**
* Add a new global message that will be displayed to all users that have
* the specified permissions (type {@link GlobalWarning#USERS_WITH_PERMISSIONS}.
*
* @param requiredResourceType required resource type
* @param requiredPermissions required permissions
* @param message message
*/
public static void addGlobalWarning(ResourceType requiredResourceType, Permission[] requiredPermissions,
BundleActionMessage message) {
HttpSession servletSession;
for (Iterator i = CoreRequestProcessor.getSessions().entrySet().iterator(); i.hasNext();) {
servletSession = (HttpSession) ((Map.Entry) i.next()).getValue();
SessionInfo info = CoreServlet.getServlet().getLogonController().getSessionInfo(servletSession);
if(info != null) {
try {
if(CoreServlet.getServlet().getPolicyDatabase().isResourcePermissionAllowed(requiredResourceType, requiredPermissions, info.getUser(), false)) {
addSingleSessionGlobalWarning(servletSession, message);
}
}
catch(Exception e) {
log.error("Failed to add global warning. ", e);
}
}
}
List servletContextGlobalWarnings = (List) CoreServlet.getServlet().getServletContext().getAttribute(
Constants.SESSION_GLOBAL_WARNINGS);
if (servletContextGlobalWarnings == null) {
servletContextGlobalWarnings = new ArrayList();
}
servletContextGlobalWarnings.add(new GlobalWarning(requiredResourceType, requiredPermissions, message));
CoreServlet.getServlet().getServletContext().setAttribute(Constants.SESSION_GLOBAL_WARNINGS, servletContextGlobalWarnings);
}
/**
* Add a new global message for types {@link GlobalWarning#SUPER_USER}, {@link GlobalWarning#MANAGEMENT_USERS}
* or {@link GlobalWarning#ALL_USERS}.
*
* @param type global warning type
* @param message message
* @throws IllegalArgumentException if incorrect type
* @see GlobalWarning
*/
public static void addMultipleGlobalWarning(int type, BundleActionMessage message) {
HttpSession servletSession = null;
for (Iterator i = CoreRequestProcessor.getSessions().entrySet().iterator(); i.hasNext();) {
servletSession = (HttpSession) ((Map.Entry) i.next()).getValue();
SessionInfo info = CoreServlet.getServlet().getLogonController().getSessionInfo(servletSession);
if(info != null) {
try {
if( ( type == GlobalWarning.SUPER_USER && CoreServlet.getServlet().getLogonController().isAdministrator(info.getUser()) ) ||
(type == GlobalWarning.MANAGEMENT_USERS && CoreServlet.getServlet().getPolicyDatabase().isAnyResourcePermissionAllowed(info.getUser(),
true, true, false) ) ) {
addSingleSessionGlobalWarning(servletSession, message);
}
} catch (Exception e) {
log.error("Failed to add global warning.", e);
}
}
}
List servletContextGlabalWarnings = (List) CoreServlet.getServlet().getServletContext().getAttribute(
Constants.SESSION_GLOBAL_WARNINGS);
if (servletContextGlabalWarnings == null) {
servletContextGlabalWarnings = new ArrayList();
}
servletContextGlabalWarnings.add(new GlobalWarning(type, message));
CoreServlet.getServlet().getServletContext().setAttribute(Constants.SESSION_GLOBAL_WARNINGS,
servletContextGlabalWarnings);
}
/**
* Add a new global message for type {@link GlobalWarning#SINGLE_SESSION}.
*
* @param servletSession session to add warning to
* @param message message
* @throws IllegalArgumentException if incorrect type
* @see GlobalWarning
*/
public static void addSingleSessionGlobalWarning(HttpSession servletSession, BundleActionMessage message) {
synchronized (servletSession) {
SessionInfo info = CoreServlet.getServlet().getLogonController().getSessionInfo(servletSession);
if(info != null) {
List l = (List) servletSession.getAttribute(Constants.GLOBAL_WARNINGS);
if (l == null) {
l = new ArrayList();
servletSession.setAttribute(Constants.GLOBAL_WARNINGS, l);
}
BundleActionMessage m = null;
boolean found = false;
for (Iterator i = l.iterator(); !found && i.hasNext();) {
m = (BundleActionMessage) i.next();
if (m.getBundle().equals(message.getBundle()) && m.getKey().equals(message.getKey())) {
found = true;
}
}
if (!found) {
l.add(message);
}
}
}
}
/**
* Remove a global warning givens its message resource key. If a
* <code>null</code> servlet session is provided then the global warning
* will be removed from all sessions
*
* @param servletSession session to remove warning from or <code>null</code>
* for all sessions
* @param key message resource key
*/
public static void removeGlobalWarning(HttpSession servletSession, String key) {
if (servletSession == null) {
for (Iterator i = CoreRequestProcessor.getSessions().entrySet().iterator(); i.hasNext();) {
servletSession = (HttpSession) ((Map.Entry) i.next()).getValue();
removeGlobalWarning(servletSession, key);
}
List servletContextGlabalWarnings = (List) CoreServlet.getServlet().getServletContext().getAttribute(
Constants.SESSION_GLOBAL_WARNINGS);
if (servletContextGlabalWarnings != null) {
Iterator iter = servletContextGlabalWarnings.iterator();
while (iter.hasNext()) {
GlobalWarning gw = (GlobalWarning) iter.next();
BundleActionMessage element = gw.getMessage();
if (element.getKey().equals(key)) {
servletContextGlabalWarnings.remove(element);
break;
}
}
}
} else {
synchronized (servletSession) {
List l = (List) servletSession.getAttribute(Constants.GLOBAL_WARNINGS);
if (l == null) {
l = new ArrayList();
servletSession.setAttribute(Constants.GLOBAL_WARNINGS, l);
}
for (int i = l.size() - 1; i >= 0; i--) {
BundleActionMessage msg = (BundleActionMessage) l.get(i);
if (msg.getKey().equals(key)) {
l.remove(i);
break;
}
}
}
}
}
/**
* Convenience method to Remove all global warnings with the specified key
* from all active sessions
*
* @param key message resources key of global warning
*/
public static void removeGlobalWarningFromAllSessions(String key) {
removeGlobalWarning(null, key);
}
/**
* Initialise a
*
* @link com.sslexplorer.core.forms.CoreForm
*
* @param form
*/
public static void initCoreForm(CoreForm f, HttpServletRequest request) {
f.setReferer(CoreUtil.getReferer(request));
}
/**
* @param session
* @param licenseFile
* @param callback
*/
public static void requestLicenseAgreement(HttpSession session, LicenseAgreement agreement) {
List l = (List) session.getAttribute(Constants.LICENSE_AGREEMENTS);
if (l == null) {
l = new ArrayList();
session.setAttribute(Constants.LICENSE_AGREEMENTS, l);
addPageInterceptListener(session, new PageInterceptListener() {
public String getId() {
return "licenseAgreement";
}
public boolean isRedirect() {
return true;
}
public ActionForward checkForForward(Action action, ActionMapping mapping, HttpServletRequest request,
HttpServletResponse response) throws PageInterceptException {
if (!(action instanceof LicenseAgreementDispatchAction)) {
return new ActionForward("/showLicenseAgreement.do", true);
}
return null;
}
});
}
l.add(agreement);
}
public static void addWarnings(HttpServletRequest request, ActionMessages warnings) {
if (warnings == null) {
return;
}
ActionMessages requestWarnings = (ActionMessages) request.getAttribute(Constants.REQ_ATTR_WARNINGS);
if (requestWarnings == null) {
requestWarnings = new ActionMessages();
}
requestWarnings.add(warnings);
if (requestWarnings.isEmpty()) {
request.removeAttribute(Constants.REQ_ATTR_WARNINGS);
return;
}
request.setAttribute(Constants.REQ_ATTR_WARNINGS, requestWarnings);
}
/**
* Get the current warnings, creating them if none exists
*
* @return the warnings that already exist in the request, or a new
* ActionMessages object if empty.
* @param request The servlet request we are processing
*/
public static ActionMessages getWarnings(HttpServletRequest request) {
ActionMessages warnings = (ActionMessages) request.getAttribute(Constants.REQ_ATTR_WARNINGS);
if (warnings == null) {
warnings = new ActionMessages();
}
return warnings;
}
/**
* Save the specified warnings messages.
*
* @param request request
* @param warnings warnings
*/
public static void saveWarnings(HttpServletRequest request, ActionMessages warnings) {
if ((warnings == null) || warnings.isEmpty()) {
request.removeAttribute(Constants.REQ_ATTR_WARNINGS);
return;
}
request.setAttribute(Constants.REQ_ATTR_WARNINGS, warnings);
}
public static void addVPNClientErrors(HttpServletRequest request, ActionMessages errors) {
if (errors == null) {
return;
}
ActionMessages requestErrors = (ActionMessages) request.getAttribute(Constants.REQ_ATTR_VPN_CLIENT_ERRORS);
if (requestErrors == null) {
requestErrors = new ActionMessages();
}
requestErrors.add(errors);
if (requestErrors.isEmpty()) {
request.removeAttribute(Constants.REQ_ATTR_VPN_CLIENT_ERRORS);
return;
}
request.setAttribute(Constants.REQ_ATTR_VPN_CLIENT_ERRORS, requestErrors);
}
public static void addVPNClientMessages(HttpServletRequest request, ActionMessages messages) {
if (messages == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -