trackerBT
Class UploadService

java.lang.Object
  extended by simple.http.serve.Component
      extended by simple.http.load.Service
          extended by trackerBT.UploadService
All Implemented Interfaces:
simple.http.serve.Resource

public class UploadService
extends simple.http.load.Service

Service called to upload a torrent file to the tracker

Titre : TrackerBT

Description : Bittorrent Tracker

Copyright : Copyright (c) 2007


Field Summary
(package private)  org.jdom.Document torrents
           
 
Fields inherited from class simple.http.load.Service
engine
 
Fields inherited from class simple.http.serve.Component
context
 
Constructor Summary
UploadService(simple.http.serve.Context context)
          Default constructor needed by Simple server
 
Method Summary
private  void addTorrent(org.jdom.Element root, java.lang.String name, java.lang.String infoFile, java.lang.String comment, java.lang.String hash)
          Add a torrent to the torrent database according to the given parameters
private  void createEmptyTrackerXML(java.io.File xmlTorrent)
          Create new XML document for registering torrents
private  byte[] generateResponse(int id)
          Generate the html file bytes representing the answer.
private  boolean isNewTorrent(org.jdom.Element root, java.lang.String hash)
          Look if the torrent is already registered on this tracker
private  java.util.Map mapParameters(java.util.List l)
          Creates a Map representing the parameters listed in l
 void process(simple.http.Request req, simple.http.Response resp)
          Process the client upload request and returns the corresponding answer.
private  java.util.List processRequest(simple.http.Request req)
          Process the request and retrieves the parameters and file contained in it.
 int registerTorrent(java.lang.String name, java.lang.String infoFile, java.lang.String comment, java.lang.String hash)
          Try to register the torrent given with the parameters
 
Methods inherited from class simple.http.load.Service
prepare
 
Methods inherited from class simple.http.serve.Component
handle, handle, handle, process, process
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

torrents

org.jdom.Document torrents
Constructor Detail

UploadService

public UploadService(simple.http.serve.Context context)
Default constructor needed by Simple server

Parameters:
context - The context of the server
Method Detail

addTorrent

private void addTorrent(org.jdom.Element root,
                        java.lang.String name,
                        java.lang.String infoFile,
                        java.lang.String comment,
                        java.lang.String hash)
Add a torrent to the torrent database according to the given parameters

Parameters:
root - Root element of the XML document representing the torrents database
name - Name of the torrent
infoFile - Name of an optional info file (.nfo, .txt, ...)
comment - Comment about the torrent
hash - Torrent id. It is the hash of the 'info' dictionary in the torrent

createEmptyTrackerXML

private void createEmptyTrackerXML(java.io.File xmlTorrent)
                            throws java.io.IOException
Create new XML document for registering torrents

Parameters:
xmlTorrent - The file the document will be saved in
Throws:
java.io.IOException

generateResponse

private byte[] generateResponse(int id)
Generate the html file bytes representing the answer. Answer depends on the response status

Parameters:
id - Represent the response status. 0 means upload was successful. Otherwise id represent the kind of error that occured and a corresponding answer is created.
Returns:
byte[] The html response bytes

isNewTorrent

private boolean isNewTorrent(org.jdom.Element root,
                             java.lang.String hash)
Look if the torrent is already registered on this tracker

Parameters:
root - Root element of the XML document representing the torrent database
hash - Id of the file being uploaded
Returns:
boolean true if the file doesn't exist on the tracker, false otherwise

mapParameters

private java.util.Map mapParameters(java.util.List l)
Creates a Map representing the parameters listed in l

Parameters:
l - List The list of parameters
Returns:
Map

process

public void process(simple.http.Request req,
                    simple.http.Response resp)
             throws java.io.IOException
Process the client upload request and returns the corresponding answer. In case of successful upload, the torrent will be registered on the tracker and freely available for download

Specified by:
process in class simple.http.serve.Component
Parameters:
req - The request received from the client. It must be a multipart data-form type request
resp - HTML file representing the result of the request
Throws:
java.io.IOException

processRequest

private java.util.List processRequest(simple.http.Request req)
Process the request and retrieves the parameters and file contained in it.

Parameters:
req - The received request
Returns:
List List of file items representing the parameters uploaded

registerTorrent

public int registerTorrent(java.lang.String name,
                           java.lang.String infoFile,
                           java.lang.String comment,
                           java.lang.String hash)
Try to register the torrent given with the parameters

Parameters:
name - Name of the torrent
infoFile - Potential info file name
comment - Comment about the torrent
hash - Torrent id
Returns:
int Integer representing an error type. 0 means no error occured