35
35
import java .net .MalformedURLException ;
36
36
import java .net .URISyntaxException ;
37
37
import java .net .URL ;
38
-
38
+ import java . net . URLDecoder ;
39
39
import java .net .URLEncoder ;
40
40
import java .util .StringTokenizer ;
41
+
41
42
import java .util .logging .Level ;
42
43
import java .util .logging .Logger ;
43
44
import org .apache .commons .httpclient .Credentials ;
47
48
import org .apache .commons .httpclient .HttpException ;
48
49
import org .apache .commons .httpclient .MultiThreadedHttpConnectionManager ;
49
50
import org .apache .commons .httpclient .URI ;
51
+ import org .apache .commons .httpclient .URIException ;
50
52
import org .apache .commons .httpclient .UsernamePasswordCredentials ;
51
53
import org .apache .commons .httpclient .auth .AuthScope ;
52
54
import org .apache .commons .httpclient .methods .ByteArrayRequestEntity ;
@@ -72,7 +74,7 @@ public class FileWebDavAccessManager {
72
74
private String password ;
73
75
private String lockToken = null ;
74
76
static Logger logger = Logger .getLogger (
75
- FileWebDavAccessManager .class .getName ());
77
+ FileWebDavAccessManager .class .getName ());
76
78
77
79
/**
78
80
* The AccessManager is inited with authentication info to avoid login prompt
@@ -88,7 +90,7 @@ public HttpClient initConnection(URL url) {
88
90
HostConfiguration hostConfig = new HostConfiguration ();
89
91
hostConfig .setHost (url .getHost ());
90
92
HttpConnectionManager connectionManager =
91
- new MultiThreadedHttpConnectionManager ();
93
+ new MultiThreadedHttpConnectionManager ();
92
94
HttpConnectionManagerParams params = new HttpConnectionManagerParams ();
93
95
int maxHostConnections = 20 ;
94
96
params .setMaxConnectionsPerHost (hostConfig , maxHostConnections );
@@ -111,40 +113,42 @@ public HttpClient initConnection(URL url) {
111
113
* @throws IOException
112
114
*/
113
115
public String retrieveFile (String url ) throws HttpException , IOException ,
114
- URISyntaxException {
115
- URL formattedUrl = new URL (url );
116
+ URISyntaxException {
117
+ URI uri = getURI (url );
118
+ URL formattedUrl = new URL (uri .getEscapedURI ());
116
119
HttpClient client = initConnection (formattedUrl );
117
120
118
- logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.locking" ) +
119
- ' ' + encodeUrl ( url ));
121
+ logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.locking" )
122
+ + ' ' + uri . getEscapedURI ( ));
120
123
//Let's lock the file
121
- LockMethod lockMethod = new LockMethod (encodeUrl ( url ),
122
- Scope .EXCLUSIVE , Type .WRITE ,
123
- userName , 600000l , false );
124
+ LockMethod lockMethod = new LockMethod (uri . getEscapedURI ( ),
125
+ Scope .EXCLUSIVE , Type .WRITE ,
126
+ userName , 600000l , false );
124
127
client .executeMethod (lockMethod );
125
128
if (lockMethod .succeeded ()) {
126
129
lockToken = lockMethod .getLockToken ();
127
130
} else {
128
- throw new IOException (MessageUtil .getMessage ("error.webdav.locking" )
129
- + ' ' + lockMethod .getStatusCode () + " - "
130
- + lockMethod .getStatusText ());
131
+ throw new IOException (MessageUtil .getMessage ("error.webdav.locking" )
132
+ + ' ' + lockMethod .getStatusCode () + " - "
133
+ + lockMethod .getStatusText ());
131
134
}
132
- logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.locked" ) + ' '
133
- + lockToken );
135
+ logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.locked" ) + ' '
136
+ + lockToken );
134
137
GetMethod method = new GetMethod ();
135
- method .setURI (new URI ( url , false ) );
138
+ method .setURI (uri );
136
139
client .executeMethod (method );
137
140
if (method .getStatusCode () != 200 ) {
138
- throw new IOException (MessageUtil .getMessage ("error.get.remote.file" ) +
139
- +' ' + method .getStatusCode () + " - " + method .getStatusText ());
141
+ throw new IOException (MessageUtil .getMessage ("error.get.remote.file" )
142
+ + +' ' + method .getStatusCode () + " - " + method .getStatusText ());
140
143
}
141
144
142
145
String fileName = formattedUrl .getFile ();
143
146
fileName = fileName .substring (fileName .lastIndexOf ('/' ) + 1 );
147
+ fileName = URLDecoder .decode (fileName , "UTF-8" );
144
148
fileName = fileName .replace (' ' , '_' );
145
149
InputStream is = method .getResponseBodyAsStream ();
146
- File tempDir = new File (System .getProperty ("java.io.tmpdir" ), "silver-" +
147
- System .currentTimeMillis ());
150
+ File tempDir = new File (System .getProperty ("java.io.tmpdir" ), "silver-"
151
+ + System .currentTimeMillis ());
148
152
tempDir .mkdirs ();
149
153
File tmpFile = new File (tempDir , fileName );
150
154
FileOutputStream fos = new FileOutputStream (tmpFile );
@@ -155,7 +159,7 @@ public String retrieveFile(String url) throws HttpException, IOException,
155
159
}
156
160
fos .close ();
157
161
logger .log (Level .INFO , MessageUtil .getMessage (
158
- "info.webdav.file.locally.saved" ) + ' ' + tmpFile .getAbsolutePath ());
162
+ "info.webdav.file.locally.saved" ) + ' ' + tmpFile .getAbsolutePath ());
159
163
return tmpFile .getAbsolutePath ();
160
164
}
161
165
@@ -169,47 +173,48 @@ public String retrieveFile(String url) throws HttpException, IOException,
169
173
* @throws IOException
170
174
*/
171
175
public void pushFile (String tmpFilePath , String url ) throws HttpException ,
172
- IOException ,
173
- MalformedURLException ,
174
- UnsupportedEncodingException ,
175
- URISyntaxException {
176
+ IOException ,
177
+ MalformedURLException ,
178
+ UnsupportedEncodingException ,
179
+ URISyntaxException ,
180
+ URIException {
176
181
/*
177
182
* Build URL object to extract host
178
183
*/
179
- URL formattedUrl = new URL (url );
180
- HttpClient client = initConnection (formattedUrl );
184
+ URI uri = getURI (url );
185
+ HttpClient client = initConnection (new URL (uri .getEscapedURI ()));
186
+
181
187
/*
182
188
* Checks if file still exists
183
189
*/
184
190
GetMethod method = new GetMethod ();
185
- method .setURI (new URI ( url , false ) );
191
+ method .setURI (uri );
186
192
client .executeMethod (method );
187
193
188
194
if (method .getStatusCode () == 200 ) {
189
- logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.unlocking" ) +
190
- ' ' + encodeUrl ( url ));
195
+ logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.unlocking" )
196
+ + ' ' + uri . getEscapedURI ( ));
191
197
//Let's lock the file
192
- UnLockMethod unlockMethod = new UnLockMethod (encodeUrl ( url ), lockToken );
198
+ UnLockMethod unlockMethod = new UnLockMethod (uri . getEscapedURI ( ), lockToken );
193
199
client .executeMethod (unlockMethod );
194
- if (unlockMethod .getStatusCode () != 200 && unlockMethod .getStatusCode () !=
195
- 204 ) {
200
+ if (unlockMethod .getStatusCode () != 200 && unlockMethod .getStatusCode ()
201
+ != 204 ) {
196
202
logger .log (Level .INFO ,
197
- MessageUtil .getMessage ("error.webdav.unlocking" ) + ' ' + unlockMethod .
198
- getStatusCode ());
203
+ MessageUtil .getMessage ("error.webdav.unlocking" ) + ' ' + unlockMethod .getStatusCode ());
199
204
}
200
205
try {
201
206
unlockMethod .checkSuccess ();
202
207
logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.unlocked" ));
203
208
} catch (DavException ex ) {
204
209
logger .log (Level .SEVERE ,
205
- MessageUtil .getMessage ("error.webdav.unlocking" ), ex );
210
+ MessageUtil .getMessage ("error.webdav.unlocking" ), ex );
206
211
throw new IOException (MessageUtil .getMessage ("error.webdav.unlocking" ),
207
- ex );
212
+ ex );
208
213
}
209
- URI uri = new URI ( url , false );
214
+
210
215
PutMethod putMethod = new PutMethod (uri .getEscapedURI ());
211
- logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.put" ) +
212
- ' ' + tmpFilePath );
216
+ logger .log (Level .INFO , MessageUtil .getMessage ("info.webdav.put" )
217
+ + ' ' + tmpFilePath );
213
218
File file = new File (tmpFilePath );
214
219
InputStream is = new FileInputStream (file );
215
220
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
@@ -230,21 +235,20 @@ public void pushFile(String tmpFilePath, String url) throws HttpException,
230
235
logger .log (Level .INFO , MessageUtil .getMessage ("info.file.deleted" ));
231
236
logger .log (Level .INFO , MessageUtil .getMessage ("info.ok" ));
232
237
} else {
233
- throw new IOException (MessageUtil .getMessage ("error.put.remote.file" ) +
234
- " - " + putMethod .getStatusCode () + " - " +
235
- putMethod .getStatusText ());
238
+ throw new IOException (MessageUtil .getMessage ("error.put.remote.file" )
239
+ + " - " + putMethod .getStatusCode () + " - "
240
+ + putMethod .getStatusText ());
236
241
}
237
242
} else {
238
243
logger .log (Level .SEVERE , MessageUtil .getMessage ("error.remote.file" ));
239
244
throw new IOException (MessageUtil .getMessage ("error.remote.file" ));
240
245
}
241
246
}
242
247
243
- public static String encodeUrl (String url ) throws MalformedURLException ,
244
- UnsupportedEncodingException ,
245
- URISyntaxException {
248
+ public static String encodeUrl (String url ) throws UnsupportedEncodingException {
246
249
int count = 0 ;
247
- StringTokenizer tokenizer = new StringTokenizer (url , "/" , true );
250
+ String urlToBeEncoded = url .replaceAll ("%20" , " " );
251
+ StringTokenizer tokenizer = new StringTokenizer (urlToBeEncoded , "/" , true );
248
252
StringBuilder buffer = new StringBuilder ();
249
253
while (tokenizer .hasMoreTokens ()) {
250
254
String token = tokenizer .nextToken ();
@@ -258,4 +262,8 @@ public static String encodeUrl(String url) throws MalformedURLException,
258
262
String resultingUrl = buffer .toString ();
259
263
return resultingUrl .replace ('+' , ' ' ).replaceAll (" " , "%20" );
260
264
}
265
+
266
+ private static URI getURI (String url ) throws URIException {
267
+ return new URI (url .replaceAll ("%20" , " " ), false , "UTF-8" );
268
+ }
261
269
}
0 commit comments