2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.core
.attachment
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.FileOutputStream
;
21 import java
.io
.FileWriter
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.io
.OutputStream
;
25 import java
.text
.DateFormat
;
26 import java
.text
.SimpleDateFormat
;
27 import java
.util
.Date
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.argeo
.slc
.SlcException
;
33 import org
.argeo
.slc
.attachment
.Attachment
;
34 import org
.argeo
.slc
.attachment
.AttachmentsStorage
;
35 import org
.springframework
.beans
.factory
.InitializingBean
;
36 import org
.springframework
.core
.io
.Resource
;
38 public class FileAttachmentsStorage
implements AttachmentsStorage
,
39 AttachmentUploader
, InitializingBean
{
40 private final static Log log
= LogFactory
41 .getLog(FileAttachmentsStorage
.class);
43 private File attachmentsDirectory
;
45 private String attachmentsTocFileName
= "attachmentsToc.csv";
47 private DateFormat dateFormatDay
= new SimpleDateFormat("yyyy-MM-dd");
48 private DateFormat dateFormatTime
= new SimpleDateFormat("HH:mm:ss");
50 public void afterPropertiesSet() {
51 if (attachmentsDirectory
== null) {
53 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
54 if (osgiInstanceArea
!= null) {
55 if (osgiInstanceArea
.startsWith("file:"))
56 osgiInstanceArea
= osgiInstanceArea
.substring("file:"
58 attachmentsDirectory
= new File(osgiInstanceArea
59 + File
.separator
+ "slcAttachments");
62 if (attachmentsDirectory
== null) {
63 String tempDir
= System
.getProperty("java.io.tmpdir");
64 attachmentsDirectory
= new File(tempDir
+ File
.separator
68 if (!attachmentsDirectory
.exists())
69 attachmentsDirectory
.mkdirs();
70 if (log
.isDebugEnabled())
71 log
.debug("File attachment storage initialized in directory "
72 + attachmentsDirectory
);
75 public void retrieveAttachment(Attachment attachment
,
76 OutputStream outputStream
) {
77 File file
= getFile(attachment
);
78 InputStream in
= null;
80 byte[] buffer
= new byte[1024 * 1024];
81 in
= new FileInputStream(file
);
83 while ((read
= in
.read(buffer
)) >= 0) {
84 outputStream
.write(buffer
, 0, read
);
86 if (log
.isTraceEnabled())
87 log
.trace("Read " + attachment
+ " from " + file
);
88 } catch (IOException e
) {
89 throw new SlcException("Cannot write attachment " + attachment
92 IOUtils
.closeQuietly(in
);
96 public void storeAttachment(Attachment attachment
, InputStream inputStream
) {
97 File file
= getFile(attachment
);
98 FileOutputStream out
= null;
100 byte[] buffer
= new byte[1024 * 1024];
101 out
= new FileOutputStream(file
);
103 while ((read
= inputStream
.read(buffer
)) >= 0) {
104 out
.write(buffer
, 0, read
);
106 if (log
.isTraceEnabled())
107 log
.trace("Wrote " + attachment
+ " to " + file
);
108 updateAttachmentToc(attachment
, file
);
109 } catch (IOException e
) {
110 throw new SlcException("Cannot write attachment " + attachment
113 IOUtils
.closeQuietly(out
);
118 public void upload(Attachment attachment
, Resource resource
) {
120 storeAttachment(attachment
, resource
.getInputStream());
121 } catch (IOException e
) {
122 throw new SlcException("Cannot upload attachment " + attachment
, e
);
126 /** For monitoring purposes only */
127 protected void updateAttachmentToc(Attachment attachment
, File file
) {
128 Date date
= new Date(file
.lastModified());
129 FileWriter writer
= null;
131 writer
= new FileWriter(attachmentsDirectory
+ File
.separator
132 + attachmentsTocFileName
, true);
133 writer
.append(dateFormatDay
.format(date
));
135 writer
.append(dateFormatTime
.format(date
));
137 writer
.append(attachment
.getUuid());
139 writer
.append(attachment
.getName());
141 writer
.append(attachment
.getContentType());
143 writer
.append(Long
.toString(file
.length()));
145 writer
.append(file
.getCanonicalPath());
147 } catch (IOException e
) {
148 log
.warn("Could not update attachments TOC for " + attachment
149 + " and file " + file
, e
);
151 IOUtils
.closeQuietly(writer
);
156 protected File
getFile(Attachment attachment
) {
157 File file
= new File(attachmentsDirectory
+ File
.separator
158 + attachment
.getUuid());
162 public void setAttachmentsDirectory(File attachmentsDirectory
) {
163 this.attachmentsDirectory
= attachmentsDirectory
;
166 public void setAttachmentsTocFileName(String attachmentsTocFileName
) {
167 this.attachmentsTocFileName
= attachmentsTocFileName
;
170 public void setDateFormatDay(DateFormat dateFormatDay
) {
171 this.dateFormatDay
= dateFormatDay
;
174 public void setDateFormatTime(DateFormat dateFormatTime
) {
175 this.dateFormatTime
= dateFormatTime
;