1
1
/**
2
2
* Copyright (C) 2000 - 2009 Silverpeas
3
3
*
4
- * This program is free software: you can redistribute it and/or modify
5
- * it under the terms of the GNU Affero General Public License as
6
- * published by the Free Software Foundation, either version 3 of the
7
- * License, or (at your option) any later version.
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the
5
+ * GNU Affero General Public License as published by the Free Software Foundation, either version 3
6
+ * of the License, or (at your option) any later version.
8
7
*
9
- * As a special exception to the terms and conditions of version 3.0 of
10
- * the GPL, you may redistribute this Program in connection with Free/Libre
11
- * Open Source Software ("FLOSS") applications as described in Silverpeas's
12
- * FLOSS exception. You should have received a copy of the text describing
13
- * the FLOSS exception, and it is also available here:
8
+ * As a special exception to the terms and conditions of version 3.0 of the GPL, you may
9
+ * redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
10
+ * applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
11
+ * text describing the FLOSS exception, and it is also available here:
14
12
* "http://repository.silverpeas.com/legal/licensing"
15
13
*
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU Affero General Public License for more details.
14
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
+ * Affero General Public License for more details.
20
17
*
21
- * You should have received a copy of the GNU Affero General Public License
22
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ * You should have received a copy of the GNU Affero General Public License along with this program.
19
+ * If not, see <http://www.gnu.org/licenses/>.
23
20
*/
24
-
25
21
package com .silverpeas .openoffice .windows .webdav ;
26
22
27
23
import java .io .IOException ;
59
55
import org .apache .jackrabbit .webdav .client .methods .PutMethod ;
60
56
import org .apache .jackrabbit .webdav .client .methods .UnLockMethod ;
61
57
58
+ import static java .net .HttpURLConnection .HTTP_OK ;
59
+ import static java .net .HttpURLConnection .HTTP_CREATED ;
60
+
62
61
/**
63
62
* Simple class to help manipulate Webdav ressources.
63
+ *
64
64
* @author ehugonnet
65
65
*/
66
66
public class WebdavManager {
@@ -70,15 +70,15 @@ public class WebdavManager {
70
70
71
71
/**
72
72
* Prepare HTTP connections to the WebDav server
73
+ *
73
74
* @param host the webdav server host name.
74
75
* @param login the login for the user on the webdav server.
75
76
* @param password the login for the user on the webdav server.
76
77
*/
77
78
public WebdavManager (String host , String login , String password ) {
78
79
HostConfiguration hostConfig = new HostConfiguration ();
79
80
hostConfig .setHost (host );
80
- HttpConnectionManager connectionManager =
81
- new MultiThreadedHttpConnectionManager ();
81
+ HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager ();
82
82
HttpConnectionManagerParams connectionParams = new HttpConnectionManagerParams ();
83
83
int maxHostConnections = 20 ;
84
84
connectionParams .setMaxConnectionsPerHost (hostConfig , maxHostConnections );
@@ -94,14 +94,15 @@ public WebdavManager(String host, String login, String password) {
94
94
95
95
/**
96
96
* Lock a ressource on a webdav server.
97
+ *
97
98
* @param uri the URI to the ressource to be locked.
98
99
* @param login the user locking the ressource.
99
100
* @return the lock token.
100
101
* @throws IOException
101
102
*/
102
103
public String lockFile (URI uri , String login ) throws IOException {
103
- logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.locking" )
104
- + ' ' + uri .getEscapedURI ());
104
+ logger .log (Level .INFO , "{0} {1}" , new Object []{ MessageUtil .getMessage ("info.webdav.locking" ),
105
+ uri .getEscapedURI ()} );
105
106
// Let's lock the file
106
107
LockMethod lockMethod = new LockMethod (uri .getEscapedURI (),
107
108
Scope .EXCLUSIVE , Type .WRITE , login , 600000l , false );
@@ -113,13 +114,13 @@ public String lockFile(URI uri, String login) throws IOException {
113
114
throw new IOException (MessageUtil .getMessage ("error.webdav.already.locked" ));
114
115
}
115
116
throw new IOException (MessageUtil .getMessage ("error.webdav.locking" )
116
- + ' ' + lockMethod .getStatusCode () + " - "
117
- + lockMethod .getStatusText ());
117
+ + ' ' + lockMethod .getStatusCode () + " - " + lockMethod .getStatusText ());
118
118
}
119
119
}
120
120
121
121
/**
122
122
* Unlock a ressource on a webdav server.
123
+ *
123
124
* @param uri the URI to the ressource to be unlocked.
124
125
* @param lockToken the current lock token.
125
126
* @throws IOException
@@ -131,22 +132,23 @@ public void unlockFile(URI uri, String lockToken) throws IOException {
131
132
UnLockMethod unlockMethod = new UnLockMethod (uri .getEscapedURI (), lockToken );
132
133
client .executeMethod (unlockMethod );
133
134
if (unlockMethod .getStatusCode () != 200 && unlockMethod .getStatusCode () != 204 ) {
134
- logger .log (Level .INFO , MessageUtil .getMessage ("error.webdav.unlocking" ) + ' '
135
- + unlockMethod .getStatusCode ());
135
+ logger .log (Level .INFO , "{0} {1}" , new Object []{MessageUtil .
136
+ getMessage ("error.webdav.unlocking" ),
137
+ unlockMethod .getStatusCode ()});
136
138
}
137
139
try {
138
140
unlockMethod .checkSuccess ();
139
141
logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.unlocked" ));
140
142
} catch (DavException ex ) {
141
143
logger .log (Level .SEVERE ,
142
144
MessageUtil .getMessage ("error.webdav.unlocking" ), ex );
143
- throw new IOException (MessageUtil .getMessage ("error.webdav.unlocking" ),
144
- ex );
145
+ throw new IOException (MessageUtil .getMessage ("error.webdav.unlocking" ), ex );
145
146
}
146
147
}
147
148
148
149
/**
149
150
* Get the ressource from the webdav server.
151
+ *
150
152
* @param uri the uri to the ressource.
151
153
* @param lockToken the current lock token.
152
154
* @return the path to the saved file on the filesystem.
@@ -178,8 +180,8 @@ public String getFile(URI uri, String lockToken) throws IOException {
178
180
fos .write (data , 0 , c );
179
181
}
180
182
} catch (InterruptedIOException ioinex ) {
181
- logger .log (Level .INFO , MessageUtil .getMessage ("info.user.cancel" ) + ' '
182
- + ioinex .getMessage ());
183
+ logger .log (Level .INFO , "{0} {1}" , new Object []{ MessageUtil .getMessage ("info.user.cancel" ),
184
+ ioinex .getMessage ()} );
183
185
unlockFile (uri , lockToken );
184
186
System .exit (0 );
185
187
} finally {
@@ -190,6 +192,7 @@ public String getFile(URI uri, String lockToken) throws IOException {
190
192
191
193
/**
192
194
* Update a ressource on the webdav file server.
195
+ *
193
196
* @param uri the uri to the ressource.
194
197
* @param localFilePath the path to the file to be uploaded on the filesystem.
195
198
* @param lockToken the current lock token.
@@ -204,12 +207,13 @@ public void putFile(URI uri, String localFilePath, String lockToken) throws IOEx
204
207
throw new IOException (MessageUtil .getMessage ("error.remote.file" ));
205
208
}
206
209
PutMethod putMethod = new PutMethod (uri .getEscapedURI ());
207
- logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.put" ) + ' ' + localFilePath );
210
+ logger .log (Level .INFO , "{0} {1}" , new Object []{MessageUtil .getMessage ("info.webdav.put" ),
211
+ localFilePath });
208
212
File file = new File (localFilePath );
209
213
UploadProgressBar progress = new UploadProgressBar ();
210
214
progress .setMaximum (new Long (file .length ()).intValue ());
211
- progress .setMessage (MessageUtil .getMessage ("uploading.remote.file" ) + ' ' +
212
- uri . getPath (). substring (
215
+ progress .setMessage (MessageUtil .getMessage ("uploading.remote.file" ) + ' ' + uri . getPath ().
216
+ substring (
213
217
uri .getPath ().lastIndexOf ('/' ) + 1 ));
214
218
MonitoredInputStream is =
215
219
new MonitoredInputStream (new BufferedInputStream (new FileInputStream (file )));
@@ -238,7 +242,7 @@ private GetMethod executeGetFile(URI uri) throws IOException {
238
242
GetMethod method = new GetMethod ();
239
243
method .setURI (uri );
240
244
client .executeMethod (method );
241
- if (method .getStatusCode () != 200 ) {
245
+ if (method .getStatusCode () != HTTP_CREATED && method . getStatusCode () != HTTP_OK ) {
242
246
throw new IOException (MessageUtil .getMessage ("error.get.remote.file" )
243
247
+ ' ' + method .getStatusCode () + " - " + method .getStatusText ());
244
248
}
0 commit comments