Example usage for org.apache.http.util EntityUtils getContentMimeType

List of usage examples for org.apache.http.util EntityUtils getContentMimeType

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils getContentMimeType.

Prototype

@Deprecated
    public static String getContentMimeType(HttpEntity httpEntity) throws ParseException 

Source Link

Usage

From source file:org.openstack.burrow.backend.http.BaseHttp.java

/**
 * Processes an HttpResponse. First gets all necessary information from the
 * response's JSONObject and then builds the List of Messages requested
 * /*  w w  w .  j  a  v  a  2  s  .c om*/
 * @param response An HttpResponse from the server that should contain
 *          requested Messages
 * @return A List of Messages
 * @throws HttpProtocolException Thrown if an issue with processes
 *           HttpResponse occurs
 * @throws MessageNotFoundException Thrown if the requested Messages were not
 *           found
 */
static List<Message> handleMultipleMessageHttpResponse(HttpResponse response)
        throws HttpProtocolException, CommandException {
    StatusLine status = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    if (entity == null)
        return null; // Is this actually the right thing to do?
    String mimeType = EntityUtils.getContentMimeType(entity);
    switch (status.getStatusCode()) {
    case SC_OK:
        if (mimeType.equals("application/json")) {
            try {
                String body = EntityUtils.toString(entity);
                JSONArray arrayJson = new JSONArray(body);
                List<Message> messages = new ArrayList<Message>(arrayJson.length());
                for (int idx = 0; idx < arrayJson.length(); idx++) {
                    JSONObject messageJson = arrayJson.getJSONObject(idx);
                    Message message = new MessageResponse(messageJson);
                    messages.add(idx, message);
                }
                EntityUtils.consume(entity);
                return messages;
            } catch (IOException e) {
                try {
                    // Consume the entity to release HttpClient resources.
                    EntityUtils.consume(entity);

                } catch (IOException e1) {
                    // If we ever stop throwing an exception in the outside block,
                    // this needs to be handled.
                }
                throw new HttpProtocolException("IOException reading http response");
            } catch (JSONException e) {
                // It is not necessary to consume the entity because at the
                // first point where this exception can be thrown,
                // the entity has already been consumed by toString();
                throw new HttpProtocolException("JSONException reading response");
            }
        } else {
            // This situation cannot be handled.
            try {
                // Consume the entity to release HttpClient resources.
                EntityUtils.consume(entity);
            } catch (IOException e1) {
                // If we ever stop throwing an exception in the outside block,
                // this needs to be handled.
            }
            throw new HttpProtocolException("Non-Json response");
        }
    case SC_NO_CONTENT:
        // This is not an error.
        try {
            // Consume the entity to release HttpClient resources.
            EntityUtils.consume(entity);
        } catch (IOException e) {
            throw new HttpProtocolException("Failed to consume HttpEntity");
        }
        return null;
    case SC_NOT_FOUND:
        try {
            EntityUtils.consume(entity);
        } catch (IOException e1) {
            // If we ever stop throwing an exception in the outside block,
            // this needs to be handled.
        }
        throw new QueueNotFoundException();
    default:
        // This is probably an error.
        try {
            // Consume the entity to release HttpClient resources.
            EntityUtils.consume(entity);
        } catch (IOException e1) {
            // If we ever stop throwing an exception in the outside block,
            // this needs to be handled.
        }
        throw new HttpProtocolException("Unhandled http response code " + status.getStatusCode());
    }
}

From source file:org.openstack.burrow.backend.http.BaseHttp.java

/**
 * Processes an HttpResponse. First gets all necessary information from the
 * response's JSONObject and then builds the List of Queues
 * /*w w  w . ja v  a2 s  . co m*/
 * @param response An HttpResponse from the server that should contain
 *          requested Queues
 * @return A List of Queues
 * @throws HttpProtocolException Thrown if an issue with processes
 *           HttpResponse occurs
 * @throws QueueNotFoundException Thrown if a requested Queue was not found
 */
static List<Queue> handleMultipleQueueHttpResponse(Account account, HttpResponse response)
        throws HttpProtocolException, AccountNotFoundException {
    StatusLine status = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    if (entity == null)
        return null; // Is this actually the right thing to do?
    String mimeType = EntityUtils.getContentMimeType(entity);
    switch (status.getStatusCode()) {
    case SC_OK:
        if (mimeType.equals("application/json")) {
            try {
                String body = EntityUtils.toString(entity);
                JSONArray arrayJson = new JSONArray(body);
                List<Queue> queues = new ArrayList<Queue>(arrayJson.length());
                try {
                    // Assume the response is an array of JSON Objects.
                    for (int idx = 0; idx < arrayJson.length(); idx++) {
                        JSONObject queueJson = arrayJson.getJSONObject(idx);
                        Queue queue = new QueueResponse(account, queueJson);
                        queues.add(idx, queue);
                    }
                } catch (JSONException e) {
                    // The response was not an array of JSON Objects. Try again,
                    // assuming it was an array of strings.
                    for (int idx = 0; idx < arrayJson.length(); idx++) {
                        String queueId = arrayJson.getString(idx);
                        Queue queue = new QueueResponse(account, queueId);
                        queues.add(idx, queue);
                    }
                }
                return queues;
            } catch (IOException e) {
                try {
                    // Consume the entity to release HttpClient resources.
                    EntityUtils.consume(entity);
                } catch (IOException e1) {
                    // If we ever stop throwing an exception in the outside block,
                    // this needs to be handled.
                }
                e.printStackTrace();
                throw new HttpProtocolException("IOException reading http response");
            } catch (JSONException e) {
                // It is not necessary to consume the entity because at the
                // first point where this exception can be thrown,
                // the entity has already been consumed by toString();
                e.printStackTrace();
                throw new HttpProtocolException("JSONException reading response");
            }
        } else {
            // This situation cannot be handled.
            try {
                // Consume the entity to release HttpClient resources.
                EntityUtils.consume(entity);
            } catch (IOException e1) {
                // If we ever stop throwing an exception in the outside block,
                // this needs to be handled.
            }
            throw new HttpProtocolException("Non-Json response");
        }
    case SC_NOT_FOUND:
        // This is not necessarily an error, but we must throw something.
        try {
            // Consume the entity to release HttpClient resources.
            EntityUtils.consume(entity);
        } catch (IOException e1) {
            // If we ever stop throwing an exception in the outside block,
            // this needs to be handled.
        }
        throw new AccountNotFoundException();
    default:
        // This is probably an error.
        try {
            // Consume the entity to release HttpClient resources.
            EntityUtils.consume(entity);
        } catch (IOException e1) {
            // If we ever stop throwing an exception in the outside block,
            // this needs to be handled.
        }
        throw new HttpProtocolException("Unhandled http response code " + status.getStatusCode());
    }
}

From source file:org.openstack.burrow.backend.http.BaseHttp.java

/**
 * Processes an HttpResponse. First gets all necessary information from the
 * response's JSONObject and then builds the Message to be returned
 * //ww w  . ja v a 2  s  .  c o m
 * @param response An HttpResponse from the server that should contain the
 *          requested Message
 * @return A Message object
 * @throws HttpProtocolException Thrown if an issue with processes
 *           HttpResponse occurs
 * @throws MessageNotFoundException Thrown if the requested Message was not
 *           found
 */
static Message handleSingleMessageHttpResponse(HttpResponse response)
        throws HttpProtocolException, CommandException, MessageNotFoundException {
    StatusLine status = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    switch (status.getStatusCode()) {
    case SC_OK:
    case SC_CREATED:
        String responseMimeType = EntityUtils.getContentMimeType(entity);
        if (responseMimeType == null) {
            try {
                EntityUtils.consume(entity);
            } catch (IOException e) {
                throw new HttpProtocolException("Failed to consume HttpEntity");
            }
            return null;
        } else if (responseMimeType.equals("application/json")) {
            try {
                String body = EntityUtils.toString(entity);
                JSONObject messageJson = new JSONObject(body);
                return new MessageResponse(messageJson);
            } catch (JSONException e) {
                // At the first place this could be thrown,
                // the entity has already been consumed by toString(entity);
                throw new HttpProtocolException("Failed to decode json");
            } catch (IOException e) {
                try {
                    // Consume the entity to release HttpClient resources.
                    EntityUtils.consume(entity);
                } catch (IOException e1) {
                    // If we ever stop throwing an exception in the outside block,
                    // this needs to be handled.
                }
                throw new HttpProtocolException("Failed to convert the entity to a string");
            }
        } else {
            // We can't do anything sensible yet.
            try {
                // Consume the entity to release HttpClient resources.
                EntityUtils.consume(entity);
            } catch (IOException e) {
                // If we ever stop throwing an exception in the outside block,
                // this needs to be handled.
            }
            // TODO: Handle body-only responses.
            throw new HttpProtocolException("Unhandled response mime type " + responseMimeType);
        }
    case SC_NO_CONTENT:
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            throw new HttpProtocolException("Failed to consume HttpEntity");
        }
        return null;
    case SC_NOT_FOUND:
        try {
            // Consume the entity to release HttpClient resources.
            EntityUtils.consume(entity);
        } catch (IOException e) {
            // If we ever stop throwing an exception in the outside block,
            // this needs to be handled.
        }
        throw new MessageNotFoundException();
    default:
        // There was an error or an unexpected return condition.
        try {
            // Consume the entity to release HttpClient resources.
            EntityUtils.consume(entity);
        } catch (IOException e) {
            // If we ever stop throwing an exception in the outside block,
            // this needs to be handled.
        }
        throw new HttpProtocolException("Unhandled return code");
    }
}