org.xsocket.connection.http.AbstractMessage Maven / Gradle / Ivy
/*
* Copyright (c) xsocket.org, 2006 - 2008. 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.xsocket.org/
*/
package org.xsocket.connection.http;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
/**
* Implementation base for the messages
*
* @author [email protected]
*/
abstract class AbstractMessage implements IMessage {
private NonBlockingBodyDataSource bodyDataSource = null;
abstract AbstractMessageHeader getMessageHeader();
/**
* sets the body data source
*
* @param body the body data source
*/
final void setBodyDataSource(NonBlockingBodyDataSource body) {
this.bodyDataSource = body;
}
/**
* sets the body data source
*
* @param body the body data source
* @param encoding the encoding
* @throws UnsupportedEncodingException if the given encoding is not supported
*/
public final void setBodyDataSource(String body, String encoding) throws UnsupportedEncodingException {
byte[] bytes = body.getBytes(encoding);
setBodyDataSource(bytes, encoding);
}
/**
* sets the body data source
*
* @param body the body data source
*/
public final void setBodyDataSource(String body) {
if (body != null) {
try {
byte[] bytes = body.getBytes(getMessageHeader().getCharacterEncoding());
setBodyDataSource(bytes, getMessageHeader().getCharacterEncoding());
} catch (UnsupportedEncodingException use) {
throw new RuntimeException(use.toString());
}
}
}
/**
* sets the body data source
*
* @param body the body data source
* @param encoding the encoding
* @throws UnsupportedEncodingException if the given encoding is not supported
*/
public final void setBodyDataSource(byte[] body, String encoding) throws 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
*/
public final void setBodyDataSource(ByteBuffer[] body, String encoding) throws UnsupportedEncodingException {
setBodyDataSource(new NonBlockingBodyDataSource(body, encoding));
if (getMessageHeader().getContentType() != null) {
String contentType = getMessageHeader().getContentType();
String headerDefinedEncoding = getMessageHeader().getCharacterEncoding();
if (!headerDefinedEncoding.equalsIgnoreCase(encoding)) {
int pos = contentType.indexOf(";");
getMessageHeader().setContentType(contentType.substring(0, pos) + "; charset=" + encoding);
}
} else {
getMessageHeader().setContentType("text/plain; charset=" + encoding);
}
if (!AbstractMessageHeader.isChunked(getMessageHeader())) {
getMessageHeader().setContentLength(getNonBlockingBody().available());
}
}
/**
* returns the non-blocking body
*
* @return the body or null
if message is bodyless
*/
public final NonBlockingBodyDataSource getNonBlockingBody() {
return bodyDataSource;
}
/**
* returns the blocking body
*
* @return the body or null
if message is bodyless
*/
public final BlockingBodyDataSource getBlockingBody() {
if (bodyDataSource == null) {
return null;
}
return new BlockingBodyDataSource(bodyDataSource);
}
/**
* returns true if the message has a body
*
* @return true, if the message has a body
*/
public final boolean hasBody() {
return bodyDataSource != null;
}
/**
* adds a raw header line
*
* @param line the headerline
*/
public void addHeaderLine(String line) {
getMessageHeader().addHeaderLine(line);
}
/**
* {@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 void removeHopByHopHeaders() {
getMessageHeader().removeHopByHopHeaders();
}
/**
* {@inheritDoc}
*/
public void removeHopByHopHeaders(String... headernames) {
getMessageHeader().removeHopByHopHeaders(headernames);
}
/**
* {@inheritDoc}
*/
public final boolean containsHeader(String headername) {
return getMessageHeader().containsHeader(headername);
}
/**
* {@inheritDoc}
*/
public final boolean containsHeaderValue(String headername, String headervalue) {
return getMessageHeader().containsHeaderValue(headername, headervalue);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public final Enumeration getHeaderNames() {
return getMessageHeader().getHeaderNames();
}
/**
* {@inheritDoc}
*/
public final Set getHeaderNameSet() {
return getMessageHeader().getHeaderNameSet();
}
/**
* {@inheritDoc}
*/
public final String getHeader(String headername) {
return getMessageHeader().getHeader(headername);
}
/**
* {@inheritDoc}
*/
public final List 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 void setTransferEncoding(String transferEncoding) {
getMessageHeader().setTransferEncoding(transferEncoding);
removeHeader("Content-length");
}
/**
* {@inheritDoc}
*/
public String getTransferEncoding() {
return getMessageHeader().getTransferEncoding();
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
if (bodyDataSource == null) {
return getMessageHeader().toString();
} else {
return getMessageHeader().toString() + bodyDataSource.toString();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy