5252
5353import org .apache .http .HttpStatus ;
5454
55+ import jcifs .CIFSContext ;
56+ import jcifs .context .SingletonContext ;
5557import jcifs .smb .SmbException ;
5658import jcifs .smb .SmbFile ;
5759import jcifs .smb .SmbFileInputStream ;
@@ -77,6 +79,7 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
7779
7880 private static final long serialVersionUID = -1173233022912141884L ;
7981 private static final long SMB_TIMEOUT = 5000 ;
82+ private static final CIFSContext SMB_CONTEXT = SingletonContext .getInstance ();
8083
8184 public static final int TLD_any_zone_filter = 255 ; // from TLD zones can be filtered during search; this is the catch-all filter
8285 private static final Pattern backPathPattern = Pattern .compile ("(/[^/]+(?<!/\\ .{1,2})/)[.]{2}(?=/|$)|/\\ .(?=/)|/(?=/)" );
@@ -2393,16 +2396,16 @@ public java.io.File getFSFile() throws MalformedURLException {
23932396 public SmbFile getSmbFile () throws MalformedURLException {
23942397 if (!isSMB ()) throw new MalformedURLException ();
23952398 final String url = unescape (this .toNormalform (true ));
2396- return new SmbFile (url );
2399+ return new SmbFile (url , SMB_CONTEXT );
23972400 }
23982401
23992402 // some methods that let the MultiProtocolURI look like a java.io.File object
24002403 // to use these methods the object must be either of type isFile() or isSMB()
24012404
24022405 public boolean exists () throws IOException {
24032406 if (isFile ()) return getFSFile ().exists ();
2404- if (isSMB ()) try {
2405- return TimeoutRequest .exists (getSmbFile () , SMB_TIMEOUT );
2407+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2408+ return TimeoutRequest .exists (smbFile , SMB_TIMEOUT );
24062409 } catch (final SmbException e ) {
24072410 throw new IOException ("SMB.exists SmbException (" + e .getMessage () + ") for " + toNormalform (false ));
24082411 } catch (final MalformedURLException e ) {
@@ -2413,8 +2416,8 @@ public boolean exists() throws IOException {
24132416
24142417 public boolean canRead () throws IOException {
24152418 if (isFile ()) return getFSFile ().canRead ();
2416- if (isSMB ()) try {
2417- return TimeoutRequest .canRead (getSmbFile () , SMB_TIMEOUT );
2419+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2420+ return TimeoutRequest .canRead (smbFile , SMB_TIMEOUT );
24182421 } catch (final SmbException e ) {
24192422 throw new IOException ("SMB.canRead SmbException (" + e .getMessage () + ") for " + toNormalform (false ));
24202423 } catch (final MalformedURLException e ) {
@@ -2425,8 +2428,8 @@ public boolean canRead() throws IOException {
24252428
24262429 public boolean canWrite () throws IOException {
24272430 if (isFile ()) return getFSFile ().canWrite ();
2428- if (isSMB ()) try {
2429- return TimeoutRequest .canWrite (getSmbFile () , SMB_TIMEOUT );
2431+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2432+ return TimeoutRequest .canWrite (smbFile , SMB_TIMEOUT );
24302433 } catch (final SmbException e ) {
24312434 throw new IOException ("SMB.canWrite SmbException (" + e .getMessage () + ") for " + toNormalform (false ));
24322435 } catch (final MalformedURLException e ) {
@@ -2437,8 +2440,8 @@ public boolean canWrite() throws IOException {
24372440
24382441 public boolean isHidden () throws IOException {
24392442 if (isFile ()) return getFSFile ().isHidden ();
2440- if (isSMB ()) try {
2441- return TimeoutRequest .isHidden (getSmbFile () , SMB_TIMEOUT );
2443+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2444+ return TimeoutRequest .isHidden (smbFile , SMB_TIMEOUT );
24422445 } catch (final SmbException e ) {
24432446 throw new IOException ("SMB.isHidden SmbException (" + e .getMessage () + ") for " + toNormalform (false ));
24442447 } catch (final MalformedURLException e ) {
@@ -2449,8 +2452,8 @@ public boolean isHidden() throws IOException {
24492452
24502453 public boolean isDirectory () throws IOException {
24512454 if (isFile ()) return getFSFile ().isDirectory ();
2452- if (isSMB ()) try {
2453- return TimeoutRequest .isDirectory (getSmbFile () , SMB_TIMEOUT );
2455+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2456+ return TimeoutRequest .isDirectory (smbFile , SMB_TIMEOUT );
24542457 } catch (final SmbException e ) {
24552458 throw new IOException ("SMB.isDirectory SmbException (" + e .getMessage () + ") for " + toNormalform (false ));
24562459 } catch (final MalformedURLException e ) {
@@ -2466,8 +2469,8 @@ public long length() {
24662469 ConcurrentLog .logException (e );
24672470 return -1 ;
24682471 }
2469- if (isSMB ()) try {
2470- return getSmbFile () .length ();
2472+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2473+ return smbFile .length ();
24712474 //return TimeoutRequest.length(getSmbFile(), SMB_TIMEOUT); // a timeout request is a bad idea, that will create a lot of concurrent threads during crawling
24722475 } catch (final Throwable e ) {
24732476 ConcurrentLog .logException (e );
@@ -2478,8 +2481,8 @@ public long length() {
24782481
24792482 public long lastModified () throws IOException {
24802483 if (isFile ()) return getFSFile ().lastModified ();
2481- if (isSMB ()) try {
2482- return getSmbFile () .lastModified ();
2484+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2485+ return smbFile .lastModified ();
24832486 // return TimeoutRequest.lastModified(getSmbFile(), SMB_TIMEOUT); // a timeout request is a bad idea, that will create a lot of concurrent threads during crawling
24842487 } catch (final SmbException e ) {
24852488 throw new IOException ("SMB.lastModified SmbException (" + e .getMessage () + ") for " + toNormalform (false ));
@@ -2491,8 +2494,8 @@ public long lastModified() throws IOException {
24912494
24922495 public String getName () throws IOException {
24932496 if (isFile ()) return getFSFile ().getName ();
2494- if (isSMB ()) try {
2495- return getSmbFile () .getName ();
2497+ if (isSMB ()) try ( final SmbFile smbFile = getSmbFile ()) {
2498+ return smbFile .getName ();
24962499 } catch (final MalformedURLException e ) {
24972500 throw new IOException ("SMB.getName MalformedURLException (" + e .getMessage () + ") for " + toNormalform (false ) );
24982501 }
@@ -2511,8 +2514,7 @@ public String getName() throws IOException {
25112514 */
25122515 public String [] list () throws IOException {
25132516 if (isFile () && !isHidden ()) return getFSFile ().list ();
2514- if (isSMB ()) try {
2515- final SmbFile sf = getSmbFile ();
2517+ if (isSMB ()) try (final SmbFile sf = getSmbFile ()) {
25162518 if (!sf .isDirectory () || sf .isHidden ()) return null ;
25172519 try {
25182520 return TimeoutRequest .list (sf , SMB_TIMEOUT );
@@ -2589,7 +2591,9 @@ public boolean exists(final ClientIdentification.Agent agent) {
25892591 return getFSFile ().exists ();
25902592 }
25912593 if (isSMB ()) {
2592- return getSmbFile ().exists ();
2594+ try (final SmbFile smbFile = getSmbFile ()) {
2595+ return smbFile .exists ();
2596+ }
25932597 }
25942598 if (isFTP ()) {
25952599 final FTPClient client = new FTPClient ();
0 commit comments