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

org.bridje.http.impl.HttpBridletResponseImpl Maven / Gradle / Ivy

/*
 * Copyright 2016 Bridje Framework.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bridje.http.impl;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.bridje.http.HttpBridletResponse;
import org.bridje.http.HttpCookie;

/**
 *
 */
class HttpBridletResponseImpl implements HttpBridletResponse
{
    private final ByteBuf buffer;

    private final OutputStream out;

    private String contentType = "text/html; charset=UTF-8";

    private int statusCode = 200;

    private final Map headers;

    private Map cookies;

    public HttpBridletResponseImpl(ByteBuf buffer)
    {
        this.buffer = buffer;
        this.buffer.retain();
        out = new ByteBufOutputStream(buffer);
        this.headers = new LinkedHashMap<>();
    }

    @Override
    public OutputStream getOutputStream()
    {
        return out;
    }

    public ByteBuf getBuffer()
    {
        return buffer;
    }

    protected void close() throws IOException
    {
        out.flush();
        out.close();
    }

    @Override
    public String getContentType()
    {
        return contentType;
    }

    @Override
    public void setContentType(String contentType)
    {
        this.contentType = contentType;
    }

    @Override
    public int getStatusCode()
    {
        return statusCode;
    }

    @Override
    public void setStatusCode(int statusCode)
    {
        this.statusCode = statusCode;
    }

    @Override
    public void setHeader(String name, Object value)
    {
        this.headers.put(name, value);
    }

    public Map getHeadersMap()
    {
        return headers;
    }
    
    protected void release()
    {
        this.buffer.release();
    }
    
    protected Map getCookies()
    {
        return cookies;
    }

    @Override
    public HttpCookie addCookie(String name, String value)
    {
        if(cookies == null)
        {
            cookies = new HashMap<>();
        }
        HttpCookieImpl c = new HttpCookieImpl(new DefaultCookie(name, value));
        cookies.put(c.getName(), c);
        return c;
    }

    protected boolean hasCookie(String name)
    {
        if(cookies != null)
        {
            return cookies.containsKey(name);
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy