Skip to content

Instantly share code, notes, and snippets.

@iambigd
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iambigd/8fcef57dd06c6ad58573 to your computer and use it in GitHub Desktop.
Save iambigd/8fcef57dd06c6ad58573 to your computer and use it in GitHub Desktop.
Java jersey client API
package tw.org.iii.cosa.javabacked.rest;
import java.util.Iterator;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
* Simple Jersey rest client bypassing cosa REST
*
* @author kenwctsai
**/
public class RESTClient {
private static Log LOGGER = LogFactory.getLog(RESTClient.class);
// jersey client
private Client client;
private WebResource webResource;
private WebResource.Builder webResourceBuild;
private ClientResponse clientResp;
private MultivaluedMap<String, String> headersMap;
public RESTClient() {
this.headersMap = new MultivaluedMapImpl();
}
public int getStatus() {
return this.clientResp.getStatus();
}
public void addHeader(String headerName, String headerValue) {
LOGGER.debug("add header as below");
LOGGER.debug(String.format("%s = %s%n", headerName, headerValue));
this.headersMap.add(headerName, headerValue);
}
public String post(String apiURL, String jsonBody) {
LOGGER.debug("use post to call api:" + apiURL);
String response = null;
this.client = Client.create();
// create resource
this.webResource = this.client.resource(apiURL);
this.webResourceBuild = webResource.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
addHeaders();
// do post
this.clientResp = this.webResourceBuild.post(ClientResponse.class,
jsonBody);
if (this.clientResp.getStatus() == 200) {
// get string of response
response = this.clientResp.getEntity(String.class);
LOGGER.debug("post response:" + response);
} else {
LOGGER.error("fail to call api:" + apiURL);
}
return response;
}
public String get(String apiURL) {
LOGGER.debug("use get to call api:" + apiURL);
String response = null;
this.client = Client.create();
// create resource
this.webResource = this.client.resource(apiURL);
this.webResourceBuild = this.webResource.type(
MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
addHeaders();
// do get
this.clientResp = this.webResourceBuild.get(ClientResponse.class);
if (this.clientResp.getStatus() == 200) {
// get string of response
response = this.clientResp.getEntity(String.class);
LOGGER.debug("get response:" + response);
} else {
LOGGER.error("fail to call api:" + apiURL);
}
return response;
}
private void addHeaders() {
LOGGER.debug("start to add headers into web resource");
if (this.headersMap.size() > 0) {
for (Iterator<String> it = headersMap.keySet().iterator(); it
.hasNext();) {
String key = it.next();
String value = headersMap.getFirst(key);
LOGGER.debug(String.format("%s = %s%n", key, value));
this.webResourceBuild = this.webResource.header(key, value);
}
//clear current headers
this.headersMap.clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment