|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.hd.d.pg2k.webSvr.upload.HTTPUploaderUtils
public final class HTTPUploaderUtils
Code snippets to support exhibit upload. Some of these are here just to keep the JSP files shorter, simpler, clearer, and faster to compile.
Thanks to Benoît Marchal for example code to handle multipart/form-data HTTP POST data.
| Field Summary | |
|---|---|
private static int |
DU_COLLECTING_FILE
doUpload() collecting file. |
private static int |
DU_COLLECTING_PARAM
doUpload() collecting parameter value. |
private static int |
DU_GOT_FILE_NAME
doUpload() got filename. |
private static int |
DU_GOT_PARAM_NAME
doUpload() got parameter name. |
private static int |
DU_INITIAL_STATE
doUpload() initial state. |
static int |
MIN_BATCH_UPLOAD_SPACE
Minimum space that must be left before we will allow an upload; strictly positive. |
private static int |
RW_BLOCK_SIZE
Chunk size for reads/writes in upload; bigger is probably more efficient. |
private static java.io.File |
transcriptName
If not null, relative pathname used to record transcript of exhibit upload. |
| Constructor Summary | |
|---|---|
private |
HTTPUploaderUtils()
Prevent instantiation. |
| Method Summary | |
|---|---|
static boolean |
doLogout(java.net.URL loginURL)
Logout from the upload server if possible. |
static void |
doUpload(javax.servlet.http.HttpServletRequest request,
UploadInfoBean uib,
int maxUploadFileSize,
java.io.File destinationDir,
SimpleLoggerIF logger)
Accepts the body of a multipart/form-data POST request. |
static void |
doUpload(UploadInfoBean uib,
java.io.File destinationDir,
javax.servlet.ServletInputStream is,
java.lang.String boundary,
int maxUploadFileSize,
SimpleLoggerIF logger)
Accepts the body of a multipart/form-data POST request. |
static boolean |
doUploadLogin(boolean justPoll,
java.net.URL loginURL,
java.lang.String userID,
java.lang.String passS)
Attempts to programmatically login at the given login URL with the user ID and password supplied. |
static void |
generateUploadPOSTBody(UploadInfoBean uib,
java.io.OutputStream os,
byte[] data,
java.lang.String boundary)
Writes the POST body for a multi-part MIME stream to upload an exhibit. |
static java.lang.String |
isLoggedInForUploads(javax.servlet.http.HttpServletRequest request)
Returns ID if user is logged in for uploaded, else null. |
static void |
runBatchUploadProtocolServerSide(SimpleLoggerIF logger,
AllExhibitProperties aep,
GenProps gp,
javax.servlet.ServletInputStream rawIS,
javax.servlet.ServletOutputStream rawOS,
java.lang.String uploadDirS,
java.lang.String userID,
java.lang.String userPass)
Run the server side of the batch upload protocol, after authentication. |
private static javax.servlet.ServletInputStream |
transcribeInputStream(javax.servlet.ServletInputStream is)
Take a transcript of the input stream. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
private static final java.io.File transcriptName
Multiple uploads' transcripts will overwrite/corrupt one another. This is not intended for anything other than helping with testing.
Is null by default for release builds.
private static final int DU_INITIAL_STATE
private static final int DU_GOT_FILE_NAME
private static final int DU_GOT_PARAM_NAME
private static final int DU_COLLECTING_FILE
private static final int DU_COLLECTING_PARAM
private static final int RW_BLOCK_SIZE
Should probably be at least 512 to exceed minimum guaranteed Internet MTU and disc sector size, and probably a power of 2.
A good value is probably from 512 to 65536;
public static final int MIN_BATCH_UPLOAD_SPACE
| Constructor Detail |
|---|
private HTTPUploaderUtils()
| Method Detail |
|---|
public static final java.lang.String isLoggedInForUploads(javax.servlet.http.HttpServletRequest request)
request - current HTTP request; never null
public static void doUpload(javax.servlet.http.HttpServletRequest request,
UploadInfoBean uib,
int maxUploadFileSize,
java.io.File destinationDir,
SimpleLoggerIF logger)
throws java.io.IOException,
java.io.InvalidObjectException
In particular this only expects one file upload at a time.
This streams the data more-or-less directly to disc, so that huge uploads can be handled with fixed, modest memory.
This will verify the magic number as the file is uploaded, and abort the upload if it is too long.
The upload is to a temporary file beside (in the same directory as) the final destination (to avoid problems moving across filesystems) and is moved into place atomically if the upload is successful, and removed if unsuccessful. (Any that are left in place after a crash are easily identifiable for manual removal.)
This puts the uploaded file below destinationDir, creating any subdirectories needed, in a path of the form categoryDir/name eg art/cat-1-ANON.jpg.
This will not allow overwrite any existing file as a security precaution.
This does not serialise access to the upload area; this must be done externally if required.
We rely on UploadInfoBean only creating sensible and safe pathnames.
FIXME: we should probably validate the uploaded file content by trying whatever tests we have to hand before finally accepting it.
request - the HTTP POST requestdestinationDir - the root directory to upload tomaxUploadFileSize - the maximum space that the uploaded file
may consume (bytes); must be positiveuib - information on the bean to upload, must be non-null and
be able to generate a valid unique name
java.io.IOException - in case of I/O problems
java.io.InvalidObjectException - if the upload has a bad magic number
public static void doUpload(UploadInfoBean uib,
java.io.File destinationDir,
javax.servlet.ServletInputStream is,
java.lang.String boundary,
int maxUploadFileSize,
SimpleLoggerIF logger)
throws java.io.IOException,
java.io.InvalidObjectException
In particular this only expects one file upload at a time.
This streams the data more-or-less directly to disc, so that huge uploads can be handled with fixed, modest memory.
This will verify the magic number as the file is uploaded, and abort the upload if it is too long.
The upload is to a temporary file beside (in the same directory as) the final destination (to avoid problems moving across filesystems) and is moved into place atomically if the upload is successful, and removed if unsuccessful. (Any that are left in place after a crash are easily identifiable for manual removal.)
This puts the uploaded file below destinationDir, creating any subdirectories needed, in a path of the form categoryDir/name eg art/cat-1-ANON.jpg.
This will not allow overwrite any existing file as a security precaution.
This does not serialise access to the upload area; this must be done externally if required.
We rely on UploadInfoBean only creating sensible and safe pathnames.
FIXME: we should probably validate the uploaded file content by trying whatever tests we have to hand before finally accepting it.
is - the input stream from the HTTP POST requestdestinationDir - the root directory to upload tomaxUploadFileSize - the maximum space that the uploaded file
may consume (bytes); must be positiveuib - information on the bean to upload, must be non-null and
be able to generate a valid unique nameboundary - the MIME multi-part boundary string
(prefixed with lots of "-"s, exactly as will start a line)
java.io.IOException - in case of I/O problems
java.io.InvalidObjectException - if the upload has a bad magic number
public static final void generateUploadPOSTBody(UploadInfoBean uib,
java.io.OutputStream os,
byte[] data,
java.lang.String boundary)
throws java.io.IOException
The non-null, non-empty, unique, not-appearing-in-the-data boundary String must be supplied.
We insist that the boundary marker is pure printable ASCII and at least 8 characters; it should usually be much longer. This should have a random component for safety.
The OutputStream should be buffered if at all possible, for efficiency.
uib - description of the the exhibit; never nullos - stream to write body to; never nulldata - raw exhibit to upload, not altered by the routine; never nullboundary - unique boundary string; never empty or null
java.io.IOException
private static javax.servlet.ServletInputStream transcribeInputStream(javax.servlet.ServletInputStream is)
throws java.io.FileNotFoundException
is - input stream to be transcribed
java.io.FileNotFoundException
public static boolean doUploadLogin(boolean justPoll,
java.net.URL loginURL,
java.lang.String userID,
java.lang.String passS)
throws java.io.IOException
justPoll - see if we remain logged in from previous attemptloginURL - non-null URL (within our codebase)userID - valid userID; never nullpassS - valid password; never null
java.io.IOException - in case of I/O problems
public static boolean doLogout(java.net.URL loginURL)
throws java.io.IOException
loginURL - non-null URL (within our codebase)
java.io.IOException - in case of I/O problems
public static void runBatchUploadProtocolServerSide(SimpleLoggerIF logger,
AllExhibitProperties aep,
GenProps gp,
javax.servlet.ServletInputStream rawIS,
javax.servlet.ServletOutputStream rawOS,
java.lang.String uploadDirS,
java.lang.String userID,
java.lang.String userPass)
throws java.io.IOException
(Can also be used stand-alone for unit testing.)
This waits for a request from the client, then sends a FAIL response in case of immediate trouble, or collects the uploaded file, then sends an OK or FAIL response as appropriate.
rawIS - input stream from client; never nullrawOS - output stream to client; never nulluploadDirS - upload directory; valid and never nulluserID - user ID authenticated with; valid syntax and never nulluserPass - password authenticated with; valid syntax and never null
java.io.IOException - in case of error
|
DHD Multimedia Gallery V1.57.21 | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||