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 package info.magnolia.module.exchangesimple;
35
36 import java.io.BufferedInputStream;
37 import java.io.DataOutputStream;
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.Map;
45
46 import org.apache.commons.io.IOUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50
51
52
53
54 public class ActivationContent implements Cloneable {
55
56 private static final Logger log = LoggerFactory.getLogger(ActivationContent.class);
57
58
59
60
61 private Map<String, File> fileList = new HashMap<String, File>();
62
63
64
65
66 private Map<String, String> properties = new HashMap<String, String>();
67
68 private File tempFile;
69
70 private boolean isClone;
71
72
73
74
75
76
77 public void prepareTempFile() throws IOException {
78 tempFile = File.createTempFile("" + System.currentTimeMillis(), ".mgnl_activation");
79
80 log.debug("prepareTempFile() " + tempFile.getPath());
81
82 tempFile.deleteOnExit();
83
84 DataOutputStream dos = null;
85 try {
86 dos = new DataOutputStream(new FileOutputStream(tempFile));
87
88 dos.writeBytes("--" + Transporter.BOUNDARY + "\r\n");
89
90 Iterator it = getFiles().keySet().iterator();
91 while (it.hasNext()) {
92 String fileName = (String) it.next();
93
94 dos.writeBytes("content-disposition: form-data; name=\""
95 + fileName
96 + "\"; filename=\""
97 + fileName
98 + "\"\r\n");
99 dos.writeBytes("content-type: application/octet-stream\r\n\r\n");
100
101 BufferedInputStream bis = null;
102 try {
103 bis = new BufferedInputStream(new FileInputStream(getFile(fileName)));
104
105 IOUtils.copy(bis, dos);
106 } finally {
107 IOUtils.closeQuietly(bis);
108 }
109
110 dos.writeBytes("\r\n" + "--" + Transporter.BOUNDARY + "\r\n");
111 }
112 } finally {
113 IOUtils.closeQuietly(dos);
114 }
115 }
116
117 public File getTempFile() {
118 return tempFile;
119 }
120
121 public void removeTempFile() {
122 if (!isClone()) {
123 tempFile.delete();
124 tempFile = null;
125 }
126 }
127
128
129
130
131 public void addFile(String resourceId, File file) {
132 this.fileList.put(resourceId, file);
133 }
134
135 public File getFile(String resourceId) {
136 return this.fileList.get(resourceId);
137 }
138
139 public void removeFile(String resourceId) {
140 this.fileList.remove(resourceId);
141 }
142
143
144
145
146 public Map<String, File> getFiles() {
147 return this.fileList;
148 }
149
150
151
152
153 public void addProperty(String key, String value) {
154 if (value == null) {
155 value = "";
156 }
157 this.properties.put(key, value);
158 }
159
160
161
162
163 public void setProperty(String key, String value) {
164 if (value == null) {
165 value = "";
166 }
167
168 this.properties.put(key, value);
169 }
170
171
172
173
174 public String getproperty(String key) {
175 return this.properties.get(key);
176 }
177
178 public String removeProperty(String key) {
179 return this.properties.remove(key);
180 }
181
182
183
184
185 public Map<String, String> getProperties() {
186 return this.properties;
187 }
188
189 public boolean isClone() {
190 return isClone;
191 }
192
193 public void setClone(boolean isClone) {
194 this.isClone = isClone;
195 }
196
197 @Override
198 public Object clone() {
199 try {
200 ActivationContent clone = (ActivationContent) super.clone();
201
202 clone.properties = new HashMap<String, String>(this.properties);
203 clone.fileList = new HashMap<String, File>(this.fileList);
204 clone.setClone(true);
205 return clone;
206 } catch (CloneNotSupportedException e) {
207
208 log.error("Failed to clone itself with " + e.getLocalizedMessage(), e);
209 return null;
210 }
211 }
212 }