private void doSetImage(
			final String name,
			final String qualifier,
			final InputStream image,
			final FormDataContentDisposition fileDetail)
	throws IOException {

		// Check image size and extension.
		final long size = fileDetail.getSize();
		final String extension = getFileExtension(fileDetail.getFileName());
		if (size > MAX_IMAGE_SIZE)
			throw new IllegalArgumentException("Image is too large: " + size);

		if (!SUPPORTED_EXTENSIONS.contains(extension))
			throw new IllegalArgumentException("Unsupported image file extension: " + extension);

		// Get the target directory.
		File targetDir;
		if (qualifier != null) {
			this.logger.fine( "Request: set template image: " + name + '/' + qualifier + "." );
			final ApplicationTemplate template = this.manager.applicationTemplateMngr().findTemplate(name, qualifier);
			if (template == null)
				throw new NoSuchElementException("Cannot find template: " + name + '/' + qualifier);

			targetDir = new File(template.getDirectory(), Constants.PROJECT_DIR_DESC);

		} else {
			this.logger.fine( "Request: set application image: " + name + "." );
			final Application application = this.manager.applicationMngr().findApplicationByName( name );
			if (application == null)
				throw new NoSuchElementException("Cannot find application: " + name);

			targetDir = new File(application.getDirectory(), Constants.PROJECT_DIR_DESC);
		}

		// First clean the previous "application.*" images, as they may be chosen instead of the one we're uploading.
		for (final String ext : SUPPORTED_EXTENSIONS) {
			File f = new File(targetDir, "application." + ext);
			Utils.deleteFilesRecursivelyAndQuietly( f );
		}

		// Now store the image: rename it to application.X, so we get sure it is chosen as THE app/template icon.
		// (where X is the uploaded file extension).
		Utils.copyStream(image, new File(targetDir, "application." + extension));
	}
private void doSetImage ( final String name , final String qualifier , final InputStream image , final FormDataContentDisposition fileDetail ) throws IOException { final long size = fileDetail . getSize ( ) ; final String extension = getFileExtension ( fileDetail . getFileName ( ) ) ; if ( size > MAX_IMAGE_SIZE ) throw new IllegalArgumentException ( "Image is too large: " + size ) ; if ( ! SUPPORTED_EXTENSIONS . contains ( extension ) ) throw new IllegalArgumentException ( "Unsupported image file extension: " + extension ) ; File targetDir ; if ( qualifier != null ) { this . logger . fine ( "Request: set template image: " + name + '/' + qualifier + "." ) ; final ApplicationTemplate template = this . manager . applicationTemplateMngr ( ) . findTemplate ( name , qualifier ) ; if ( template == null ) throw new NoSuchElementException ( "Cannot find template: " + name + '/' + qualifier ) ; targetDir = new File ( template . getDirectory ( ) , Constants . PROJECT_DIR_DESC ) ; } else { this . logger . fine ( "Request: set application image: " + name + "." ) ; final Application application = this . manager . applicationMngr ( ) . findApplicationByName ( name ) ; if ( application == null ) throw new NoSuchElementException ( "Cannot find application: " + name ) ; targetDir = new File ( application . getDirectory ( ) , Constants . PROJECT_DIR_DESC ) ; } for ( final String ext : SUPPORTED_EXTENSIONS ) { File f = new File ( targetDir , "application." + ext ) ; Utils . deleteFilesRecursivelyAndQuietly ( f ) ; } Utils . copyStream ( image , new File ( targetDir , "application." + extension ) ) ; }
Upload an image for a template/application.
<p>
If an image was already set, it is overridden by the new one.
</p>
@param name the name of the template/application.
@param qualifier the qualifier of the template, or {@code null} for an application.
@param image the uploaded image.
@param fileDetail the image details.
@throws IllegalArgumentException if the image is too large, or is not supported.
@throws NoSuchElementException if the application/template cannot be found.
@throws IOException if the image cannot be stored.