@@ -335,24 +335,40 @@ public VolumeVO uploadVolume(UploadVolumeCmd cmd) throws ResourceAllocationExcep
335335 Long zoneId = cmd .getZoneId ();
336336 String volumeName = cmd .getVolumeName ();
337337 String url = cmd .getUrl ();
338- String format = cmd .getFormat ();
338+ String format = sanitizeFormat ( cmd .getFormat () );
339339 Long diskOfferingId = cmd .getDiskOfferingId ();
340340 String imageStoreUuid = cmd .getImageStoreUuid ();
341341 DataStore store = _tmpltMgr .getImageStore (imageStoreUuid , zoneId );
342342
343343 validateVolume (caller , ownerId , zoneId , volumeName , url , format , diskOfferingId );
344344
345- VolumeVO volume = persistVolume (owner , zoneId , volumeName , url , cmd . getFormat () , diskOfferingId , Volume .State .Allocated );
345+ VolumeVO volume = persistVolume (owner , zoneId , volumeName , url , format , diskOfferingId , Volume .State .Allocated );
346346
347347 VolumeInfo vol = volFactory .getVolume (volume .getId ());
348348
349- RegisterVolumePayload payload = new RegisterVolumePayload (cmd .getUrl (), cmd .getChecksum (), cmd . getFormat () );
349+ RegisterVolumePayload payload = new RegisterVolumePayload (cmd .getUrl (), cmd .getChecksum (), format );
350350 vol .addPayload (payload );
351351
352352 volService .registerVolume (vol , store );
353353 return volume ;
354354 }
355355
356+ private String sanitizeFormat (String format ) {
357+ if (org .apache .commons .lang3 .StringUtils .isBlank (format )) {
358+ throw new CloudRuntimeException ("Please provide a format" );
359+ }
360+
361+ String uppercase = format .toUpperCase ();
362+ try {
363+ ImageFormat .valueOf (uppercase );
364+ } catch (IllegalArgumentException e ) {
365+ String msg = "Image format: " + format + " is incorrect. Supported formats are " + EnumUtils .listValues (ImageFormat .values ());
366+ s_logger .error ("ImageFormat IllegalArgumentException: " + e .getMessage (), e );
367+ throw new IllegalArgumentException (msg );
368+ }
369+ return uppercase ;
370+ }
371+
356372 @ Override
357373 @ ActionEvent (eventType = EventTypes .EVENT_VOLUME_UPLOAD , eventDescription = "uploading volume for post upload" , async = true )
358374 public GetUploadParamsResponse uploadVolume (final GetUploadParamsForVolumeCmd cmd ) throws ResourceAllocationException , MalformedURLException {
@@ -361,7 +377,7 @@ public GetUploadParamsResponse uploadVolume(final GetUploadParamsForVolumeCmd cm
361377 final Account owner = _entityMgr .findById (Account .class , ownerId );
362378 final Long zoneId = cmd .getZoneId ();
363379 final String volumeName = cmd .getName ();
364- String format = cmd .getFormat ();
380+ String format = sanitizeFormat ( cmd .getFormat () );
365381 final Long diskOfferingId = cmd .getDiskOfferingId ();
366382 String imageStoreUuid = cmd .getImageStoreUuid ();
367383 final DataStore store = _tmpltMgr .getImageStore (imageStoreUuid , zoneId );
@@ -372,11 +388,11 @@ public GetUploadParamsResponse uploadVolume(final GetUploadParamsForVolumeCmd cm
372388 @ Override
373389 public GetUploadParamsResponse doInTransaction (TransactionStatus status ) throws MalformedURLException {
374390
375- VolumeVO volume = persistVolume (owner , zoneId , volumeName , null , cmd . getFormat () , diskOfferingId , Volume .State .NotUploaded );
391+ VolumeVO volume = persistVolume (owner , zoneId , volumeName , null , format , diskOfferingId , Volume .State .NotUploaded );
376392
377393 VolumeInfo vol = volFactory .getVolume (volume .getId ());
378394
379- RegisterVolumePayload payload = new RegisterVolumePayload (null , cmd .getChecksum (), cmd . getFormat () );
395+ RegisterVolumePayload payload = new RegisterVolumePayload (null , cmd .getChecksum (), format );
380396 vol .addPayload (payload );
381397
382398 Pair <EndPoint , DataObject > pair = volService .registerVolumeForPostUpload (vol , store );
@@ -464,12 +480,7 @@ private boolean validateVolume(Account caller, long ownerId, Long zoneId, String
464480 _resourceLimitMgr .checkResourceLimit (_accountMgr .getAccount (ownerId ), ResourceType .secondary_storage );
465481 }
466482
467- try {
468- ImageFormat .valueOf (format .toUpperCase ());
469- } catch (IllegalArgumentException e ) {
470- s_logger .debug ("ImageFormat IllegalArgumentException: " + e .getMessage ());
471- throw new IllegalArgumentException ("Image format: " + format + " is incorrect. Supported formats are " + EnumUtils .listValues (ImageFormat .values ()));
472- }
483+ sanitizeFormat (format );
473484
474485 // Check that the the disk offering specified is valid
475486 if (diskOfferingId != null ) {
0 commit comments