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

com.threewks.thundr.request.servlet.ServletResponse Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://3wks.github.io/thundr/
 * Copyright (C) 2015 3wks, 
 *
 * 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 com.threewks.thundr.request.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.threewks.thundr.exception.BaseException;
import com.threewks.thundr.http.ContentType;
import com.threewks.thundr.http.Cookie;
import com.threewks.thundr.http.StatusCode;
import com.threewks.thundr.request.BaseResponse;
import com.threewks.thundr.request.Response;
import com.threewks.thundr.transformer.TransformerManager;

public class ServletResponse extends BaseResponse implements Response {
	protected HttpServletResponse resp;
	protected List cookies = new ArrayList<>();
	protected Map> headers = new LinkedHashMap<>();
	protected StatusCode statusCode = StatusCode.OK;
	protected String statusMessage = null;
	private Long contentLength;

	public ServletResponse(HttpServletResponse resp, TransformerManager transformerManager) {
		super(transformerManager);
		this.resp = resp;
	}

	@Override
	public boolean isCommitted() {
		return resp.isCommitted();
	}

	@Override
	public OutputStream getOutputStream() throws IOException {
		return resp.getOutputStream();
	}

	@Override
	public Response withBody(String body) {
		try {
			resp.getWriter().write(body);
		} catch (IOException e) {
			throw new BaseException(e);
		}
		return this;
	}

	/*
	 * @Override
	 * public Response withContent(InputStream inputStream) throws IOException {
	 * ServletOutputStream outputStream = resp.getOutputStream();
	 * Streams.copy(inputStream, outputStream);
	 * outputStream.flush();
	 * return this;
	 * }
	 */

	@Override
	protected Object getRawResponse() {
		return resp;
	}

	@Override
	protected void setHeaderInternal(String key, List values) {
		boolean first = true;
		for (String value : values) {
			if (first) {
				resp.setHeader(key, value);
			} else {
				resp.addHeader(key, value);
			}
			first = false;
		}
		this.headers.put(key, values);
	}

	@Override
	public Response withStatusCode(StatusCode statusCode) {
		this.statusCode = statusCode;
		return this;
	}

	@Override
	public Response withStatusMessage(String message) {
		this.statusMessage = message;
		return this;
	}

	@Override
	public Response withContentType(String contentType) {
		resp.setContentType(contentType);
		return this;
	}

	@Override
	public Response withCookie(Cookie cookie) {
		this.cookies.add(cookie);
		resp.addCookie(ServletSupport.toServletCookie(cookie));
		return this;
	}

	@Override
	public Response withCookies(Collection cookies) {
		this.cookies.addAll(cookies);
		for (Cookie cookie : cookies) {
			resp.addCookie(ServletSupport.ToServletCookie.from(cookie));
		}
		return this;
	}

	@Override
	public Response withCharacterEncoding(String characterEncoding) {
		resp.setCharacterEncoding(characterEncoding);
		return this;
	}

	@Override
	public Response withContentLength(long length) {
		this.contentLength = length;
		resp.setContentLength((int) length);
		return this;
	}

	@Override
	public List getHeaders(String name) {
		return Collections.unmodifiableList(this.headers.get(name));
	}

	@Override
	public Map> getAllHeaders() {
		return Collections.unmodifiableMap(this.headers);
	}

	@Override
	public Cookie getCookie(String name) {
		for (Cookie cookie : this.cookies) {
			if (cookie.getName().equals(name)) {
				return cookie;
			}
		}
		return null;
	}

	@Override
	public List getAllCookies() {
		return Collections.unmodifiableList(this.cookies);
	}

	@Override
	public Response finaliseHeaders() {
		resp.setStatus(statusCode.getCode());
		return this;
	}

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

	@Override
	public ContentType getContentType() {
		return ContentType.from(getContentTypeString());
	}

	@Override
	public String getContentTypeString() {
		return resp.getContentType();
	}

	@Override
	public List getCookies(String name) {
		return Collections.unmodifiableList(cookies);
	}

	@Override
	public String getCharacterEncoding() {
		return resp.getCharacterEncoding();
	}

	@Override
	public Long getContentLength() {
		return contentLength;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy