/
CFPath.java
445 lines (395 loc) · 15 KB
/
CFPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package ch.cyberduck.core.cf;
/*
* Copyright (c) 2008 David Kocher. All rights reserved.
* http://cyberduck.ch/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Bug fixes, suggestions and comments should be sent to:
* dkocher@cyberduck.ch
*/
import ch.cyberduck.core.*;
import ch.cyberduck.core.cloud.CloudPath;
import ch.cyberduck.core.cloud.Distribution;
import ch.cyberduck.core.i18n.Locale;
import ch.cyberduck.core.io.BandwidthThrottle;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.jets3t.service.utils.ServiceUtils;
import org.w3c.util.DateParser;
import org.w3c.util.InvalidDateException;
import com.rackspacecloud.client.cloudfiles.FilesContainerInfo;
import com.rackspacecloud.client.cloudfiles.FilesObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* Rackspace Cloud Files Implementation
*
* @version $Id$
*/
public class CFPath extends CloudPath {
private static Logger log = Logger.getLogger(CFPath.class);
static {
PathFactory.addFactory(Protocol.MOSSO, new Factory());
}
private static class Factory extends PathFactory<CFSession> {
@Override
protected Path create(CFSession session, String path, int type) {
return new CFPath(session, path, type);
}
@Override
protected Path create(CFSession session, String parent, String name, int type) {
return new CFPath(session, parent, name, type);
}
@Override
protected Path create(CFSession session, String path, Local file) {
return new CFPath(session, path, file);
}
@Override
protected <T> Path create(CFSession session, T dict) {
return new CFPath(session, dict);
}
}
private final CFSession session;
protected CFPath(CFSession s, String parent, String name, int type) {
super(parent, name, type);
this.session = s;
}
protected CFPath(CFSession s, String path, int type) {
super(path, type);
this.session = s;
}
protected CFPath(CFSession s, String parent, Local file) {
super(parent, file);
this.session = s;
}
protected <T> CFPath(CFSession s, T dict) {
super(dict);
this.session = s;
}
@Override
public CFSession getSession() throws ConnectionCanceledException {
if(null == session) {
throw new ConnectionCanceledException();
}
return session;
}
@Override
public boolean exists() {
if(this.isRoot()) {
return true;
}
try {
if(this.isContainer()) {
return this.getSession().getClient().containerExists(this.getName());
}
}
catch(IOException e) {
return false;
}
return super.exists();
}
@Override
public void readSize() {
try {
this.getSession().check();
this.getSession().message(MessageFormat.format(Locale.localizedString("Getting size of {0}", "Status"),
this.getName()));
if(this.isContainer()) {
attributes.setSize(
this.getSession().getClient().getContainerInfo(this.getContainerName()).getTotalSize()
);
}
else if(this.attributes.isFile()) {
attributes.setSize(
Long.valueOf(this.getSession().getClient().getObjectMetaData(this.getContainerName(), this.getKey()).getContentLength())
);
}
}
catch(IOException e) {
this.error("Cannot read file attributes", e);
}
}
@Override
public void readTimestamp() {
try {
this.getSession().check();
this.getSession().message(MessageFormat.format(Locale.localizedString("Getting timestamp of {0}", "Status"),
this.getName()));
if(!this.isContainer()) {
try {
attributes.setModificationDate(
ServiceUtils.parseRfc822Date(this.getSession().getClient().getObjectMetaData(this.getContainerName(),
this.getKey()).getLastModified()).getTime()
);
}
catch(ParseException e) {
log.error(e);
}
}
}
catch(IOException e) {
this.error("Cannot read file attributes", e);
}
}
@Override
public void readPermission() {
;
}
/**
* Only content distribution is possible but no fine grained grants
*
* @return Always false
*/
@Override
public boolean isWritePermissionsSupported() {
return false;
}
@Override
public void writePermissions(Permission perm, boolean recursive) {
;
}
@Override
public boolean isWriteModificationDateSupported() {
return false;
}
@Override
public void writeModificationDate(long millis) {
;
}
@Override
public AttributedList<Path> list() {
final AttributedList<Path> childs = new AttributedList<Path>();
try {
this.getSession().check();
this.getSession().message(MessageFormat.format(Locale.localizedString("Listing directory {0}", "Status"),
this.getName()));
if(this.isRoot()) {
// List all containers
for(FilesContainerInfo container : this.getSession().getClient().listContainersInfo()) {
Path p = PathFactory.createPath(this.getSession(), this.getAbsolute(), container.getName(),
Path.VOLUME_TYPE | Path.DIRECTORY_TYPE);
p.attributes.setSize(container.getTotalSize());
p.attributes.setOwner(this.getSession().getClient().getUserName());
childs.add(p);
}
}
else {
final int limit = Preferences.instance().getInteger("cf.list.limit");
List<FilesObject> list;
String marker = null;
do {
list = this.getSession().getClient().listObjects(this.getContainerName(), this.getKey(), limit, marker);
for(FilesObject object : list) {
final Path file = PathFactory.createPath(this.getSession(), this.getContainerName(), object.getName(),
"application/directory".equals(object.getMimeType()) ? Path.DIRECTORY_TYPE : Path.FILE_TYPE);
if(file.getParent().equals(this)) {
file.setParent(this);
if(file.attributes.getType() == Path.FILE_TYPE) {
file.attributes.setSize(object.getSize());
file.attributes.setChecksum(object.getMd5sum());
}
try {
final Date modified = DateParser.parse(object.getLastModified());
if(null != modified) {
file.attributes.setModificationDate(modified.getTime());
}
}
catch(InvalidDateException e) {
log.warn("Not ISO 8601 format:" + e.getMessage());
}
file.attributes.setOwner(this.attributes.getOwner());
childs.add(file);
}
marker = object.getName();
}
}
while(list.size() == limit);
}
this.getSession().setWorkdir(this);
}
catch(IOException e) {
childs.attributes().setReadable(false);
this.error("Listing directory failed", e);
}
return childs;
}
@Override
public void download(final BandwidthThrottle throttle, final StreamListener listener, boolean check) {
if(attributes.isFile()) {
OutputStream out = null;
InputStream in = null;
try {
if(check) {
this.getSession().check();
}
this.getSession().message(MessageFormat.format(Locale.localizedString("Downloading {0}", "Status"),
this.getName()));
in = this.getSession().getClient().getObjectAsStream(this.getContainerName(), this.getKey());
if(null == in) {
throw new IOException("Unable opening data stream");
}
getStatus().setCurrent(0);
out = new Local.OutputStream(this.getLocal(), this.getStatus().isResume());
this.download(in, out, throttle, listener);
}
catch(IOException e) {
this.error("Download failed", e);
}
finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
if(attributes.isDirectory()) {
this.getLocal().mkdir(true);
}
}
@Override
protected void upload(final BandwidthThrottle throttle, final StreamListener listener, Permission p, boolean check) {
try {
if(check) {
this.getSession().check();
}
if(attributes.isFile()) {
// No Content-Range support
final Status stat = this.getStatus();
stat.setCurrent(0);
final InputStream in = new Local.InputStream(this.getLocal());
this.getSession().message(MessageFormat.format(Locale.localizedString("Compute MD5 hash of {0}", "Status"),
this.getName()));
String md5sum = this.getLocal().getChecksum();
this.getSession().message(MessageFormat.format(Locale.localizedString("Uploading {0}", "Status"),
this.getName()));
final HashMap<String, String> metadata = new HashMap<String, String>();
this.getSession().getClient().storeObjectAs(this.getContainerName(), this.getKey(),
new InputStreamRequestEntity(in, this.getLocal().attributes.getSize(), this.getLocal().getMimeType()) {
boolean requested = false;
@Override
public void writeRequest(OutputStream out) throws IOException {
if(requested) {
in.reset();
stat.reset();
stat.setCurrent(0);
}
try {
CFPath.this.upload(out, in, throttle, listener);
}
finally {
requested = true;
}
}
@Override
public boolean isRepeatable() {
return true;
}
},
metadata, md5sum
);
}
if(attributes.isDirectory()) {
this.mkdir();
}
}
catch(IOException e) {
this.error("Upload failed", e);
}
}
@Override
public void mkdir(boolean recursive) {
log.debug("mkdir:" + this.getName());
try {
this.getSession().check();
this.getSession().message(MessageFormat.format(Locale.localizedString("Making directory {0}", "Status"),
this.getName()));
if(this.isContainer()) {
// Create container at top level
this.getSession().getClient().createContainer(this.getName());
}
else {
// Create virtual directory
this.getSession().getClient().createFullPath(this.getContainerName(), this.getKey());
}
}
catch(IOException e) {
this.error("Cannot create folder", e);
}
}
@Override
public void delete() {
log.debug("delete:" + this.toString());
try {
this.getSession().check();
if(!this.isContainer()) {
this.getSession().message(MessageFormat.format(Locale.localizedString("Deleting {0}", "Status"),
this.getName()));
this.getSession().getClient().deleteObject(this.getContainerName(), this.getKey());
}
else if(attributes.isDirectory()) {
for(AbstractPath i : this.childs()) {
if(!this.getSession().isConnected()) {
break;
}
i.delete();
}
if(this.isContainer()) {
this.getSession().getClient().deleteContainer(this.getContainerName());
}
}
}
catch(IOException e) {
if(this.attributes.isFile()) {
this.error("Cannot delete file", e);
}
if(this.attributes.isDirectory()) {
this.error("Cannot delete folder", e);
}
}
}
@Override
public boolean isRenameSupported() {
return false;
}
@Override
public void rename(AbstractPath renamed) {
throw new UnsupportedOperationException();
}
/**
* @return Publicy accessible URL of given object
*/
@Override
public String toHttpURL() {
final Distribution distribution;
try {
distribution = this.getSession().readDistribution(this.getContainerName(), Distribution.DOWNLOAD);
}
catch(ConnectionCanceledException e) {
log.error(e.getMessage());
return super.toHttpURL();
}
if(null == distribution.getUrl()) {
return super.toHttpURL();
}
StringBuilder b = new StringBuilder();
b.append(distribution.getUrl());
if(!this.isContainer()) {
b.append(this.encode(this.getKey()));
}
return b.toString();
}
}