Package org.xlightweb

Source Code of org.xlightweb.HttpResponse

/*
*  Copyright (c) xlightweb.org, 2008 - 2009. All rights reserved.
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


import org.xsocket.DataConverter;





/**
* http response
*
* @author grro@xlightweb.org
*/
public class HttpResponse extends AbstractHttpMessage implements IHttpResponse {
 
  private final IHttpResponseHeader responseHeader;

 

  /**
   * constructor
   *
   * @param responseHeader   the response header
   */
  public HttpResponse(IHttpResponseHeader responseHeader) {
    this.responseHeader = responseHeader;
 
 
 
 
  /**
   * constructor
   *
   * @param responseHeader      the response header
   * @param bodyDataSource      the response body
   *
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(IHttpResponseHeader responseHeader, NonBlockingBodyDataSource bodyDataSource) throws IOException {
    this.responseHeader = responseHeader;
    setBodyDataSource(bodyDataSource);
 

 
  /**
   * constructor
   *
   * @param responseHeader      the response header
   * @param bodyDataSource      the response body
   *
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(String contentType, NonBlockingBodyDataSource bodyDataSource) throws IOException {
    responseHeader = new HttpResponseHeader(200, contentType);
    setBodyDataSource(bodyDataSource);
 

 
  /**
   * constructor. The status will be set to 200
   *
   * @param responseHeader      the response header
   * @param bodyDataSource      the response body
   *
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(String contentType, BlockingBodyDataSource bodyDataSource) throws IOException {
    responseHeader = new HttpResponseHeader(200, contentType);
    setBodyDataSource(bodyDataSource.getUnderliyingBodyDataSource());
 
 

  /**
   * constructor. The status will be set to 200
   *
   * @param contentType    the content type
   * @param body           the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(String contentType, String body) throws IOException {
    this(200, contentType, body);
 

 
 
  /**
   * constructor. The status will be set to 200
   *
   * @param body           the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(String body) throws IOException {
    this(new HttpResponseHeader(200), body);
 


  /**
   * constructor. The status will be set to 200
   *
   * @param file           the file
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(File file) throws IOException {
    this(new HttpResponseHeader(200), new NonBlockingBodyDataSource(BodyType.IN_MEMORY, AbstractHeader.DEFAULT_ENCODING));

    setContentType(getContentTypeByFileExtension(file));
    setContentLength((int) file.length());
    removeHeader("Transfer-Encoding");
   
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    FileChannel fc = raf.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(getContentLength());
    fc.read(buffer);
    fc.close();
    raf.close();
    buffer.flip();
   
    getNonBlockingBody().append(true, buffer);
    getNonBlockingBody().setComplete(true);
 
 

  
  /**
   * constructor
   *
   * @param status       the status
   */
  public HttpResponse(int status)  {
    responseHeader = new HttpResponseHeader(status);
 

 
 
  /**
   * constructor
   *
   * @param status       the status
   * @param contentType  the content type
   * @param body         the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String contentType, String body) throws IOException {
    this(status, contentType, new ByteBuffer[] { DataConverter.toByteBuffer(body, "UTF-8") }, "UTF-8");
 


  /**
   * constructor
   *
   * @param status       the status
   * @param body         the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String body) throws IOException {
    this(new HttpResponseHeader(status), new ByteBuffer[] { DataConverter.toByteBuffer(body, AbstractHeader.DEFAULT_ENCODING)});
 

  /**
   * constructor. The status will be set to 200
   * 
   * @param contentType    the content type
   * @param body           the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(String contentType, byte[] body) throws IOException {
    this(200, contentType, body);
 



  /**
   * constructor
   *
   * @param status         the status
   * @param contentType    the content type
   * @param contentLength  the content length
   * @param body           the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String contentType, int contentLength, NonBlockingBodyDataSource body) throws IOException {
    responseHeader = new HttpResponseHeader(status, contentType);
    responseHeader.setContentLength(contentLength);
   
    setBodyDataSource(body);   
 
 
 
 
  /**
   * constructor
   *
   * @param status         the status
   * @param contentType    the content type
   * @param contentLength  the content length
   * @param body           the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String contentType, int contentLength, BlockingBodyDataSource body) throws IOException {
    responseHeader = new HttpResponseHeader(status, contentType);
    responseHeader.setContentLength(contentLength);
   
    setBodyDataSource(body.getUnderliyingBodyDataSource());   
 

 
 
  /**
   * constructor
   *
   * @param status         the status
   * @param contentType    the content type
   * @param body           the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String contentType, NonBlockingBodyDataSource body) throws IOException {
    if (body == null) {
      throw new NullPointerException("body has to be set");
    }
    responseHeader = new HttpResponseHeader(status, contentType);
    setBodyDataSource(body);   
 
 
 
  /**
   * constructor
   *
   * @param status         the status
   * @param contentType    the content type
   * @param body           the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String contentType, BlockingBodyDataSource body) throws IOException {
    responseHeader = new HttpResponseHeader(status, contentType);
    setBodyDataSource(body.getUnderliyingBodyDataSource());   
 
 
 
 
  /**
   * constructor
   *
   * @param status       the status
   * @param contentType  the content type
   * @param body         the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String contentType, byte[] body) throws IOException {
    responseHeader = new HttpResponseHeader(status, contentType);
    responseHeader.setContentLength(body.length);
   
    setBodyDataSource(body, responseHeader.getCharacterEncoding());
 

 

  /**
   * constructor. The status will be set to 200
   *
   * @param contentType    the content type
   * @param body           the body
   * @throws IOException if an io exception occurs 
   */
  public HttpResponse(String contentType, ByteBuffer[] body) throws IOException {
    this(200, contentType, body);
 
 
 
  /**
   * constructor
   *
   * @param status       the status
   * @param contentType  the content type
   * @param body         the body
   * @throws IOException if an io exception occurs
   */
  public HttpResponse(int status, String contentType, ByteBuffer[] body) throws IOException {
    this(status, contentType, body, "UTF-8");     
 

 
  /**
   * constructor
   *
   *
   * @param status        the status
   * @param contentType   the content type
   * @param body          the body
   * @param encoding      the encoding
   * @throws IOException if an io exception occurs
   */
  HttpResponse(int status, String contentType, ByteBuffer[] body, String encoding) throws IOException {
    responseHeader = new HttpResponseHeader(status, contentType + "; charset=" + encoding);
   
    try {
      setBodyDataSource(body, encoding);
    } catch (UnsupportedEncodingException use) {
      throw new RuntimeException(use.toString());
    }
   
    responseHeader.setContentLength(getNonBlockingBody().available());
 
 
   
  /**
   * constructor
   *
   * @param responseHeader   the response header
   * @param body             the body
   * @throws IOException if an io exception occurs 
   */
  public HttpResponse(IHttpResponseHeader responseHeader, String body) throws IOException {   
    this.responseHeader = responseHeader;
   
    try {
      setBodyDataSource(body, responseHeader.getCharacterEncoding());
    } catch (UnsupportedEncodingException use) {
      throw new RuntimeException(use.toString());
    }
  }

 
  /**
   * constructor
   *
   * @param responseHeader   the response header
   * @param body             the body
   * @throws IOException if an io exception occurs 
   */
  public HttpResponse(IHttpResponseHeader responseHeader, byte[] body) throws IOException {
    this.responseHeader = responseHeader;
   
    try {
      setBodyDataSource(body, responseHeader.getCharacterEncoding());
    } catch (UnsupportedEncodingException use) {
      throw new RuntimeException(use.toString());
    }     
  }
 
 
  /**
   * constructor
   *
   * @param responseHeader   the response header
   * @param body             the body
   * @throws IOException if an io exception occurs 
   */
  public HttpResponse(IHttpResponseHeader responseHeader, ByteBuffer[] body) throws IOException {
    this.responseHeader = responseHeader;
   
    try {
      setBodyDataSource(body, responseHeader.getCharacterEncoding());
    } catch (UnsupportedEncodingException use) {
      throw new RuntimeException(use.toString());
    }     
  }

     
  /**
   * {@inheritDoc}
   */
  public final int getStatus() {
    return responseHeader.getStatus();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public final void setStatus(int status) {
    responseHeader.setStatus(status);
  }
 
 
  /**
   * {@inheritDoc}
   */
  public String getServer() {
    return responseHeader.getServer();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public void setServer(String server) {
    responseHeader.setServer(server);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public void setDate(String date) {
    responseHeader.setDate(date);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public String getDate() {
    return responseHeader.getDate();
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public final String getReason() {
    return responseHeader.getReason();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public void setReason(String reason) {
    responseHeader.setReason(reason);
  }
 
 
  /**
   * {@inheritDoc}
   */
  public String getProtocol() {
    return responseHeader.getProtocol();
  }

 
  /**
   * {@inheritDoc}
   */
  public String getProtocolVersion() {
    return responseHeader.getProtocolVersion();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public final void setProtocol(String protocol) {
    responseHeader.setProtocol(protocol);
  }
 

  /**
   * returns the response header
   * 
   * @return the response header
   */
  public final IHttpResponseHeader getResponseHeader() {
    return responseHeader;
  }
 
 
  /**
   * {@inheritDoc}
   */
  public IHttpHeader getMessageHeader() {
    return getResponseHeader();
  }
}
TOP

Related Classes of org.xlightweb.HttpResponse

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.