View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Container for all information about activated (to be) content.
52   * @author Sameer Charles $Id$
53   */
54  public class ActivationContent implements Cloneable {
55  
56      private static final Logger log = LoggerFactory.getLogger(ActivationContent.class);
57  
58      /**
59       * Collection of files to be transfered during activation.
60       */
61      private Map<String, File> fileList = new HashMap<String, File>();
62  
63      /**
64       * Collection of properties describing activated content (path, repo, etc). Supported property keys are listed in {@link BaseSyndicatorImpl}.
65       */
66      private Map<String, String> properties = new HashMap<String, String>();
67  
68      private File tempFile;
69  
70      private boolean isClone;
71  
72      /**
73       * Aggregates all transfer data including boundaries into single temporary file.
74       *
75       * @throws java.io.IOException when some activation content can't be read or writing into temp file fails.
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(); // just to be sure
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      * Adds resource to the list of files for transfer.
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      * Cats collection of all files. This collection is not a copy, but a reference to internal collection!
145      */
146     public Map<String, File> getFiles() {
147         return this.fileList;
148     }
149 
150     /**
151      * Adds property to the list of properties. Null values are automatically converted to empty strings. If the key already exists, existing value will be replaced with the one provided to this method.
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      * @see #setProperty(String, String)
162      */
163     public void setProperty(String key, String value) {
164         if (value == null) {
165             value = "";
166         }
167         // HashMap replaces existing value on put
168         this.properties.put(key, value);
169     }
170 
171     /**
172      * Gets value of property with specified key or null if such property was not set.
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      * Gets collection of all properties. Such collection is not a copy, but the reference to internal collection!
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             // need to clone maps otherwise cloned object would reference the original ones
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             // should never be thrown since we support cloning.
208             log.error("Failed to clone itself with " + e.getLocalizedMessage(), e);
209             return null;
210         }
211     }
212 }