Java Code Examples for org.apache.commons.fileupload.disk.DiskFileItemFactory#createItem()
The following examples show how to use
org.apache.commons.fileupload.disk.DiskFileItemFactory#createItem() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageTemplatesJspBeanTest.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Test of doCreatePageTemplate method, of fr.paris.lutece.portal.web.style.PageTemplatesJspBean. * * @throws AccessDeniedException * @throws IOException */ public void testDoCreatePageTemplate( ) throws AccessDeniedException, IOException { final String desc = getRandomName( ); Map<String, String [ ]> parameters = new HashMap<>( ); parameters.put( Parameters.PAGE_TEMPLATE_DESCRIPTION, new String [ ] { desc } ); parameters.put( SecurityTokenService.PARAMETER_TOKEN, new String [ ] { SecurityTokenService.getInstance( ).getToken( request, "admin/style/create_page_template.html" ) } ); DiskFileItemFactory fileItemFactory = new DiskFileItemFactory( ); Map<String, List<FileItem>> files = new HashMap<>( ); List<FileItem> pageTemplateFiles = new ArrayList<>( ); FileItem pageTemplateFile = fileItemFactory.createItem( "page_template_file", "text/html", false, "junit.html" ); pageTemplateFile.getOutputStream( ).write( new byte [ 1] ); pageTemplateFiles.add( pageTemplateFile ); files.put( "page_template_file", pageTemplateFiles ); List<FileItem> pageTemplatePictures = new ArrayList<>( ); FileItem pageTemplatePicture = fileItemFactory.createItem( "page_template_picture", "image/jpg", false, "junit.jpg" ); pageTemplatePicture.getOutputStream( ).write( new byte [ 1] ); pageTemplatePictures.add( pageTemplatePicture ); files.put( "page_template_picture", pageTemplatePictures ); MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest( request, files, parameters ); try { instance.doCreatePageTemplate( multipartRequest ); assertTrue( PageTemplateHome.getPageTemplatesList( ).stream( ).anyMatch( t -> t.getDescription( ).equals( desc ) ) ); } finally { PageTemplateHome.getPageTemplatesList( ).stream( ).filter( t -> t.getDescription( ).equals( desc ) ) .forEach( t -> PageTemplateHome.remove( t.getId( ) ) ); } }
Example 2
Source File: PageTemplatesJspBeanTest.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testDoCreatePageTemplateInvalidToken( ) throws AccessDeniedException, IOException { final String desc = getRandomName( ); Map<String, String [ ]> parameters = new HashMap<>( ); parameters.put( Parameters.PAGE_TEMPLATE_DESCRIPTION, new String [ ] { desc } ); parameters.put( SecurityTokenService.PARAMETER_TOKEN, new String [ ] { SecurityTokenService.getInstance( ).getToken( request, "admin/style/create_page_template.html" ) + "b" } ); DiskFileItemFactory fileItemFactory = new DiskFileItemFactory( ); Map<String, List<FileItem>> files = new HashMap<>( ); List<FileItem> pageTemplateFiles = new ArrayList<>( ); FileItem pageTemplateFile = fileItemFactory.createItem( "page_template_file", "text/html", false, "junit.html" ); pageTemplateFile.getOutputStream( ).write( new byte [ 1] ); pageTemplateFiles.add( pageTemplateFile ); files.put( "page_template_file", pageTemplateFiles ); List<FileItem> pageTemplatePictures = new ArrayList<>( ); FileItem pageTemplatePicture = fileItemFactory.createItem( "page_template_picture", "image/jpg", false, "junit.jpg" ); pageTemplatePicture.getOutputStream( ).write( new byte [ 1] ); pageTemplatePictures.add( pageTemplatePicture ); files.put( "page_template_picture", pageTemplatePictures ); MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest( request, files, parameters ); try { instance.doCreatePageTemplate( multipartRequest ); fail( "Should have thrown" ); } catch( AccessDeniedException e ) { assertFalse( PageTemplateHome.getPageTemplatesList( ).stream( ).anyMatch( t -> t.getDescription( ).equals( desc ) ) ); } finally { PageTemplateHome.getPageTemplatesList( ).stream( ).filter( t -> t.getDescription( ).equals( desc ) ) .forEach( t -> PageTemplateHome.remove( t.getId( ) ) ); } }
Example 3
Source File: PageTemplatesJspBeanTest.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testDoCreatePageTemplateNoToken( ) throws AccessDeniedException, IOException { final String desc = getRandomName( ); Map<String, String [ ]> parameters = new HashMap<>( ); parameters.put( Parameters.PAGE_TEMPLATE_DESCRIPTION, new String [ ] { desc } ); DiskFileItemFactory fileItemFactory = new DiskFileItemFactory( ); Map<String, List<FileItem>> files = new HashMap<>( ); List<FileItem> pageTemplateFiles = new ArrayList<>( ); FileItem pageTemplateFile = fileItemFactory.createItem( "page_template_file", "text/html", false, "junit.html" ); pageTemplateFile.getOutputStream( ).write( new byte [ 1] ); pageTemplateFiles.add( pageTemplateFile ); files.put( "page_template_file", pageTemplateFiles ); List<FileItem> pageTemplatePictures = new ArrayList<>( ); FileItem pageTemplatePicture = fileItemFactory.createItem( "page_template_picture", "image/jpg", false, "junit.jpg" ); pageTemplatePicture.getOutputStream( ).write( new byte [ 1] ); pageTemplatePictures.add( pageTemplatePicture ); files.put( "page_template_picture", pageTemplatePictures ); MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest( request, files, parameters ); try { instance.doCreatePageTemplate( multipartRequest ); fail( "Should have thrown" ); } catch( AccessDeniedException e ) { assertFalse( PageTemplateHome.getPageTemplatesList( ).stream( ).anyMatch( t -> t.getDescription( ).equals( desc ) ) ); } finally { PageTemplateHome.getPageTemplatesList( ).stream( ).filter( t -> t.getDescription( ).equals( desc ) ) .forEach( t -> PageTemplateHome.remove( t.getId( ) ) ); } }
Example 4
Source File: UploadDatasetFileResource.java From Knowage-Server with GNU Affero General Public License v3.0 | 3 votes |
private FileItem unzipUploadedFile(FileItem uploaded) { logger.debug("Method unzipUploadedFile(): Start"); FileItem tempFileItem = null; try { ZipInputStream zippedInputStream = new ZipInputStream(uploaded.getInputStream()); ZipEntry zipEntry = null; if ((zipEntry = zippedInputStream.getNextEntry()) != null) { String zipItemName = zipEntry.getName(); logger.debug("Method unzipUploadedFile(): Zip entry [ " + zipItemName + " ]"); if (zipEntry.isDirectory()) { throw new SpagoBIServiceException(getActionName(), "The uploaded file is a folder. Zip directly the file."); } DiskFileItemFactory factory = new DiskFileItemFactory(); tempFileItem = factory.createItem(uploaded.getFieldName(), "application/octet-stream", uploaded.isFormField(), zipItemName); OutputStream tempFileItemOutStream = tempFileItem.getOutputStream(); IOUtils.copy(zippedInputStream, tempFileItemOutStream); tempFileItemOutStream.close(); } zippedInputStream.close(); logger.debug("Method unzipUploadedFile(): End"); return tempFileItem; } catch (Throwable t) { logger.error("Error while unzip file. Invalid archive file: " + t); throw new SpagoBIServiceException(getActionName(), "Error while unzip file. Invalid archive file", t); } }
Example 5
Source File: UploadDatasetFileResource.java From Knowage-Server with GNU Affero General Public License v3.0 | 3 votes |
private FileItem ungzipUploadedFile(FileItem uploaded) { logger.debug("Method ungzipUploadedFile(): Start"); FileItem tempFileItem = null; try { GZIPInputStream zippedInputStream = new GZIPInputStream(uploaded.getInputStream()); String gzipItemName = uploaded.getName().lastIndexOf('.') > 0 ? uploaded.getName().substring(0, uploaded.getName().lastIndexOf('.')) : null; if (gzipItemName == null || gzipItemName.equals("")) { throw new SpagoBIServiceException(getActionName(), "Invalid filename for gzip file"); } DiskFileItemFactory factory = new DiskFileItemFactory(); tempFileItem = factory.createItem(uploaded.getFieldName(), "application/octet-stream", uploaded.isFormField(), gzipItemName); OutputStream tempFileItemOutStream = tempFileItem.getOutputStream(); IOUtils.copy(zippedInputStream, tempFileItemOutStream); tempFileItemOutStream.close(); zippedInputStream.close(); logger.debug("Method ungzipUploadedFile(): End"); return tempFileItem; } catch (Throwable t) { logger.error("Error while unzip file. Invalid archive file: " + t); throw new SpagoBIServiceException(getActionName(), "Error while unzip file. Invalid archive file", t); } }
Example 6
Source File: UploadDatasetFileAction.java From Knowage-Server with GNU Affero General Public License v3.0 | 3 votes |
private FileItem unzipUploadedFile(FileItem uploaded) { logger.debug("Method unzipUploadedFile(): Start"); FileItem tempFileItem = null; try { ZipInputStream zippedInputStream = new ZipInputStream(uploaded.getInputStream()); ZipEntry zipEntry = null; if ((zipEntry = zippedInputStream.getNextEntry()) != null) { String zipItemName = zipEntry.getName(); logger.debug("Method unzipUploadedFile(): Zip entry [ " + zipItemName + " ]"); if (zipEntry.isDirectory()) { throw new SpagoBIServiceException(getActionName(), "The uploaded file is a folder. Zip directly the file."); } DiskFileItemFactory factory = new DiskFileItemFactory(); tempFileItem = factory.createItem(uploaded.getFieldName(), "application/octet-stream", uploaded.isFormField(), zipItemName); OutputStream tempFileItemOutStream = tempFileItem.getOutputStream(); IOUtils.copy(zippedInputStream, tempFileItemOutStream); tempFileItemOutStream.close(); } zippedInputStream.close(); logger.debug("Method unzipUploadedFile(): End"); return tempFileItem; } catch (Throwable t) { logger.error("Error while unzip file. Invalid archive file: " + t); throw new SpagoBIServiceException(getActionName(), "Error while unzip file. Invalid archive file", t); } }
Example 7
Source File: UploadDatasetFileAction.java From Knowage-Server with GNU Affero General Public License v3.0 | 3 votes |
private FileItem ungzipUploadedFile(FileItem uploaded) { logger.debug("Method ungzipUploadedFile(): Start"); FileItem tempFileItem = null; try { GZIPInputStream zippedInputStream = new GZIPInputStream(uploaded.getInputStream()); String gzipItemName = uploaded.getName().lastIndexOf('.') > 0 ? uploaded.getName().substring(0, uploaded.getName().lastIndexOf('.')) : null; if (gzipItemName == null || gzipItemName.equals("")) { throw new SpagoBIServiceException(getActionName(), "Invalid filename for gzip file"); } DiskFileItemFactory factory = new DiskFileItemFactory(); tempFileItem = factory.createItem(uploaded.getFieldName(), "application/octet-stream", uploaded.isFormField(), gzipItemName); OutputStream tempFileItemOutStream = tempFileItem.getOutputStream(); IOUtils.copy(zippedInputStream, tempFileItemOutStream); tempFileItemOutStream.close(); zippedInputStream.close(); logger.debug("Method ungzipUploadedFile(): End"); return tempFileItem; } catch (Throwable t) { logger.error("Error while unzip file. Invalid archive file: " + t); throw new SpagoBIServiceException(getActionName(), "Error while unzip file. Invalid archive file", t); } }