Thursday, November 17, 2016

Invoke HTTPS URL from java & GET ACCESS TOKEN FORM WSO2IS


INVOKE HTTPS URL AND OAUTH2 GETTING AUTHENTICATION TOKEN FROM WSO2 IDENTITY SERVER pre-requisities:
1) org.apache.oltu.oauth2.common-0.31.jar
2) org.apache.oltu.oauth2.client-0.31.jar

import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;

public class TestAuth {

 protected static void init() {
  try {
   SSLContext sc = SSLContext.getInstance("SSL");

   HostnameVerifier hv = new HostnameVerifier() {
    public boolean verify(String urlHostName, SSLSession session) {
     return true;
    }
   };
   TrustManager[] trustAllCerts = { new X509TrustManager() {
    public X509Certificate[] getAcceptedIssuers() {
     return null;
    }

    public void checkClientTrusted(X509Certificate[] certs,
      String authType) {
    }

    public void checkServerTrusted(X509Certificate[] certs,
      String authType) {
    }
   } };
   sc.init(null, trustAllCerts, new SecureRandom());

   SSLContext.setDefault(sc);
   HttpsURLConnection.setDefaultHostnameVerifier(hv);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  init();
  try {
    OAuthClientRequest request = OAuthClientRequest
     .tokenLocation("https://localhost:9447/oauth2/token")
     .setGrantType(GrantType.PASSWORD)
     .setClientId("v79zb9om_TRquw1AVwxfsfV2inQa")
     .setClientSecret("H8S8qfY6GsjNOJKZ9y8juvkb_9Ya")
     .setUsername("admin").setPassword("admin")
     .buildBodyMessage();
   OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
   oAuthClient.accessToken(request);
   OAuthJSONAccessTokenResponse tokenResponse = (OAuthJSONAccessTokenResponse) oAuthClient
     .accessToken(request, OAuthJSONAccessTokenResponse.class);
   System.out.println("TOKEN :" + tokenResponse.getAccessToken());
  } catch (OAuthSystemException e) {
     e.printStackTrace();
  } catch (OAuthProblemException e) {
     e.printStackTrace();
  }
 }

}

Thursday, May 26, 2016

Eclipse hangs at startup showing only the splash screen

Eclipse hangs at start-up showing only the splash screen


 In your workspace directory perform the following steps:
  1. find the  ".metadata" folder (note: it was hidden) & .plugins
  2. found the folder with name  "org.eclipse.core.resources org.eclipse.core.resources" and take the back up by renaming the folder "org.eclipse.core.resources org.eclipse.core.resources.bkp". 
  3. Start eclipse. (It should show an error message or an empty workspace because no project is found.)
  4. Start eclipse and start working. :-)

Sunday, May 22, 2016

[Eclipse] : Internal error, trying to import from git

I am using Eclipse Luna, and I am trying to import a project from github. However, it says "Internal error; consult Eclipse error log" when I pasted the URI in:
enter image description here

Solution :

Delete the "secure_storage" file from the below location.

C:\Users\<username>\.eclipse\org.eclipse.equinox.security\secure_storage
Restart the eclipse and enjoy..