-
Notifications
You must be signed in to change notification settings - Fork 34
/
FileService.java
218 lines (202 loc) · 7.67 KB
/
FileService.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
/*
* (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* This library 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
* Lesser General Public License for more details.
*
* Contributors:
* bstefanescu
*
* $Id$
*/
package org.nuxeo.ecm.core.rest;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.model.Property;
import org.nuxeo.ecm.core.api.model.PropertyException;
import org.nuxeo.ecm.core.storage.StorageBlob;
import org.nuxeo.ecm.core.versioning.VersioningService;
import org.nuxeo.ecm.platform.web.common.ServletHelper;
import org.nuxeo.ecm.webengine.WebException;
import org.nuxeo.ecm.webengine.forms.FormData;
import org.nuxeo.ecm.webengine.model.WebAdapter;
import org.nuxeo.ecm.webengine.model.exceptions.IllegalParameterException;
import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
/**
* File Service - manages attachments to a document.
* <p>
* Accepts the following methods:
* <ul>
* <li>GET - get the attached file
* <li>POST - create an attachment
* </ul>
*
* @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
*/
@WebAdapter(name = "file", type = "FileService", targetType = "Document")
public class FileService extends DefaultAdapter {
@GET
public Response doGet(@Context
Request request) {
DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
FormData form = ctx.getForm();
String xpath = form.getString(FormData.PROPERTY);
if (xpath == null) {
if (doc.hasSchema("file")) {
xpath = "file:content";
} else {
throw new IllegalParameterException(
"Missing request parameter named 'property' that specify the blob property xpath to fetch");
}
}
try {
Property p = doc.getProperty(xpath);
Blob blob = (Blob) p.getValue();
if (blob == null) {
throw new WebResourceNotFoundException("No attached file at "
+ xpath);
}
String fileName = blob.getFilename();
if (fileName == null) {
p = p.getParent();
if (p.isComplex()) { // special handling for file and files
// schema
try {
fileName = (String) p.getValue("filename");
} catch (PropertyException e) {
fileName = "Unknown";
}
}
}
EntityTag etag = null;
if (blob instanceof StorageBlob) {
etag = new EntityTag(((StorageBlob) blob).getBinary().getDigest());
}
if (etag != null) {
ResponseBuilder builder = request.evaluatePreconditions(etag);
if (builder != null) {
return builder.build();
}
}
String contentDisposition = ServletHelper.getRFC2231ContentDisposition(
ctx.getRequest(), fileName);
// cached resource did change or no ETag -> serve updated content
ResponseBuilder builder = Response.ok(blob).header(
"Content-Disposition", contentDisposition).type(
blob.getMimeType());
if (etag != null) {
builder.tag(etag);
}
return builder.build();
} catch (Exception e) {
throw WebException.wrap("Failed to get the attached file", e);
}
}
@POST
public Response doPost() {
DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
FormData form = ctx.getForm();
form.fillDocument(doc);
String xpath = ctx.getForm().getString(FormData.PROPERTY);
if (xpath == null) {
if (doc.hasSchema("file")) {
xpath = "file:content";
} else if (doc.hasSchema("files")) {
xpath = "files:files";
} else {
throw new IllegalArgumentException(
"Missing request parameter named 'property' that specifies "
+ "the blob property xpath to fetch");
}
}
Blob blob = form.getFirstBlob();
if (blob == null) {
throw new IllegalArgumentException(
"Could not find any uploaded file");
}
try {
Property p = doc.getProperty(xpath);
if (p.isList()) { // add the file to the list
if ("files".equals(p.getSchema().getName())) { // treat the
// files schema
// separately
Map<String, Serializable> map = new HashMap<String, Serializable>();
map.put("filename", blob.getFilename());
map.put("file", (Serializable) blob);
p.addValue(map);
} else {
p.addValue(blob);
}
} else {
if ("file".equals(p.getSchema().getName())) { // for
// compatibility
// with deprecated
// filename
p.getParent().get("filename").setValue(blob.getFilename());
}
p.setValue(blob);
}
// make snapshot
doc.putContextData(VersioningService.VERSIONING_OPTION,
form.getVersioningOption());
CoreSession session = ctx.getCoreSession();
session.saveDocument(doc);
session.save();
return redirect(getTarget().getPath());
} catch (WebException e) {
throw e;
} catch (Exception e) {
throw WebException.wrap("Failed to attach file", e);
}
}
@GET
@Path("delete")
public Response remove() {
return doDelete();
}
@DELETE
public Response doDelete() {
DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
FormData form = ctx.getForm();
String xpath = form.getString(FormData.PROPERTY);
if (xpath == null) {
if (doc.hasSchema("file")) {
xpath = "file:content";
} else {
throw new IllegalArgumentException(
"Missing request parameter named 'property' that specifies "
+ "the blob property xpath to fetch");
}
}
try {
doc.getProperty(xpath).remove();
CoreSession session = ctx.getCoreSession();
session.saveDocument(doc);
session.save();
} catch (Exception e) {
throw WebException.wrap("Failed to delete attached file", e);
}
return redirect(getTarget().getPath());
}
}