maia/.svn/pristine/42/42f7cfbfd1c461396af23950aa4...

59 lines
1.8 KiB
Plaintext
Executable File

/**
*
*/
package com.fp.alfresco.util;
import java.io.IOException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
import com.fp.alfresco.exception.ExceptionWebscript;
/**
* Clase que maneja las respuestas de una petició en el web scripts api client
* @author bpt
*
*/
public class ApiResponseHandler implements ResponseHandler<String>{
/**
* M&eacute;todo manejador de respuestas
*/
/* (non-Javadoc)
* @see org.apache.http.client.ResponseHandler#handleResponse(org.apache.http.HttpResponse)
*/
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
return null;
}
/**
* M&eacute;todo manejador de respuestas y su conversi&oacute;n a un objeto espec&iacute;fico
* @param response respuesta recibida
* @param isGetEntity si se lo debe retornar como un Stream un String o un objeto JSON
* @return la respuesta
* @throws Exception
*/
public Object handleResponse(HttpResponse response, boolean isGetEntity) throws Exception{
int status = response.getStatusLine().getStatusCode();
HttpEntity entityResponse = response.getEntity();
if ((status >= 200) && (status < 300)) {
if(isGetEntity){
return entityResponse.getContent();
} else {
return EntityUtils.toString(entityResponse);
}
} else {
JSONObject json = (JSONObject) JSONSerializer.toJSON(EntityUtils.toString(entityResponse));
String message = json.getString("message");
throw new ExceptionWebscript(message);
}
}
}