📄 coreutil.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.taglib.tiles.ComponentConstants;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ModuleUtils;
import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.KeyStoreManager;
import com.sslexplorer.boot.ReplacementEngine;
import com.sslexplorer.boot.Replacer;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.actions.CoreAction;
import com.sslexplorer.core.actions.LicenseAgreementDispatchAction;
import com.sslexplorer.core.forms.CoreForm;
import com.sslexplorer.extensions.ExtensionBundle;
import com.sslexplorer.extensions.ExtensionDescriptor;
import com.sslexplorer.policyframework.Permission;
import com.sslexplorer.policyframework.ResourceType;
import com.sslexplorer.properties.PropertyChangeEvent;
import com.sslexplorer.properties.PropertyProfile;
import com.sslexplorer.security.AuthenticationScheme;
import com.sslexplorer.security.Constants;
import com.sslexplorer.security.Role;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.security.User;
import com.sslexplorer.security.UserAttributeDefinition;
import com.sslexplorer.security.UserDatabase;
import com.sslexplorer.vfs.UploadDetails;
import com.sslexplorer.vfs.UploadManager;
import com.sslexplorer.vfs.webdav.DAVUtilities;
/**
* Useful utility method used throught the core webapplication.
*
* @author Brett Smith <brett@3sp.com>
*/
public class CoreUtil {
final static Log log = LogFactory.getLog(CoreUtil.class);
/**
* Get the ID of the sessions current property profile
*
* @param session session
* @return property profile ID
*/
public static int getCurrentPropertyProfileId(HttpSession session) {
PropertyProfile p = (PropertyProfile) session.getAttribute(Constants.SELECTED_PROFILE);
if (p != null) {
return p.getResourceId();
}
return 0;
}
/**
* Get the path to the theme the user has selected or default if there is
* none
*/
public static String getThemePath(HttpSession session) {
try {
SessionInfo info = CoreServlet.getServlet().getLogonController().getSessionInfo(session);
return getProperty("ui.theme", info.getUser(), getCurrentPropertyProfileId(session));
} catch (Exception e) {
}
return "/theme/default";
}
/**
* Get network place properties.
*/
public static String getNetworkPlaceProfileAttribute(HttpSession session, String property) {
try {
SessionInfo info = CoreServlet.getServlet().getLogonController().getSessionInfo(session);
return getProperty(property, info.getUser(), CoreUtil.getCurrentPropertyProfileId(session));
} catch (Exception e) {
}
return null;
}
/**
* Get if tool tips are enabled for the specified session
*/
public static boolean getToolTipsEnabled(HttpSession session) {
try {
SessionInfo info = CoreServlet.getServlet().getLogonController().getSessionInfo(session);
return "true".equals(getProperty("ui.toolTips", info.getUser(), CoreUtil.getCurrentPropertyProfileId(session)));
} catch (Exception e) {
}
return true;
}
/**
* Add a page intercept listener to the provided session. This will be
* invoked on every page request until it is removed. The listener will then
* have the oppurtunity to return a forward redirecting the user if some
* condition is satisified.
*
* @param servletSession session to add listener to
* @param listener listener to add
*/
public static void addPageInterceptListener(HttpSession servletSession, PageInterceptListener listener) {
synchronized (servletSession) {
List l = (List) servletSession.getAttribute(Constants.PAGE_INTERCEPT_LISTENERS);
if (l == null) {
l = new ArrayList();
servletSession.setAttribute(Constants.PAGE_INTERCEPT_LISTENERS, l);
}
l.add(listener);
}
}
/**
* Remove a page intercept listener from the provided session. This listener
* will no longer be messaged upon every required
*
* @param servletSession session to remove listener from
* @param listener listener to remove
*/
public static void removePageInterceptListener(HttpSession servletSession, PageInterceptListener listener) {
synchronized (servletSession) {
List l = (List) servletSession.getAttribute(Constants.PAGE_INTERCEPT_LISTENERS);
if (l != null) {
l.remove(listener);
if (l.size() == 0) {
servletSession.removeAttribute(Constants.PAGE_INTERCEPT_LISTENERS);
}
PageInterceptListener pil = (PageInterceptListener) servletSession.getAttribute(Constants.PAGE_INTERCEPTED);
if (pil == listener) {
servletSession.removeAttribute(Constants.PAGE_INTERCEPTED);
}
}
}
}
/**
* Get a page intercept listener given its id. <code>null</code> will be
* returned if no listener with the given id exists.
*
* @param servletSession session that contains the listener
* @param id listener id
* @return listener
*/
public static PageInterceptListener getPageInterceptListenerById(HttpSession servletSession, String id) {
synchronized (servletSession) {
List l = (List) servletSession.getAttribute(Constants.PAGE_INTERCEPT_LISTENERS);
if (l != null) {
for (Iterator i = l.iterator(); i.hasNext();) {
PageInterceptListener listener = (PageInterceptListener) i.next();
if (listener.getId().equals(id)) {
return listener;
}
}
}
}
return null;
}
/**
* Convenience method to remove a page intercept listener from the provided
* session given its ID. This listener will no longer be messaged upon every
* required
*
* @param servletSession session to remove listener from
* @param listener listener to remove
*/
public static void removePageInterceptListener(HttpSession session, String id) {
PageInterceptListener l = getPageInterceptListenerById(session, id);
if (l != null) {
removePageInterceptListener(session, l);
}
}
/**
* Check if there are page intercepts for the current action. If there are a
* forward will be returned pointing to which page should be displayed next
*
* @param action action object
* @param mapping action mapping
* @param request request object
* @param response response object
* @return forward
* @throws Exception on any error
*/
public static ActionForward checkIntercept(Action action, ActionMapping mapping, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ActionForward fwd = null;
try {
List l = (List) request.getSession().getAttribute(Constants.PAGE_INTERCEPT_LISTENERS);
if (l != null) {
PageInterceptListener currentIntercept = (PageInterceptListener) request.getSession().getAttribute(
Constants.PAGE_INTERCEPTED);
PageInterceptListener pil = null;
if (currentIntercept != null) {
pil = currentIntercept;
fwd = currentIntercept.checkForForward(action, mapping, request, response);
} else {
for (Iterator i = l.iterator(); fwd == null && i.hasNext();) {
pil = (PageInterceptListener) i.next();
fwd = pil.checkForForward(action, mapping, request, response);
}
}
if (fwd != null) {
if (!pil.isRedirect()) {
request.getSession().setAttribute(Constants.PAGE_INTERCEPTED, pil);
} else {
CoreUtil.removePageInterceptListener(request.getSession(), pil);
}
return fwd;
}
}
return null;
} catch (Exception e) {
log.error("Page intercept failed.", e);
throw e;
}
}
/**
* Check if there any vpn message parameters in the request. The VPN client
* will sometimes send back a status message for displaying to the user and
* this is the mechanism used. Every action invokes this method
*
* @param action action processing the requre
* @param request the request object
*/
public static ActionMessages checkForVPNMessages(Action action, HttpServletRequest request) {
/*
* The VPN client may supply error and message request parameters after
* a redirect. This allows thoses messages to be displaed
*/
ActionMessages msgs = new ActionMessages();
if (request.getParameter("vpnMessage") != null) {
String[] val = request.getParameterValues("vpnMessage");
for (int i = 0; i < val.length; i++) {
msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage("tunnels", "vpnClient.message.text", val[i]));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -