Package org.xlightweb

Source Code of org.xlightweb.AbstractHttpMessage

/*
*  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 java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;





/**
* Implementation base of a message
*
* @author grro@xlightweb.org
*/
abstract class AbstractHttpMessage implements IHttpMessage {
 
  private final AtomicReference<NonBlockingBodyDataSource> bodyDataSource = new AtomicReference<NonBlockingBodyDataSource>();


  /**
   * {@inheritDoc}
   */
  public abstract IHttpHeader getMessageHeader();


  /**
   * {@inheritDoc}
   */
  public final void setAttribute(String name, Object o) {
    getMessageHeader().setAttribute(name, o);
  }

 
  /**
   * {@inheritDoc}
   */
  public final Object getAttribute(String name) {
    return getMessageHeader().getAttribute(name);
  }
 
 
  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("unchecked")
  public final Enumeration getAttributeNames() {
    return getMessageHeader().getAttributeNames();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public final Set<String> getAttributeNameSet() {
    return getMessageHeader().getAttributeNameSet();
  }
 
 
  /**
   * sets the body data source
   *
   * @param body  the body data source
   */
  final void setBodyDataSource(NonBlockingBodyDataSource body) throws IOException {
    if (body == null) {
      return;
    }
   
   
    setBodyDataSourceSilence(body);

    if (AbstractHttpConnection.isChunkedTransferEncoding(getMessageHeader())) {
      return;
    } else if (getMessageHeader().getContentLength() >= 0) {
      return;
    }

   
    if (body.isComplete()) {
      getMessageHeader().setContentLength(body.available());
     
    } else {
      getMessageHeader().setTransferEncoding("Chunked");
    }
  }
 
  /**
   * sets the body data source
   *
   * @param body  the body data source
   */
  final void setBodyDataSourceSilence(NonBlockingBodyDataSource body) throws IOException {
    bodyDataSource.set(body);
  }
 
 

  /**
   * sets the body data source
   *
   * @param body       the body data source
   * @param encoding   the encoding
   *
   * @return the body length
   *
   * @throws UnsupportedEncodingException  if the given encoding is not supported
   */
  final int setBodyDataSource(String body, String encoding) throws IOException, UnsupportedEncodingException {
    byte[] bytes = body.getBytes(encoding);
    setBodyDataSource(bytes, encoding);
   
    return bytes.length;
  }

 
  /**
   *sets the body data source
   *
   * @param body       the body data source
   */
  final void setBodyDataSource(String body) throws IOException {
    if (body != null) {
      try {
        byte[] bytes = body.getBytes(getMessageHeader().getCharacterEncoding());
        setBodyDataSource(bytes, getMessageHeader().getCharacterEncoding());
      } catch (UnsupportedEncodingException use) {
        throw new RuntimeException(use.toString());
      }
    }
  }

 
  final void setBody(File file) throws IOException {
    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);
  }
 

 
  /**
   * sets the body data source
   *
   * @param body       the body data source
   * @return the body length
   *  
   * @throws UnsupportedEncodingException  if the given encoding is not supported
   */
  final int setBodyDataSource(byte[] body) throws IOException, UnsupportedEncodingException {
    setBodyDataSource(body, null);
    return body.length;
  }
 
  /**
   * sets the body data source
   *
   * @param body       the body data source
   * @param encoding   the encoding
   * @throws UnsupportedEncodingException  if the given encoding is not supported
   */
  final void setBodyDataSource(byte[] body, String encoding) throws IOException, UnsupportedEncodingException {
    setBodyDataSource(new ByteBuffer[] { ByteBuffer.wrap(body)}, encoding);
  }
 
 
  /**
   * sets the body data source
   *
   * @param body       the body data source
   * @param encoding   the encoding
   * @throws UnsupportedEncodingException  if the given encoding is not supported
   */
  final void setBodyDataSource(ByteBuffer[] body, String encoding) throws IOException, UnsupportedEncodingException {
    setBodyDataSource(new NonBlockingBodyDataSource(BodyType.IN_MEMORY, body, encoding));
  }



  /**
   * {@inheritDoc}
   */
  public final NonBlockingBodyDataSource getNonBlockingBody() throws IOException {
    return bodyDataSource.get();
  }

 
  /**
   * {@inheritDoc}
   */
  public final BlockingBodyDataSource getBlockingBody() throws IOException {
    if (bodyDataSource.get() == null) {
      return null;
    }
    return new BlockingBodyDataSource(bodyDataSource.get());
  }

 
  /**
   * returns true if the message has a body
   *
   * @return  true, if the message has a body
   */
  public final boolean hasBody() {
    return bodyDataSource.get() != null;
  }
 

  /**
   * {@inheritDoc}
   */
  public void addHeaderLine(String line) {
    getMessageHeader().addHeaderLine(line);
  }
 
 
  /**
     * {@inheritDoc}
     */
  public void addHeaderlines(String... lines) {
      getMessageHeader().addHeaderlines(lines);
     
  }
 

  /**
   * {@inheritDoc}
   */
  public final void addHeader(String headername, String headervalue) {
    getMessageHeader().addHeader(headername, headervalue);
  }

 
  /**
   * {@inheritDoc}
   */
  public final void setHeader(String headername, String headervalue) {
    getMessageHeader().setHeader(headername, headervalue);
  }

 
  /**
   * {@inheritDoc}
   */
  public final void removeHeader(String headername) {
    getMessageHeader().removeHeader(headername);
  }
 
 

 
  /**
   * {@inheritDoc}
   */
  public final boolean containsHeader(String headername) {
    return getMessageHeader().containsHeader(headername);
  }
 
 
  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("unchecked")
  public final Enumeration getHeaderNames() {
    return getMessageHeader().getHeaderNames();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public final Set<String> getHeaderNameSet() {
    return getMessageHeader().getHeaderNameSet();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public final String getHeader(String headername) {
    return getMessageHeader().getHeader(headername);
  }
 
 
  /**
   * {@inheritDoc}
   */
  public final List<String> getHeaderList(String headername) {
    return getMessageHeader().getHeaderList(headername);
  }
 
  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("unchecked")
  public final Enumeration getHeaders(String headername) {
    return getMessageHeader().getHeaders(headername);
  }

 
  /**
   * {@inheritDoc}
   */
  public void setContentType(String type) {
    getMessageHeader().setContentType(type);
  }

  /**
   * {@inheritDoc}
   */
  public String getContentType() {
    return getMessageHeader().getContentType();
  }

 

  /**
   * {@inheritDoc}
   */
  public String getCharacterEncoding() {
    return getMessageHeader().getCharacterEncoding();
  }
 
 
  /**
   * {@inheritDoc}
   */
  public void setContentLength(int length) {
    getMessageHeader().setContentLength(length);
  }
 
 
  /**
   * {@inheritDoc}
   */
  public int getContentLength() {
    return getMessageHeader().getContentLength();
  }

 
    /**
     * {@inheritDoc}
     */
  public String getDisposition() {
      return getMessageHeader().getDisposition();
  }
 
 
  /**
     * {@inheritDoc}
     */
  public String getDispositionType() {
      return getMessageHeader().getDispositionType();
  }
 
 
  /**
     * {@inheritDoc}
     */
  public String getDispositionParam(String name) {
      return getMessageHeader().getDispositionParam(name);
  }
 

 
  /**
   * {@inheritDoc}
   */
  public void setTransferEncoding(String transferEncoding) {
    getMessageHeader().setTransferEncoding(transferEncoding);
    removeHeader("Content-length");
  }
 
 
  /**
   * {@inheritDoc}
   */
  public String getTransferEncoding() {
    return getMessageHeader().getTransferEncoding();
  }
 
 
  /**
   * gets the body tpye
   *
   * @return  the body type
   * @throws IOException  if an exception occurs
   */
  BodyType getBodyType() throws IOException {
    if (getNonBlockingBody() != null) {
      return getNonBlockingBody().getBodyType();
    } else {
      return BodyType.NO_BODY;
    }
  }
 

 
  /**
     * returns the content type based on the file extension
     *
     * @param file the file
     */
    static final String getContentTypeByFileExtension(File file) {
       
        String name = file.getName();
        int pos = name.lastIndexOf(".");
        if (pos != -1) {
            String extension = name.substring(pos + 1, name.length());
            String mimeType = HttpUtils.getMimeTypeMapping().get(extension);
            if (mimeType != null) {
                return mimeType;
            }
        }
       
        return "application/octet-stream";
    }
   
   
 
  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {
    if (bodyDataSource.get() == null) {
      return getMessageHeader().toString() + "\r\n";
    } else {
      return getMessageHeader().toString() + "\r\n" + bodyDataSource.toString();
    }
  }
}
TOP

Related Classes of org.xlightweb.AbstractHttpMessage

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.