Getting server information

From OpenCms Wiki
Revision as of 11:26, 10 December 2015 by Paul (Talk | contribs)
Jump to: navigation, search

You can get a variety of information about your server from OpenCms, however sometimes it is a more round-about route.

Contents

Get the Server IP address

To get the IP of the client use the following code. If you use the request object, you most likely will get the proxy IP.

CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
String ip = cms.getRequestContext().getRemoteAddress();

Get the hostname

This one is pretty typical of any JSP.

String hostname = request.getServerName();

Get the URI of the requested resource

This will get the URI that was requested, relative to the current site:

CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
String requestFileUri = cms.getRequestContext().getUri(); // Will return e.g.: "/en/myfolder/mypage.html"

Similarly, you can get the folder URI of the requested resource:

String requestFolderUri = cms.getRequestContext().getFolderUri();

Alternatively, you can use the CmsObject, which also offers the getRequestContext() method:

CmsObject cmso = cms.getCmsObject();
String requestFileUri = cmso.getRequestContext().getUri();
String requestFolderUri = cmso.getRequestContext().getFolderUri();

Get the locale of the requested resource

This will return the locale of the requested resource:

CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
Locale locale = cms.getRequestContext().getLocale();

Get the "warped" time

If you need to consider situations where the user has activated "time warp" (in the workplace preferences), you can read the current "warped" time from the session:

/**
 * Gets the workplace timestamp as a Date instance.
 * 
 * If time warp is active, the returned datetime will be the "warped" time. 
 * Otherwise, the actual "now" is returned.
 *
 * @param session The relevant session instance.
 * @return Date The current workplace "now" - either the actual "now" or a time-warped "now".
 */
public static Date getOpenCmsCurrentTime(HttpSession session) {
    long userCurrentTime = new Date().getTime();
    try {
        CmsWorkplaceSettings wpSettings = (CmsWorkplaceSettings)session.getAttribute(CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS);
        userCurrentTime = wpSettings.getUserSettings().getTimeWarp(); // Note: will return 0 if time warp is not active
        if (userCurrentTime <= 0)
            userCurrentTime = new Date().getTime();
    } catch (Throwable t) {}
 
    return new Date(userCurrentTime);
}

... or from the current user's "additional info":

/**
 * Gets the workplace timestamp as a Date instance.
 * 
 * If time warp is active, the returned datetime will be the "warped" time. 
 * Otherwise, the actual "now" is returned.
 *
 * @param cmso An initialized CmsObject.
 * @return Date The current workplace "now" - either the actual "now" or a time-warped "now".
 */
public static Date getOpenCmsCurrentTime(CmsObject cmso) {
    long userCurrentTime = new Date().getTime();
    Object timeWarpObj = cmso.getRequestContext().getCurrentUser().getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_TIMEWARP);
    try {
        userCurrentTime = (Long)timeWarpObj;
    } catch (ClassCastException e) {
        try {
            userCurrentTime = Long.parseLong((String)timeWarpObj);
            if (userCurrentTime <= 0) {
                userCurrentTime = new Date().getTime();
            }
        } catch (Throwable t) {}
    } catch (Throwable t) {}
 
    return new Date(userCurrentTime);
}

Author's note: The latter method, getOpenCmsCurrentTime(CmsObject), is currently untested, but it is based on code from the OpenCms core.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox