// Path: core/src/main/java/com/siteworx/cq5/ooyala/client/request/OoyalaPutRequest.java
// public class OoyalaPutRequest extends OoyalaRequest {
// 
// 	private JSONObject body;
// 	
// 	public OoyalaPutRequest(OoyalaApiCredential credentials) {
// 		super(credentials, HTTPMethod.PUT, "/v2/assets");
// 	}
// 	
// 	public OoyalaPutRequest(OoyalaApiCredential credentials, String pathSuffix) {
// 		super (credentials, HTTPMethod.PUT, "/v2/assets/" +pathSuffix);
// 	}
// 
// 	public JSONObject getBody() {
// 		return body;
// 	}
// 
// 	public void setBody(JSONObject body) {
// 		this.body = body;
// 	}
// }


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map.Entry;
import java.util.SortedMap;
import javax.ws.rs.core.MediaType;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.siteworx.cq5.ooyala.client.request.OoyalaPostRequest;
import com.siteworx.cq5.ooyala.client.request.OoyalaPutRequest;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.core.util.Base64;
	 */
	public String request(OoyalaPostRequest request) throws OoyalaClientException {
		return request(request.getProtocol(), request.getHost(), request.getCredentials(), request.getMethod().name(), request.getRequestPath(), request.getParameters(), request.getBody());
	}
	/**
	 * Sends the request to Ooyala, expecting a JSON response.
	 *
	 * @param protocol The protocol to use for the request.
	 * @param host The host to use for the request.
	 * @param credentials The {@link OoyalaApiCredential} object for authenticating the API request.
	 * @param method The HTTP method to be used when connecting to Ooyala. Usually GET.
	 * @param requestPath The request path for the type of object expected in the API response.
	 * @param parameters The request parameters.
	 * @param queryParameters The request query parameters.
	 * @return The JSON string returned by Ooyala's API.
	 * @throws OoyalaClientException
	 */
	private String request(String protocol, String host, OoyalaApiCredential credentials, String method, String requestPath, SortedMap<String, String> parameters, JSONObject body) throws OoyalaClientException {
		if(!parameters.containsKey("expires")){
			parameters.put("expires", Long.toString(((System.currentTimeMillis()/1000)+60)));
		}
		
		final StringBuilder requestBuilder = new StringBuilder(protocol).append("://").append(host).append(requestPath).append("?");
		
		final String signature = getSignature(credentials.getApiSecret(), method, parameters, body, requestPath);
		final String parameterString = getParameters(parameters, signature);

		final String request = requestBuilder.append(parameterString).toString();
		if(log.isDebugEnabled())log.debug("Ooyala: request URL = " + request);
		
// Path: core/src/main/java/com/siteworx/cq5/ooyala/client/request/OoyalaPutRequest.java
// public class OoyalaPutRequest extends OoyalaRequest {
// 
// 	private JSONObject body;
// 	
// 	public OoyalaPutRequest(OoyalaApiCredential credentials) {
// 		super(credentials, HTTPMethod.PUT, "/v2/assets");
// 	}
// 	
// 	public OoyalaPutRequest(OoyalaApiCredential credentials, String pathSuffix) {
// 		super (credentials, HTTPMethod.PUT, "/v2/assets/" +pathSuffix);
// 	}
// 
// 	public JSONObject getBody() {
// 		return body;
// 	}
// 
// 	public void setBody(JSONObject body) {
// 		this.body = body;
// 	}
// }


// Path: core/src/main/java/com/siteworx/cq5/ooyala/client/OoyalaPostClient.java
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map.Entry;
import java.util.SortedMap;
import javax.ws.rs.core.MediaType;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.siteworx.cq5.ooyala.client.request.OoyalaPostRequest;
import com.siteworx.cq5.ooyala.client.request.OoyalaPutRequest;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.core.util.Base64;

	 */
	public String request(OoyalaPostRequest request) throws OoyalaClientException {
		return request(request.getProtocol(), request.getHost(), request.getCredentials(), request.getMethod().name(), request.getRequestPath(), request.getParameters(), request.getBody());
	}
	/**
	 * Sends the request to Ooyala, expecting a JSON response.
	 *
	 * @param protocol The protocol to use for the request.
	 * @param host The host to use for the request.
	 * @param credentials The {@link OoyalaApiCredential} object for authenticating the API request.
	 * @param method The HTTP method to be used when connecting to Ooyala. Usually GET.
	 * @param requestPath The request path for the type of object expected in the API response.
	 * @param parameters The request parameters.
	 * @param queryParameters The request query parameters.
	 * @return The JSON string returned by Ooyala's API.
	 * @throws OoyalaClientException
	 */
	private String request(String protocol, String host, OoyalaApiCredential credentials, String method, String requestPath, SortedMap<String, String> parameters, JSONObject body) throws OoyalaClientException {
		if(!parameters.containsKey("expires")){
			parameters.put("expires", Long.toString(((System.currentTimeMillis()/1000)+60)));
		}
		
		final StringBuilder requestBuilder = new StringBuilder(protocol).append("://").append(host).append(requestPath).append("?");
		
		final String signature = getSignature(credentials.getApiSecret(), method, parameters, body, requestPath);
		final String parameterString = getParameters(parameters, signature);

		final String request = requestBuilder.append(parameterString).toString();
		if(log.isDebugEnabled())log.debug("Ooyala: request URL = " + request);
		
		long start = System.nanoTime();