diff --git a/client/Java/Demo.class b/client/Java/Demo.class new file mode 100644 index 00000000..ffed26e2 Binary files /dev/null and b/client/Java/Demo.class differ diff --git a/client/Java/Demo.java b/client/Java/Demo.java new file mode 100644 index 00000000..9dee0fcf --- /dev/null +++ b/client/Java/Demo.java @@ -0,0 +1,50 @@ + +import java.util.ArrayList; +import java.util.List; +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import com.google.gson.Gson; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +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.cookie.Cookie; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.protocol.HTTP; + +public class Demo { + public static final String BASE_URL = "http://example.com/gallery3/index.php"; + public static final String USERNAME = "admin"; + public static final String PASSWORD = "admin"; + + public static void main(String[] argv) throws java.io.UnsupportedEncodingException, java.io.IOException { + DefaultHttpClient httpclient = new DefaultHttpClient(); + HttpResponse response; + Gson gson = new Gson(); + + // Get the REST API key + HttpPost post = new HttpPost(BASE_URL + "/rest"); + ArrayList nvps = new ArrayList (); + nvps.add(new BasicNameValuePair("user", USERNAME)); + nvps.add(new BasicNameValuePair("password", USERNAME)); + post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); + response = httpclient.execute(post); + String api_key = gson.fromJson(new BufferedReader( + new InputStreamReader(response.getEntity().getContent())).readLine(), String.class); + System.out.println("API Key:" + api_key); + + // Get the JSON representation of the root album, which we know has id 1 + HttpGet get = new HttpGet(BASE_URL + "/rest/item/1"); + get.setHeader("X-Gallery-Request-Method", "GET"); + get.setHeader("X-Gallery-Request-Key", api_key); + response = httpclient.execute(get); + + System.out.println( + "Response: " + new BufferedReader(new InputStreamReader(response.getEntity().getContent())).readLine()); + } +} diff --git a/client/Java/README b/client/Java/README new file mode 100644 index 00000000..2a11795f --- /dev/null +++ b/client/Java/README @@ -0,0 +1,10 @@ +This is very, very rough sample code for how to do some Java REST +requests. To run this code: + +1) Edit Demo.java and set the BASE_URL to your Gallery3 install. +2) Edit USERNAME and PASSWORD as appropriate +3) In a shell, do "sh build.sh" +4) In a shell, do "sh run.sh" + +Note that there is NO error checking, so if something goes wrong +you'll have to debug it. diff --git a/client/Java/build.sh b/client/Java/build.sh new file mode 100644 index 00000000..146932bd --- /dev/null +++ b/client/Java/build.sh @@ -0,0 +1 @@ +javac -classpath lib/httpclient-4.0.1.jar:lib/httpcore-4.0.1.jar:lib/gson-1.4.jar Demo.java diff --git a/client/Java/lib/commons-logging-api-1.1.1.jar b/client/Java/lib/commons-logging-api-1.1.1.jar new file mode 100644 index 00000000..bd451168 Binary files /dev/null and b/client/Java/lib/commons-logging-api-1.1.1.jar differ diff --git a/client/Java/lib/gson-1.4.jar b/client/Java/lib/gson-1.4.jar new file mode 100644 index 00000000..b9c33d03 Binary files /dev/null and b/client/Java/lib/gson-1.4.jar differ diff --git a/client/Java/lib/httpclient-4.0.1.jar b/client/Java/lib/httpclient-4.0.1.jar new file mode 100644 index 00000000..e9c961f1 Binary files /dev/null and b/client/Java/lib/httpclient-4.0.1.jar differ diff --git a/client/Java/lib/httpcore-4.0.1.jar b/client/Java/lib/httpcore-4.0.1.jar new file mode 100644 index 00000000..4638daa5 Binary files /dev/null and b/client/Java/lib/httpcore-4.0.1.jar differ diff --git a/client/Java/run.sh b/client/Java/run.sh new file mode 100644 index 00000000..94ccb6e6 --- /dev/null +++ b/client/Java/run.sh @@ -0,0 +1 @@ +java -classpath lib/httpclient-4.0.1.jar:lib/httpcore-4.0.1.jar:lib/commons-logging-api-1.1.1.jar:lib/gson-1.4.jar:. Demo