/** * */ 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{ /** * Mé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étodo manejador de respuestas y su conversión a un objeto especí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); } } }