2323import static com .google .common .collect .Lists .newArrayList ;
2424
2525import java .io .File ;
26+ import java .io .UnsupportedEncodingException ;
2627import java .net .URI ;
2728import java .util .ArrayList ;
2829import java .util .Arrays ;
@@ -811,7 +812,7 @@ public Answer createVolume(final CreateObjectCommand cmd) {
811812 final SR poolSr = hypervisorResource .getStorageRepository (conn ,
812813 CitrixHelper .getSRNameLabel (primaryStore .getUuid (), primaryStore .getPoolType (), primaryStore .getPath ()));
813814 VDI .Record vdir = new VDI .Record ();
814- vdir .nameLabel = volume .getName ();
815+ vdir .nameLabel = getEncodedVolumeName ( volume .getName () );
815816 vdir .SR = poolSr ;
816817 vdir .type = Types .VdiType .USER ;
817818
@@ -832,6 +833,26 @@ public Answer createVolume(final CreateObjectCommand cmd) {
832833 }
833834 }
834835
836+ private String getEncodedVolumeName (String volumeName ) throws UnsupportedEncodingException {
837+ byte [] utf8Bytes = volumeName .getBytes ("UTF-8" );
838+ // Decode UTF-8 into a Java String (UTF-16)
839+ String decoded = new String (utf8Bytes , "UTF-8" );
840+ // Print each code unit as a Unicode escape
841+ StringBuilder unicodeEscaped = new StringBuilder ();
842+ for (int i = 0 ; i < decoded .length (); i ++) {
843+ char ch = decoded .charAt (i );
844+ if (ch <= 127 && Character .isLetterOrDigit (ch )) {
845+ // Keep ASCII alphanumerics as-is
846+ unicodeEscaped .append (ch );
847+ } else {
848+ // Escape non-ASCII characters
849+ unicodeEscaped .append (String .format ("\\ u%04X" , (int ) ch ));
850+ }
851+ }
852+
853+ return unicodeEscaped .toString ();
854+ }
855+
835856 @ Override
836857 public Answer cloneVolumeFromBaseTemplate (final CopyCommand cmd ) {
837858 final Connection conn = hypervisorResource .getConnection ();
0 commit comments