/*********************************************************************
* HttpResponseParser.java
* created on 02.11.2006 by netseeker
* $Id: HttpResponseParser.java,v 1.1 2006/11/06 08:51:38 netseeker Exp $
* $Log: HttpResponseParser.java,v $
* Revision 1.1 2006/11/06 08:51:38 netseeker
* fixed java 1.4 support
* moved HTTP support classes to package de.netseeker.ejoe.http
*
* Revision 1.2 2006/11/05 23:56:27 netseeker
* added support of gzip encoding in case of http if the HTTP header does request it but the header byte did not (usually browser requests)
* added support of persistent http connections
*
* Revision 1.1 2006/11/05 16:30:37 netseeker
* finished the partial HTTP support
*
*
* ====================================================================
*
* Copyright 2005-2006 netseeker aka Michael Manske
*
* 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.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.http;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Parser class for HTTP responses send by EJServer
*
* @author netseeker
* @since 0.3.9.1
*/
public class HttpResponseParser extends HttpHeaderParser
{
private static final Logger logger = Logger.getLogger( HttpResponseParser.class.getName() );
private static Pattern pEncoding = Pattern.compile( ".*^Content-Type:\\s([^ ]+)+$.*", Pattern.CASE_INSENSITIVE
| Pattern.MULTILINE | Pattern.DOTALL );
private int code;
/**
* Creates a new parser using the given ByteBuffer containing HTTP response data
*
* @param buf ByteBuffer containing a HTTP response
*/
public HttpResponseParser(ByteBuffer buf)
{
super( buf );
code = extractCode();
Matcher matcher = pEncoding.matcher( getCharHeader() );
setCompression( (matcher.matches() && (matcher.group( 1 ).indexOf( "gzip" ) > -1)) );
}
/**
* Returns the response status code, eg. 200 OK
*
* @return the code
*/
public int getCode()
{
return code;
}
/**
* Extracts the http status code from the underlying buffer
*
* @return the response status code
*/
protected int extractCode()
{
try
{
String[] tokens = pLineEnd.split( getCharHeader(), 2 );
tokens = tokens[0].split( " " );
return Integer.parseInt( tokens[1].trim() );
}
catch ( Exception e )
{
logger.log( Level.WARNING, "Failed to determine HTTP status code!", e );
}
return -1;
}
}