Skip to content

Commit 3f2f34b

Browse files
committed
Merge pull request Silverpeas#5 from mmoqui/master
Fix some bugs with the online edition of documents whose the name has some non-ASCII characters.
2 parents c67d2ba + 148f77b commit 3f2f34b

File tree

4 files changed

+72
-58
lines changed

4 files changed

+72
-58
lines changed

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.silverpeas</groupId>
66
<artifactId>parent</artifactId>
7-
<version>26-SNAPSHOT</version>
7+
<version>26</version>
88
</parent>
99
<scm>
1010
<connection>scm:git:git@github.com:Silverpeas/OfficeOnline.git</connection>
@@ -60,6 +60,7 @@
6060
<dependency>
6161
<groupId>org.apache.jackrabbit</groupId>
6262
<artifactId>jackrabbit-webdav</artifactId>
63+
<version>2.6.5</version>
6364
<exclusions>
6465
<exclusion>
6566
<artifactId>commons-collections</artifactId>
@@ -70,6 +71,7 @@
7071
<dependency>
7172
<artifactId>jackrabbit-jcr-commons</artifactId>
7273
<groupId>org.apache.jackrabbit</groupId>
74+
<version>2.6.5</version>
7375
</dependency>
7476
<dependency>
7577
<groupId>xerces</groupId>

src/main/java/org/silverpeas/openoffice/util/PasswordManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
import java.security.Key;
2828
import java.util.logging.Level;
2929
import java.util.logging.Logger;
30-
3130
import javax.crypto.Cipher;
3231
import javax.crypto.spec.SecretKeySpec;
33-
3432
import org.silverpeas.openoffice.AuthenticationInfo;
3533
import org.silverpeas.openoffice.Launcher;
3634

@@ -102,8 +100,8 @@ public static AuthenticationInfo extractAuthenticationInfo(String login, String
102100
} else {
103101
clearPwd = null;
104102
}
105-
if (clearPwd == null || clearPwd.length <= 0) {
106-
clearPwd = promptForpassword();
103+
if (clearPwd == null) {
104+
clearPwd = new char[0];
107105
}
108106
String decodedLogin = URLDecoder.decode(login, "UTF-8");
109107
return new AuthenticationInfo(decodedLogin, clearPwd);

src/main/java/org/silverpeas/openoffice/windows/FileWebDavAccessManager.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
import java.io.IOException;
2525
import java.util.logging.Level;
2626
import java.util.logging.Logger;
27-
2827
import org.apache.commons.httpclient.HttpException;
2928
import org.apache.commons.httpclient.URI;
3029
import org.apache.commons.httpclient.URIException;
31-
3230
import org.silverpeas.openoffice.AuthenticationInfo;
3331
import org.silverpeas.openoffice.util.MessageUtil;
3432
import org.silverpeas.openoffice.windows.webdav.WebdavManager;
@@ -40,10 +38,10 @@
4038
*/
4139
public class FileWebDavAccessManager {
4240

43-
private String userName;
44-
private char[] password;
41+
private final String userName;
42+
private final char[] password;
4543
private String lockToken = null;
46-
static Logger logger = Logger.getLogger(FileWebDavAccessManager.class.getName());
44+
static final Logger logger = Logger.getLogger(FileWebDavAccessManager.class.getName());
4745

4846
/**
4947
* The AccessManager is inited with authentication info to avoid login prompt
@@ -75,10 +73,15 @@ public String retrieveFile(String url) throws HttpException, IOException {
7573
lockToken = webdav.lockFile(uri, userName);
7674
logger.log(Level.INFO, "{0}{1}{2}", new Object[]{MessageUtil.getMessage("info.webdav.locked"),
7775
' ', lockToken});
78-
String tmpFile = webdav.getFile(uri, lockToken);
79-
logger.log(Level.INFO, "{0}{1}{2}", new Object[]{MessageUtil.getMessage(
80-
"info.webdav.file.locally.saved"), ' ', tmpFile});
81-
return tmpFile;
76+
try {
77+
String tmpFile = webdav.getFile(uri, lockToken);
78+
logger.log(Level.INFO, "{0}{1}{2}", new Object[]{MessageUtil.getMessage(
79+
"info.webdav.file.locally.saved"), ' ', tmpFile});
80+
return tmpFile;
81+
} catch (IOException ex) {
82+
webdav.unlockFile(uri, lockToken);
83+
throw ex;
84+
}
8285
}
8386

8487
/**
@@ -108,6 +111,6 @@ public void pushFile(String tmpFilePath, String url) throws HttpException, IOExc
108111
}
109112

110113
private static URI getURI(String url) throws URIException {
111-
return new URI(url.replaceAll("%20", " "), false, "UTF-8");
114+
return new URI(url, false, "UTF-8");
112115
}
113-
}
116+
}

src/main/java/org/silverpeas/openoffice/windows/webdav/WebdavManager.java

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,45 @@
2020
*/
2121
package org.silverpeas.openoffice.windows.webdav;
2222

23-
import java.io.IOException;
24-
import java.util.logging.Level;
25-
import java.util.logging.Logger;
26-
import org.apache.commons.httpclient.URI;
27-
import org.apache.commons.httpclient.Credentials;
28-
import org.apache.commons.httpclient.HostConfiguration;
29-
import org.apache.commons.httpclient.HttpClient;
30-
import org.apache.commons.httpclient.HttpConnectionManager;
31-
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
32-
import org.apache.commons.httpclient.UsernamePasswordCredentials;
33-
import org.apache.commons.httpclient.auth.AuthScope;
34-
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
35-
import org.apache.jackrabbit.webdav.client.methods.LockMethod;
36-
import org.apache.jackrabbit.webdav.lock.Scope;
37-
import org.apache.jackrabbit.webdav.lock.Type;
38-
39-
import org.silverpeas.openoffice.util.MessageUtil;
4023
import java.io.BufferedInputStream;
4124
import java.io.ByteArrayOutputStream;
4225
import java.io.File;
4326
import java.io.FileInputStream;
4427
import java.io.FileOutputStream;
28+
import java.io.IOException;
4529
import java.io.InterruptedIOException;
4630
import java.net.URLDecoder;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
4733
import javax.swing.ProgressMonitor;
4834
import javax.swing.ProgressMonitorInputStream;
4935
import javax.swing.UIManager;
36+
import org.apache.commons.httpclient.Credentials;
37+
import org.apache.commons.httpclient.HostConfiguration;
38+
import org.apache.commons.httpclient.HttpClient;
39+
import org.apache.commons.httpclient.HttpConnectionManager;
5040
import org.apache.commons.httpclient.HttpVersion;
41+
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
42+
import org.apache.commons.httpclient.URI;
43+
import org.apache.commons.httpclient.URIException;
44+
import org.apache.commons.httpclient.UsernamePasswordCredentials;
45+
import org.apache.commons.httpclient.auth.AuthScope;
5146
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
5247
import org.apache.commons.httpclient.methods.GetMethod;
48+
import org.apache.commons.httpclient.methods.HeadMethod;
5349
import org.apache.commons.httpclient.methods.RequestEntity;
5450
import org.apache.commons.httpclient.params.HttpClientParams;
51+
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
5552
import org.apache.jackrabbit.webdav.DavException;
53+
import org.apache.jackrabbit.webdav.client.methods.LockMethod;
5654
import org.apache.jackrabbit.webdav.client.methods.PutMethod;
5755
import org.apache.jackrabbit.webdav.client.methods.UnLockMethod;
56+
import org.apache.jackrabbit.webdav.lock.Scope;
57+
import org.apache.jackrabbit.webdav.lock.Type;
58+
import org.silverpeas.openoffice.util.MessageUtil;
5859

59-
import static java.net.HttpURLConnection.HTTP_OK;
6060
import static java.net.HttpURLConnection.HTTP_CREATED;
61+
import static java.net.HttpURLConnection.HTTP_OK;
6162

6263
/**
6364
* Simple class to help manipulate Webdav ressources.
@@ -66,7 +67,7 @@
6667
*/
6768
public class WebdavManager {
6869

69-
private HttpClient client;
70+
private final HttpClient client;
7071
static final Logger logger = Logger.getLogger(WebdavManager.class.getName());
7172

7273
/**
@@ -102,11 +103,11 @@ public WebdavManager(String host, String login, String password) {
102103
* @throws IOException
103104
*/
104105
public String lockFile(URI uri, String login) throws IOException {
106+
String url = decodeURI(uri);
105107
logger.log(Level.INFO, "{0} {1}", new Object[]{MessageUtil.getMessage("info.webdav.locking"),
106-
uri.getEscapedURI()});
108+
url});
107109
// Let's lock the file
108-
LockMethod lockMethod = new LockMethod(uri.getEscapedURI(),
109-
Scope.EXCLUSIVE, Type.WRITE, login, 600000l, false);
110+
LockMethod lockMethod = new LockMethod(url, Scope.EXCLUSIVE, Type.WRITE, login, 600000l, false);
110111
client.executeMethod(lockMethod);
111112
if (lockMethod.succeeded()) {
112113
return lockMethod.getLockToken();
@@ -130,12 +131,13 @@ public void unlockFile(URI uri, String lockToken) throws IOException {
130131
if (lockToken == null || lockToken.isEmpty()) {
131132
return;
132133
}
133-
UnLockMethod unlockMethod = new UnLockMethod(uri.getEscapedURI(), lockToken);
134+
String url = decodeURI(uri);
135+
UnLockMethod unlockMethod = new UnLockMethod(url, lockToken);
134136
client.executeMethod(unlockMethod);
135137
if (unlockMethod.getStatusCode() != 200 && unlockMethod.getStatusCode() != 204) {
136138
logger.log(Level.INFO, "{0} {1}", new Object[]{MessageUtil.
137-
getMessage("error.webdav.unlocking"),
138-
unlockMethod.getStatusCode()});
139+
getMessage("error.webdav.unlocking"),
140+
unlockMethod.getStatusCode()});
139141
}
140142
try {
141143
unlockMethod.checkSuccess();
@@ -175,14 +177,14 @@ public String getFile(URI uri, String lockToken) throws IOException {
175177
File tmpFile = new File(tempDir, fileName);
176178
FileOutputStream fos = new FileOutputStream(tmpFile);
177179
byte[] data = new byte[64];
178-
int c = 0;
180+
int c;
179181
try {
180182
while ((c = is.read(data)) > -1) {
181183
fos.write(data, 0, c);
182184
}
183185
} catch (InterruptedIOException ioinex) {
184186
logger.log(Level.INFO, "{0} {1}", new Object[]{MessageUtil.getMessage("info.user.cancel"),
185-
ioinex.getMessage()});
187+
ioinex.getMessage()});
186188
unlockFile(uri, lockToken);
187189
System.exit(0);
188190
} finally {
@@ -200,28 +202,26 @@ public String getFile(URI uri, String lockToken) throws IOException {
200202
* @throws IOException
201203
*/
202204
public void putFile(URI uri, String localFilePath, String lockToken) throws IOException {
205+
String url = decodeURI(uri);
203206
// Checks if file still exists
204-
try {
205-
executeGetFile(uri);
206-
} catch (IOException ioex) {
207+
if (!isFileExist(url)) {
207208
logger.log(Level.SEVERE, MessageUtil.getMessage("error.remote.file"));
208209
throw new IOException(MessageUtil.getMessage("error.remote.file"));
209210
}
210-
PutMethod putMethod = new PutMethod(uri.getEscapedURI());
211+
PutMethod putMethod = new PutMethod(url);
211212
logger.log(Level.INFO, "{0} {1}", new Object[]{MessageUtil.getMessage("info.webdav.put"),
212-
localFilePath});
213-
File file = new File(localFilePath);
213+
localFilePath});
214+
File localFile = new File(localFilePath);
215+
String remoteFileName = uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1);
214216
UploadProgressBar progress = new UploadProgressBar();
215-
progress.setMaximum(new Long(file.length()).intValue());
216-
progress.setMessage(MessageUtil.getMessage("uploading.remote.file") + ' ' + uri.getPath().
217-
substring(
218-
uri.getPath().lastIndexOf('/') + 1));
219-
MonitoredInputStream is =
220-
new MonitoredInputStream(new BufferedInputStream(new FileInputStream(file)));
217+
progress.setMaximum(new Long(localFile.length()).intValue());
218+
progress.setMessage(MessageUtil.getMessage("uploading.remote.file") + ' ' + remoteFileName);
219+
MonitoredInputStream is = new MonitoredInputStream(new BufferedInputStream(new FileInputStream(
220+
localFile)));
221221
is.addPropertyChangeListener(progress);
222222
ByteArrayOutputStream baos = new ByteArrayOutputStream();
223223
byte[] data = new byte[64];
224-
int c = 0;
224+
int c;
225225
while ((c = is.read(data)) > -1) {
226226
baos.write(data, 0, c);
227227
}
@@ -240,13 +240,24 @@ public void putFile(URI uri, String localFilePath, String lockToken) throws IOEx
240240
}
241241

242242
private GetMethod executeGetFile(URI uri) throws IOException {
243-
GetMethod method = new GetMethod();
244-
method.setURI(uri);
243+
String url = decodeURI(uri);
244+
logger.log(Level.INFO, "Get file located at: {0}", url);
245+
GetMethod method = new GetMethod(url);
245246
client.executeMethod(method);
246247
if (method.getStatusCode() != HTTP_CREATED && method.getStatusCode() != HTTP_OK) {
247248
throw new IOException(MessageUtil.getMessage("error.get.remote.file")
248249
+ ' ' + method.getStatusCode() + " - " + method.getStatusText());
249250
}
250251
return method;
251252
}
253+
254+
private boolean isFileExist(String url) throws IOException {
255+
HeadMethod method = new HeadMethod(url);
256+
client.executeMethod(method);
257+
return method.getStatusCode() == HTTP_OK;
258+
}
259+
260+
private String decodeURI(URI uri) throws URIException {
261+
return uri.getURI(); //.replaceAll(" ", "%20");
262+
}
252263
}

0 commit comments

Comments
 (0)