Saturday 22 August 2015

Liferay 6.x cstom plugin or out of box web services consumption via Java program

Required class path dependencies are :


  1. portal-client
  2. axis
  3. axis-jaxrpc
  4. commons-logging
  5. commons-discovery
  6. wsdl4j

Java code:

public class CompetencyRemoteAuth {

                static final Logger LOGGER = LoggerFactory
                                                .getLogger(CompetencyRemoteAuth.class);

                static final String LIFERAY_USER_NAME = "satya.kolliboina@gmail.com";
                static final String LIFERAY_USER_PASSWORD = "test1";

                static final String USER_SERVICE = "Portal_UserService";
                static final String COMPANY_SERVICE = "Portal_CompanyService";
                static final String Competency_AUTHENTICATION_SERVICE = "Plugin_Competency_CompetencyAuthenticationService";
                static final String Competency_REGISTRATION_SERVICE = "Plugin_Competency_RegistrationService";

                /**
                * @param args
                */
                public static void main(String[] args) {
                                try {
                                                System.out.println("UserEmail: " + args[0] + "...user pwd:"+args[1]);
                                                URL CompetencyAuthenticationServiceEndPoint = _getURL(args[0],args[1], Competency_AUTHENTICATION_SERVICE);
                                             
                                                System.out.println("Try lookup CompetencyAuthentication Service by End Point: "         + CompetencyAuthenticationServiceEndPoint + "...");
                                             
                                                URL companyServiceEndPoint = _getURL(args[0],args[1], COMPANY_SERVICE);
                                             
                                                System.out.println("Try lookup company Service by End Point: "               + CompetencyAuthenticationServiceEndPoint + "...");
                                             
                                                CompetencyAuthenticationServiceSoapServiceLocator CompetencyAuthenticationLocator = new CompetencyAuthenticationServiceSoapServiceLocator();
                                                CompetencyAuthenticationServiceSoap CompetencyAuthenticationService = CompetencyAuthenticationLocator.getPlugin_Competency_CompetencyAuthenticationService(CompetencyAuthenticationServiceEndPoint);
                                                                             
                                                ((Plugin_Competency_CompetencyAuthenticationServiceSoapBindingStub) competencyAuthenticationService)
                                                .setUsername(args[0]);
                                                ((Plugin_Competency_CompetencyAuthenticationServiceSoapBindingStub) competencyAuthenticationService)
                                                .setPassword(args[1]);
             
                                             
                                                CompanyServiceSoapServiceLocator locatorCompany = new CompanyServiceSoapServiceLocator();
                                                CompanyServiceSoap companyService = locatorCompany.getPortal_CompanyService(companyServiceEndPoint);
                                                CompanySoap companySoap = companyService.getCompanyByVirtualHost("localhost");

                                                System.out.println("Get UserID" + "...");
                                                long userId = 0;
                                             
                                             
                                                userId = CompetencyAuthenticationService.CompetencyAuthentication(args[0], args[1], args[2]);
                                                System.out.println("UserId for user named " + args[0]   + " is " + userId);

                                             

                                } catch (RemoteException re) {
                                                LOGGER.error(re.getClass().getCanonicalName()
                                                                                + re.getMessage());
                                } catch (ServiceException se) {
                                                LOGGER.error(se.getClass().getCanonicalName()
                                                                                + se.getMessage());
                                }
                }
             
                /**
                * Get the URL Liferay SOAP Service
                *
                 * @param remoteUser
                * @param password
                * @param serviceName
                * @return
                */
                private static URL _getURL(String remoteUser, String password, String serviceName) {
//http://localhost:9090/CompetencyAuthentication-portlet/api/axis/Plugin_Competency_CompetencyAuthenticationService?wsdl
                                final String LIFERAY_PROTOCOL = "http://";
                                final String LIFERAY_TCP_PORT = "9090";
                                final String LIFERAY_FQDN = "127.0.0.1";
                                String LIFERAY_AXIS_PATH = "";
                             
                                try {
                                                if(serviceName.split("_")[0].equalsIgnoreCase("Plugin")){
                                                                LIFERAY_AXIS_PATH = "/CompetencyMobility-portlet/api/secure/axis/";
                                                }else{
                                                                LIFERAY_AXIS_PATH = "/api/secure/axis/";
                                                }
                                                return new URL(LIFERAY_PROTOCOL
                                                                                + URLEncoder.encode(remoteUser, "UTF-8") + ":"
                                                                                + URLEncoder.encode(password, "UTF-8") + "@" + LIFERAY_FQDN
                                                                                + ":" + LIFERAY_TCP_PORT + LIFERAY_AXIS_PATH + serviceName);
                                } catch (Exception e) {
                                                LOGGER.error(e.getMessage());
                                                e.printStackTrace();
                                                return null;
                                }
                }
}

No comments:

Post a Comment