com.planet_ink.coffee_mud.Common
Class DefaultHttpClient

java.lang.Object
  extended by com.planet_ink.coffee_mud.Common.DefaultHttpClient
All Implemented Interfaces:
CMCommon, HttpClient, CMObject, Tickable, java.lang.Cloneable, java.lang.Comparable<CMObject>

public class DefaultHttpClient
extends java.lang.Object
implements HttpClient, java.lang.Cloneable


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.planet_ink.coffee_mud.Common.interfaces.HttpClient
HttpClient.Method
 
Field Summary
protected  int connectTimeout
           
protected  java.io.InputStream in
           
protected  int maxReadBytes
           
protected  HttpClient.Method meth
           
protected  java.io.OutputStream out
           
protected  byte[] outBody
           
protected  int readTimeout
           
protected  java.util.Map<java.lang.String,java.lang.String> reqHeaders
           
protected  java.util.Map<java.lang.String,java.util.List<java.lang.String>> respHeaders
           
protected  java.lang.Integer respStatus
           
protected  java.net.Socket sock
           
 
Fields inherited from interface com.planet_ink.coffee_mud.core.interfaces.Tickable
STATUS_AFFECT, STATUS_ALIVE, STATUS_BEHAVIOR, STATUS_CLASS, STATUS_DEAD, STATUS_END, STATUS_FIGHT, STATUS_MISC, STATUS_MISC2, STATUS_MISC3, STATUS_MISC4, STATUS_MISC5, STATUS_MISC6, STATUS_MISC7, STATUS_NOT, STATUS_OTHER, STATUS_RACE, STATUS_REBIRTH, STATUS_SCRIPT, STATUS_START, STATUS_WEATHER, TICKID_AREA, TICKID_BEAMWEAPON, TICKID_CLAN, TICKID_CLANITEM, TICKID_DEADBODY_DECAY, TICKID_ELECTRONICS, TICKID_EMAIL, TICKID_EXIT_BEHAVIOR, TICKID_EXIT_REOPEN, TICKID_ITEM_BEHAVIOR, TICKID_ITEM_BOUNCEBACK, TICKID_LIGHT_FLICKERS, TICKID_LIVEAUCTION, TICKID_LONGERMASK, TICKID_MISCELLANEOUS, TICKID_MOB, TICKID_PROPERTY_SPECIAL, TICKID_QUEST, TICKID_READYTOSTOP, TICKID_ROOM_BEHAVIOR, TICKID_ROOM_ITEM_REJUV, TICKID_SOLITARYMASK, TICKID_SPECIALCOMBAT, TICKID_SPECIALMANEUVER, TICKID_SPELL_AFFECT, TICKID_SUPPORT, TICKID_TIMEAUCTION, TICKID_TRAP_DESTRUCTION, TICKID_TRAP_RESET
 
Constructor Summary
DefaultHttpClient()
           
 
Method Summary
 HttpClient body(byte[] body)
          An http request builder method that sets the body to send.
 HttpClient body(java.lang.String body)
          An http request builder method that sets the body to send.
 int compareTo(CMObject o)
           
protected  void conditionalHeader(java.lang.String key, java.lang.String value, java.util.List<java.lang.String> clearSet)
           
 HttpClient connectTimeout(int ms)
          An http request builder method that sets the maximum number of milliseconds that the reader will remain idle waiting for a connection to occur.
 CMObject copyOf()
          Similar to Cloneable.clone(), but does its best to make sure that any internal objects to this class are also copyOfed.
 HttpClient doGet(java.lang.String url)
          An http request builder method that causes this request to occur as a GET
 HttpClient doHead(java.lang.String url)
          An http request builder method that causes this request to occur as a HEAD
 HttpClient doRequest(java.lang.String url)
          An http request builder method that causes this request to occur.
 void finished()
          Closes this connection, entirely.
 java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeaders(java.lang.String urlStr)
          Calls GET on the given url, waiting no more than a few seconds for connection, and returns the headers from the response.
 byte[] getRawUrl(java.lang.String urlStr)
          Reads the simple raw return content from a given url and returns it as a byte array.
 byte[] getRawUrl(java.lang.String urlStr, int maxLength, int readTimeout)
          Reads the simple raw return content from a given url and returns it as a byte array.
 byte[] getRawUrl(java.lang.String urlStr, java.lang.String cookieStr)
          Reads the simple raw return content from a given url and returns it as a byte array.
 byte[] getRawUrl(java.lang.String urlStr, java.lang.String cookieStr, int maxLength, int readTimeout)
          Reads the simple raw return content from a given url and returns it as a byte array.
 java.io.InputStream getResponseBody()
          Returns an input stream to the body of the response to this request.
 int getResponseCode()
          Returns the http status code from the response
 int getResponseContentLength()
          Gets the length of the body of the response to this request.
 java.util.Map<java.lang.String,java.util.List<java.lang.String>> getResponseHeaders()
          Returns the headers in the response to this request
 int getTickStatus()
          A coded status for this object during the period where its tick method is being called.
 HttpClient header(java.lang.String key, java.lang.String value)
          An http request builder method that adds a header.
 java.lang.String ID()
          The CoffeeMud Java Class ID shared by all instances of this object.
 void initializeClass()
          Called ONCE after all objects are loaded, but before the map is read in during initialization.
 HttpClient maxReadBytes(int bytes)
          An http request builder method that sets the maximum number of bytes that can be read by the request processor.
 HttpClient method(HttpClient.Method meth)
          An http request builder method that sets the http method
 java.lang.String name()
          The nice displayable name of this instance of this object
 CMObject newInstance()
          Returns a new instance of this class.
 HttpClient readTimeout(int ms)
          An http request builder method that sets the maximum number of milliseconds that the reader will remain idle waiting for a byte of data.
 HttpClient reset()
          An http request builder method that resets the client obj so that the connection can be used for another request.
 boolean tick(Tickable ticking, int tickID)
          this is the method which is called periodically by the threading engine.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

reqHeaders

protected java.util.Map<java.lang.String,java.lang.String> reqHeaders

respHeaders

protected java.util.Map<java.lang.String,java.util.List<java.lang.String>> respHeaders

sock

protected java.net.Socket sock

out

protected java.io.OutputStream out

in

protected java.io.InputStream in

meth

protected HttpClient.Method meth

connectTimeout

protected int connectTimeout

readTimeout

protected int readTimeout

maxReadBytes

protected int maxReadBytes

outBody

protected byte[] outBody

respStatus

protected java.lang.Integer respStatus
Constructor Detail

DefaultHttpClient

public DefaultHttpClient()
Method Detail

ID

public java.lang.String ID()
Description copied from interface: CMObject
The CoffeeMud Java Class ID shared by all instances of this object. Unlike the Java Class name, this method does not include package information. However, it must return a String value unique to its class category in the ClassLoader. Class categories include Libraries, Common, Areas, Abilities, Behaviors, CharClasses, Commands, Exits Locales, MOBS, Races, WebMacros, Basic Items, Armor, Weapons, ClanItems, Tech. The name is typically identical to the class name.

Specified by:
ID in interface CMObject
Returns:
the name of this class

name

public java.lang.String name()
Description copied from interface: Tickable
The nice displayable name of this instance of this object

Specified by:
name in interface CMObject
Specified by:
name in interface Tickable
Returns:
the displayable name of this object instance
See Also:
Environmental.Name()

newInstance

public CMObject newInstance()
Description copied from interface: CMObject
Returns a new instance of this class.

Specified by:
newInstance in interface CMObject
Returns:
a new instance of this class

initializeClass

public void initializeClass()
Description copied from interface: CMObject
Called ONCE after all objects are loaded, but before the map is read in during initialization.

Specified by:
initializeClass in interface CMObject

header

public HttpClient header(java.lang.String key,
                         java.lang.String value)
Description copied from interface: HttpClient
An http request builder method that adds a header.

Specified by:
header in interface HttpClient
Parameters:
key - the header name
value - the header value
Returns:
this

method

public HttpClient method(HttpClient.Method meth)
Description copied from interface: HttpClient
An http request builder method that sets the http method

Specified by:
method in interface HttpClient
Parameters:
meth - the method
Returns:
this

body

public HttpClient body(java.lang.String body)
Description copied from interface: HttpClient
An http request builder method that sets the body to send.

Specified by:
body in interface HttpClient
Parameters:
body - the body to send
Returns:
this

body

public HttpClient body(byte[] body)
Description copied from interface: HttpClient
An http request builder method that sets the body to send.

Specified by:
body in interface HttpClient
Parameters:
body - the body to send
Returns:
this

reset

public HttpClient reset()
Description copied from interface: HttpClient
An http request builder method that resets the client obj so that the connection can be used for another request.

Specified by:
reset in interface HttpClient
Returns:
this

connectTimeout

public HttpClient connectTimeout(int ms)
Description copied from interface: HttpClient
An http request builder method that sets the maximum number of milliseconds that the reader will remain idle waiting for a connection to occur.

Specified by:
connectTimeout in interface HttpClient
Parameters:
ms - the maximum number of ms to wait, or 0 for unlimited
Returns:
this

readTimeout

public HttpClient readTimeout(int ms)
Description copied from interface: HttpClient
An http request builder method that sets the maximum number of milliseconds that the reader will remain idle waiting for a byte of data.

Specified by:
readTimeout in interface HttpClient
Parameters:
ms - the maximum number of ms to wait, or 0 for unlimited
Returns:
this

maxReadBytes

public HttpClient maxReadBytes(int bytes)
Description copied from interface: HttpClient
An http request builder method that sets the maximum number of bytes that can be read by the request processor.

Specified by:
maxReadBytes in interface HttpClient
Parameters:
bytes - max bytes to read, or 0 for no limit
Returns:
this

conditionalHeader

protected void conditionalHeader(java.lang.String key,
                                 java.lang.String value,
                                 java.util.List<java.lang.String> clearSet)

getResponseCode

public int getResponseCode()
Description copied from interface: HttpClient
Returns the http status code from the response

Specified by:
getResponseCode in interface HttpClient
Returns:
the http status code from the response

getResponseHeaders

public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getResponseHeaders()
Description copied from interface: HttpClient
Returns the headers in the response to this request

Specified by:
getResponseHeaders in interface HttpClient
Returns:
the headers in the response to this request

doRequest

public HttpClient doRequest(java.lang.String url)
                     throws java.io.IOException
Description copied from interface: HttpClient
An http request builder method that causes this request to occur.

Specified by:
doRequest in interface HttpClient
Parameters:
url - the url to use
Returns:
this
Throws:
java.io.IOException - a socket error

getRawUrl

public byte[] getRawUrl(java.lang.String urlStr,
                        java.lang.String cookieStr)
Description copied from interface: HttpClient
Reads the simple raw return content from a given url and returns it as a byte array. Good for getting files or web pages! Returns null if any error occurs, including a 404, timeouts, or read failures.

Specified by:
getRawUrl in interface HttpClient
Parameters:
urlStr - the url to fetch
cookieStr - cookies to send, or "", or null for none
Returns:
null or a completed byte array of the returned content

getRawUrl

public byte[] getRawUrl(java.lang.String urlStr)
Description copied from interface: HttpClient
Reads the simple raw return content from a given url and returns it as a byte array. Good for getting files or web pages! Returns null if any error occurs, including a 404, timeouts, or read failures.

Specified by:
getRawUrl in interface HttpClient
Parameters:
urlStr - the url to fetch
Returns:
null or a completed byte array of the returned content

getRawUrl

public byte[] getRawUrl(java.lang.String urlStr,
                        int maxLength,
                        int readTimeout)
Description copied from interface: HttpClient
Reads the simple raw return content from a given url and returns it as a byte array. Good for getting files or web pages! Returns null if any error occurs, including a 404, timeouts, or read failures.

Specified by:
getRawUrl in interface HttpClient
Parameters:
urlStr - the url to fetch
maxLength - the maximum size of the content, or 0 for any size
readTimeout - the maximum time, in ms, to wait for connects, and reads
Returns:
null or a completed byte array of the returned content

getResponseContentLength

public int getResponseContentLength()
Description copied from interface: HttpClient
Gets the length of the body of the response to this request.

Specified by:
getResponseContentLength in interface HttpClient
Returns:
the length of the body

getResponseBody

public java.io.InputStream getResponseBody()
Description copied from interface: HttpClient
Returns an input stream to the body of the response to this request.

Specified by:
getResponseBody in interface HttpClient
Returns:
an input stream to the body of the response

doGet

public HttpClient doGet(java.lang.String url)
                 throws java.io.IOException
Description copied from interface: HttpClient
An http request builder method that causes this request to occur as a GET

Specified by:
doGet in interface HttpClient
Parameters:
url - the url to use
Returns:
this
Throws:
java.io.IOException - a socket error

doHead

public HttpClient doHead(java.lang.String url)
                  throws java.io.IOException
Description copied from interface: HttpClient
An http request builder method that causes this request to occur as a HEAD

Specified by:
doHead in interface HttpClient
Parameters:
url - the url to use
Returns:
this
Throws:
java.io.IOException - a socket error

getRawUrl

public byte[] getRawUrl(java.lang.String urlStr,
                        java.lang.String cookieStr,
                        int maxLength,
                        int readTimeout)
Description copied from interface: HttpClient
Reads the simple raw return content from a given url and returns it as a byte array. Good for getting files or web pages! Returns null if any error occurs, including a 404, timeouts, or read failures.

Specified by:
getRawUrl in interface HttpClient
Parameters:
urlStr - the url to fetch
cookieStr - cookies to send, or "", or null for none
maxLength - the maximum size of the content, or 0 for any size
readTimeout - the maximum time, in ms, to wait for connects, and reads
Returns:
null or a completed byte array of the returned content

finished

public void finished()
Description copied from interface: HttpClient
Closes this connection, entirely. Always call this in a finally block!

Specified by:
finished in interface HttpClient

getHeaders

public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeaders(java.lang.String urlStr)
Description copied from interface: HttpClient
Calls GET on the given url, waiting no more than a few seconds for connection, and returns the headers from the response.

Specified by:
getHeaders in interface HttpClient
Parameters:
urlStr - the url to GET
Returns:
the map of headers

getTickStatus

public int getTickStatus()
Description copied from interface: Tickable
A coded status for this object during the period where its tick method is being called. The statis is defined, at least in part, by constants in this interface STATUS_*. STATUS_NOT should be returned when the objects tick method is not currently in execution. It should never return STATUS_NOT when the objects tick method is in execution.

Specified by:
getTickStatus in interface Tickable
Returns:
the numeric status of this object
See Also:
Tickable.tick(Tickable, int)

tick

public boolean tick(Tickable ticking,
                    int tickID)
Description copied from interface: Tickable
this is the method which is called periodically by the threading engine. How often it is called depends on the parameters passed to the threadding engine when it is submitted for thread access. Typically the period is once per TIME_TICK period, but that is determined when the object is submitted to the thread engine.

Specified by:
tick in interface Tickable
Parameters:
ticking - a reference to this Tickable object
tickID - the TICKID_ constant describing this periodic call, as defined in Tickable
Returns:
true always, unless this object no longer wishes to ever tick again, in which case false
See Also:
Tickable, ServiceEngine, TickableGroup

copyOf

public CMObject copyOf()
Description copied from interface: CMObject
Similar to Cloneable.clone(), but does its best to make sure that any internal objects to this class are also copyOfed.

Specified by:
copyOf in interface CMObject
Returns:
a clone of this object

compareTo

public int compareTo(CMObject o)
Specified by:
compareTo in interface java.lang.Comparable<CMObject>