org.hd.d.pg2k.svrCore.uploader
Class UploaderUtils

java.lang.Object
  extended by org.hd.d.pg2k.svrCore.uploader.UploaderUtils

public final class UploaderUtils
extends java.lang.Object

Utilities for exhibit uploader.


Nested Class Summary
static class UploaderUtils.BatchUploadClientRequest
          Encapsulated immutable upload request from batch-upload client.
 
Field Summary
static int BATCH_UPLOAD_BUFSIZE
          Size of (large) buffers used for I/O efficiency; strictly positive.
static char BATCH_UPLOAD_MSG_TERMINATOR
          Terminator (7-bit ASCII) character byte for all messages.
 
Constructor Summary
private UploaderUtils()
          Prevent creation of instances.
 
Method Summary
static int computeLengthOfRequestFromBatchUploadClient(ExhibitStaticAttr esa, ROByteArray hashMD5, java.lang.String description)
          Compute length of upload request for given exhibit.
static UploaderUtils.BatchUploadClientRequest decodeRequestFromBatchUploadClient(java.io.InputStream is)
          Decode an upload request from a batch client and return details.
static java.lang.String decodeResponseToBatchUploadClient(java.io.InputStream is)
          Decode response to batch upload client from server.
static java.lang.String makeSelectBody(java.lang.String[] values, java.lang.String[] labels, java.lang.String selected)
          Make body of select HTML select statement, marking any extant value as selected.
static java.lang.String quoteHTMLArg(java.lang.String s)
          Puts double quotes round any tag attribute value that needs them.
static void sendRequestFromBatchUploadClient(java.io.OutputStream os, ExhibitStaticAttr esa, ROByteArray hashMD5, java.lang.String description, java.io.InputStream exhibitData, java.util.concurrent.atomic.AtomicLong progress)
          Send upload request and data from batch client to server.
static void sendResponseToBatchUploadClient(SimpleLoggerIF logger, java.io.OutputStream os, boolean isOK, java.lang.String exhibitName, java.lang.String detail)
          Send response to batch upload client.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BATCH_UPLOAD_BUFSIZE

public static final int BATCH_UPLOAD_BUFSIZE
Size of (large) buffers used for I/O efficiency; strictly positive.

See Also:
Constant Field Values

BATCH_UPLOAD_MSG_TERMINATOR

public static final char BATCH_UPLOAD_MSG_TERMINATOR
Terminator (7-bit ASCII) character byte for all messages.

See Also:
Constant Field Values
Constructor Detail

UploaderUtils

private UploaderUtils()
Prevent creation of instances.

Method Detail

makeSelectBody

public static java.lang.String makeSelectBody(java.lang.String[] values,
                                              java.lang.String[] labels,
                                              java.lang.String selected)
Make body of select HTML select statement, marking any extant value as selected. The labels[] array can be null, and if so then the values will be used as the text; if not null it must be the same length as values[] and must not contain nulls.

Parameters:
values - the values returned by the select item
labels - (optional) the text values shown for the select items
selected - if equal to one of the values[] entries then that item is marked as selected

quoteHTMLArg

public static final java.lang.String quoteHTMLArg(java.lang.String s)
                                           throws java.lang.IllegalArgumentException
Puts double quotes round any tag attribute value that needs them. Any tag that does not just contain letters and digits will get "..." wrapped round it. If an embedded " is found, or the tag is found to be too long, an exception is thrown. We include '_' in this notion of letters.

If this does not need to change the input it is returned as is.

TODO: jUnit tests

Parameters:
s - non-null, unquoted HTML tag attribute value.
Throws:
java.lang.IllegalArgumentException - impossible to form tag

sendResponseToBatchUploadClient

public static void sendResponseToBatchUploadClient(SimpleLoggerIF logger,
                                                   java.io.OutputStream os,
                                                   boolean isOK,
                                                   java.lang.String exhibitName,
                                                   java.lang.String detail)
                                            throws java.io.IOException
Send response to batch upload client.

This routine does not close the output stream when done.

This routine does not flush the output stream when done; the caller should possibly do so itself.

Parameters:
logger -
os - stream to write to; never null
isOK - if true then this is an "OK" response, else a "FAIL" response
exhibitName - the syntatically-correct name of the exhibit upload to which this response applies, or null for before the first upload
detail - the optional extra information on the failure; null/"" if to be omitted
Throws:
java.io.IOException - in case of error writing to the stream

decodeResponseToBatchUploadClient

public static java.lang.String decodeResponseToBatchUploadClient(java.io.InputStream is)
                                                          throws java.io.IOException
Decode response to batch upload client from server. On a fail response the detail message will be embedded in the detail of the IOException.

If this detects malformed input then it will throw an IOException.

Returns:
exhibitName or null on success/OK response
Throws:
java.io.IOException - on problem reading the input stream or on a fail response

computeLengthOfRequestFromBatchUploadClient

public static int computeLengthOfRequestFromBatchUploadClient(ExhibitStaticAttr esa,
                                                              ROByteArray hashMD5,
                                                              java.lang.String description)
Compute length of upload request for given exhibit. Can be used to set a content-length header (etc) before calling sendRequestFromBatchUploadClient to deliver the actual data.


sendRequestFromBatchUploadClient

public static void sendRequestFromBatchUploadClient(java.io.OutputStream os,
                                                    ExhibitStaticAttr esa,
                                                    ROByteArray hashMD5,
                                                    java.lang.String description,
                                                    java.io.InputStream exhibitData,
                                                    java.util.concurrent.atomic.AtomicLong progress)
                                             throws java.io.IOException
Send upload request and data from batch client to server. We send up the wire, in order:
  1. The putative exhibit length.
  2. The putative (full, syntactically-valid, unique) exhibit name.
  3. The putative exhibit MD5 hash.
  4. The putative exhibit description (or "" if none).
  5. The raw exhibit data.

The exhibit hash serves two purposes:

This routine does not close the output stream when done.

This routine does not flush the output stream when done; the caller should possibly do so itself. The routine may flush the output from time to time, eg after writing a large block.

If the caller supplies a null esa/hash/datastream then we send a request up the wire for a zero-length exhibit upload, which is treated as a request to drop the connection (the client should drop it after sending this).

This can update a progress monitor value while it is working.

Parameters:
progress - if non-null, is set to the amount of bytes uploaded so far, and -1 before any bytes have been uploaded (and Long.MAX_VALUE after we have finished while buffered data xfers)
Throws:
java.io.IOException - in case of error sending data to the output stream; if this occurs then the output stream should be closed and discarded

decodeRequestFromBatchUploadClient

public static UploaderUtils.BatchUploadClientRequest decodeRequestFromBatchUploadClient(java.io.InputStream is)
                                                                                 throws java.io.IOException
Decode an upload request from a batch client and return details. In case of success returns the name/hash/length data, with the input stream positioned directly before the exhibit data (which in turn is followed by the message terminator).

(The timestamp in the returned ExhibitStaticAttr currently contains no useful data.)

In case of failure this throws an IOException and the input stream should be discarded as being in an unknown state.

A zero length is treated as a request from the client to drop the connection and we return null in this case.

Returns:
in case of successfuul decoding, a tuple of the exhibit data and the MD5 exhibit content hash
Throws:
java.io.IOException - in case of error in input

DHD Multimedia Gallery V1.57.21

Copyright (c) 1996-2011, Damon Hart-Davis. All rights reserved.