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

net.craftforge.essential.context.servlet.ServletWebResponse Maven / Gradle / Ivy

/*
 * This file is part of essential.
 *
 *     essential is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     essential is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with essential.  If not, see .
 */

package net.craftforge.essential.context.servlet;

import net.craftforge.essential.context.Response;
import net.craftforge.essential.core.constants.HttpStatusCode;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * Implementation of a response for servlets.
 * Uses the {@link HttpServletResponse} as target of
 * response operations.
 *
 * @author Christian Bick
 * @since 26.12.11
 */
public class ServletWebResponse implements Response {

    private HttpStatusCode httpStatusCode;
    private Map headers;
    private HttpServletResponse response;

    @Inject
    public ServletWebResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * {@inheritDoc}
     */
    public void sendHeaders(HttpStatusCode httpStatusCode, Map headers) {
        this.httpStatusCode = httpStatusCode;
        this.headers = headers;
        response.setStatus(httpStatusCode.getCode());
        for (String headerName : headers.keySet()) {
            for (String headerValue : headers.get(headerName)) {
                response.addHeader(headerName, headerValue);
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    public HttpStatusCode getStatusCode() {
        return httpStatusCode;
    }

    /**
     * {@inheritDoc}
     */
    public Map getHeaders() {
        return headers;
    }

    /**
     * {@inheritDoc}
     */
    public OutputStream getBodyOutputStream() {
        try {
            return response.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException("Unable to resolve response body output stream", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy