com.itzmeds.rac.core.client.ServiceClientUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-api-client Show documentation
Show all versions of rest-api-client Show documentation
Java client(Android friendly) for CRUD operations on REST APIs
The newest version!
package com.itzmeds.rac.core.client;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ws.rs.client.WebTarget;
/**
* Utility class to construct query parameters and path parameters for the REST
* API target URL
*
* @author itzmeds
*
*/
public class ServiceClientUtils {
/**
* Adds query parameters to the REST API web target
*
* @param
* - M is of type 'String' for single valued query parameter and type
* 'List' for multi-valued query parameters
*
* @param queryParams
* - map of query parameters
* @param restResource
* - jersey web target
* @return WebTarget - web target with query parameters appended
*/
@SuppressWarnings("rawtypes")
public static WebTarget addQueryParameters(Map queryParams, WebTarget restResource) {
Iterator queryParamIter = queryParams.keySet().iterator();
while (queryParamIter.hasNext()) {
String queryParamKey = queryParamIter.next();
Object queryParamValue = queryParams.get(queryParamKey);
if (queryParamValue instanceof List) {
for (int i = 0; i < ((List) queryParamValue).size(); i++) {
restResource = restResource.queryParam(queryParamKey, ((List) queryParamValue).get(i));
}
} else {
restResource = restResource.queryParam(queryParamKey, queryParams.get(queryParamKey));
}
}
return restResource;
}
/**
* Adds path parameters to the REST API web target
*
* @param pathParams
* - map of query parameters
* @param restResource
* - jersey web target
* @return WebTarget - web target with path parameters appended
*/
public static WebTarget addPathParameters(Map pathParams, WebTarget restResource) {
Iterator pathParamIter = pathParams.keySet().iterator();
while (pathParamIter.hasNext()) {
String pathParamKey = pathParamIter.next();
restResource = restResource.path(pathParams.get(pathParamKey));
}
return restResource;
}
}