Ila

org.topoweb.ila
Class HttpStateBuffer

java.lang.Object
  |
  +--org.topoweb.ila.StateBuffer
        |
        +--org.topoweb.ila.HttpStateBuffer
All Implemented Interfaces:
NodeConstants, StateConstants

public class HttpStateBuffer
extends StateBuffer

Creates and transmutes immutable State and HttpState instances. Depending on the State this instance represents, the toState method of this class returns either a State instance or an instance of its direct subclass, an HttpState instance.

Implementation Notes

This class is not safe under concurrent access. Exceptions thrown by methods of this class are thrown in a fail-fast manner: if an exception is thrown (typically a java.lang.IllegalArgumentException instance), the state of the buffer remains unchanged.

Author:
Babak Farhang
See Also:
HttpState, StateBuffer, Node.getState()

Field Summary
 
Fields inherited from interface org.topoweb.ila.StateConstants
NF, NK, NV, OK
 
Fields inherited from interface org.topoweb.ila.NodeConstants
BACK, FIRST_PAGE, FORE, LAST_PAGE, LINK_MASK, PAGE_MASK
 
Constructor Summary
HttpStateBuffer()
           
 
Method Summary
 StateBuffer clear()
          Clears the buffer.
 int getContentLength()
          Returns the content-length, or zero if not set.
 java.lang.String getContentType()
          Returns the content-type, or null if not set.
 int getHttpStatus()
          Returns the HTTP status code, or zero if not set.
 java.lang.String getUserAgent()
          Returns the user-agent, or null if not set.
 StateBuffer load(HttpState state)
          Loads the given HttpState) instance.
 StateBuffer load(State state)
          Loads the given state object.
 StateBuffer setNotFound()
          Should not be invoked directly.
 HttpStateBuffer setNotFound4xx(int httpStatus, java.lang.String userAgent)
          Sets the state to that of an HTTP 4xx status code.
 StateBuffer setOkay(int linkCount, int coLinkCount)
          Should not be invoked directly.
 HttpStateBuffer setOkay2xx(int httpStatus, java.lang.String contentType, int contentLength, long lastModified, long expiration, java.lang.String title, java.lang.String userAgent, int linkCount, int coLinkCount)
          Sets the state to that of an HTTP 2xx status code.
 HttpStateBuffer setOkay3xx(int httpStatus, java.lang.String userAgent, boolean coLink)
          Sets the state to that of an HTTP 3xx status code.
 HttpState toHttpState()
          Returns this an HttpState representation of this instance.
 State toState()
          Returns the state of this buffer as an immutable State or HttpState instance.
 
Methods inherited from class org.topoweb.ila.StateBuffer
addBacklink, addCoBacklink, bumpSyncVersion, bumpVersion, getCoLinkCount, getLinkCount, getStatus, getSyncVersionId, getSyncVersionTimestamp, getVersionId, getVersionTimestamp, initCoLinked, initLinked, removeBacklink, removeCoBacklink
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpStateBuffer

public HttpStateBuffer()
Method Detail

load

public StateBuffer load(State state)
Loads the given state object. After invocation, the fields of this buffer instance assume the corresponding values of the passed in state. If the state object passed in is an instance of HttpState, then the fields corresponding to that of an HttpState instance are also set; otherwise, they are cleared.

Overrides:
load in class StateBuffer
Parameters:
state - the object used to set the fields of this buffer
Returns:
this instance
See Also:
getHttpStatus(), getContentType(), getContentLength(), getUserAgent()

load

public StateBuffer load(HttpState state)
Loads the given HttpState) instance.

Returns:
this instance
See Also:
load(State)

toState

public State toState()
Returns the state of this buffer as an immutable State or HttpState instance. Specifically, if the instance has an HTTP status code (getHttpStatus() > 0), then this method returns a HttpState instance; otherwise a State instance is returned.

Overrides:
toState in class StateBuffer
Returns:
an immutable State instance
See Also:
toHttpState()

toHttpState

public HttpState toHttpState()
                      throws java.lang.IllegalStateException
Returns this an HttpState representation of this instance. The instance must be in either of the OK or NF states with an HTTP status code. This method is here so that a user can avoid a class cast when it is known that the toState method will return an HttpState instance.

Returns:
an HttpState representation of this instance
Throws:
java.lang.IllegalStateException - if this instance is not in either of the OK or NF states
See Also:
toState()

setOkay2xx

public HttpStateBuffer setOkay2xx(int httpStatus,
                                  java.lang.String contentType,
                                  int contentLength,
                                  long lastModified,
                                  long expiration,
                                  java.lang.String title,
                                  java.lang.String userAgent,
                                  int linkCount,
                                  int coLinkCount)
                           throws java.lang.IllegalArgumentException
Sets the state to that of an HTTP 2xx status code. Only 200 and 203 are considered valid status codes. The instance is in the OK state when the method returns.

Parameters:
httpStatus - the HTTP response code (200 or 203)
contentType - the content-type (not null)
contentLength - the content-length (>= 0)
userAgent - the user-agent (not null)
linkCount - the total fore link count
coLinkCount - the co-link count (<= linkCount)
Returns:
this instance
Throws:
java.lang.IllegalArgumentException - if any of the arguments voilate the constraints specified above

setOkay3xx

public HttpStateBuffer setOkay3xx(int httpStatus,
                                  java.lang.String userAgent,
                                  boolean coLink)
                           throws java.lang.IllegalArgumentException
Sets the state to that of an HTTP 3xx status code. Only 301 and 302 and 307 are considered valid status codes. A link count of 1 (to the redirect URL) is implied. The instance is in the OK state when the method returns.

Parameters:
httpStatus - the HTTP response code (301, 302, or 307)
userAgent - the user-agent (not null)
coLink - true, if the redirect URL is considered a co-link (e.g. from the same host); false, o.w.
Returns:
this instance
Throws:
java.lang.IllegalArgumentException - if any of the arguments voilate the constraints specified above

setNotFound4xx

public HttpStateBuffer setNotFound4xx(int httpStatus,
                                      java.lang.String userAgent)
                               throws java.lang.IllegalArgumentException
Sets the state to that of an HTTP 4xx status code. Only 400-405, 410 and 414 are considered valid status codes. The instance is either in the NF or NK state when this method returns. (Recall that if the total of fore- and back links is zero, then the node is elligible for removal and is in the NK state.)

Parameters:
httpStatus - the HTTP response code (400-405, 410, or 414)
userAgent - the user-agent (not null)
Returns:
this instance
Throws:
java.lang.IllegalArgumentException - if any of the arguments voilate the constraints specified above

setOkay

public StateBuffer setOkay(int linkCount,
                           int coLinkCount)
                    throws java.lang.UnsupportedOperationException
Should not be invoked directly.

Overrides:
setOkay in class StateBuffer
Parameters:
linkCount - the total forelink count
coLinkCount - the co-forelink count
Returns:
this instance
Throws:
java.lang.UnsupportedOperationException - if invoked
See Also:
setOkay2xx(int,String,int,long,long,String,String,int,int), setOkay3xx(int,String,boolean)

setNotFound

public StateBuffer setNotFound()
                        throws java.lang.UnsupportedOperationException
Should not be invoked directly.

Overrides:
setNotFound in class StateBuffer
Returns:
this instance
Throws:
java.lang.UnsupportedOperationException - if invoked
See Also:
setNotFound4xx(int,String)

clear

public StateBuffer clear()
Description copied from class: StateBuffer
Clears the buffer. The status is set to NK, and all the link counts and version data are set to zero.

Overrides:
clear in class StateBuffer
Returns:
this instance

getHttpStatus

public int getHttpStatus()
Returns the HTTP status code, or zero if not set. If the value returned is non-zero, then the instance is in either of the NF or OK states; specifically if it is 2xx or 3xx class status code, then we are in the OK state; otherwise (4xx code), we are in the NF state.


getContentType

public java.lang.String getContentType()
Returns the content-type, or null if not set. If the value returned is not null, then the instance is in the OK state.


getContentLength

public int getContentLength()
Returns the content-length, or zero if not set. If the value returned is non-zero, then the instance is in the OK state.


getUserAgent

public java.lang.String getUserAgent()
Returns the user-agent, or null if not set. If the value returned is not null, then we can only guarantee that the instance is not in the NV state.


Ila

Copyright (C) 2002 Babak Farhang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program 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 General Public License for more details.