Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 67e3c0f

Browse files
author
Emmanuel Hugonnet
committed
Fixing unlock for Vista too early
Progress Bar(download/upload) for Vista git-svn-id: https://www.silverpeas.org/svn/silverpeas/services/office-online/trunk@510 a8e77078-a1c7-4fa5-b8fc-53c5178a176c
1 parent 291ba4f commit 67e3c0f

File tree

3 files changed

+54
-42
lines changed

3 files changed

+54
-42
lines changed

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

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
import com.silverpeas.openoffice.*;
2727
import com.silverpeas.openoffice.util.MessageUtil;
28+
import java.io.BufferedInputStream;
2829
import java.io.ByteArrayOutputStream;
2930
import java.io.File;
31+
import java.io.File;
3032
import java.io.FileInputStream;
3133
import java.io.FileOutputStream;
3234
import java.io.IOException;
@@ -41,6 +43,8 @@
4143

4244
import java.util.logging.Level;
4345
import java.util.logging.Logger;
46+
import javax.swing.ProgressMonitor;
47+
import javax.swing.ProgressMonitorInputStream;
4448
import org.apache.commons.httpclient.Credentials;
4549
import org.apache.commons.httpclient.HostConfiguration;
4650
import org.apache.commons.httpclient.HttpClient;
@@ -117,13 +121,11 @@ public String retrieveFile(String url) throws HttpException, IOException,
117121
URI uri = getURI(url);
118122
URL formattedUrl = new URL(uri.getEscapedURI());
119123
HttpClient client = initConnection(formattedUrl);
120-
121124
logger.log(Level.INFO, MessageUtil.getMessage("info.webdav.locking")
122125
+ ' ' + uri.getEscapedURI());
123126
//Let's lock the file
124127
LockMethod lockMethod = new LockMethod(uri.getEscapedURI(),
125-
Scope.EXCLUSIVE, Type.WRITE,
126-
userName, 600000l, false);
128+
Scope.EXCLUSIVE, Type.WRITE, userName, 600000l, false);
127129
client.executeMethod(lockMethod);
128130
if (lockMethod.succeeded()) {
129131
lockToken = lockMethod.getLockToken();
@@ -139,27 +141,30 @@ public String retrieveFile(String url) throws HttpException, IOException,
139141
client.executeMethod(method);
140142
if (method.getStatusCode() != 200) {
141143
throw new IOException(MessageUtil.getMessage("error.get.remote.file")
142-
+ +' ' + method.getStatusCode() + " - " + method.getStatusText());
144+
+ ' ' + method.getStatusCode() + " - " + method.getStatusText());
143145
}
144-
145146
String fileName = formattedUrl.getFile();
146147
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
147148
fileName = URLDecoder.decode(fileName, "UTF-8");
149+
ProgressMonitorInputStream is = new ProgressMonitorInputStream(null,
150+
MessageUtil.getMessage("downloading.remote.file") + " " + fileName,
151+
new BufferedInputStream(method.getResponseBodyAsStream()));
148152
fileName = fileName.replace(' ', '_');
149-
InputStream is = method.getResponseBodyAsStream();
153+
ProgressMonitor monitor = is.getProgressMonitor();
154+
monitor.setMaximum(new Long(method.getResponseContentLength()).intValue());
150155
File tempDir = new File(System.getProperty("java.io.tmpdir"), "silver-"
151156
+ System.currentTimeMillis());
152157
tempDir.mkdirs();
153158
File tmpFile = new File(tempDir, fileName);
154159
FileOutputStream fos = new FileOutputStream(tmpFile);
155-
int data = is.read();
156-
while (data != -1) {
157-
fos.write(data);
158-
data = is.read();
160+
byte[] data = new byte[64];
161+
int c = 0;
162+
while ((c = is.read(data)) > -1) {
163+
fos.write(data, 0, c);
159164
}
160165
fos.close();
161-
logger.log(Level.INFO, MessageUtil.getMessage(
162-
"info.webdav.file.locally.saved") + ' ' + tmpFile.getAbsolutePath());
166+
logger.log(Level.INFO, MessageUtil.getMessage("info.webdav.file.locally.saved") + ' '
167+
+ tmpFile.getAbsolutePath());
163168
return tmpFile.getAbsolutePath();
164169
}
165170

@@ -183,52 +188,53 @@ public void pushFile(String tmpFilePath, String url) throws HttpException,
183188
*/
184189
URI uri = getURI(url);
185190
HttpClient client = initConnection(new URL(uri.getEscapedURI()));
186-
191+
187192
/*
188193
* Checks if file still exists
189194
*/
190195
GetMethod method = new GetMethod();
191196
method.setURI(uri);
192197
client.executeMethod(method);
193-
194198
if (method.getStatusCode() == 200) {
195-
logger.log(Level.INFO, MessageUtil.getMessage("info.webdav.unlocking")
196-
+ ' ' + uri.getEscapedURI());
197-
//Let's lock the file
198-
UnLockMethod unlockMethod = new UnLockMethod(uri.getEscapedURI(), lockToken);
199-
client.executeMethod(unlockMethod);
200-
if (unlockMethod.getStatusCode() != 200 && unlockMethod.getStatusCode()
201-
!= 204) {
202-
logger.log(Level.INFO,
203-
MessageUtil.getMessage("error.webdav.unlocking") + ' ' + unlockMethod.getStatusCode());
204-
}
205-
try {
206-
unlockMethod.checkSuccess();
207-
logger.log(Level.INFO, MessageUtil.getMessage("info.webdav.unlocked"));
208-
} catch (DavException ex) {
209-
logger.log(Level.SEVERE,
210-
MessageUtil.getMessage("error.webdav.unlocking"), ex);
211-
throw new IOException(MessageUtil.getMessage("error.webdav.unlocking"),
212-
ex);
213-
}
214-
215199
PutMethod putMethod = new PutMethod(uri.getEscapedURI());
216200
logger.log(Level.INFO, MessageUtil.getMessage("info.webdav.put")
217201
+ ' ' + tmpFilePath);
218202
File file = new File(tmpFilePath);
219-
InputStream is = new FileInputStream(file);
203+
ProgressMonitorInputStream is = new ProgressMonitorInputStream(null,
204+
MessageUtil.getMessage("uploading.remote.file") + " " + file.getName(),
205+
new BufferedInputStream(new FileInputStream(file)));
206+
ProgressMonitor monitor = is.getProgressMonitor();
207+
monitor.setMaximum(new Long(file.length()).intValue());
220208
ByteArrayOutputStream baos = new ByteArrayOutputStream();
221-
int data = is.read();
222-
while (data != -1) {
223-
baos.write(data);
224-
data = is.read();
209+
byte[] data = new byte[64];
210+
int c = 0;
211+
while ((c = is.read(data)) > -1) {
212+
baos.write(data, 0, c);
225213
}
226-
byte[] bytes = baos.toByteArray();
227-
RequestEntity requestEntity = new ByteArrayRequestEntity(bytes);
214+
RequestEntity requestEntity = new ByteArrayRequestEntity(baos.toByteArray());
228215
putMethod.setRequestEntity(requestEntity);
216+
putMethod.setRequestHeader(PutMethod.HEADER_LOCK_TOKEN, lockToken);
229217
client.executeMethod(putMethod);
230218
if (putMethod.succeeded()) {
231219
logger.log(Level.INFO, MessageUtil.getMessage("info.file.updated"));
220+
logger.log(Level.INFO, MessageUtil.getMessage("info.webdav.unlocking")
221+
+ ' ' + uri.getEscapedURI());
222+
//Let's unlock the file
223+
UnLockMethod unlockMethod = new UnLockMethod(uri.getEscapedURI(), lockToken);
224+
client.executeMethod(unlockMethod);
225+
if (unlockMethod.getStatusCode() != 200 && unlockMethod.getStatusCode() != 204) {
226+
logger.log(Level.INFO, MessageUtil.getMessage("error.webdav.unlocking") + ' '
227+
+ unlockMethod.getStatusCode());
228+
}
229+
try {
230+
unlockMethod.checkSuccess();
231+
logger.log(Level.INFO, MessageUtil.getMessage("info.webdav.unlocked"));
232+
} catch (DavException ex) {
233+
logger.log(Level.SEVERE,
234+
MessageUtil.getMessage("error.webdav.unlocking"), ex);
235+
throw new IOException(MessageUtil.getMessage("error.webdav.unlocking"),
236+
ex);
237+
}
232238
// delete temp file
233239
file.delete();
234240
file.getParentFile().delete();

src/main/resources/com/silverpeas/openoffice/messages.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ error.webdav.locking=Couldn't lock the remote file:
5252
error.get.remote.file=Couldn't get the remote file:
5353
error.put.remote.file=Couldn't upload the file on the server
5454
error.remote.file=Remote file doesn't exist anymore
55-
error.webdav.unlocking=Couldn't unlock the remote file:
55+
error.webdav.unlocking=Couldn't unlock the remote file:
56+
57+
downloading.remote.file=Downloading file:
58+
uploading.remote.file=Uploading file:

src/main/resources/com/silverpeas/openoffice/messages_fr.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ error.get.remote.file=Impossible de r\u00E9cup\u00E9rer le fichier distant:
5353
error.put.remote.file=Impossible de poser le fichier sur le serveur
5454
error.remote.file=Le fichier distant n'existe plus
5555
error.webdav.unlocking=Impossible de deverrouiller le fichier distant:
56+
57+
downloading.remote.file=T\u00E9l\u00E9chargement du fichier:
58+
uploading.remote.file=Envoi sur le serveur du fichier:

0 commit comments

Comments
 (0)