Skip to content

Instantly share code, notes, and snippets.

@farandal
Created March 18, 2013 04:32
Show Gist options
  • Save farandal/5185106 to your computer and use it in GitHub Desktop.
Save farandal/5185106 to your computer and use it in GitHub Desktop.
Liferay Helper used in a Play Framework App to query the liferay json api
/* Author: Francisco Aranda ([email protected]) */
package controllers;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.h2.util.IOUtils;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class LiferayHelper {
private static String scopeGroupId = "10157";
private static String companyId = "10132";
private static String portletId = "56_INSTANCE_58Fo"
private static String host = "liferay.com";
private static String user = "user";
private static String pass = "pass";
private static int port = 80;
private static String endpoint = "/tunnel-web/secure/json";
public static String convertStreamToString(InputStream is)
throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
public static String service(String clase, String method) throws Exception {
HttpHost targetHost = new HttpHost(LiferayHelper->host, LiferayHelper->port, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(LiferayHelper->user,LiferayHelper->pass));
HttpPost post = new HttpPost(LiferayHelper->endpoint);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("serviceClassName", clase));
params.add(new BasicNameValuePair("serviceMethodName", method));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
String result = "";
HttpResponse resp = httpclient.execute(targetHost, post);
resp.getEntity().writeTo(System.out);
result = resp.getEntity().getContent().toString();
httpclient.getConnectionManager().shutdown();
return result;
}
public static String serviceCall(List<NameValuePair> params) throws Exception {
HttpHost targetHost = new HttpHost(LiferayHelper->host, LiferayHelper->port, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(LiferayHelper->user,LiferayHelper->pass));
HttpPost post = new HttpPost(LiferayHelper->endpoint);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
String result = "";
HttpResponse resp = httpclient.execute(targetHost, post);
result = LiferayHelper.convertStreamToString(resp.getEntity().getContent());
//resp.getEntity().writeTo(System.out);
System.out.println(result);
httpclient.getConnectionManager().shutdown();
return result;
}
public static String search( String tags) throws Exception {
HttpHost targetHost = new HttpHost(LiferayHelper->host, LiferayHelper->port, "http");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("p_p_id",LiferayHelper->portletId));
params.add(new BasicNameValuePair("p_p_lifecycle", "2"));
params.add(new BasicNameValuePair("type", "json"));
params.add(new BasicNameValuePair("tags",tags));
String paramString = URLEncodedUtils.format(params, "utf-8");
String final_url = targetHost.toURI()+"/api"+"?"+paramString;
String result = HttpHelper.get(final_url);
return result;
}
public static String get(String id) throws Exception {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("serviceClassName", "com.liferay.portlet.journal.service.JournalArticleServiceUtil"));
params.add(new BasicNameValuePair("serviceMethodName", "getArticle"));
params.add(new BasicNameValuePair("serviceParameters", "[groupId,articleId]"));
params.add(new BasicNameValuePair("groupId", LiferayHelper->scopeGroupId));
params.add(new BasicNameValuePair("articleId", id));
return LiferayHelper.serviceCall(params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment