net.ymate.platform.mvc.web.view.impl.HttpStatusView.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.mvc.web.view.impl.HttpStatusView.java

Source

/*
 * Copyright 2007-2107 the original author or authors.
 *
 * 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 net.ymate.platform.mvc.web.view.impl;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import net.ymate.platform.mvc.web.WebMVC;
import net.ymate.platform.mvc.web.context.WebContext;
import net.ymate.platform.mvc.web.view.AbstractWebView;

/**
 * <p>
 * HttpStatusView
 * </p>
 * <p>
 * HTTP?
 * </p>
 * 
 * @author (suninformation@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">?</th><th width="100px"></th><th
 *          width="100px"></th><th width="100px"></th>
 *          </tr>
 *          <!--  Table ?? -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td></td>
 *          <td></td>
 *          <td>2011-10-29?02:26:25</td>
 *          </tr>
 *          </table>
 */
public class HttpStatusView extends AbstractWebView {

    /**
     * HTTP?
     */
    private final int status;
    private final String msg;
    private final boolean __error;

    private String body;

    /**
     * 
     * @param status HTTP?
     */
    public HttpStatusView(int status) {
        this.status = status;
        this.msg = null;
        this.__error = true;
    }

    /**
     * 
     * @param status HTTP?
     * @param useError ?sendError
     */
    public HttpStatusView(int status, boolean useError) {
        this.status = status;
        this.__error = useError;
        this.msg = null;
    }

    /**
     * 
     * @param status HTTP?
     * @param msg ???
     */
    public HttpStatusView(int status, String msg) {
        this.status = status;
        this.msg = msg;
        this.__error = false;
    }

    /**
     * ??(:useError=false)
     * @param bodyStr
     * @return
     */
    public HttpStatusView writeBody(String bodyStr) {
        this.body = bodyStr;
        return this;
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.mvc.web.view.AbstractWebView#renderView()
     */
    protected void renderView() throws Exception {
        if (StringUtils.isNotBlank(body)) {
            IOUtils.write(body, WebContext.getResponse().getOutputStream(), StringUtils.defaultIfEmpty(
                    WebMVC.getConfig().getCharsetEncoding(), WebContext.getResponse().getCharacterEncoding()));
        }
        if (StringUtils.isNotBlank(msg)) {
            WebContext.getResponse().sendError(status, msg);
        } else {
            if (__error) {
                WebContext.getResponse().sendError(status);
            } else {
                WebContext.getResponse().setStatus(status);
            }
        }
    }

}