1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.web.jcr.rest.client.json;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.net.URL;
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.TimeZone;
32 import net.jcip.annotations.Immutable;
33 import org.codehaus.jettison.json.JSONObject;
34 import org.modeshape.common.util.Base64;
35 import org.modeshape.common.util.CheckArg;
36 import org.modeshape.web.jcr.rest.client.IJcrConstants;
37 import org.modeshape.web.jcr.rest.client.Utils;
38 import org.modeshape.web.jcr.rest.client.domain.Workspace;
39
40
41
42
43
44 @Immutable
45 public final class FileNode extends JsonNode {
46
47
48
49
50
51
52
53
54 private final File file;
55
56
57
58
59 private final String path;
60
61
62
63
64 private final Workspace workspace;
65
66
67
68
69
70
71
72
73
74
75
76 public FileNode( Workspace workspace,
77 String path,
78 File file ) throws Exception {
79 super(file.getName());
80
81 CheckArg.isNotNull(workspace, "workspace");
82 CheckArg.isNotNull(path, "path");
83
84 this.file = file;
85 this.path = path;
86 this.workspace = workspace;
87
88
89 JSONObject properties = new JSONObject();
90 put(IJsonConstants.PROPERTIES_KEY, properties);
91 properties.put(IJcrConstants.PRIMARY_TYPE_PROPERTY, IJcrConstants.FILE_NODE_TYPE);
92
93
94 JSONObject children = new JSONObject();
95 put(IJsonConstants.CHILDREN_KEY, children);
96
97
98 JSONObject kid = new JSONObject();
99 children.put(IJcrConstants.CONTENT_PROPERTY, kid);
100
101
102 properties = new JSONObject();
103 kid.put(IJsonConstants.PROPERTIES_KEY, properties);
104 properties.put(IJcrConstants.PRIMARY_TYPE_PROPERTY, IJcrConstants.RESOURCE_NODE_TYPE);
105
106
107 Calendar lastModified = Calendar.getInstance();
108 lastModified.setTimeInMillis(file.lastModified());
109 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
110 formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
111 properties.put(IJcrConstants.LAST_MODIFIED, formatter.format(lastModified.getTime()));
112
113
114 properties.put(IJcrConstants.MIME_TYPE, Utils.getMimeType(file));
115 }
116
117
118
119
120
121
122
123
124
125
126 @Override
127 public byte[] getContent() throws Exception {
128
129 JSONObject children = (JSONObject)get(IJsonConstants.CHILDREN_KEY);
130 JSONObject kid = (JSONObject)children.get(IJcrConstants.CONTENT_PROPERTY);
131 JSONObject props = (JSONObject)kid.get(IJsonConstants.PROPERTIES_KEY);
132 props.put(IJcrConstants.DATA_PROPERTY, readFile());
133
134 return super.getContent();
135 }
136
137
138
139
140
141
142
143
144
145 String getFileContents( String jsonResponse ) throws Exception {
146 CheckArg.isNotNull(jsonResponse, "jsonResponse");
147 JSONObject contentNode = new JSONObject(jsonResponse);
148 JSONObject props = (JSONObject)contentNode.get(IJsonConstants.PROPERTIES_KEY);
149 String encodedContents = props.getString(IJcrConstants.DATA_PROPERTY);
150 return encodedContents;
151 }
152
153
154
155
156
157
158
159 URL getFileContentsUrl() throws Exception {
160 StringBuilder url = new StringBuilder(getUrl().toString());
161 url.append('/').append(IJcrConstants.CONTENT_PROPERTY);
162 return new URL(url.toString());
163 }
164
165
166
167
168 public String getPath() {
169 return this.path;
170 }
171
172
173
174
175
176
177 @Override
178 public URL getUrl() throws Exception {
179 FolderNode folderNode = new FolderNode(this.workspace, getPath());
180 StringBuilder url = new StringBuilder(folderNode.getUrl().toString());
181
182
183 url.append('/').append(JsonUtils.encode(this.file.getName()));
184 return new URL(url.toString());
185 }
186
187
188
189
190
191 String readFile() throws Exception {
192 return Base64.encode(new FileInputStream(this.file.getAbsoluteFile()));
193 }
194
195 }