Getting server information

From OpenCms Wiki
Revision as of 17:19, 10 November 2014 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 get the current "warped" time like this:

CmsWorkplaceSettings wpSettings = (CmsWorkplaceSettings)session.getAttribute(CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS);
long timeWarpMillis = wpSettings.getUserSettings().getTimeWarp(); // Note: will return 0 if time warp is not active
Date warpedDate = new Date(timeWarpMillis);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox