=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java 2012-04-17 14:57:04 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java 2012-04-18 13:51:28 +0000 @@ -398,9 +398,31 @@ } /** - * Test for zip stream signature. - * - * Signature of zip stream from + * Test for ZIP stream signature. Wraps the input stream in a + * BufferedInputStream. If ZIP test is true wraps again in ZipInputStream. + * + * @param in the InputStream. + * @return the wrapped InputStream. + */ + public static InputStream wrapAndCheckZip( InputStream in ) + throws IOException + { + BufferedInputStream bufferedIn = new BufferedInputStream( in ); + + if ( isZip( bufferedIn ) ) + { + ZipInputStream zipIn = new ZipInputStream( bufferedIn ); + zipIn.getNextEntry(); + return zipIn; + } + + return bufferedIn; + } + + /** + * Test for ZIP stream signature. + * + * Signature of ZIP stream from * http://www.pkware.com/documents/casestudies/APPNOTE.TXT Local file * header: local file header signature 4 bytes (0x04034b50) * @@ -433,15 +455,15 @@ throw new RuntimeException( "Couldn't reset stream ", ex ); } - return Arrays.equals( b, zipSig ) ? true : false; + return Arrays.equals( b, zipSig ); } /** - * Test for Gzip stream signature. + * Test for GZIP stream signature. * - * Signature of gzip stream from RFC 1952: ID1 (IDentification 1) ID2 + * Signature of GZIP stream from RFC 1952: ID1 (IDentification 1) ID2 * (IDentification 2) These have the fixed values ID1 = 31 (0x1f, \037), - * ID2 = 139 (0x8b, \213), to identify the file as being in gzip format. + * ID2 = 139 (0x8b, \213), to identify the file as being in GZIP format. * * @param instream the BufferedInputStream to test. */ @@ -467,7 +489,7 @@ throw new RuntimeException( "Couldn't reset stream ", ex ); } - return (b[0] == 31 && b[1] == -117) ? true : false; + return ( b[0] == 31 && b[1] == -117 ); } /** @@ -543,5 +565,5 @@ public static boolean exists( String path ) { return new File( path ).exists(); - } + } } === modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java' --- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java 2010-01-29 09:05:27 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/StreamUtilsTest.java 2012-04-18 13:51:28 +0000 @@ -27,53 +27,82 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + import java.io.BufferedInputStream; +import java.io.InputStreamReader; +import java.io.Reader; -import junit.framework.TestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** - * * @author bobj - * @version created 19-Dec-2009 */ public class StreamUtilsTest - extends TestCase { - +{ public static BufferedInputStream zipStream; public static BufferedInputStream gzipStream; - @Override + public static BufferedInputStream plainStream; + + @Before public void setUp() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - zipStream = new BufferedInputStream(classLoader.getResourceAsStream( "dxfA.zip" )); + zipStream = new BufferedInputStream( classLoader.getResourceAsStream( "dxfA.zip" ) ); - gzipStream = new BufferedInputStream(classLoader.getResourceAsStream( "Export.xml.gz" )); + gzipStream = new BufferedInputStream( classLoader.getResourceAsStream( "Export.xml.gz" ) ); + + plainStream = new BufferedInputStream( classLoader.getResourceAsStream( "Export.xml" ) ); } - @Override - public void tearDown() throws Exception + @After + public void tearDown() + throws Exception { zipStream.close(); gzipStream.close(); - } - - public void testZip() - { - assertTrue(StreamUtils.isZip(zipStream)); - - assertFalse(StreamUtils.isGZip(zipStream)); - } - - public void testGZip() - { - assertTrue(StreamUtils.isGZip(gzipStream)); - - assertFalse(StreamUtils.isZip(gzipStream)); + + plainStream.close(); + } + + @Test + public void testIsZip() + { + assertTrue( StreamUtils.isZip( zipStream ) ); + + assertFalse( StreamUtils.isGZip( zipStream ) ); + + assertFalse( StreamUtils.isZip( plainStream ) ); + } + + @Test + public void testIsGZip() + { + assertTrue( StreamUtils.isGZip( gzipStream ) ); + + assertFalse( StreamUtils.isZip( gzipStream ) ); + + assertFalse( StreamUtils.isGZip( plainStream ) ); + } + + @Test + public void testWrapAndCheckZip() + throws Exception + { + Reader reader = new InputStreamReader( StreamUtils.wrapAndCheckZip( zipStream ) ); + + assertEquals( '<', reader.read() ); + assertEquals( '?', reader.read() ); + assertEquals( 'x', reader.read() ); + assertEquals( 'm', reader.read() ); + assertEquals( 'l', reader.read() ); } } - - === modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java' --- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java 2012-03-26 18:11:26 +0000 +++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java 2012-04-18 13:51:28 +0000 @@ -33,6 +33,8 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -201,4 +203,21 @@ objects.clear(); } } + + /** + * Creates a ZipOutputStream based on the HttpServletResponse and puts a + * new ZipEntry with the given filename to it. + * + * @param response the HttpServletResponse. + * @param fileName the filename of the file inside the zip archive. + * @return a ZipOutputStream + * @throws IOException + */ + public static ZipOutputStream getZipOut( HttpServletResponse response, String fileName ) + throws IOException + { + ZipOutputStream out = new ZipOutputStream( response.getOutputStream() ); + out.putNextEntry( new ZipEntry( fileName ) ); + return out; + } } === modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ExportDataValueAction.java' --- dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ExportDataValueAction.java 2012-04-17 21:38:26 +0000 +++ dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ExportDataValueAction.java 2012-04-18 13:51:28 +0000 @@ -31,15 +31,13 @@ import static org.hisp.dhis.system.util.DateUtils.getMediumDate; import static org.hisp.dhis.util.ContextUtils.CONTENT_TYPE_CSV; import static org.hisp.dhis.util.ContextUtils.CONTENT_TYPE_XML; +import static org.hisp.dhis.util.ContextUtils.getZipOut; -import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Collection; import java.util.HashSet; import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; import javax.servlet.http.HttpServletResponse; @@ -133,7 +131,7 @@ { ContextUtils.configureResponse( response, CONTENT_TYPE_CSV, true, getFileName( EXTENSION_CSV_ZIP ), true ); - Writer writer = new OutputStreamWriter( getZipOut( response, EXTENSION_CSV ) ); + Writer writer = new OutputStreamWriter( getZipOut( response, getFileName( EXTENSION_CSV ) ) ); dataValueSetService.writeDataValueSetCsv( selectedDataSets, getMediumDate( startDate ), getMediumDate( endDate ), orgUnits, writer ); } @@ -141,7 +139,7 @@ { ContextUtils.configureResponse( response, CONTENT_TYPE_XML, true, getFileName( EXTENSION_XML_ZIP ), true ); - dataValueSetService.writeDataValueSet( selectedDataSets, getMediumDate( startDate ), getMediumDate( endDate ), orgUnits, getZipOut( response, EXTENSION_XML ) ); + dataValueSetService.writeDataValueSet( selectedDataSets, getMediumDate( startDate ), getMediumDate( endDate ), orgUnits, getZipOut( response, getFileName( EXTENSION_XML ) ) ); } return SUCCESS; @@ -162,13 +160,4 @@ return fileName + extension; } - - private ZipOutputStream getZipOut( HttpServletResponse response, String extension ) - throws IOException - { - ZipOutputStream out = new ZipOutputStream( response.getOutputStream() ); - out.putNextEntry( new ZipEntry( getFileName( extension ) ) ); - - return out; - } } === modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ImportDataValueAction.java' --- dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ImportDataValueAction.java 2012-04-15 20:48:08 +0000 +++ dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ImportDataValueAction.java 2012-04-18 13:51:28 +0000 @@ -27,7 +27,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.io.BufferedInputStream; +import static org.hisp.dhis.importexport.action.util.ImportDataValueTask.FORMAT_CSV; + import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -43,13 +44,12 @@ import org.hisp.dhis.scheduling.TaskCategory; import org.hisp.dhis.scheduling.TaskId; import org.hisp.dhis.system.scheduling.Scheduler; +import org.hisp.dhis.system.util.StreamUtils; import org.hisp.dhis.user.CurrentUserService; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.Action; -import static org.hisp.dhis.importexport.action.util.ImportDataValueTask.FORMAT_CSV; - /** * @author Lars Helge Overland */ @@ -109,14 +109,16 @@ public String execute() throws Exception { - final TaskId taskId = new TaskId( TaskCategory.DATAVALUE_IMPORT, currentUserService.getCurrentUser() ); - - final ImportOptions options = new ImportOptions( IdentifiableProperty.UID, IdentifiableProperty.UID, dryRun, strategy ); - - final InputStream in = !FORMAT_CSV.equals( importFormat ) ? new BufferedInputStream( new FileInputStream( upload ) ) : null; - - final Reader reader = FORMAT_CSV.equals( importFormat ) ? new BufferedReader( new InputStreamReader( new FileInputStream( upload ) ) ) : null; - + InputStream in = new FileInputStream( upload ); + + in = StreamUtils.wrapAndCheckZip( in ); + + Reader reader = FORMAT_CSV.equals( importFormat ) ? new BufferedReader( new InputStreamReader( in ) ) : null; + + TaskId taskId = new TaskId( TaskCategory.DATAVALUE_IMPORT, currentUserService.getCurrentUser() ); + + ImportOptions options = new ImportOptions( IdentifiableProperty.UID, IdentifiableProperty.UID, dryRun, strategy ); + scheduler.executeTask( new ImportDataValueTask( dataValueSetService, in, reader, options, taskId, importFormat ) ); return SUCCESS;