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 ;
@@ -810,7 +811,7 @@ public Answer createVolume(final CreateObjectCommand cmd) {
810811 final SR poolSr = hypervisorResource .getStorageRepository (conn ,
811812 CitrixHelper .getSRNameLabel (primaryStore .getUuid (), primaryStore .getPoolType (), primaryStore .getPath ()));
812813 VDI .Record vdir = new VDI .Record ();
813- vdir .nameLabel = volume .getName ();
814+ vdir .nameLabel = getEncodedVolumeName ( volume .getName () );
814815 vdir .SR = poolSr ;
815816 vdir .type = Types .VdiType .USER ;
816817
@@ -831,6 +832,26 @@ public Answer createVolume(final CreateObjectCommand cmd) {
831832 }
832833 }
833834
835+ private String getEncodedVolumeName (String volumeName ) throws UnsupportedEncodingException {
836+ byte [] utf8Bytes = volumeName .getBytes ("UTF-8" );
837+ // Decode UTF-8 into a Java String (UTF-16)
838+ String decoded = new String (utf8Bytes , "UTF-8" );
839+ // Print each code unit as a Unicode escape
840+ StringBuilder unicodeEscaped = new StringBuilder ();
841+ for (int i = 0 ; i < decoded .length (); i ++) {
842+ char ch = decoded .charAt (i );
843+ if (ch <= 127 && Character .isLetterOrDigit (ch )) {
844+ // Keep ASCII alphanumerics as-is
845+ unicodeEscaped .append (ch );
846+ } else {
847+ // Escape non-ASCII characters
848+ unicodeEscaped .append (String .format ("\\ u%04X" , (int ) ch ));
849+ }
850+ }
851+
852+ return unicodeEscaped .toString ();
853+ }
854+
834855 @ Override
835856 public Answer cloneVolumeFromBaseTemplate (final CopyCommand cmd ) {
836857 final Connection conn = hypervisorResource .getConnection ();
0 commit comments