Difference between revisions of "ClientContextLibrary"

From Gcube Wiki
Jump to: navigation, search
(Created page with "__TOC__ =Client Context for gCube Portlets = Starting from '''gCube 4.2''', a new mechanism to retrieve the current user identifier and the current context identifier at clie...")
 
(Case 2 - HTTP GET or POST made with GWT RequestBuilder)
 
(32 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__TOC__
 
__TOC__
=Client Context for gCube Portlets =
+
=Client Context library for gCube Portlets =
  
Starting from '''gCube 4.2''', a new mechanism to retrieve the current user identifier and the current context identifier at client side has been introduced for gCube's portlets. The ''Client Context library'' allows to easily retrieve these information by relying on the ''Liferay.ThemeDisplay'' javascript object <ref>[https://www.liferay.com/it Liferay] is the portal technology adopted in the D4Science Infrastructure which is built upon the gCube software</ref>, and inject them at server side (they are transparently sent along the header of the remote requests). At server side, you should use the PortalManager library<ref>More information about the PortalManager are reported [https://wiki.gcube-system.org/gcube/Portal_Manager here]</ref>, to retrieve and convert them automatically to the current user's username and gCube context.
+
Starting from '''gCube 4.2''', the current Context identifier resides client side. If your Portlet does use '''AJAX calls''' (i.e. XMLHttpRequest to exchange data with a server behind the scenes) '''you don't need this component'''. Everything is already set up for you by the gCube Portal/Gateway in which your application is running.
  
= The Client Context Widget =
+
If you use standard http GET or POST to exchange data with the server instead, and you need to pass to the server the current Infrastructure scope identifier (the Liferay groupId).
  
The Client Context has been developed as a GWT Widget. Its source code is available in the gCube SVN repository, at this [http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/client-context-library url]. Basically, it offers two self-explanatory methods
+
== Getting the Client Context in Javascript ==
 
+
<source lang="xml">
<source lang="java">
+
<script>
public static native String getCurrentContextId();
+
var groupId = Liferay.ThemeDisplay.getScopeGroupId();
 +
</script>
 
</source>
 
</source>
  
and
+
Read more about Liferay Javascript Object: [1]
  
<source lang="java">
+
== Getting the Client Context in GWT (Widget) ==
public static native String getCurrentUserId();
+
</source>
+
  
This code is translated as native Javascript code, starting from pure Java code by the GWT Framework. Please note that both methods actually returns a number value.
+
The Client Context has been wrapped as a GWT Widget. Its source code is available in the gCube SVN repository, at this [http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/client-context-library url].  
  
 
To use the widget, you need to declare the following maven dependency in your project's pom.xml
 
To use the widget, you need to declare the following maven dependency in your project's pom.xml
Line 38: Line 37:
 
</source>
 
</source>
  
Finally, in the ''onModuleLoad()'' method of your gwt web application, you must declare the following code line
+
then you just use the GCubeClientContext.getCurrentContextId() method:
  
 
<source lang="java">
 
<source lang="java">
public class ... implements EntryPoint {
+
public static native String getCurrentContextId();
+
</source>
  public void onModuleLoad() {
+
 
 
+
Either if you pass this identifier explicitly as parameter, or embed it in the header of your call, in the server you can use the following method to get the infrastructure scope (note: scopeGroupId parameter is the value returned from the GCubeClientContext.getCurrentContextId() method described previously:
    /**
+
 
      * Inject client context to automatically pass the context id and the user id to the server side  
+
<source lang="java">
      */
+
import org.gcube.common.portal;
      GCubeClientContext.injectContext();
+
...
     
+
PortalContext pContext = PortalContext.getConfiguration();
      ...
+
 
  }  
+
/**
  }
+
*  
 +
* @param scopeGroupId the liferay groupid number (as String) of the VRE/VO
 +
* @return the scope (context)
 +
*/
 +
String currentScope = pContext.getCurrentScope(String scopeGroupId);
 +
 
 +
</source>
 +
 
 +
==Examples for passing the Client Context via Standard HTTP POST or GET ==
 +
 
 +
It can happen that in your web applications you have to use standard HTTP POST or GET for your servlet. This is true, for instance, when you have to '''upload a file from the browser'''.
 +
 
 +
Other cases may involve the usage of the GWT '''RequestBuilder''' object (com.google.gwt.http.client.RequestBuilder) for standard HTTP Calls.
 +
 
 +
For both cases we here suggests to possible solution, in order to pass the ClientContext to your servlets.
 +
 
 +
=== Case 1 - HTTP POST requests ===
 +
 
 +
A possible approach is to use one (HTML) Hidden field in the form and to read this hidden field value in the doPost servlet's method.
 +
 
 +
The following example is written in GWT.
 +
 
 +
<source lang="java">
 +
//client side code
 +
...
 +
VerticalPanel panel = new VerticalPanel();
 +
formPanel.setWidget(panel); //com.google.gwt.user.client.ui.FormPanel
 +
fileUpload.setName(...);  //com.google.gwt.user.client.ui.FileUpload
 +
 
 +
 
 +
// Add hidden parameters
 +
String currentContextId = GCubeClientContext.getCurrentContextId();
 +
 
 +
//$YourAttributeName is any attribute name you like, it has to be the same when you'll read it in the servlet's doPost
 +
panel.add(new Hidden($YourAttributeName, currentContextId)); <-- here is where you put the clientContextId,
 +
 
 +
...
 +
</source>
 +
 
 +
---
 +
 
 +
<source lang="java">
 +
//server side code
 +
...
 +
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 +
...
 +
/**
 +
* An iterator to instances of <code>FileItemStream</code>
 +
* parsed from the request, in the order that they were
 +
*transmitted.
 +
*/
 +
FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request);
 +
String clientContextId = "";
 +
//GET FILE STREAM
 +
while (fileItemIterator.hasNext()) {
 +
FileItemStream item = fileItemIterator.next();
 +
        ...
 +
        if (item.isFormField() && $YourAttributeName.equals(item.getFieldName())){  <-- here is where you read the clientContextId in the server  $YourAttributeName must match the hidden value above
 +
        clientContextId = Streams.asString(item.openStream());
 +
}
 +
        ...
 +
 
 +
}
 +
String currentScope= PortalContext.getConfiguration().getCurrentScope(clientContextId)); <-- here  where you get the currentScope from the clientContextId
 +
...
 +
}
 +
</source>
 +
 
 +
 
 +
=== Case 2 - HTTP GET or POST made with GWT RequestBuilder ===
 +
 
 +
This case is even easier than the Case 1,  ClientContext injection for GWT RequestBuilder is already supported, nothing to do client side (we report the client snippet just for clarification). Server side code is the same as in PortalContext
 +
 
 +
<source lang="java">
 +
 
 +
import com.google.gwt.http.client.*;
 +
 
 +
//client side code
 +
String url = "http://www.myserver.com/getData?type=3";
 +
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
 +
try {
 +
  Request request = builder.sendRequest(null, new RequestCallback() {
 +
....
 +
...
 +
</source>
 +
 
 +
 
 +
<source lang="java">
 +
//server side code
 +
 
 +
import org.gcube.common.portal;
  
 +
public void doGet(HttpServletRequest request, HttpServletResponse response) {
 +
...
 +
PortalContext pContext = PortalContext.getConfiguration();
 +
String currentScope = pContext.getCurrentScope(request);
 +
...
 
</source>
 
</source>
  
== Portlet Example ==
+
== Related links and references ==
  
A portlet example's source code is available [here url] It uses the both the Client Context library and the PortalManager one, together with the UserManagementLibrary, which abstracts Liferay's concepts and map them to gCube's ones.  
+
[1] Liferay Javascript Object usage: https://web.liferay.com/web/pankaj.kathiriya/blog/-/blogs/usage-of-liferay-js-object
  
 +
[2] gCube Portal Manager API: https://wiki.gcube-system.org/gcube/Portal_Context
  
=== Tips for Development Environments ===
+
[3] gCube UsersManagement Library https://wiki.gcube-system.org/gcube/UserManagement_Core#UserManager_APIs

Latest revision as of 16:44, 25 November 2016

Client Context library for gCube Portlets

Starting from gCube 4.2, the current Context identifier resides client side. If your Portlet does use AJAX calls (i.e. XMLHttpRequest to exchange data with a server behind the scenes) you don't need this component. Everything is already set up for you by the gCube Portal/Gateway in which your application is running.

If you use standard http GET or POST to exchange data with the server instead, and you need to pass to the server the current Infrastructure scope identifier (the Liferay groupId).

Getting the Client Context in Javascript

<script>
var groupId = Liferay.ThemeDisplay.getScopeGroupId();
</script>

Read more about Liferay Javascript Object: [1]

Getting the Client Context in GWT (Widget)

The Client Context has been wrapped as a GWT Widget. Its source code is available in the gCube SVN repository, at this url.

To use the widget, you need to declare the following maven dependency in your project's pom.xml

<dependency>
        <groupId>org.gcube.portal</groupId>
	<artifactId>client-context-library</artifactId>
	<version>[1.0.0-SNAPSHOT,)</version>
	<scope>compile</scope>
</dependency>

as well as the following line in the gwt.xml file of your gwt-portlet:

<!--Inherit the GCubeClientContext widget code -->
<inherits name='org.gcube.portal.clientcontext.GCubeClientContext' />

then you just use the GCubeClientContext.getCurrentContextId() method:

public static native String getCurrentContextId();

Either if you pass this identifier explicitly as parameter, or embed it in the header of your call, in the server you can use the following method to get the infrastructure scope (note: scopeGroupId parameter is the value returned from the GCubeClientContext.getCurrentContextId() method described previously:

import org.gcube.common.portal;
...
PortalContext pContext = PortalContext.getConfiguration();
 
/**
 * 
 * @param scopeGroupId the liferay groupid number (as String) of the VRE/VO
 * @return the scope (context)
 */
String currentScope = pContext.getCurrentScope(String scopeGroupId);

Examples for passing the Client Context via Standard HTTP POST or GET

It can happen that in your web applications you have to use standard HTTP POST or GET for your servlet. This is true, for instance, when you have to upload a file from the browser.

Other cases may involve the usage of the GWT RequestBuilder object (com.google.gwt.http.client.RequestBuilder) for standard HTTP Calls.

For both cases we here suggests to possible solution, in order to pass the ClientContext to your servlets.

Case 1 - HTTP POST requests

A possible approach is to use one (HTML) Hidden field in the form and to read this hidden field value in the doPost servlet's method.

The following example is written in GWT.

//client side code
...
VerticalPanel panel = new VerticalPanel();
formPanel.setWidget(panel); //com.google.gwt.user.client.ui.FormPanel
fileUpload.setName(...);  //com.google.gwt.user.client.ui.FileUpload
 
 
// Add hidden parameters
String currentContextId = GCubeClientContext.getCurrentContextId();
 
//$YourAttributeName is any attribute name you like, it has to be the same when you'll read it in the servlet's doPost
panel.add(new Hidden($YourAttributeName, currentContextId)); <-- here is where you put the clientContextId, 
 
...

---

//server side code
...
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
/**
 * An iterator to instances of <code>FileItemStream</code>
 * parsed from the request, in the order that they were
 *transmitted.
 */
FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request);
String clientContextId = "";
//GET FILE STREAM
while (fileItemIterator.hasNext()) {
	FileItemStream item = fileItemIterator.next();
        ...
        if (item.isFormField() && $YourAttributeName.equals(item.getFieldName())){  <-- here is where you read the clientContextId in the server  $YourAttributeName must match the hidden value above
	         clientContextId = Streams.asString(item.openStream());
	}
        ...
 
}
String currentScope= PortalContext.getConfiguration().getCurrentScope(clientContextId));  <-- here  where you get the currentScope from the clientContextId
...
}


Case 2 - HTTP GET or POST made with GWT RequestBuilder

This case is even easier than the Case 1, ClientContext injection for GWT RequestBuilder is already supported, nothing to do client side (we report the client snippet just for clarification). Server side code is the same as in PortalContext

import com.google.gwt.http.client.*;
 
//client side code
String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
try {
  Request request = builder.sendRequest(null, new RequestCallback() {
....
...


//server side code
 
import org.gcube.common.portal;
 
public void doGet(HttpServletRequest request, HttpServletResponse response) {
...
PortalContext pContext = PortalContext.getConfiguration();
String currentScope = pContext.getCurrentScope(request);
...

Related links and references

[1] Liferay Javascript Object usage: https://web.liferay.com/web/pankaj.kathiriya/blog/-/blogs/usage-of-liferay-js-object

[2] gCube Portal Manager API: https://wiki.gcube-system.org/gcube/Portal_Context

[3] gCube UsersManagement Library https://wiki.gcube-system.org/gcube/UserManagement_Core#UserManager_APIs