wttools.protocols.cache
Class ProtocolImpl

java.lang.Object
  |
  +--wttools.protocols.cache.ProtocolImpl
All Implemented Interfaces:
ProtocolIfc

public class ProtocolImpl
extends java.lang.Object
implements ProtocolIfc

It is implementation of cache subsystem for all protocols. This class should work as wrapper class for particular protocol implementation. Created: Sun Oct 28 16:46:03 2001

Version:
$Revision: 1.8 $
Author:
Artur Hefczyc

Fields inherited from interface wttools.protocols.ifc.ProtocolIfc
CODE_BAD_REQUEST, CODE_CONNECTION_ERROR, CODE_CONNECTION_OK, CODE_FORBIDDEN, CODE_HOST_NOT_FOUND, CODE_INTERNAL_SERVER_ERROR, CODE_LENGTH_REQUIRED, CODE_NETWORK_NOT_REACHABLE, CODE_NO_CONTENT, CODE_NOT_FOUND, CODE_NOT_INITIALIZED, CODE_PARTIAL_CONTENT, CODE_REDIRECT, CODE_REQUEST_TIMEOUT, CODE_SERVER_NOT_REACHABLE, CODE_UNAUTHORIZED, CODE_VERSION_NOT_SUPPORTED, METHOD_GET, METHOD_HEAD
 
Constructor Summary
ProtocolImpl(CacheContext context)
           
 
Method Summary
 void connect()
          connect method performs all necessary actions required to connect to remote resource.
 void disconnect()
          disconnect method closes network connection.
 java.util.HashMap getAllHeaders()
          Methods getAllHeaders returns all received header from the web server.
 java.lang.String getClientId()
          getClientId method returns client identification string which it sends to web sever during connection.
 java.lang.Object getConnectionInfo()
          getConnectionInfo method should return some connection parameters.
 int getConnectionResult()
          getConnectionResult method returns standarized for all protocols connection with server result.
 java.util.List getConnectionStatuses()
          getConnectionStatuses method calculates and returns connection statuses in List.
 byte[] getContentData()
           
 long getContentLength()
          getContentLength method returns expected content length.
 java.lang.String getContentType()
          getContentType method returns type of data stored in resource.
 URI getEffectiveURI()
          getEffectiveURI returns effective uri of requested resource.
 long getHeaderIntValue(java.lang.String param1)
          Method getHeaderStrValue returns header value as long integer for given header name.
 java.lang.String getHeaderStrValue(java.lang.String param1)
          Method getHeaderStrValue returns header value as String for given header name.
 int getInputBufferSize()
          getInputBufferSize method returns input buffer size which is set for this protocol.
 java.io.InputStream getInputStream()
          getInputStream method returns InputStream where from user can read resource data.
 java.lang.String getProtocolType()
          getProtocolType method returns protocols type it implementing.
 long getRealContentSize()
          getRealContentSize method returns received size of data.
 java.lang.String getRequestData()
           
 java.lang.String getServerId()
          getServerId method returns web server identification string detected during making connection.
 int getStreamReadTimeout()
          getStreamReadTimeout method returns value of stream read timeout.
 float getTransferSpeed()
          getTransferSpeed method calculates and returns transfer speed in bytes/sec.
 URI getURI()
          getURI method returns URI for which performing connection.
 java.lang.String getUserName()
          getUserName method returns user name used during authorization process.
 java.lang.String getUserPassword()
          getUserPassword method returns user password used during authorization process.
 boolean isAllowAllCookies()
           
 boolean isAllowRedirect()
           
 boolean isAllowUserInteraction()
           
 boolean isDebug()
           
 boolean isErrorRecoverable(int code)
          This method isErrorRecoverable should be able to return info if given error code is permanent or not.
 boolean isKeepAlive()
          getKeepAlive method returns value of keepAlive field.
 void saveContent(java.io.OutputStream param1)
          saveContent method performs saving resource data to given OutputStream.
 void saveContent(java.lang.String param1)
          saveContent method performs saving resource data to given disk file.
 void setAllowAllCookies(boolean allow)
           
 void setAllowRedirect(boolean redirect)
           
 void setAllowUserInteraction(boolean allow)
           
 void setClientId(java.lang.String param1)
          setClientId method sets client identification string.
 void setDebug(boolean param1)
          Method setDebug sets debug mode.
 void setInputBufferSize(int param1)
          setInputBufferSize method InputStream returns data in packets.
 void setKeepAlive(boolean param1)
          setKeepAlive method sets keepAlive variable.
 void setRequestData(java.lang.String data)
           
 void setStreamReadTimeout(int param1)
          setStreamReadTimeout method sets timeout for reading from input stream taken from socket.
 void setURI(URI v)
          setURI method performs
 void setUserName(java.lang.String param1)
          setUserName method sets user name for authorization.
 void setUserPassword(java.lang.String param1)
          setUserPassword method sets user password dor authorization.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProtocolImpl

public ProtocolImpl(CacheContext context)
Method Detail

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Description copied from interface: ProtocolIfc
getInputStream method returns InputStream where from user can read resource data. For some protocols implementation InputStream should return not only resource data but also resource headers - it is important in mail protocols for example (POP3, IMAP4, SMTP).
Specified by:
getInputStream in interface ProtocolIfc
Returns:
Throws:
java.io.IOException -

connect

public void connect()
             throws java.io.IOException
Description copied from interface: ProtocolIfc
connect method performs all necessary actions required to connect to remote resource. After successfuly completing this method connection should be established and remote data should be ready to download.
User is able both to retrieve special resource info (data headers only) or resource data. For example user should be allowed to return only mail headers instead of full message.
Specified by:
connect in interface ProtocolIfc
Throws:
java.io.IOException -

getContentLength

public long getContentLength()
Description copied from interface: ProtocolIfc
getContentLength method returns expected content length. In many situations web server sends size of resource data, but sometimes we don't know how much data we receive till end of transmision. If expected content length is not known protocol should return -1.
Specified by:
getContentLength in interface ProtocolIfc
Returns:

getContentType

public java.lang.String getContentType()
Description copied from interface: ProtocolIfc
getContentType method returns type of data stored in resource. for some protocols implementation it is easy to return info received from web server (HTTP server returns Content-Type header with such info) for others it is necessary to create engine to determine content type. This engine may base on file extension or based on mime-magic file.
Specified by:
getContentType in interface ProtocolIfc
Returns:

getConnectionResult

public int getConnectionResult()
                        throws java.io.IOException
Description copied from interface: ProtocolIfc
getConnectionResult method returns standarized for all protocols connection with server result. For some protocols such HTTP it could translated server response code for other particular protocol implementation should decide what code return in given situation.
Specified by:
getConnectionResult in interface ProtocolIfc
Returns:
Throws:
java.io.IOException -

getProtocolType

public java.lang.String getProtocolType()
Description copied from interface: ProtocolIfc
getProtocolType method returns protocols type it implementing. It should allways be a string taken from package name. For example for HTTP implementation we have class wttools.protocols.http.ProtocolImpl. And we know what protocol it implement from class name, becouse it is stored in last package name level wttools.protocols.http.
Specified by:
getProtocolType in interface ProtocolIfc
Returns:

disconnect

public void disconnect()
                throws java.io.IOException
Description copied from interface: ProtocolIfc
disconnect method closes network connection. All received resource info should be still available but not additional data resource data.
Specified by:
disconnect in interface ProtocolIfc
Throws:
java.io.IOException -

getHeaderStrValue

public java.lang.String getHeaderStrValue(java.lang.String param1)
Description copied from interface: ProtocolIfc
Method getHeaderStrValue returns header value as String for given header name.
Specified by:
getHeaderStrValue in interface ProtocolIfc
Parameters:
param1 -
Returns:

getHeaderIntValue

public long getHeaderIntValue(java.lang.String param1)
Description copied from interface: ProtocolIfc
Method getHeaderStrValue returns header value as long integer for given header name.
Specified by:
getHeaderIntValue in interface ProtocolIfc
Parameters:
param1 -
Returns:

getAllHeaders

public java.util.HashMap getAllHeaders()
Description copied from interface: ProtocolIfc
Methods getAllHeaders returns all received header from the web server.
Specified by:
getAllHeaders in interface ProtocolIfc
Returns:

getTransferSpeed

public float getTransferSpeed()
Description copied from interface: ProtocolIfc
getTransferSpeed method calculates and returns transfer speed in bytes/sec. This info should be available during downloading data to allow breaking transfer if ransfer speed is too low.
Specified by:
getTransferSpeed in interface ProtocolIfc
Returns:

getServerId

public java.lang.String getServerId()
Description copied from interface: ProtocolIfc
getServerId method returns web server identification string detected during making connection. in HTTP protocol server identification string can be found in header named Server.
Specified by:
getServerId in interface ProtocolIfc
Returns:

getConnectionInfo

public java.lang.Object getConnectionInfo()
Description copied from interface: ProtocolIfc
getConnectionInfo method should return some connection parameters. for example remote host IP number, socket buffers size, socket timeout, remote server info and so on.
Specified by:
getConnectionInfo in interface ProtocolIfc
Returns:

getConnectionStatuses

public java.util.List getConnectionStatuses()
Description copied from interface: ProtocolIfc
getConnectionStatuses method calculates and returns connection statuses in List. All objects stored in returned List should have implemented String toString() method to allow displaing info.
As statuses should be put info about transmision: average transfer speed, bytes received, transfer time and so on.
Specified by:
getConnectionStatuses in interface ProtocolIfc
Returns:

setURI

public void setURI(URI v)
            throws java.io.IOException
setURI method performs
Specified by:
setURI in interface ProtocolIfc
Parameters:
v - an URI value
Throws:
java.io.IOException - if an error occurs

getURI

public URI getURI()
Description copied from interface: ProtocolIfc
getURI method returns URI for which performing connection.
Specified by:
getURI in interface ProtocolIfc
Returns:

setClientId

public void setClientId(java.lang.String param1)
Description copied from interface: ProtocolIfc
setClientId method sets client identification string. I don't know if it make sense but there is "User-Aget" header in HTTP protocol there which client sends to HTTP server.
Specified by:
setClientId in interface ProtocolIfc
Parameters:
param1 -

getClientId

public java.lang.String getClientId()
Description copied from interface: ProtocolIfc
getClientId method returns client identification string which it sends to web sever during connection. In HTTP protocol it is value of "User-Agent" header.
Specified by:
getClientId in interface ProtocolIfc
Returns:

setStreamReadTimeout

public void setStreamReadTimeout(int param1)
Description copied from interface: ProtocolIfc
setStreamReadTimeout method sets timeout for reading from input stream taken from socket. When some communication problem occures during perfmorming data transmision application may infinitely wait for data which will never arrive. To prevent such situation you can set stream read timeout to ensure waiting for data no longer than you expect. If set to 0 it means turn off timeout and allow infinite reading.
Specified by:
setStreamReadTimeout in interface ProtocolIfc
Parameters:
param1 -

getStreamReadTimeout

public int getStreamReadTimeout()
Description copied from interface: ProtocolIfc
getStreamReadTimeout method returns value of stream read timeout.
Specified by:
getStreamReadTimeout in interface ProtocolIfc
Returns:

setInputBufferSize

public void setInputBufferSize(int param1)
Description copied from interface: ProtocolIfc
setInputBufferSize method InputStream returns data in packets. With this method you can sets size of this packet. The smaller size of packet should be used if there are some transmision problems and bigger size should be used if we use network with very good bandwich to improve transmision efficiency.
Specified by:
setInputBufferSize in interface ProtocolIfc
Parameters:
param1 -

getInputBufferSize

public int getInputBufferSize()
Description copied from interface: ProtocolIfc
getInputBufferSize method returns input buffer size which is set for this protocol.
Specified by:
getInputBufferSize in interface ProtocolIfc
Returns:

setKeepAlive

public void setKeepAlive(boolean param1)
Description copied from interface: ProtocolIfc
setKeepAlive method sets keepAlive variable. This variable determines keeping connection to web server after completing user request. If we know that we want reach only one resource from thet web server we can set keepAlive to false and protocol implementation should disconnect after finishing transmision. But very often we want to download many files from web server for example from FTP or HTTP server. It is not effective to reconnection every time. It is better to keep alive connection between downloading every file.
For example suppose we have to download 10 files from HTTP server. Each file is about 10kB size. So downloading each of them with good internet connection takes about 1 sec. But establishing connection takes from 1 sec to 30 sec. Lets suppose that it takes about 10 sec. So we can see how much time we can save with using keep alive connections.
Specified by:
setKeepAlive in interface ProtocolIfc
Parameters:
param1 -

isKeepAlive

public boolean isKeepAlive()
Description copied from interface: ProtocolIfc
getKeepAlive method returns value of keepAlive field. keepAlive field switches between permaneent connection mode and single transfer connection mode. Permanent connection mode should allow to transfer many files with one persistent connection to server.
Specified by:
isKeepAlive in interface ProtocolIfc
Returns:

saveContent

public void saveContent(java.lang.String param1)
                 throws java.io.IOException
Description copied from interface: ProtocolIfc
saveContent method performs saving resource data to given disk file. I have doubt what mean resource data. In general we can divide resource info on resource headers and resource data. It is very usefull if we think about HTTP resources. But if we think about mail message headers are almost as important as message data. So in particular protocol implementation headers should be joined with data.
Specified by:
saveContent in interface ProtocolIfc
Parameters:
param1 -
Throws:
java.io.IOException -

saveContent

public void saveContent(java.io.OutputStream param1)
                 throws java.io.IOException
Description copied from interface: ProtocolIfc
saveContent method performs saving resource data to given OutputStream. This method does not open nor close given stream. Closing stream should be performed by user.
Specified by:
saveContent in interface ProtocolIfc
Parameters:
param1 -
Throws:
java.io.IOException -

getRealContentSize

public long getRealContentSize()
Description copied from interface: ProtocolIfc
getRealContentSize method returns received size of data. First of all if undetected network error occurs we can recieve less data that we expect. So during verification we can check if expected data size is equal to received. Second if web server don't send expected data size then with this method we can know how many bytes were received.
Specified by:
getRealContentSize in interface ProtocolIfc
Returns:

setUserName

public void setUserName(java.lang.String param1)
Description copied from interface: ProtocolIfc
setUserName method sets user name for authorization. in general user name and pasword should be encoded in url with the form: http://user:passwd@www.sf.net:8000/path/file.html but sometimes it isn't possible to put user name and password in url and then you can set them with these methods.
Specified by:
setUserName in interface ProtocolIfc
Parameters:
param1 -

getUserName

public java.lang.String getUserName()
Description copied from interface: ProtocolIfc
getUserName method returns user name used during authorization process. User name can be taken from URI or from data received in setUserName(Strin name) method.
Specified by:
getUserName in interface ProtocolIfc
Returns:

setUserPassword

public void setUserPassword(java.lang.String param1)
Description copied from interface: ProtocolIfc
setUserPassword method sets user password dor authorization. in general user name and pasword should be encoded in url with the form: http://user:passwd@www.sf.net:8000/path/file.html but sometimes it isn't possible to put user name and password in url and then you can set them with these methods.
Specified by:
setUserPassword in interface ProtocolIfc
Parameters:
param1 -

getUserPassword

public java.lang.String getUserPassword()
Description copied from interface: ProtocolIfc
getUserPassword method returns user password used during authorization process. User name can be taken from URI or from data received in setUserName(Strin name) method.
Specified by:
getUserPassword in interface ProtocolIfc
Returns:

setDebug

public void setDebug(boolean param1)
Description copied from interface: ProtocolIfc
Method setDebug sets debug mode.
Specified by:
setDebug in interface ProtocolIfc
Parameters:
param1 -

isDebug

public boolean isDebug()
Specified by:
isDebug in interface ProtocolIfc
Returns:

setAllowUserInteraction

public void setAllowUserInteraction(boolean allow)
Specified by:
setAllowUserInteraction in interface ProtocolIfc

isAllowUserInteraction

public boolean isAllowUserInteraction()
Specified by:
isAllowUserInteraction in interface ProtocolIfc

setAllowAllCookies

public void setAllowAllCookies(boolean allow)
Specified by:
setAllowAllCookies in interface ProtocolIfc

isAllowAllCookies

public boolean isAllowAllCookies()
Specified by:
isAllowAllCookies in interface ProtocolIfc

setRequestData

public void setRequestData(java.lang.String data)
Specified by:
setRequestData in interface ProtocolIfc

getRequestData

public java.lang.String getRequestData()
Specified by:
getRequestData in interface ProtocolIfc

isErrorRecoverable

public boolean isErrorRecoverable(int code)
Description copied from interface: ProtocolIfc
This method isErrorRecoverable should be able to return info if given error code is permanent or not. I mean for example if error code means "Page not found" it mean that there is no page under requested URL. It is most probably permanent error. However if error code means "Network not reachable" or "Request timeout" it mean that in future during next try download may be successful.
Specified by:
isErrorRecoverable in interface ProtocolIfc
Following copied from interface: wttools.protocols.ifc.ProtocolIfc
Parameters:
code - an int value of requested error code.
Returns:
a boolean calculated value of recoverability of error.

getContentData

public byte[] getContentData()
                      throws java.io.IOException
Specified by:
getContentData in interface ProtocolIfc

setAllowRedirect

public void setAllowRedirect(boolean redirect)
Specified by:
setAllowRedirect in interface ProtocolIfc

isAllowRedirect

public boolean isAllowRedirect()
Specified by:
isAllowRedirect in interface ProtocolIfc

getEffectiveURI

public URI getEffectiveURI()
                    throws java.io.IOException
Description copied from interface: ProtocolIfc
getEffectiveURI returns effective uri of requested resource. If during connecting to web server redirection response occurs it means that effective URI is different then original.
Specified by:
getEffectiveURI in interface ProtocolIfc
Following copied from interface: wttools.protocols.ifc.ProtocolIfc
Returns:
an URI value of effective resource URI.


Copyright © GNU, wttools developers Team.