001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.requests;
019
020import javax.servlet.ServletOutputStream;
021import javax.servlet.http.Cookie;
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024import java.io.IOException;
025import java.io.PrintWriter;
026
027import static io.stallion.utils.Literals.empty;
028
029public class StResponse {
030    private HttpServletResponse response;
031    private MetaInformation meta = new MetaInformation();
032    private PerPageLiterals pageFooterLiterals = new PerPageLiterals();
033    private PerPageLiterals pageHeadLiterals = new PerPageLiterals();
034    private SandboxedResponse sandboxedResponse;
035    private String contentType = "";
036
037
038    public StResponse() {
039
040    }
041
042    public StResponse(HttpServletResponse response) {
043        this.response = response;
044    }
045
046
047    public HttpServletResponse getHttpServletResponse() {
048        return this.response;
049    }
050
051
052    public StResponse setDefaultContentType(String contentType) {
053        response.setContentType(contentType);
054        return this;
055    }
056
057    public boolean isContentTypeSet() {
058        return !empty(contentType);
059    }
060
061    public StResponse setContentType(String contentType) {
062        this.contentType = contentType;
063        response.setContentType(contentType);
064        return this;
065    }
066
067    public StResponse setStatus(int sc) {
068        response.setStatus(sc);
069        return this;
070    }
071
072    public int getStatus() {
073        return response.getStatus();
074    }
075
076    public PrintWriter getWriter() throws IOException {
077        return response.getWriter();
078    }
079
080    public StResponse addHeader(String name, String value) {
081        response.addHeader(name, value);
082        return this;
083    }
084
085    public ServletOutputStream getOutputStream() throws IOException{
086        return response.getOutputStream();
087    }
088
089    public StResponse setDateHeader(String name, Long value) {
090        response.setDateHeader(name, value);
091        return this;
092    }
093
094    public void setContentLength(int length) {
095        response.setContentLength(length);
096    }
097
098    public String getHeader(String name) {
099        return response.getHeader(name);
100    }
101
102    public String getContentType() {
103        return getHeader("Content-type");
104    }
105
106    public Cookie addCookie(String name, String value) {
107        Cookie cookie = new Cookie(name, value);
108        cookie.setPath("/");
109        addCookie(cookie);
110        return cookie;
111    }
112
113    public Cookie addCookie(String name, String value, int maxAge) {
114        Cookie cookie = new Cookie(name, value);
115        cookie.setMaxAge(maxAge);
116        cookie.setPath("/");
117        addCookie(cookie);
118        return cookie;
119    }
120
121    public Cookie addCookie(Cookie cookie) {
122        response.addCookie(cookie);
123        return cookie;
124    }
125
126    public MetaInformation getMeta() {
127        return meta;
128    }
129
130    public void setMeta(MetaInformation meta) {
131        this.meta = meta;
132    }
133
134
135    public PerPageLiterals getPageFooterLiterals() {
136        return pageFooterLiterals;
137    }
138
139    public PerPageLiterals getPageHeadLiterals() {
140        return pageHeadLiterals;
141    }
142
143
144    public SandboxedResponse getSandboxedResponse() {
145        if (sandboxedResponse == null) {
146            sandboxedResponse = new SandboxedResponse(this);
147        }
148        return sandboxedResponse;
149    }
150}