Getting server information

(Difference between revisions)
Jump to: navigation, search
(Get the Server IP address)
Line 12: Line 12:
 
  String hostname = request.getServerName();
 
  String hostname = request.getServerName();
  
==Get the URI==
+
==Get the URI of the requested resource==
There may be other ways, but this is what I got to work.
+
This will get the URI that was requested, relative to the current site:
  CmsJspActionElement element = new CmsJspActionElement(pageContext, request, response);
+
  CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
  CmsObject obj = element.getCmsObject();
+
String requestFileUri = cms.getRequestContext().getUri(); // Will return e.g.: "/en/myfolder/mypage.html"
  String uri = obj.getRequestContext().getUri();
+
Similarly, you can get the folder URI of the requested resource:
 +
String requestFolderUri = cms.getRequestContext().getFolderUri();
 +
Alternatively, you can use the <tt>CmsObject</tt>, which also offers the <tt>getRequestContext()</tt> 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);

Revision as of 18:19, 10 November 2014

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