Search in sources :

Example 1 with Serializer

use of org.apache.pivot.serialization.Serializer in project pivot by apache.

the class Query method execute.

@SuppressWarnings("unchecked")
protected Object execute(final Method method, final Object value) throws QueryException {
    Object result = value;
    URL location = getLocation();
    HttpURLConnection connection = null;
    Serializer<Object> serializerLocal = (Serializer<Object>) this.serializer;
    bytesSent.set(0);
    bytesReceived.set(0);
    bytesExpected = -1;
    status = 0;
    String message = null;
    try {
        // Clear any properties from a previous response
        responseHeaders.clear();
        // Open a connection
        if (proxy == null) {
            connection = (HttpURLConnection) location.openConnection();
        } else {
            connection = (HttpURLConnection) location.openConnection(proxy);
        }
        connection.setRequestMethod(method.toString());
        connection.setAllowUserInteraction(false);
        connection.setInstanceFollowRedirects(false);
        connection.setUseCaches(false);
        if (connection instanceof HttpsURLConnection && hostnameVerifier != null) {
            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
            httpsConnection.setHostnameVerifier(hostnameVerifier);
        }
        // Set the request headers
        if (result != null) {
            connection.setRequestProperty("Content-Type", serializerLocal.getMIMEType(result));
        }
        for (String key : requestHeaders) {
            for (int i = 0, n = requestHeaders.getLength(key); i < n; i++) {
                if (i == 0) {
                    connection.setRequestProperty(key, requestHeaders.get(key, i));
                } else {
                    connection.addRequestProperty(key, requestHeaders.get(key, i));
                }
            }
        }
        // Set the input/output state
        connection.setDoInput(true);
        connection.setDoOutput(result != null);
        // Connect to the server
        connection.connect();
        queryListeners.connected(this);
        // Write the request body
        if (result != null) {
            try (OutputStream outputStream = connection.getOutputStream()) {
                serializerLocal.writeObject(result, new MonitoredOutputStream(outputStream));
            }
        }
        // Notify listeners that the request has been sent
        queryListeners.requestSent(this);
        // Set the response info
        status = connection.getResponseCode();
        message = connection.getResponseMessage();
        // Record the content length
        bytesExpected = connection.getContentLengthLong();
        // NOTE Header indexes start at 1, not 0
        int i = 1;
        for (String key = connection.getHeaderFieldKey(i); key != null; key = connection.getHeaderFieldKey(++i)) {
            responseHeaders.add(key, connection.getHeaderField(i));
        }
        // If the response was anything other than 2xx, throw an exception
        int statusPrefix = status / 100;
        if (statusPrefix != 2) {
            queryListeners.failed(this);
            throw new QueryException(status, message);
        }
        // Read the response body
        if (method == Method.GET && status == Query.Status.OK) {
            try (InputStream inputStream = connection.getInputStream()) {
                result = serializerLocal.readObject(new MonitoredInputStream(inputStream));
            }
        }
        // Notify listeners that the response has been received
        queryListeners.responseReceived(this);
    } catch (IOException exception) {
        queryListeners.failed(this);
        throw new QueryException(exception);
    } catch (SerializationException exception) {
        queryListeners.failed(this);
        throw new QueryException(exception);
    } catch (RuntimeException exception) {
        queryListeners.failed(this);
        throw exception;
    }
    return result;
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) JSONSerializer(org.apache.pivot.json.JSONSerializer) Serializer(org.apache.pivot.serialization.Serializer)

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 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 JSONSerializer (org.apache.pivot.json.JSONSerializer)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 Serializer (org.apache.pivot.serialization.Serializer)1