com.bennavetta.appsite.serve.HttpServletResp.java Source code

Java tutorial

Introduction

Here is the source code for com.bennavetta.appsite.serve.HttpServletResp.java

Source

/**
 * Copyright 2013 Ben Navetta <ben@bennavetta.com>
 *
 * 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.bennavetta.appsite.serve;

import java.nio.charset.Charset;
import java.util.Locale;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;

import com.bennavetta.appsite.processing.Response;
import com.google.common.net.MediaType;

public class HttpServletResp implements Response {
    private HttpServletResponse response;

    private HttpStatus current;

    public HttpServletResp(HttpServletResponse response) {
        super();
        this.response = response;
    }

    @Override
    public HttpStatus getStatus() {
        return current;
    }

    @Override
    public void setStatus(HttpStatus status) {
        response.setStatus(status.value());
        current = status;
    }

    @Override
    public void setCookie(String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

    @Override
    public Charset getCharacterEncoding() {
        return (response.getCharacterEncoding() == null ? Charset.defaultCharset()
                : Charset.forName(response.getCharacterEncoding()));
    }

    @Override
    public void setCharacterEncoding(Charset encoding) {
        response.setCharacterEncoding(encoding.name());
    }

    @Override
    public void setContentLength(int length) {
        response.setContentLength(length);
    }

    @Override
    public MediaType getContentType() {
        return (response.getContentType() == null ? MediaType.ANY_TYPE
                : MediaType.parse(response.getContentType()));
    }

    @Override
    public void setContentType(MediaType type) {
        response.setContentType(type.toString());
    }

    @Override
    public Locale getLocale() {
        return response.getLocale();
    }

    @Override
    public void setLocale(Locale locale) {
        response.setLocale(locale);
    }

}