Monday, July 1, 2013

Rest Full Web Services in Liferay

Here i am writing a method which will how to  implement the RestFull Web Services

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

String response = "";
public static String restfullResponse(){ // example method which return the Restfull webservice response

   HttpClient client = new HttpClient(); //creating client to perform restfull webservices
   String url = "URL"; // give the url to which you want to send the web services request
   Credentials credentials = new UsernamePasswordCredentials("username","password");
   client.getState().setCredentials(AuthScope.ANY, credentials);
   client.getParams().setAuthenticationPreemptive(true);
// The above 3 lines are used for the authentication checking
   PostMethod method = new PostMethod(url);// based upon requirement u can use post or get
   method.addParameter("name", "inthiyaz");//to add any parameters to the URL
   try {
int returnCode = client.executeMethod(method); //to execute the post or get method
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.out.println("The Post method is not implemented by this URL");
response = method.getResponseBodyAsString();
} else {
br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
while (((response = br.readLine()) != null)) {
return response;
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}

Liferay DXP JNDI Data Source Cofiguration

 This Blog will help us to learn about the JNDI Data Source Configuration in Liferay DXP. We have tested this with Liferay 7.3 with Tomcat. ...