|
From: Antony C. <an...@sm...> - 2007-05-10 16:44:34
|
Hi Adam,
I can only get anonymous access to cvs so until I can commit here's the
code...
Add attached file to:
templates/style_default/room
templates/style_default/suite
------------------------------------------------------------------------
------------------------------------------------------------------------
---
Changes to
BuildingServlet.java
public void doProcessing( Request breq, Response res )
case Request.REQUEST_TYPE_VIRTUAL:
//res.disableCaching();
// It's up to the individual methods that output virtual
// files to decide whether to disable caching
+ //uhi:awc zips up webDocument content packages in resource
tree
+ if ( breq.getPageName().equals( "imsCP_WebDocuments.zip" )
) {
+ ResourceUtils.sendWebDocumentContentPackages(breq,
res);
+ break;
+ }
breq.getFacility().sendVirtualFile( breq, res );
break;
------------------------------------------------------------------------
------------------------------------------------------------------------
---
Add these methods to
ResourceUtils.java
//uhi:awc
/**
* Walk the Resource tree starting from current node and add
content packages to a ZipFile
* when a MediaDocument (WebDocument) is encountered. Response
contains error if method is
* called from a containing resource that is not a room or suite of
rooms. Exceptions encountered
* when attempting to add a file to the ZipFile will be handled by
adding an 'error' ZipEntry .
* @param request The request.
* @param response The response that zipOutputStream writes to.
* @throws ServletException
* @throws java.io.IOException
* @see
org.bodington.servlet.facilities.MediaDocumentFacility#sendVirtualFile(
Request, javax.servlet.http.HttpServletResponse)
*/
public static void sendWebDocumentContentPackages( Request request,
Response response) throws ServletException, IOException
{
Resource resource = request.getResource();
if (resource.getResourceType() == Resource.RESOURCE_SUITE ||
resource.getResourceType() == Resource.RESOURCE_ROOM) {
ZipOutputStream zipOutputStream = null;
try {
response.setContentType( "application/octet-stream" );
zipOutputStream = new ZipOutputStream(
response.getOutputStream() );
walkResourceTree(resource, "imsCP_" +
resource.getName() + "/", zipOutputStream);
} catch (BuildingServerException e) {
throw new ServletException("An error occurred whilst
attempting to zip WebDocument content packages", e);
} finally {
zipOutputStream.close();
}
} else {
response.sendError( response.SC_INTERNAL_SERVER_ERROR,
"Incorrect resource - the command can only be run from within a room or
suite of rooms." );
BuildingContext.trace( "ending, incorrect resource" );
BuildingContext.dumpTrace();
}
}
private static void walkResourceTree( Resource resource, String
path, ZipOutputStream zipOutputStream ) throws BuildingServerException
{
if (resource.getResourceType() == Resource.RESOURCE_SUITE ||
resource.getResourceType() == Resource.RESOURCE_ROOM ) {
// handle any uploaded files in these container
resources
processUploadedFiles( resource, path, zipOutputStream );
//recursively get child resources for above container
resources
Vector ids = resource.getChildIds();
Enumeration childIds = ids.elements();
while (childIds.hasMoreElements()){
resource =
Resource.findResource((PrimaryKey)childIds.nextElement());
walkResourceTree(resource, path +
resource.getName() + "/", zipOutputStream);
}
} else if (resource.getResourceType() ==
Resource.RESOURCE_MEDIADOCUMENT) {
// handle uploaded files in Media (Web) document
processUploadedFiles( resource, path, zipOutputStream );
}
}
private static void processUploadedFiles( Resource resource, String
path, ZipOutputStream zipOutputStream )
{
String root = "/";
try {
BuildingSession session =
BuildingSessionManagerImpl.getSession( resource );
session.getFileAndDescendentSummaryTree( null, false );
session.generateImsManifest();
// Obtain a list of entries in the root directory and its
descendants
UploadedFileSummary[] summaries =
session.getFileAndDescendentSummaries( root, true );
// Obtain the location of the filestore for the entries
File source_filestore = resource.getWebPublishFolder();
// summary for current resource and manifest, if more then
file has been uploaded here
for ( int summaryIndex = 0; summaryIndex <
summaries.length; summaryIndex++ )
{
UploadedFile f = UploadedFile.findUploadedFile(
summaries[summaryIndex].getUploadedFileId() );
// don't emit deleted files and folders; don't emit
folders
if ( f==null || f.isDeleted() || f.isFolder() )
continue;
File source = new File( source_filestore,
f.getRealNameInResource() );
FileInputStream inputStream = new FileInputStream(
source );
writeToZip(zipOutputStream, inputStream, path +
f.getNameInResource());
}
} catch (Exception e) {
try {
//read exception and write to zip
ByteArrayInputStream inputStream = new
ByteArrayInputStream(e.toString().getBytes());
writeToZip(zipOutputStream, inputStream, "errors/" +
path.substring(path.indexOf('_') + 1) + "missing_entry.txt");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private static void writeToZip( ZipOutputStream zipOutputStream,
InputStream inputStream, String zipEntryName ) throws IOException
{
try {
//create and add zip entry
ZipEntry zipEntry = new ZipEntry( zipEntryName );
zipOutputStream.putNextEntry( zipEntry );
//write bytes from the inputStream
byte[] readBuffer = new byte[8192];
int bytesIn = 0;
while( ( bytesIn = inputStream.read( readBuffer ) ) != -1 )
{
zipOutputStream.write( readBuffer, 0, bytesIn );
}
} finally {
//close the stream and zipEntry
inputStream.close();
zipOutputStream.closeEntry();
}
}
|