-
Notifications
You must be signed in to change notification settings - Fork 131
/
DcacheDirectoryResource.java
198 lines (182 loc) · 6.9 KB
/
DcacheDirectoryResource.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
package org.dcache.webdav;
import io.milton.http.Auth;
import io.milton.http.HttpManager;
import io.milton.http.LockInfo;
import io.milton.http.LockTimeout;
import io.milton.http.LockToken;
import io.milton.http.Range;
import io.milton.http.Request;
import io.milton.http.exceptions.BadRequestException;
import io.milton.http.exceptions.ConflictException;
import io.milton.http.exceptions.NotAuthorizedException;
import io.milton.resource.CollectionResource;
import io.milton.resource.DeletableResource;
import io.milton.resource.GetableResource;
import io.milton.resource.LockingCollectionResource;
import io.milton.resource.MakeCollectionableResource;
import io.milton.resource.PutableResource;
import io.milton.resource.Resource;
import org.eclipse.jetty.io.EofException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.FileExistsCacheException;
import diskCacheV111.util.FileNotFoundCacheException;
import diskCacheV111.util.FsPath;
import diskCacheV111.util.PermissionDeniedCacheException;
import org.dcache.vehicles.FileAttributes;
/**
* Exposes dCache directories as resources in the Milton WebDAV
* framework.
*/
public class DcacheDirectoryResource
extends DcacheResource
implements PutableResource, GetableResource, DeletableResource,
MakeCollectionableResource, LockingCollectionResource
{
public DcacheDirectoryResource(DcacheResourceFactory factory,
FsPath path, FileAttributes attributes)
{
super(factory, path, attributes);
}
@Override
public String checkRedirect(Request request)
{
String url = request.getAbsoluteUrl();
if (request.getMethod() == Request.Method.GET && !url.endsWith("/")) {
return url + "/";
}
return null;
}
@Override
public Resource child(String childName)
{
FsPath fchild = new FsPath(_path, childName);
return _factory.getResource(fchild);
}
@Override
public List<? extends Resource> getChildren()
{
try {
return _factory.list(_path);
} catch (FileNotFoundCacheException e) {
return Collections.emptyList();
} catch (PermissionDeniedCacheException e) {
throw new UnauthorizedException(e.getMessage(), e, this);
} catch (CacheException | InterruptedException e) {
throw new WebDavException(e.getMessage(), e, this);
}
}
@Override
public Resource createNew(String newName, InputStream inputStream,
Long length, String contentType)
throws ConflictException, NotAuthorizedException, BadRequestException
{
try {
FsPath path = new FsPath(_path, newName);
if (_factory.shouldRedirect(HttpManager.request())) {
throw new RedirectException(this, _factory.getWriteUrl(path, length));
} else {
return _factory.createFile(path, inputStream, length);
}
} catch (IOException e) {
// Milton reacts badly to receiving any IOException and wraps the
// IOException in a RuntimeException, which dCache logs as a bug.
// Here, we translate this to a 500 Internal Server Error response.
//
// Note that a 400 Bad Request response indicates that "The client
// SHOULD NOT repeat the request without modifications." --
// RFC 2616.
//
throw new WebDavException("Problem with transferred data: " + e.getMessage(), e, this);
} catch (PermissionDeniedCacheException e) {
throw new NotAuthorizedException(this);
} catch (FileExistsCacheException e) {
throw new ConflictException(this);
} catch (CacheException e) {
throw new WebDavException(e.getMessage(), e, this);
} catch (InterruptedException e) {
throw new WebDavException("Transfer was interrupted", e, this);
} catch (URISyntaxException e) {
throw new WebDavException("Invalid request URI: " + e.getMessage(), e, this);
}
}
@Override
public void sendContent(OutputStream out, Range range,
Map<String,String> params, String contentType)
throws IOException, NotAuthorizedException
{
try {
_factory.list(_path, new OutputStreamWriter(out, "UTF-8"));
} catch (PermissionDeniedCacheException e) {
throw new NotAuthorizedException(this);
} catch (CacheException | InterruptedException e) {
throw new WebDavException(e.getMessage(), e, this);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("This should not happen as UTF-8 " +
"is a required encoding for JVM", e);
} catch (URISyntaxException e) {
throw new WebDavException("Badly formed URI: " + e.getMessage(), this);
}
}
@Override
public Long getMaxAgeSeconds(Auth auth)
{
return null;
}
@Override
public String getContentType(String accepts)
{
return "text/html; charset=utf-8";
}
@Override
public Long getContentLength()
{
return null;
}
@Override
public void delete()
throws NotAuthorizedException, ConflictException, BadRequestException
{
try {
_factory.deleteDirectory(_attributes.getPnfsId(), _path);
} catch (PermissionDeniedCacheException e) {
throw new NotAuthorizedException(this);
} catch (CacheException e) {
throw new WebDavException(e.getMessage(), e, this);
}
}
@Override
public CollectionResource createCollection(String newName)
throws NotAuthorizedException, ConflictException
{
try {
return _factory.makeDirectory(_attributes,
new FsPath(_path, newName));
} catch (PermissionDeniedCacheException e) {
throw new NotAuthorizedException(this);
} catch (CacheException e) {
throw new WebDavException(e.getMessage(), e, this);
}
}
@Override
public LockToken createAndLock(String name, LockTimeout timeout, LockInfo lockInfo)
{
/* We do not currently support createAndLock, but as Mac OS X
* insists on lock support before it allows writing to a
* WebDAV store, we return a lock with zero lifetime.
*
* We do not currently create the entry, as the normal action
* after createAndLock is to perform a PUT which immediately
* overwrites the empty site.
*/
return createNullLock();
}
}