3737import org .apache .commons .codec .DecoderException ;
3838import org .apache .commons .codec .net .URLCodec ;
3939import org .apache .commons .lang .NotImplementedException ;
40+ import org .apache .hadoop .fs .Path ;
4041import org .apache .http .client .utils .URIBuilder ;
4142
4243import com .microsoft .azure .storage .AccessCondition ;
@@ -80,7 +81,7 @@ public InMemoryBlockBlobStore getBackingStore() {
8081 * Mocks the situation where a container already exists before WASB comes in,
8182 * i.e. the situation where a user creates a container then mounts WASB on the
8283 * pre-existing container.
83- *
84+ *
8485 * @param uri
8586 * The URI of the container.
8687 * @param metadata
@@ -137,9 +138,20 @@ private static String convertUriToDecodedString(URI uri) {
137138
138139 private static URI convertKeyToEncodedUri (String key ) {
139140 try {
140- return new URIBuilder ().setPath (key ).build ();
141+ Path p = new Path (key );
142+ URI unEncodedURI = p .toUri ();
143+ return new URIBuilder ().setPath (unEncodedURI .getPath ())
144+ .setScheme (unEncodedURI .getScheme ()).build ();
141145 } catch (URISyntaxException e ) {
142- throw new AssertionError ("Failed to encode key: " + key );
146+ int i = e .getIndex ();
147+ String details ;
148+ if (i >= 0 ) {
149+ details = " -- \" " + e .getInput ().charAt (i ) + "\" " ;
150+ } else {
151+ details = "" ;
152+ }
153+ throw new AssertionError ("Failed to encode key: " + key
154+ + ": " + e + details );
143155 }
144156 }
145157
@@ -148,8 +160,8 @@ public CloudBlobContainerWrapper getContainerReference(String name)
148160 throws URISyntaxException , StorageException {
149161 String fullUri ;
150162 URIBuilder builder = new URIBuilder (baseUriString );
151- fullUri = builder .setPath ( builder . getPath () + "/" + name ). toString () ;
152-
163+ String path = builder .getPath () == null ? "" : builder . getPath () + "/" ;
164+ fullUri = builder . setPath ( path + name ). toString ();
153165 MockCloudBlobContainerWrapper container = new MockCloudBlobContainerWrapper (
154166 fullUri , name );
155167 // Check if we have a pre-existing container with that name, and prime
@@ -354,11 +366,11 @@ protected MockCloudBlobWrapper(URI uri, HashMap<String, String> metadata,
354366 this .uri = uri ;
355367 this .metadata = metadata ;
356368 this .properties = new BlobProperties ();
357-
369+
358370 this .properties =updateLastModifed (this .properties );
359371 this .properties =updateLength (this .properties ,length );
360372 }
361-
373+
362374 protected BlobProperties updateLastModifed (BlobProperties properties ){
363375 try {
364376 Method setLastModified =properties .getClass ().
@@ -371,7 +383,7 @@ protected BlobProperties updateLastModifed(BlobProperties properties){
371383 }
372384 return properties ;
373385 }
374-
386+
375387 protected BlobProperties updateLength (BlobProperties properties ,int length ) {
376388 try {
377389 Method setLength =properties .getClass ().
@@ -383,7 +395,7 @@ protected BlobProperties updateLength(BlobProperties properties,int length) {
383395 }
384396 return properties ;
385397 }
386-
398+
387399 protected void refreshProperties (boolean getMetadata ) {
388400 if (backingStore .exists (convertUriToDecodedString (uri ))) {
389401 byte [] content = backingStore .getContent (convertUriToDecodedString (uri ));
0 commit comments