First of all, Don't Panic!
This is a simple JAVA project to be used as a File Server using REST API.
You can use to store your files on another place, and consume everything from a HTTP API. Upload files specifying a path, create new revisions, list files on a path, download files or download revisions.
Its simple, but usefull if you want to move your storage to another place. I know, I know... "But why not Amazon S3?" you say... Well... there you cannot read the code and learn from that, and with this code you can have yout own File Server.. thats not awsome? :p
To run this project as is, you will need to setup:
- JVM Java 6+
- SGBD Mysql 5+
- Servlet Container - TOMCAT 6 (or another)
- Import schema.sql to your database
- Do deploy of .war to your tomcat
All failed requisitions will have your equivalent HTTP Response Code, and on the body a JSON explaing the problem.
- 400: Invalid parameters. More details on body as JSON
- 404: File or path not found
- 405: Unexpected request method. Usually GET or POST
- 5xx: Server error. More details on body as JSON
Donwload a file.
http://base_path/files/{id} {id} is the id of file
GET
1.0
rev - (integer) the number of file revision to be returned. As default the last revision is returned
The file with defined id
- 404: When the file or revision is not found
Upload a file or a new revision of file
http://base_path/files/
POST
1.0
file - required (multipart/form-data) the content of file sent from a html form with enctype="multipart/form-data" overwrite - (boolean) when true, if the file on the same path with the same name already exists, create a new revision for that file path - the path where this file should be saved
All file metadata.
{
"id":"0",
"isDir":"false",
"mimeType":"image/png",
"name":"image.png",
"path":"path/where/is/file",
"currentRevision":"1"
}
- 400: Error to read file, or some required params was not sent
Return the metadata from all file revisions
http://base_path/revisions/{id} {id} is the id of file
GET
1.0
No one needed
File metadata and the list of revisions.
{
“metadata”:
{
"id":"0",
"isDir":"false",
"mimeType":"image/png",
"name":"image.png",
"path":"path/where/is/file",
"currentRevision":"1"
},
“revisions” :
{
“revision”: 0,
“created_at”: “2012-01-01 00:00:00”
},
{
“revision”: 1,
“created_at”: “2012-01-01 01:34:22”
}
}
- 404: File not found
Return the metadata from all files on the specified path
http://base_path/paths
GET
1.0
path required (String) path to be checked
File metadata of the files with the specified path.
{
“metadatas”: [
{
"id":"0",
"isDir":"false",
"mimeType":"image/png",
"name":"image.png",
"path":"path/where/is/file",
"currentRevision":"1"
},
{
"id":"1",
"isDir":"false",
"mimeType":"image/png",
"name":"image1.png",
"path":"path/where/is/file",
"currentRevision":"0"
}
]
}
none
- JAVA 6+ SDK
- Eclipse (http://eclipse.org) ** I'm using the plugin m2eclipse (http://maven.apache.org/eclipse-plugin.html) (If you want to user another IDE, be free to do that. :) But all my instructions are to run on Eclipse... )
- Create a Tomcat server on Eclipse. (do some search on goole)
- Import the project to eclipse
- Run maven install to dondload all dependencias. (you can use the plugin m2eclipse)
- Right button on the project -> Run as -> Maven Install or ...
- on project dir, exec the command "mvn install"
- Config the database credentials on file src/main/webapp/WEB-INF/config/db.xml
- Import tables from schema.sql to the configured database
- Publish the project on Tomcat
- Run tomcat
- Jersey (jersey.java.net) RESTFUL API
- Spring - For IC and Configurations
- src/main/java - Java classes
- src/main/config - configuration files (log4j)
- src/main/webapp - Sprint and ServletContainer configuration files
- file.server.model.bean - the beans of the project, every class that load data
- file.server.model.dao - class to manage beans on database
- file.server.rest - "servlets" jersey, represents the communication with external clients
- file.server.service - service classes, the link between rest and dao, here I put all bussiness logical
- file.server.util - util classes
- Change the config of the database to your production server on src/main/webapp/WEB-INF/config/db.xml (I know, its not the best approach... sorry about that)
- On eclipse, click with the right button of yout mouse on the project and select the option Export -> WAR File
- NNF on the wizard and be happy!