]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java
Add server capabilities to SLC RCP
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / attachment / FileAttachmentsStorage.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.attachment;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.argeo.slc.SlcException;
34 import org.springframework.beans.factory.InitializingBean;
35 import org.springframework.core.io.Resource;
36
37 public class FileAttachmentsStorage implements AttachmentsStorage,
38 AttachmentUploader, InitializingBean {
39 private final static Log log = LogFactory
40 .getLog(FileAttachmentsStorage.class);
41
42 private File attachmentsDirectory;
43
44 private String attachmentsTocFileName = "attachmentsToc.csv";
45
46 private DateFormat dateFormatDay = new SimpleDateFormat("yyyy-MM-dd");
47 private DateFormat dateFormatTime = new SimpleDateFormat("HH:mm:ss");
48
49 public void afterPropertiesSet() {
50 if (attachmentsDirectory == null) {
51
52 String osgiInstanceArea = System.getProperty("osgi.instance.area");
53 if (osgiInstanceArea != null) {
54 if (osgiInstanceArea.startsWith("file:"))
55 osgiInstanceArea = osgiInstanceArea.substring("file:"
56 .length());
57 attachmentsDirectory = new File(osgiInstanceArea
58 + File.separator + "slcAttachments");
59 }
60
61 if (attachmentsDirectory == null) {
62 String tempDir = System.getProperty("java.io.tmpdir");
63 attachmentsDirectory = new File(tempDir + File.separator
64 + "slcAttachments");
65 }
66 }
67 if (!attachmentsDirectory.exists())
68 attachmentsDirectory.mkdirs();
69 if (log.isDebugEnabled())
70 log.debug("File attachment storage initialized in directory "
71 + attachmentsDirectory);
72 }
73
74 public void retrieveAttachment(Attachment attachment,
75 OutputStream outputStream) {
76 File file = getFile(attachment);
77 InputStream in = null;
78 try {
79 byte[] buffer = new byte[1024 * 1024];
80 in = new FileInputStream(file);
81 int read = -1;
82 while ((read = in.read(buffer)) >= 0) {
83 outputStream.write(buffer, 0, read);
84 }
85 if (log.isTraceEnabled())
86 log.trace("Read " + attachment + " from " + file);
87 } catch (IOException e) {
88 throw new SlcException("Cannot write attachment " + attachment
89 + " to " + file, e);
90 } finally {
91 IOUtils.closeQuietly(in);
92 }
93 }
94
95 public void storeAttachment(Attachment attachment, InputStream inputStream) {
96 File file = getFile(attachment);
97 FileOutputStream out = null;
98 try {
99 byte[] buffer = new byte[1024 * 1024];
100 out = new FileOutputStream(file);
101 int read = -1;
102 while ((read = inputStream.read(buffer)) >= 0) {
103 out.write(buffer, 0, read);
104 }
105 if (log.isTraceEnabled())
106 log.trace("Wrote " + attachment + " to " + file);
107 updateAttachmentToc(attachment, file);
108 } catch (IOException e) {
109 throw new SlcException("Cannot write attachment " + attachment
110 + " to " + file, e);
111 } finally {
112 IOUtils.closeQuietly(out);
113 }
114
115 }
116
117 public void upload(Attachment attachment, Resource resource) {
118 try {
119 storeAttachment(attachment, resource.getInputStream());
120 } catch (IOException e) {
121 throw new SlcException("Cannot upload attachment " + attachment, e);
122 }
123 }
124
125 /** For monitoring purposes only */
126 protected void updateAttachmentToc(Attachment attachment, File file) {
127 Date date = new Date(file.lastModified());
128 FileWriter writer = null;
129 try {
130 writer = new FileWriter(attachmentsDirectory + File.separator
131 + attachmentsTocFileName, true);
132 writer.append(dateFormatDay.format(date));
133 writer.append(',');
134 writer.append(dateFormatTime.format(date));
135 writer.append(',');
136 writer.append(attachment.getUuid());
137 writer.append(',');
138 writer.append(attachment.getName());
139 writer.append(',');
140 writer.append(attachment.getContentType());
141 writer.append(',');
142 writer.append(Long.toString(file.length()));
143 writer.append(',');
144 writer.append(file.getCanonicalPath());
145 writer.append('\n');
146 } catch (IOException e) {
147 log.warn("Could not update attachments TOC for " + attachment
148 + " and file " + file, e);
149 } finally {
150 IOUtils.closeQuietly(writer);
151 }
152
153 }
154
155 protected File getFile(Attachment attachment) {
156 File file = new File(attachmentsDirectory + File.separator
157 + attachment.getUuid());
158 return file;
159 }
160
161 public void setAttachmentsDirectory(File attachmentsDirectory) {
162 this.attachmentsDirectory = attachmentsDirectory;
163 }
164
165 public void setAttachmentsTocFileName(String attachmentsTocFileName) {
166 this.attachmentsTocFileName = attachmentsTocFileName;
167 }
168
169 public void setDateFormatDay(DateFormat dateFormatDay) {
170 this.dateFormatDay = dateFormatDay;
171 }
172
173 public void setDateFormatTime(DateFormat dateFormatTime) {
174 this.dateFormatTime = dateFormatTime;
175 }
176
177 }