Showing posts with label ADF. Show all posts
Showing posts with label ADF. Show all posts

Sunday, 22 February 2015

Clicking on a button using Javascript in adf until all the adf components are processed



About:

In this blog i will be explaining on how to execute a hidden button virtually in ADF until all the page components are loaded

Description:

Consider a scenario where you want to perform some operation on a component on page load by click on the button, sometimes that component may not be loaded when the button executes, this may cause errors.

This can be handled by using the adf javascript api's to verify whether the page is fully synchronized before the button is called virtually

Java Script Code

   function invokeLoadEvnt() {
                  if (window.AdfPage && window.AdfPage.PAGE &&   window.AdfPage.PAGE.isSynchronizedWithServer()) {
                      alert('I am Synchronized');
                      var button = AdfPage.PAGE.findComponentByAbsoluteId('b1');
                      AdfActionEvent.queue(button,true);
                  }
                  else {

                      alert('Retry Once again');
                      window.setTimeout(invokeLoadEvnt, 10000);
                  }
              }

In the jspx page under the af:document call the client listener

<af:clientListener type="load" method="invokeLoadEvnt"/>

Tuesday, 6 May 2014

File Upload using Rest Data Control in ADF


About:

In my previous post, I explained on how to create a Restful service using which File Upload can be performed without using Multipart Form data. In this blog, i will be explaining on how to consume such a service in RestDataControl in ADF.

Pre-requisites:

1. Lets create a jersey rest service as per this blog and deploy it to the server
2. Now create a XSD file for the service as shown below

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified">
  <xsd:element name="fileData">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="fileInfo" type="xsd:base64Binary"/>
        <xsd:element name="fileName" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>


Description:

Create a Fusion Web Application and using the url of the rest service deployed above create a Rest Data Control from New -> Gallery -> WebService DataControl and select Type as REST.






Now create a jspx page and backing bean in the view controller. Then create pagedef for the page by right clicking on the jspx page and selecting Go to PageDef .  In the Page Definition add the methodAction from the DC



 Add the following piece of code in the Backing bean associated with the page.

/**
      *  Method to Upload the file and post it to the rest service
      */
public void fileUploaded(ValueChangeEvent event)
      {
        UploadedFile file = (UploadedFile) event.getNewValue();
        if (file != null)
        {
         FacesContext context = FacesContext.getCurrentInstance();
         FacesMessage message = new FacesMessage("Successfully uploaded file " + file.getFilename()+" (" + file.getLength()+" bytes)");
         context.addMessage(event.getComponent().getClientId(context), message);
         DCBindingContainer bc = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
         OperationBinding op = bc.getOperationBinding("posting");
             try {
                 byte[] buffer = readFully(file.getInputStream());
                 String encodedStr = new sun.misc.BASE64Encoder().encode(buffer);
                 if(op!=null){
                    Map tempMap = new HashMap();
                    
                    tempMap.put("fileInfo",encodedStr);
                    tempMap.put("fileName",file.getFilename());
                    op.getParamsMap().put("fileData", tempMap);
                    op.execute();
                 }
             } catch (IOException e) {
                 System.out.println(e);
             }
             
        }
      }
    /**
      *  Method to convert the InputStream to ByteArray
      */

    public byte[] readFully(InputStream input) throws IOException
      {
          byte[] buffer = new byte[8192];
          int bytesRead;
          ByteArrayOutputStream output = new ByteArrayOutputStream();
          while ((bytesRead = input.read(buffer)) != -1)
          {
              output.write(buffer, 0, bytesRead);
          }
          return output.toByteArray();
      }

In the Page create a InputFileComponent and bind the ValuechangeListener to the fileUploaded method in the backing bean. Now add Command button for Submit.

That's it we are ready to transfer the file, run the jspx page and select the file to be transferred using the browse button and click on submit.

Thursday, 24 April 2014

File Upload in Jersey Restful service


About:

There are many blogs which discuss on how to do file transfer in JAX-RS /Restful service all this blogs discuss using the Multipartformdata i.e. FormDataParam as the input Stream.
In this Blog, I will  be discussing on one of the way about how to create a service which will accept the File Object as input for the method which it consumes in the form XML.


Code: 

Bean Class
package model;
import java.io.InputStream;
import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class FileData {
    public FileData() {
        super();
    }
    
    String fileName;
    byte[] fileInfo;


    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileName() {
        return fileName;
    }


    public void setFileInfo(byte[] fileInfo) {
        this.fileInfo = fileInfo;
    }

    public byte[] getFileInfo() {
        return fileInfo;
    }

}

Service Class

package model;


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("model")
public class FileService {
    public FileService() {
        super();
    }


    @POST
    @Consumes("application/xml")
    public void uploadFile(FileData fd) {
        try {
            OutputStream os = new FileOutputStream("" + fd.getFileName());
            InputStream is = new ByteArrayInputStream(fd.getFileInfo());
             
            byte[] buffer = new byte[1024];
            int bytesRead;
            //read from is to buffer
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            is.close();
            //flush OutputStream to write any buffered data to file
            os.flush();
            os.close();


        } catch (Exception e) {
            System.out.println("Inside the Service"+e);
        }

    }

}


For Client :

I will explain in another blog on how to consume this in REST DataControl / URL DataControl 

Monday, 24 March 2014

Accessing a Twitter GET API in URLDC which is secured with OAuth 1.0



About :

In this Blog, I will be explaining about how to access Twitter GET Resource API from URLDC by passing the OAuth Access token. This can be used as model to the same for other Resource API secured with OAuth 1.0



OAuth Support in Twitter:

Twitter uses the OAuth authentication for the external applications to access its resources.  We need to create an API in Twitter which the external application will access and through which the resources will be shared by twitter to the external application. 


Twitter uses the HMAC-SHA1 Signature method to create the Authorization requests. So we will be needing the following HMACUtil java class to sign our OAuth Request calls
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.regex.Pattern;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class HMACSHAUtil {
    private static final String TIMESTAMP = "oauth_timestamp";
    private static final String SIGN_METHOD = "oauth_signature_method";
    private static final String SIGNATURE = "oauth_signature";
    private static final String CONSUMER_KEY = "oauth_consumer_key";
    private static final String VERSION = "oauth_version";
    private static final String NONCE = "oauth_nonce";
    private static final String TOKEN = "oauth_token";
    private static final String VERIFIER = "oauth_verifier";
    private static String CHARSET = "UTF-8";
    private static Timer timer = new Timer();
    private static final String EMPTY_STRING = "";
    private static final Map ENCODING_RULES;
    private static final String AMPERSAND_SEPARATED_STRING = "%s&%s&%s";
    private static final String HMAC_SHA1 = "HmacSHA1";
    private static final String CARRIAGE_RETURN = "\r\n";

    static {
        Map rules = new HashMap();
        rules.put("*", "%2A");
        rules.put("+", "%20");
        rules.put("%7E", "~");
        ENCODING_RULES = Collections.unmodifiableMap(rules);
    }

    public HMACSHAUtil() {
        super();
    }

 public   static String OAUTH1Header(String oauth_token, String consumer_key,  String httpMethod,
                                      String url, String consumerSecret, String oauth_token_secret,String verifier) throws Exception {
        Map oauthParameters = new TreeMap();
        oauthParameters.put(TOKEN, oauth_token);
        oauthParameters.put(TIMESTAMP, getTimestampInSeconds());
        oauthParameters.put(NONCE, getNonce());
        oauthParameters.put(CONSUMER_KEY, consumer_key);
        oauthParameters.put(SIGN_METHOD, "HMAC-SHA1");
        oauthParameters.put(VERSION, "1.0");/**/
        if(verifier!=null && !verifier.isEmpty())
        {
          oauthParameters.put(VERIFIER, verifier);
        }
        oauthParameters.put(VERSION, "1.0");


        String params = encode(asFormUrlEncodedString(oauthParameters));
        String baseString =
            String.format(AMPERSAND_SEPARATED_STRING, encode(httpMethod), encode(getSanitizedUrl(url)), params);
        String signature = doSign(baseString, encode(consumerSecret) + '&' + encode(oauth_token_secret));

        oauthParameters.put(SIGNATURE, signature);
        return generateOAuth1Header(oauthParameters);
    }

    private static String generateOAuth1Header(Map parameters) {
        StringBuffer header = new StringBuffer(parameters.size() * 20);
        header.append("OAuth ");
        for (String key : parameters.keySet()) {
            if (header.length() > "OAuth ".length()) {
                header.append(", ");
            }
            header.append(String.format("%s=\"%s\"", key, encode(parameters.get(key))));
        }
        return header.toString();
    }

    private static String doSign(String toSign, String keyString) throws Exception {
        SecretKeySpec key = new SecretKeySpec((keyString).getBytes(CHARSET), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(key);
        byte[] bytes = mac.doFinal(toSign.getBytes(CHARSET));
        return new String(Base64.encodeBase64(bytes)).replace(CARRIAGE_RETURN, EMPTY_STRING);
    }

    static class Timer {
        Long getMilis() {
            return System.currentTimeMillis();
        }


        Integer getRandomInteger() {
            return new Random().nextInt();
        }
    }

    private static String getNonce() {
        Long ts = getTs();
        return String.valueOf(ts + timer.getRandomInteger());
    }

    private static String getTimestampInSeconds() {
        return String.valueOf(getTs());
    }

    private static String getSanitizedUrl(String url) {
        return url.replaceAll("\\?.*", "").replace("\\:\\d{4}", "");
    }

    private static Long getTs() {
        return timer.getMilis() / 1000;
    }

    private static String asFormUrlEncodedString(Map oauthParameters) {
        if (oauthParameters.size() == 0)
            return EMPTY_STRING;

        StringBuilder builder = new StringBuilder();
        for (String p : oauthParameters.keySet()) {
            builder.append('&').append(encode(p).concat("=").concat(encode(oauthParameters.get(p))));
        }
        return builder.toString().substring(1);
    }

    private static String applyRule(String encoded, String toReplace, String replacement) {
        return encoded.replaceAll(Pattern.quote(toReplace), replacement);
    }

    private static String encode(String plain) {
        String encoded = "";
        try {
            encoded = URLEncoder.encode(plain, CHARSET);
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException("Charset not found while encoding string: " + CHARSET, uee);
        }
        for (Map.Entry rule : ENCODING_RULES.entrySet()) {
            encoded = applyRule(encoded, rule.getKey(), rule.getValue());
        }
        return encoded;
    }


}

Pre requisites:
1. Obtain the Consumer key and Secret for the application created in twitter

2. The HMACSHA utility java class was used to created the encoded authorized value from the parameters provided.
3. Any Jdev Versions with Http Header Parameter support in URL DataControl 

Steps in JDeveloper :
1. Create a jspx page e.g index page with backing bean, in the backing bean create a method in which URLConnection is used to access the http://api.twitter.com/oauth/request_token and the authorization parameters are set to the URL before connecting. HMACSHAUtil.OAUTH1Header method is used to create the Authorization value using the Consumer key and Consumer Secret obtained while creating the application in Twitter .

Code Snippet of backing bean method :

String requestToken = HMACSHAUtil.OAUTH1Header("",this.getConsumer_key(),"POST","http://api.twitter.com/oauth/request_token", this.getConsumer_secret(),"","");
URL  url = new URL( "http://api.twitter.com/oauth/request_token" );
URLConnection  myURLConnection = url.openConnection();
HttpURLConnection conn = (HttpURLConnection)myURLConnection;
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", requestToken);
conn.connect();
 

2. Once the Connection is successful read the content of the response. Twitter sends the response stream in two different ways randomly; they are normal UTF-8 Input Stream and encoded GZIPInput Stream so ensure that your code handles both the types of response

3. Read the Temporary oauth token and oauth secret value from the response streams and set them to the session scope as we will need them later.

4. Now navigate to the following URL https://api.twitter.com/oauth/authorize?oauth_token=”Value obtained in the previous step”.

5. The User will be prompted with a login screen to provide their twitter username and password to authorize the application to read their tweets.

6. Once the Authorization is successful, twitter redirects the request to the call back URL provided in the API, and along with it sends the oauth verifier as a parameter.

7. In the jspx e.g redirect page to which twitter redirects, retrieve the oauth verifier from the URL  and set in an bean.

8. In the same page using the Client and Server listener, call a method in the backing bean in which the URL connection is again used to connect to the following
URL :  http://api.twitter.com/oauth/access_token with the appropriate authorization parameters to get the permanent access token as shown below.

Code Snippet 
String requestToken = HMACSHAUtil.OAUTH1Header(oauh_token,consumKey,"POST","http://api.twitter.com/oauth/access_token",consumSec,oauth_secret,oauth_verifier);  
URL url = new URL( "http://api.twitter.com/oauth/access_token" );
myURLConnection = url.openConnection();
conn = (HttpURLConnection)myURLConnection;
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", requestToken);
conn.connect();

Here consumSec is the consumer secret obtained after registering the application in Twitter.

9. Read the response stream the same way as mentioned in the step 2, now from the response stream get the permanent oauth token and permanent oauth secret.

10. Using the utility class create an Authorization parameter which will be used to fetch the tweets from twitter as shown in the code snippet below.

Code Snippet 
String HeaderToken = HMACSHAUtil.OAUTH1Header(perm_oauth_token,consumkey,"GET","http://api.twitter.com/1/statuses/friends_timeline.xml",consumSec,perm_oauth_secret,null);

The URL which will be used to fetch the tweets and along with the operation type (GET) is passed as parameters to the Utility method so that this authorization parameter allows twitter to recognize the user requesting for the appropriate operation.

11.The above steps have to be executed only once to obtain the permanent authorization parameters. Set the HeaderToken(Authorization parameter) value in the Session Scope.

FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getSessionMap().put("FinalHeaderToken",HeaderToken);

12. Now create a new jspx page e.g viewtweets.jspx with a backing bean associated to it.

13. Create URLDC with GET operation type with a dummy url where ensure you select the Include Http Header option and provide the following xsd

14. After creating the DC, Open the DataControl.dcx file and edit the attribute  Source="/1/statuses/friends_timeline.xml" and in Connections.xml update the attribute
url="http://api.twitter.com" of urlconnection field.

15. In the backing bean of the viewtweets.jspx put a Map variable and generate accessors as shown below
private Map httpHeaders = new HashMap();
public void setHttpHeaders(Map httpHeaders) {
        this.httpHeaders = httpHeaders;
    }

public Map getHttpHeaders() {
        return httpHeaders;
    }


16. In the same backing bean, inside the method setF1 which gets called first when the page is rendering as shown below
 public void setF1(RichForm f1) {
        this.f1 = f1;
        String HeaderToken = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("FinalHeaderToken");
        System.out.println("Session Header Token:"+HeaderToken);
        httpHeaders.put("Authorization",HeaderToken);
    }

17. Drag and Drop the status from the return type of the method in the DC palette onto the page as ADF Form

18. In the Edit Action Binding,set the map as the value for the Header parameter exposed.

Running the Application :

Before running the application we have to ensure that the HostNameVerifier is set to NONE in the WLS.
When Run  the Index page and click on  the Button the following process takes place which is shown as pictorial representation below



Friday, 16 August 2013

Passing Multiple Complex Parameters in WSDC via Backing Bean


UseCase Description

One of the most common usecases in WS is passing complex Input parameters, but incase if the user wants to pass the multiple occurence of the same complex Input parameter type, how to achieve this in WSDC ?.
This blog explains about passing the multiple complex Employee records to the WebService from the WSDC via the Backing Bean
 Let us Consider an ADFBC Component which contains an Employees Entity, now the user can select multiple employee records from the page containing List of Employee records and post it to the WebService.

Implementation Steps

1. Create an Web Service which takes the Input Argument as ArrayList of Employee Object  and returns the ArrayList .
The Sample SOAP Request/Response looks like as shown below ,
Here we can observe that the the occurence of the "emp" object is dynamic which contains different employee records with three attributes (empEmail, empId, empName).
SOAP Request
<ns1:addemp>
            <emp>
                <empemail>SLROCK</empemail>
                <empid>101</empid>
                <empname>ShawnRock</empname>
            </emp>
            <emp>
                <empemail>OKOKHLO</empemail>
                <empid>202</empid>
                <empname>OnkBank</empname>
            </emp>
  </ns1:addemp>

SOAP Response
 <ns2:addempresponse xmlns:ns2="http://project1/">
            <return>
                <empemail>SLROCK</empemail>
                <empid>101</empid>
                <empname>ShawnRock</empname>
            </return>
            <return>
                <empemail>OKOKHLO</empemail>
                <empid>202</empid>
                <empname>OnkBank</empname>
            </return>
 </ns2:addempresponse>

2. Create Business Components from Employee Table.
3. Drag and Drop the EmployeesView1 node from the DC palette onto the page1.jspx as ADF Table.Ensure that the page1.jspx is bounded to a Backing Bean
4. Create a WSDC using the WSDL url of the service deployed to the server
5. Create a Command Button in page1.jspx and bound it to an Action Listener method in the Backing Bean . The Method is used to retrieve the records selected by the user from the page1.jspx and post it to the WS.
BindingContainer bindings = getBindings();
RowKeySet selectedEmps = getT1().getSelectedRowKeys();   
Iterator selectedEmpIter = selectedEmps.iterator();
DCBindingContainer bx =  (DCBindingContainer) getBindings();
DCIteratorBinding empIter = bx.findIteratorBinding("EmployeesView1Iterator");
RowSetIterator empRSIter = empIter.getRowSetIterator();
6. This is Achieved by marshalling the selected data as an Employee Objects and adding them to an ArrayList.
ArrayList al = new ArrayList();
while(selectedEmpIter.hasNext()){
Key key = (Key)((List)selectedEmpIter.next()).get(0);
Row currentRow = empRSIter.getRow(key);                
al.add(new Employee((String)currentRow.getAttribute("FirstName"),(Integer)currentRow.getAttribute("EmployeeId"),(String)currentRow.getAttribute("Email")));
}

7. This ArrayList is then passed onto the Params Map of the WSDC method as shown below
OperationBinding operationBinding =  bindings.getOperationBinding("addEmp");
operationBinding.getParamsMap().put("emp",al);
operationBinding.execute();
8. In page2.jspx, Drag and Drop the Return type of the WSDC method as ADF Table
9. Run the Page1.jspx, select the list of Employee records and click on the post to webservice button



10. In the Next Page we can observe that the selected 4 records are posted to the WebService


Tuesday, 30 July 2013

Integrating Google Authentication in Oracle ADF Application


This blog explains about securing an ADF application using the Google Sign in Functionality which uses the OAuth 2.0 security framework

Prerequisites
package view;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Arrays;
public class MyGoogleHelperClass {

 
  private static final String CLIENT_ID = "YOUR CLIENT ID";
    private static final String CLIENT_SECRET = "YOUR CLIENT SECRET";
    private static final String CALLBACK_URI = "YOUR CALL BACK URI";
    private static final Iterable SCOPE = Arrays.asList("https://www.googleapis.com/auth/userinfo.profile;https://www.googleapis.com/auth/userinfo.email".split(";"));
    private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo";
    private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    private String stateToken;
    private final GoogleAuthorizationCodeFlow flow;
    
    public MyGoogleHelperClass() {
        super();
        flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT,
                        JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, SCOPE).build();
        generateStateToken();
    }
    
    public String buildLoginUrl() {
            final GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl();                   
            return url.setRedirectUri(CALLBACK_URI).setState(stateToken).build();
    }
    
    private void generateStateToken(){
            SecureRandom sr1 = new SecureRandom();
            stateToken = "google;"+sr1.nextInt();
    }
    public void setStateToken(String stateToken) {
        this.stateToken = stateToken;
    }

    public String getStateToken() {
        return stateToken;
    }
    public String getUserInfoJson(final String authCode) throws IOException {                           
            final GoogleTokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(CALLBACK_URI).execute();
            final Credential credential = flow.createAndStoreCredential(response, null);
            final HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);
            // Make an authenticated request
            final GenericUrl url = new GenericUrl(USER_INFO_URL);
            final HttpRequest request = requestFactory.buildGetRequest(url);
            request.getHeaders().setContentType("application/json");
            final String jsonIdentity = request.execute().parseAsString(); 
            return jsonIdentity;

    }
}

Implementation Steps
1. Create a Fusion Application. In the Model create a WSDC of a WS which returns an complex employee Object
2. In the View Controller, create a jspx page i.e DCPage
3. Drag and Drop the Return type node of the method onto the page as ADF Table
4. Create another index.jspx page with a backing bean, then create a command menu button/link and then bind to the method in the backing Bean which contains code as shown below
public void verifytheUser(ActionEvent awt)
    {
     FacesContext context = FacesContext.getCurrentInstance();
     MyGoogleHelperClass ghc = new MyGoogleHelperClass();
     String urlPath     =        ghc.buildLoginUrl();       
try {
            context.getExternalContext().getSessionMap().put("StateToken",ghc.getStateToken());
           context.getExternalContext().redirect(urlPath);
        } catch (IOException e) {
        System.out.println(e);
        }
    }

5. Run the index.jspx page and click on the action button/link, we can see that the page is navigated to the Google Sign-In Page where the user is prompted to login with his credentials
6. If Login is successful, the user is asked to allow the application to access his resource, once accepted the page will be redirected back to the Callback URI
7. In the CallBack Page, get the following request parameters from the redirect uri. here we should check that the state which we sent while making the request matches with the state sent as query param along with the redirect uri. This Security is needed to avoid CSRF Hacking.
public void gettheUserdata()
{
FacesContext context = FacesContext.getCurrentInstance();
Map hm =   context.getExternalContext().getRequestParameterMap();
GoogleHelperClass ghc = new GoogleHelperClass();
// This needs to be verified to Avoid CSRF(Cross-Site Request Forgery)
String sentState = (String)context.getExternalContext().getSessionMap().get("StateToken ");
try {
if(hm.containsKey("code")&&(sentState.equalsIgnoreCase(hm.get("state"))))
{
String userInfo = ghc.getUserInfoJson((String)hm.get("code"));
context.getExternalContext().getSessionMap().put("USERNAME",extracttheName(userInfo));
context.getExternalContext().redirect("DC PAGE URL");            
} else{
context.getExternalContext().redirect("ACCESS DENIED URL");
}
}catch (IOException e) {

                    }
}


 public String extracttheName(String jsonStr)
    {
        String nmeoftheUser=null;
        try {
            JSONArray array = new JSONArray("["+jsonStr+"]");
            for (int i = 0; i < array.length(); i++) {
            JSONObject element = array.getJSONObject(0);
            NmeoftheUser = element.get("name");
            }      
        } catch (JSONException e) {
            System.out.println(e);
        }
        return nmeoftheUser;
    }
  • Code
  • State
8.  Use the Value set in the Session to display the name of the user on the page with the resource