All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.graphql.entry.http.HttpServletResponseWriter Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
package io.smallrye.graphql.entry.http;

import java.io.IOException;

import jakarta.json.Json;
import jakarta.json.JsonWriter;
import jakarta.json.JsonWriterFactory;
import jakarta.servlet.http.HttpServletResponse;

import io.smallrye.graphql.execution.ExecutionResponse;
import io.smallrye.graphql.execution.ExecutionResponseWriter;

/**
 * Writing the response to HTTP servlet
 *
 * @author Phillip Kruger ([email protected])
 */
public class HttpServletResponseWriter implements ExecutionResponseWriter {
    private static final String APPLICATION_JSON_UTF8 = "application/json;charset=UTF-8";
    private static final JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(null);

    private final HttpServletResponse response;

    public HttpServletResponseWriter(HttpServletResponse response) {
        this.response = response;
    }

    @Override
    public void write(ExecutionResponse executionResponse) {
        if (executionResponse != null) {
            try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(response.getOutputStream())) {
                response.setContentType(APPLICATION_JSON_UTF8);
                jsonWriter.writeObject(executionResponse.getExecutionResultAsJsonObject());
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy