Skip to content

Commit

Permalink
initial import of java rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
fredsakr committed Oct 14, 2012
1 parent 2726d28 commit 0d2e714
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 0 deletions.
6 changes: 6 additions & 0 deletions RestClient/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions RestClient/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>RestClient</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions RestClient/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
85 changes: 85 additions & 0 deletions RestClient/src/com/rest/api/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.rest.api;

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;

public class Client
{
private String _authToken;
private String _baseUrl;

public Client(String user, String password, String url)
{
_baseUrl = url;

String authString = user + ":" + password;
_authToken = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes());
}

public Response get(String uri)
{
return this.execute(uri, Method.GET, null);
}

public Response post(String uri, String body)
{
return this.execute(uri, Method.POST, body);
}

public Response put(String uri, String body)
{
return this.execute(uri, Method.PUT, body);
}

public void delete(String uri)
{
this.execute(uri, Method.DELETE, null);
}

public Response execute(String uri, Method method, String body)
{
Response response = new Response();

try
{
URL url = new URL(_baseUrl + uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setInstanceFollowRedirects(false);
conn.setRequestMethod(method.toString());
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", _authToken);

if (method == Method.POST || method == Method.PUT)
{
conn.setDoOutput(true);
final OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
}

InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader( is));

String line;
while ((line = rd.readLine()) != null)
{
response.body += line;
}
rd.close();

response.statusCode = conn.getResponseCode();
conn.disconnect();
}
catch (Exception e)
{
response.exception = e.getMessage();
}
return response;
}
}
9 changes: 9 additions & 0 deletions RestClient/src/com/rest/api/Method.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.rest.api;

public enum Method
{
GET,
POST,
PUT,
DELETE
}
8 changes: 8 additions & 0 deletions RestClient/src/com/rest/api/Response.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.rest.api;

public class Response
{
public int statusCode;
public String body = "";
public String exception;
}
9 changes: 9 additions & 0 deletions Samples/ContactSample/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/gson-2.2.2.jar"/>
<classpathentry kind="src" path="/RestClient"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions Samples/ContactSample/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ContactSample</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions Samples/ContactSample/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Binary file added Samples/ContactSample/lib/gson-2.2.2.jar
Binary file not shown.
48 changes: 48 additions & 0 deletions Samples/ContactSample/src/com/rest/api/samples/ContactHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.rest.api.samples;

import com.rest.api.*;
import com.rest.api.samples.models.*;
import com.google.gson.Gson;

public class ContactHelper
{
private Client _client;

public ContactHelper(String site, String user, String password, String baseUrl)
{
_client = new Client(site + "\\" + user, password, baseUrl);
}

public Contact GetContact(int id)
{
Contact contact = new Contact();

try
{
Response response = _client.get("/data/contact/" + String.valueOf(id));
Gson gson = new Gson();
contact = gson.fromJson(response.body, Contact.class);
}
catch (Exception e)
{
e.printStackTrace();
}
return contact;
}

public SearchResponse<Contact> GetContacts(String search, int page, int count)
{
SearchResponse<Contact> contacts = null;
try
{
Response response = _client.get("/data/contacts?search=" + search + "&page=" + page + "&count=" + count);
Gson gson = new Gson();
contacts = gson.fromJson(response.body, SearchResponse.class);
}
catch (Exception e)
{
e.printStackTrace();
}
return contacts;
}
}
23 changes: 23 additions & 0 deletions Samples/ContactSample/src/com/rest/api/samples/models/Contact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.rest.api.samples.models;

import java.util.List;

public class Contact
{
public String accountName;
public String address1;
public String address2;
public String address3;
public String businessPhone;
public String city;
public String country;
public String firstName;
public String emailAddress;
public int id;
public boolean isBounceback;
public boolean isSubscribed;
public String lastName;
public String salesPerson;
public String title;
public List<FieldValue> fieldValues;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.rest.api.samples.models;

public class FieldValue
{
public int id;
public String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.rest.api.samples.models;

import java.util.List;

public class SearchResponse<T>
{
public List<T> elements;
public int total;
public int page;
public int pageSize;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.rest.api.samples.tests;

import junit.framework.Assert;
import com.rest.api.samples.models.*;
import org.junit.Before;
import org.junit.Test;

import com.rest.api.samples.ContactHelper;


public class ContactHelperTests
{
private ContactHelper _helper;

@Before
public void Setup()
{
_helper = new ContactHelper("site", "user", "password", "https://secure.eloqua.com/API/REST/1.0");
}

@Test
public void GetContactTest()
{
int id = 152365;
Contact contact = _helper.GetContact(id);
Assert.assertEquals(id, contact.id);
}

@Test
public void GetContactsTest()
{
SearchResponse<Contact> contacts = _helper.GetContacts("*", 1, 10);
Assert.assertTrue(contacts.total > 0);
}
}

0 comments on commit 0d2e714

Please sign in to comment.