Skip to content

Commit f2b0f41

Browse files
committed
migrated jcifs, mime4j to latest
1 parent 91a9527 commit f2b0f41

4 files changed

Lines changed: 52 additions & 35 deletions

File tree

ivy.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<dependency org="io.opentracing" name="opentracing-util" rev="0.33.0"/>
3232
<dependency org="javax.servlet" name="javax.servlet-api" rev="4.0.1"/>
3333
<dependency org="javainetlocator" name="inetaddresslocator" rev="2.18" />
34-
<dependency org="jcifs" name="jcifs" rev="1.3.17" conf="compile->master" />
34+
<dependency org="eu.agno3.jcifs" name="jcifs-ng" rev="2.1.10" />
3535
<dependency org="net.arnx" name="jsonic" rev="1.3.10"/>
3636
<dependency org="net.jthink" name="jaudiotagger" rev="3.0.1"/>
3737
<dependency org="net.sourceforge.jchardet" name="jchardet" rev="1.0"/>
@@ -44,7 +44,8 @@
4444
<dependency org="org.apache.commons" name="commons-lang3" rev="3.20.0" />
4545
<dependency org="org.apache.httpcomponents" name="httpclient" rev="4.5.14"/>
4646
<dependency org="org.apache.httpcomponents" name="httpmime" rev="4.5.14"/>
47-
<dependency org="org.apache.james" name="apache-mime4j" rev="0.6"/>
47+
<dependency org="org.apache.james" name="apache-mime4j-core" rev="0.8.14"/>
48+
<dependency org="org.apache.james" name="apache-mime4j-dom" rev="0.8.14"/>
4849
<dependency org="org.apache.lucene" name="lucene-analysis-common" rev="9.0.0"/>
4950
<dependency org="org.apache.lucene" name="lucene-backward-codecs" rev="9.0.0" />
5051
<dependency org="org.apache.lucene" name="lucene-classification" rev="9.0.0" />

source/net/yacy/cora/document/id/MultiProtocolURL.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
import org.apache.http.HttpStatus;
5454

55+
import jcifs.CIFSContext;
56+
import jcifs.context.SingletonContext;
5557
import jcifs.smb.SmbException;
5658
import jcifs.smb.SmbFile;
5759
import 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();

source/net/yacy/crawler/retrieval/SMBLoader.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Date;
3939
import java.util.List;
4040

41+
import jcifs.CIFSContext;
42+
import jcifs.context.SingletonContext;
4143
import jcifs.smb.SmbException;
4244
import jcifs.smb.SmbFile;
4345
import jcifs.smb.SmbFileInputStream;
@@ -60,6 +62,7 @@
6062
public class SMBLoader {
6163

6264
public static final long DEFAULT_MAXFILESIZE = 1024 * 1024 * 10;
65+
private static final CIFSContext SMB_CONTEXT = SingletonContext.getInstance();
6366

6467
private final Switchboard sb;
6568
private final ConcurrentLog log;
@@ -120,8 +123,9 @@ public StreamResponse openInputStream(final Request request, final boolean accep
120123
s = MultiProtocolURL.escape(s).toString();
121124
if (!s.endsWith("/") && !s.endsWith("\\")) {
122125
// check if this is a directory
123-
SmbFile sf = new SmbFile(u + s);
124-
if (sf.isDirectory()) s = s + "/";
126+
try (final SmbFile sf = new SmbFile(u + s, SMB_CONTEXT)) {
127+
if (sf.isDirectory()) s = s + "/";
128+
}
125129
}
126130
list.add(u + s);
127131
}
@@ -198,20 +202,17 @@ public StreamResponse openInputStream(final Request request, final boolean accep
198202
}
199203

200204
public static void main(String[] args) {
201-
//jcifs.Config.setProperty( "jcifs.netbios.wins", "192.168.1.220" );
202-
//NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
203-
SmbFileInputStream in;
204-
try {
205-
SmbFile sf = new SmbFile(args[0]);
205+
try (final SmbFile sf = new SmbFile(args[0], SMB_CONTEXT)) {
206206
if (sf.isDirectory()) {
207207
String[] s = sf.list();
208208
for (String t: s) System.out.println(t);
209209
} else {
210-
in = new SmbFileInputStream(sf);
211-
byte[] b = new byte[8192];
212-
int n;
213-
while(( n = in.read( b )) > 0 ) {
214-
System.out.write( b, 0, n );
210+
try (final SmbFileInputStream in = new SmbFileInputStream(sf)) {
211+
byte[] b = new byte[8192];
212+
int n;
213+
while(( n = in.read( b )) > 0 ) {
214+
System.out.write( b, 0, n );
215+
}
215216
}
216217
}
217218
} catch (final SmbException e) {

test/java/net/yacy/cora/document/id/MultiProtocolURLTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.yacy.cora.document.id;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertSame;
45
import static org.junit.Assert.assertTrue;
56
import static org.junit.Assert.fail;
67

@@ -15,10 +16,21 @@
1516

1617
import org.junit.Test;
1718

19+
import jcifs.context.SingletonContext;
20+
import jcifs.smb.SmbFile;
21+
1822
/**
1923
* Automated unit tests for the {@link MultiProtocolURL} class.
2024
*/
2125
public class MultiProtocolURLTest {
26+
27+
@Test
28+
public void testSmbFileUsesSharedContext() throws MalformedURLException {
29+
final MultiProtocolURL url = new MultiProtocolURL("smb://example.test/share/file.txt");
30+
try (final SmbFile smbFile = url.getSmbFile()) {
31+
assertSame(SingletonContext.getInstance(), smbFile.getContext());
32+
}
33+
}
2234

2335
@Test
2436
public void testSessionIdRemoval() throws MalformedURLException {
@@ -511,4 +523,3 @@ public void testFileConstructor() throws MalformedURLException, URISyntaxExcepti
511523
}
512524

513525

514-

0 commit comments

Comments
 (0)