25
25
26
26
import com .silverpeas .openoffice .*;
27
27
import com .silverpeas .openoffice .util .MessageUtil ;
28
+ import java .io .BufferedInputStream ;
28
29
import java .io .ByteArrayOutputStream ;
29
30
import java .io .File ;
31
+ import java .io .File ;
30
32
import java .io .FileInputStream ;
31
33
import java .io .FileOutputStream ;
32
34
import java .io .IOException ;
41
43
42
44
import java .util .logging .Level ;
43
45
import java .util .logging .Logger ;
46
+ import javax .swing .ProgressMonitor ;
47
+ import javax .swing .ProgressMonitorInputStream ;
44
48
import org .apache .commons .httpclient .Credentials ;
45
49
import org .apache .commons .httpclient .HostConfiguration ;
46
50
import org .apache .commons .httpclient .HttpClient ;
@@ -117,13 +121,11 @@ public String retrieveFile(String url) throws HttpException, IOException,
117
121
URI uri = getURI (url );
118
122
URL formattedUrl = new URL (uri .getEscapedURI ());
119
123
HttpClient client = initConnection (formattedUrl );
120
-
121
124
logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.locking" )
122
125
+ ' ' + uri .getEscapedURI ());
123
126
//Let's lock the file
124
127
LockMethod lockMethod = new LockMethod (uri .getEscapedURI (),
125
- Scope .EXCLUSIVE , Type .WRITE ,
126
- userName , 600000l , false );
128
+ Scope .EXCLUSIVE , Type .WRITE , userName , 600000l , false );
127
129
client .executeMethod (lockMethod );
128
130
if (lockMethod .succeeded ()) {
129
131
lockToken = lockMethod .getLockToken ();
@@ -139,27 +141,30 @@ public String retrieveFile(String url) throws HttpException, IOException,
139
141
client .executeMethod (method );
140
142
if (method .getStatusCode () != 200 ) {
141
143
throw new IOException (MessageUtil .getMessage ("error.get.remote.file" )
142
- + + ' ' + method .getStatusCode () + " - " + method .getStatusText ());
144
+ + ' ' + method .getStatusCode () + " - " + method .getStatusText ());
143
145
}
144
-
145
146
String fileName = formattedUrl .getFile ();
146
147
fileName = fileName .substring (fileName .lastIndexOf ('/' ) + 1 );
147
148
fileName = URLDecoder .decode (fileName , "UTF-8" );
149
+ ProgressMonitorInputStream is = new ProgressMonitorInputStream (null ,
150
+ MessageUtil .getMessage ("downloading.remote.file" ) + " " + fileName ,
151
+ new BufferedInputStream (method .getResponseBodyAsStream ()));
148
152
fileName = fileName .replace (' ' , '_' );
149
- InputStream is = method .getResponseBodyAsStream ();
153
+ ProgressMonitor monitor = is .getProgressMonitor ();
154
+ monitor .setMaximum (new Long (method .getResponseContentLength ()).intValue ());
150
155
File tempDir = new File (System .getProperty ("java.io.tmpdir" ), "silver-"
151
156
+ System .currentTimeMillis ());
152
157
tempDir .mkdirs ();
153
158
File tmpFile = new File (tempDir , fileName );
154
159
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 );
159
164
}
160
165
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 ());
163
168
return tmpFile .getAbsolutePath ();
164
169
}
165
170
@@ -183,52 +188,53 @@ public void pushFile(String tmpFilePath, String url) throws HttpException,
183
188
*/
184
189
URI uri = getURI (url );
185
190
HttpClient client = initConnection (new URL (uri .getEscapedURI ()));
186
-
191
+
187
192
/*
188
193
* Checks if file still exists
189
194
*/
190
195
GetMethod method = new GetMethod ();
191
196
method .setURI (uri );
192
197
client .executeMethod (method );
193
-
194
198
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
-
215
199
PutMethod putMethod = new PutMethod (uri .getEscapedURI ());
216
200
logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.put" )
217
201
+ ' ' + tmpFilePath );
218
202
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 ());
220
208
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 );
225
213
}
226
- byte [] bytes = baos .toByteArray ();
227
- RequestEntity requestEntity = new ByteArrayRequestEntity (bytes );
214
+ RequestEntity requestEntity = new ByteArrayRequestEntity (baos .toByteArray ());
228
215
putMethod .setRequestEntity (requestEntity );
216
+ putMethod .setRequestHeader (PutMethod .HEADER_LOCK_TOKEN , lockToken );
229
217
client .executeMethod (putMethod );
230
218
if (putMethod .succeeded ()) {
231
219
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
+ }
232
238
// delete temp file
233
239
file .delete ();
234
240
file .getParentFile ().delete ();
0 commit comments