Search in sources :

Example 1 with ConnectRestException

use of org.apache.kafka.connect.runtime.rest.errors.ConnectRestException in project kafka by apache.

the class RestServer method httpRequest.

/**
     * @param url               HTTP connection will be established with this url.
     * @param method            HTTP method ("GET", "POST", "PUT", etc.)
     * @param requestBodyData   Object to serialize as JSON and send in the request body.
     * @param responseFormat    Expected format of the response to the HTTP request.
     * @param <T>               The type of the deserialized response to the HTTP request.
     * @return The deserialized response to the HTTP request, or null if no data is expected.
     */
public static <T> HttpResponse<T> httpRequest(String url, String method, Object requestBodyData, TypeReference<T> responseFormat) {
    HttpURLConnection connection = null;
    try {
        String serializedBody = requestBodyData == null ? null : JSON_SERDE.writeValueAsString(requestBodyData);
        log.debug("Sending {} with input {} to {}", method, serializedBody, url);
        connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod(method);
        connection.setRequestProperty("User-Agent", "kafka-connect");
        connection.setRequestProperty("Accept", "application/json");
        // connection.getResponseCode() implicitly calls getInputStream, so always set to true.
        // On the other hand, leaving this out breaks nothing.
        connection.setDoInput(true);
        connection.setUseCaches(false);
        if (requestBodyData != null) {
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);
            OutputStream os = connection.getOutputStream();
            os.write(serializedBody.getBytes());
            os.flush();
            os.close();
        }
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            return new HttpResponse<>(responseCode, connection.getHeaderFields(), null);
        } else if (responseCode >= 400) {
            InputStream es = connection.getErrorStream();
            ErrorMessage errorMessage = JSON_SERDE.readValue(es, ErrorMessage.class);
            es.close();
            throw new ConnectRestException(responseCode, errorMessage.errorCode(), errorMessage.message());
        } else if (responseCode >= 200 && responseCode < 300) {
            InputStream is = connection.getInputStream();
            T result = JSON_SERDE.readValue(is, responseFormat);
            is.close();
            return new HttpResponse<>(responseCode, connection.getHeaderFields(), result);
        } else {
            throw new ConnectRestException(Response.Status.INTERNAL_SERVER_ERROR, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Unexpected status code when handling forwarded request: " + responseCode);
        }
    } catch (IOException e) {
        log.error("IO error forwarding REST request: ", e);
        throw new ConnectRestException(Response.Status.INTERNAL_SERVER_ERROR, "IO Error trying to forward REST request: " + e.getMessage(), e);
    } finally {
        if (connection != null)
            connection.disconnect();
    }
}
Also used : ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ErrorMessage(org.apache.kafka.connect.runtime.rest.entities.ErrorMessage) URL(java.net.URL)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 ErrorMessage (org.apache.kafka.connect.runtime.rest.entities.ErrorMessage)1 ConnectRestException (org.apache.kafka.connect.runtime.rest.errors.ConnectRestException)1