Getting server information

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
(Get the "warped" time: Added second method. Rewrote exisiting code into a method.)
m (Added source lang="java" (formatting). Minor text fixes.)
 
Line 2: Line 2:
  
 
==Get the Server IP address==
 
==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.
+
To get the IP of the client use the following code. If you use the <tt>request</tt> object, you most likely will get the proxy IP.
  
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
+
<source lang="java">
String ip = cms.getRequestContext().getRemoteAddress();
+
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
 +
String ip = cms.getRequestContext().getRemoteAddress();
 +
</source>
  
 
==Get the hostname==
 
==Get the hostname==
 
This one is pretty typical of any JSP.
 
This one is pretty typical of any JSP.
  
String hostname = request.getServerName();
+
<source lang="java">
 +
String hostname = request.getServerName();
 +
</source>
  
 
==Get the URI of the requested resource==
 
==Get the URI of the requested resource==
 
This will get the URI that was requested, relative to the current site:
 
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"
+
<source lang="java">
 +
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
 +
String requestFileUri = cms.getRequestContext().getUri(); // Will return e.g.: "/en/myfolder/mypage.html"
 +
</source>
 
Similarly, you can get the folder URI of the requested resource:
 
Similarly, you can get the folder URI of the requested resource:
String requestFolderUri = cms.getRequestContext().getFolderUri();
+
<source lang="java">
Alternatively, you can use the <tt>CmsObject</tt>, which also offers the <tt>getRequestContext()</tt> method:
+
String requestFolderUri = cms.getRequestContext().getFolderUri();
CmsObject cmso = cms.getCmsObject();
+
</source>
String requestFileUri = cmso.getRequestContext().getUri();
+
Alternatively, you can use an <tt>CmsObject</tt> instance, which also offers the <tt>getRequestContext()</tt> method:
String requestFolderUri = cmso.getRequestContext().getFolderUri();
+
<source lang="java">
 +
CmsObject cmso = cms.getCmsObject();
 +
String requestFileUri = cmso.getRequestContext().getUri();
 +
String requestFolderUri = cmso.getRequestContext().getFolderUri();
 +
</source>
  
 
==Get the locale of the requested resource==
 
==Get the locale of the requested resource==
 
This will return the locale of the requested resource:
 
This will return the locale of the requested resource:
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
+
<source lang="java">
Locale locale = cms.getRequestContext().getLocale();
+
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
 +
Locale locale = cms.getRequestContext().getLocale();
 +
</source>
  
 
==Get the "warped" time==
 
==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:
 
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:
 
 
<source lang="java">
 
<source lang="java">
 
/**
 
/**

Latest revision as of 11:34, 10 December 2015

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 an CmsObject instance, 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