return response;
}
private FirebaseResponse processResponse( FirebaseRestMethod method, HttpResponse httpResponse ) throws FirebaseException {
FirebaseResponse response = null;
// sanity-checks
if( method == null ) {
String msg = "method cannot be null";
LOGGER.error( msg );
throw new FirebaseException( msg );
}
if( httpResponse == null ) {
String msg = "httpResponse cannot be null";
LOGGER.error( msg );
throw new FirebaseException( msg );
}
// get the response-entity
HttpEntity entity = httpResponse.getEntity();
// get the response-code
int code = httpResponse.getStatusLine().getStatusCode();
// set the response-success
boolean success = false;
switch( method ) {
case DELETE:
if( httpResponse.getStatusLine().getStatusCode() == 204
&& "No Content".equalsIgnoreCase( httpResponse.getStatusLine().getReasonPhrase() ) )
{
success = true;
}
break;
case PUT:
case POST:
case GET:
if( httpResponse.getStatusLine().getStatusCode() == 200
&& "OK".equalsIgnoreCase( httpResponse.getStatusLine().getReasonPhrase() ) )
{
success = true;
}
break;
default:
break;
}
// get the response-body
Writer writer = new StringWriter();
if( entity != null ) {
try {
InputStream is = entity.getContent();
char[] buffer = new char[1024];
Reader reader = new BufferedReader( new InputStreamReader( is, "UTF-8" ) );
int n;
while( (n=reader.read(buffer)) != -1 ) {
writer.write( buffer, 0, n );
}
} catch( Throwable t ) {
String msg = "unable to read response-content; read up to this point: '" + writer.toString() + "'";
writer = new StringWriter(); // don't want to later give jackson partial JSON it might choke on
LOGGER.error( msg );
throw new FirebaseException( msg, t );
}
}
// convert response-body to map
Map<String, Object> body = null;
try {
body = JacksonUtility.GET_JSON_STRING_AS_MAP( writer.toString() );
} catch( JacksonUtilityException jue ) {
String msg = "unable to convert response-body into map; response-body was: '" + writer.toString() + "'";
LOGGER.error( msg );
throw new FirebaseException( msg, jue );
}
// build the response
response = new FirebaseResponse( success, code, body, writer.toString() );
return response;
}