OW2 Consortium elastic-grid

Rev

Rev 472 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
472 jeje 1
/**
2
 * Elastic Grid
530 jeje 3
 * Copyright (C) 2008-2010 Elastic Grid, LLC.
472 jeje 4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
package com.elasticgrid.storage.rackspace;
19
 
20
import com.elasticgrid.storage.Container;
21
import com.elasticgrid.storage.Storable;
22
import com.elasticgrid.storage.StorableNotFoundException;
23
import com.elasticgrid.storage.StorageException;
24
import com.mosso.client.cloudfiles.FilesClient;
25
import com.mosso.client.cloudfiles.FilesContainer;
26
import com.mosso.client.cloudfiles.FilesObject;
27
import org.apache.commons.io.FileUtils;
28
import org.apache.commons.io.IOUtils;
29
import javax.activation.MimetypesFileTypeMap;
30
import java.io.File;
31
import java.io.InputStream;
32
import java.util.ArrayList;
33
import java.util.Collections;
34
import java.util.List;
35
import java.util.logging.Level;
36
import java.util.logging.Logger;
37
import java.lang.reflect.Field;
38
 
39
/**
40
 * {@link Container} providing support for Rackspace Cloud Files.
41
 *
42
 * @author Jerome Bernard
43
 */
44
public class CloudFilesContainer implements Container {
45
    private final FilesClient rackspace;
46
    private final FilesContainer rackspaceContainer;
47
    private final MimetypesFileTypeMap mimes;
48
    private Field mimeTypeField;
49
    private final Logger logger = Logger.getLogger(CloudFilesContainer.class.getName());
50
 
51
    public CloudFilesContainer(final FilesClient rackspace, final FilesContainer rackspaceContainer) throws NoSuchFieldException {
52
        this.rackspace = rackspace;
53
        this.rackspaceContainer = rackspaceContainer;
54
        this.mimes = new MimetypesFileTypeMap();
55
        mimeTypeField = FilesObject.class.getDeclaredField("mimeType");
56
        mimeTypeField.setAccessible(true);
57
    }
58
 
59
    public String getName() {
60
        return rackspaceContainer.getName();
61
    }
62
 
63
    public List<Storable> listStorables() throws StorageException {
64
        try {
65
            rackspace.login();
66
            List<FilesObject> objects = rackspace.listObjects(getName());
67
            List<Storable> storables = new ArrayList<Storable>(objects.size());
68
            for (FilesObject object : objects) {
69
                // small hack in order to fetch the mime type of the underlying object
70
                String mimeType = (String) mimeTypeField.get(object);
71
                if (!"application/directory".equals(mimeType))      // skip directories
72
                    storables.add(new CloudFilesStorable(rackspace, object));
73
            }
74
            return storables;
75
        } catch (Exception e) {
76
            throw new StorageException("Can't list storables", e);
77
        }
78
    }
79
 
80
    public Storable findStorableByName(String name) throws StorableNotFoundException, StorageException {
81
        try {
82
            rackspace.login();
83
            int hasPath = name.lastIndexOf('/');
84
            List<FilesObject> objects = null;
85
            if (hasPath == -1) {
86
                objects = rackspace.listObjects(getName());
87
            } else {
88
                String path = name.substring(0, hasPath);
89
                objects = rackspace.listObjects(getName(), path);
90
            }
91
            if (objects.isEmpty())
92
                throw new StorableNotFoundException(name);
93
            FilesObject found = null;
94
            for (FilesObject object : objects) {
95
                if (object.getName().equals(name)) {
96
                    found = object;
97
                    break;
98
                }
99
            }
100
            if (found == null)
101
                throw new StorableNotFoundException(name);
102
            else
103
                return new CloudFilesStorable(rackspace, found);
104
        } catch (StorableNotFoundException snfe) {
105
            throw snfe;
106
        } catch (Exception e) {
107
            throw new StorageException("Can't find storage", e);
108
        }
109
    }
110
 
111
    public Storable uploadStorable(File file) throws StorageException {
112
        return uploadStorable(file.getName(), file);
113
    }
114
 
115
    public Storable uploadStorable(String name, File file) throws StorageException {
116
        try {
117
            rackspace.login();
118
            // create directories if needed
119
            String path = name.substring(0, name.lastIndexOf('/'));
120
            logger.log(Level.FINE, "Creating full path \"{0}\"", path);
121
            rackspace.createFullPath(getName(), path);
122
            // upload the file
123
            logger.log(Level.FINE, "Uploading \"{0}\"", name);
124
            InputStream stream = null;
125
            try {
126
                stream = FileUtils.openInputStream(file);
127
                rackspace.storeStreamedObject(getName(), stream, mimes.getContentType(file), name,
128
                        Collections.<String, String>emptyMap());
129
            } finally {
130
                IOUtils.closeQuietly(stream);
131
            }
132
            // retrieve rackspace object
133
            return findStorableByName(name);
134
        } catch (Exception e) {
135
            throw new StorageException("Can't upload storable from file", e);
136
        }
137
    }
138
 
139
    public Storable uploadStorable(String name, InputStream stream, String mimeType) throws StorageException {
140
        try {
141
            rackspace.login();
142
            // create directories if needed
143
            String path = name.substring(0, name.lastIndexOf('/'));
144
            logger.log(Level.FINE, "Creating full path \"{0}\"", path);
145
            rackspace.createFullPath(getName(), path);
146
            // upload the file
147
            logger.log(Level.FINE, "Uploading \"{0}\"", name);
148
            try {
149
                rackspace.storeStreamedObject(getName(), stream, mimeType, name,
150
                        Collections.<String, String>emptyMap());
151
            } finally {
152
                IOUtils.closeQuietly(stream);
153
            }
154
            // retrieve rackspace object
155
            return findStorableByName(name);
156
        } catch (Exception e) {
157
            throw new StorageException("Can't upload storable from stream", e);
158
        }
159
    }
160
 
161
    public void deleteStorable(String name) throws StorageException {
162
        try {
163
            rackspace.deleteObject(getName(), name);
164
        } catch (Exception e) {
165
            throw new StorageException("Can't delete storable", e);
166
        }
167
    }
168
}