20
20
*/
21
21
package org .silverpeas .openoffice .windows .webdav ;
22
22
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 ;
40
23
import java .io .BufferedInputStream ;
41
24
import java .io .ByteArrayOutputStream ;
42
25
import java .io .File ;
43
26
import java .io .FileInputStream ;
44
27
import java .io .FileOutputStream ;
28
+ import java .io .IOException ;
45
29
import java .io .InterruptedIOException ;
46
30
import java .net .URLDecoder ;
31
+ import java .util .logging .Level ;
32
+ import java .util .logging .Logger ;
47
33
import javax .swing .ProgressMonitor ;
48
34
import javax .swing .ProgressMonitorInputStream ;
49
35
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 ;
50
40
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 ;
51
46
import org .apache .commons .httpclient .methods .ByteArrayRequestEntity ;
52
47
import org .apache .commons .httpclient .methods .GetMethod ;
48
+ import org .apache .commons .httpclient .methods .HeadMethod ;
53
49
import org .apache .commons .httpclient .methods .RequestEntity ;
54
50
import org .apache .commons .httpclient .params .HttpClientParams ;
51
+ import org .apache .commons .httpclient .params .HttpConnectionManagerParams ;
55
52
import org .apache .jackrabbit .webdav .DavException ;
53
+ import org .apache .jackrabbit .webdav .client .methods .LockMethod ;
56
54
import org .apache .jackrabbit .webdav .client .methods .PutMethod ;
57
55
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 ;
58
59
59
- import static java .net .HttpURLConnection .HTTP_OK ;
60
60
import static java .net .HttpURLConnection .HTTP_CREATED ;
61
+ import static java .net .HttpURLConnection .HTTP_OK ;
61
62
62
63
/**
63
64
* Simple class to help manipulate Webdav ressources.
66
67
*/
67
68
public class WebdavManager {
68
69
69
- private HttpClient client ;
70
+ private final HttpClient client ;
70
71
static final Logger logger = Logger .getLogger (WebdavManager .class .getName ());
71
72
72
73
/**
@@ -102,11 +103,11 @@ public WebdavManager(String host, String login, String password) {
102
103
* @throws IOException
103
104
*/
104
105
public String lockFile (URI uri , String login ) throws IOException {
106
+ String url = decodeURI (uri );
105
107
logger .log (Level .INFO , "{0} {1}" , new Object []{MessageUtil .getMessage ("info.webdav.locking" ),
106
- uri . getEscapedURI () });
108
+ url });
107
109
// 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 );
110
111
client .executeMethod (lockMethod );
111
112
if (lockMethod .succeeded ()) {
112
113
return lockMethod .getLockToken ();
@@ -130,12 +131,13 @@ public void unlockFile(URI uri, String lockToken) throws IOException {
130
131
if (lockToken == null || lockToken .isEmpty ()) {
131
132
return ;
132
133
}
133
- UnLockMethod unlockMethod = new UnLockMethod (uri .getEscapedURI (), lockToken );
134
+ String url = decodeURI (uri );
135
+ UnLockMethod unlockMethod = new UnLockMethod (url , lockToken );
134
136
client .executeMethod (unlockMethod );
135
137
if (unlockMethod .getStatusCode () != 200 && unlockMethod .getStatusCode () != 204 ) {
136
138
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 ()});
139
141
}
140
142
try {
141
143
unlockMethod .checkSuccess ();
@@ -175,14 +177,14 @@ public String getFile(URI uri, String lockToken) throws IOException {
175
177
File tmpFile = new File (tempDir , fileName );
176
178
FileOutputStream fos = new FileOutputStream (tmpFile );
177
179
byte [] data = new byte [64 ];
178
- int c = 0 ;
180
+ int c ;
179
181
try {
180
182
while ((c = is .read (data )) > -1 ) {
181
183
fos .write (data , 0 , c );
182
184
}
183
185
} catch (InterruptedIOException ioinex ) {
184
186
logger .log (Level .INFO , "{0} {1}" , new Object []{MessageUtil .getMessage ("info.user.cancel" ),
185
- ioinex .getMessage ()});
187
+ ioinex .getMessage ()});
186
188
unlockFile (uri , lockToken );
187
189
System .exit (0 );
188
190
} finally {
@@ -200,28 +202,26 @@ public String getFile(URI uri, String lockToken) throws IOException {
200
202
* @throws IOException
201
203
*/
202
204
public void putFile (URI uri , String localFilePath , String lockToken ) throws IOException {
205
+ String url = decodeURI (uri );
203
206
// Checks if file still exists
204
- try {
205
- executeGetFile (uri );
206
- } catch (IOException ioex ) {
207
+ if (!isFileExist (url )) {
207
208
logger .log (Level .SEVERE , MessageUtil .getMessage ("error.remote.file" ));
208
209
throw new IOException (MessageUtil .getMessage ("error.remote.file" ));
209
210
}
210
- PutMethod putMethod = new PutMethod (uri . getEscapedURI () );
211
+ PutMethod putMethod = new PutMethod (url );
211
212
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 );
214
216
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 )));
221
221
is .addPropertyChangeListener (progress );
222
222
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
223
223
byte [] data = new byte [64 ];
224
- int c = 0 ;
224
+ int c ;
225
225
while ((c = is .read (data )) > -1 ) {
226
226
baos .write (data , 0 , c );
227
227
}
@@ -240,13 +240,24 @@ public void putFile(URI uri, String localFilePath, String lockToken) throws IOEx
240
240
}
241
241
242
242
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 );
245
246
client .executeMethod (method );
246
247
if (method .getStatusCode () != HTTP_CREATED && method .getStatusCode () != HTTP_OK ) {
247
248
throw new IOException (MessageUtil .getMessage ("error.get.remote.file" )
248
249
+ ' ' + method .getStatusCode () + " - " + method .getStatusText ());
249
250
}
250
251
return method ;
251
252
}
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
+ }
252
263
}
0 commit comments