1 | package fr.mael.jiwigo.transverse.util; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.ByteArrayOutputStream; |
---|
6 | import java.io.File; |
---|
7 | import java.io.FileInputStream; |
---|
8 | import java.io.FileOutputStream; |
---|
9 | import java.io.IOException; |
---|
10 | import java.io.InputStream; |
---|
11 | import java.io.InputStreamReader; |
---|
12 | import java.io.PrintWriter; |
---|
13 | import java.io.StringReader; |
---|
14 | import java.io.StringWriter; |
---|
15 | import java.io.Writer; |
---|
16 | import java.net.URL; |
---|
17 | import java.net.URLClassLoader; |
---|
18 | import java.security.MessageDigest; |
---|
19 | import java.util.ArrayList; |
---|
20 | |
---|
21 | import org.apache.sanselan.Sanselan; |
---|
22 | import org.apache.sanselan.common.IImageMetadata; |
---|
23 | import org.apache.sanselan.formats.jpeg.JpegImageMetadata; |
---|
24 | import org.apache.sanselan.formats.jpeg.JpegPhotoshopMetadata; |
---|
25 | import org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter; |
---|
26 | import org.apache.sanselan.formats.jpeg.iptc.JpegIptcRewriter; |
---|
27 | import org.apache.sanselan.formats.jpeg.iptc.PhotoshopApp13Data; |
---|
28 | import org.apache.sanselan.formats.tiff.write.TiffOutputSet; |
---|
29 | import org.jdom.Document; |
---|
30 | import org.jdom.JDOMException; |
---|
31 | import org.jdom.input.SAXBuilder; |
---|
32 | import org.jdom.output.XMLOutputter; |
---|
33 | |
---|
34 | /** |
---|
35 | Copyright (c) 2010, Mael |
---|
36 | All rights reserved. |
---|
37 | |
---|
38 | Redistribution and use in source and binary forms, with or without |
---|
39 | modification, are permitted provided that the following conditions are met: |
---|
40 | * Redistributions of source code must retain the above copyright |
---|
41 | notice, this list of conditions and the following disclaimer. |
---|
42 | * Redistributions in binary form must reproduce the above copyright |
---|
43 | notice, this list of conditions and the following disclaimer in the |
---|
44 | documentation and/or other materials provided with the distribution. |
---|
45 | * Neither the name of jiwigo nor the |
---|
46 | names of its contributors may be used to endorse or promote products |
---|
47 | derived from this software without specific prior written permission. |
---|
48 | |
---|
49 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
50 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
51 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
52 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
53 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
54 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
55 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
56 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
57 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
58 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
59 | |
---|
60 | |
---|
61 | * @author mael |
---|
62 | * |
---|
63 | */ |
---|
64 | public class Outil { |
---|
65 | /** |
---|
66 | * Logger |
---|
67 | */ |
---|
68 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(Outil.class); |
---|
69 | |
---|
70 | /** |
---|
71 | * Transformation d'un inputstream en string.<br/> |
---|
72 | * Util pour lire la réponse du webservice |
---|
73 | * @param input le stream |
---|
74 | * @return la string |
---|
75 | * @throws IOException |
---|
76 | */ |
---|
77 | public static String readInputStreamAsString(InputStream input) throws IOException { |
---|
78 | // DataInputStream dis = new DataInputStream(new BufferedInputStream(input)); |
---|
79 | // |
---|
80 | // String temp; |
---|
81 | // StringBuffer buffer = new StringBuffer(); |
---|
82 | // while ((temp = dis.readLine()) != null) { |
---|
83 | // buffer.append(temp); |
---|
84 | // } |
---|
85 | // return buffer.toString(); |
---|
86 | StringWriter writer = new StringWriter(); |
---|
87 | InputStreamReader streamReader = new InputStreamReader(input); |
---|
88 | //le buffer permet le readline |
---|
89 | BufferedReader buffer = new BufferedReader(streamReader); |
---|
90 | String line = ""; |
---|
91 | while (null != (line = buffer.readLine())) { |
---|
92 | writer.write(line); |
---|
93 | } |
---|
94 | // Sortie finale dans le String |
---|
95 | return writer.toString(); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Transformation d'une string en document |
---|
100 | * @param string la string |
---|
101 | * @return le document correspondant |
---|
102 | * @throws JDOMException |
---|
103 | * @throws IOException |
---|
104 | */ |
---|
105 | public static Document stringToDocument(String string) throws JDOMException, IOException { |
---|
106 | SAXBuilder sb = new SAXBuilder(); |
---|
107 | System.out.println(string); |
---|
108 | Document doc = sb.build(new StringReader(string)); |
---|
109 | return doc; |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Transformation d'un inputstream en document |
---|
115 | * @param input l'inputstream |
---|
116 | * @return le document |
---|
117 | * @throws JDOMException |
---|
118 | * @throws IOException |
---|
119 | */ |
---|
120 | public static Document readInputStreamAsDocument(InputStream input) throws JDOMException, IOException { |
---|
121 | return stringToDocument(readInputStreamAsString(input)); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Transformation d'un document en string |
---|
126 | * @param doc le document à transformer |
---|
127 | * @return la string |
---|
128 | */ |
---|
129 | public static String documentToString(Document doc) { |
---|
130 | return new XMLOutputter().outputString(doc); |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Fonction qui retour l'url d'un fichier donné. |
---|
136 | * Utile pour récupérer le chemin des images à l'intérieur du jar |
---|
137 | * @param fileName le chemin du fichier |
---|
138 | * @return l'url |
---|
139 | */ |
---|
140 | public static URL getURL(String fileName) { |
---|
141 | URLClassLoader urlLoader = (URLClassLoader) Outil.class.getClassLoader(); |
---|
142 | URL fileLocation = urlLoader.findResource(fileName); |
---|
143 | return fileLocation; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Fonction qui retourne la somme de controle MD5 d'un fichier donné |
---|
148 | * @param filename le chemin du fichier |
---|
149 | * @return la somme de controle |
---|
150 | * @throws Exception |
---|
151 | */ |
---|
152 | public static String getMD5Checksum(String filename) throws Exception { |
---|
153 | byte[] b = createChecksum(filename); |
---|
154 | String result = ""; |
---|
155 | for (int i = 0; i < b.length; i++) { |
---|
156 | result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1); |
---|
157 | } |
---|
158 | return result; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Creation de la somme de controle d'un fichier |
---|
163 | * @param filename le chemin du fichier |
---|
164 | * @return la somme de controle sous forme de byte[] |
---|
165 | * @throws Exception |
---|
166 | */ |
---|
167 | private static byte[] createChecksum(String filename) throws Exception { |
---|
168 | InputStream fis = new FileInputStream(filename); |
---|
169 | |
---|
170 | byte[] buffer = new byte[1024]; |
---|
171 | MessageDigest complete = MessageDigest.getInstance("MD5"); |
---|
172 | int numRead; |
---|
173 | do { |
---|
174 | numRead = fis.read(buffer); |
---|
175 | if (numRead > 0) { |
---|
176 | complete.update(buffer, 0, numRead); |
---|
177 | } |
---|
178 | } while (numRead != -1); |
---|
179 | fis.close(); |
---|
180 | return complete.digest(); |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Fonction qui transforme un fichier en tableau de bytes |
---|
185 | * @param file le fichier |
---|
186 | * @return le tableau de bytes |
---|
187 | * @throws IOException |
---|
188 | */ |
---|
189 | public static byte[] getBytesFromFile(File file) throws IOException { |
---|
190 | InputStream is = new FileInputStream(file); |
---|
191 | long length = file.length(); |
---|
192 | |
---|
193 | byte[] bytes = new byte[(int) length]; |
---|
194 | int offset = 0; |
---|
195 | int numRead = 0; |
---|
196 | while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { |
---|
197 | offset += numRead; |
---|
198 | } |
---|
199 | |
---|
200 | if (offset < bytes.length) { |
---|
201 | throw new IOException("Could not completely read file " + file.getName()); |
---|
202 | } |
---|
203 | is.close(); |
---|
204 | return bytes; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Fonction qui vérifie que si la réponse du webservice est positive |
---|
209 | * @param doc le document que retourne le webservice |
---|
210 | * @return true si c'est ok |
---|
211 | */ |
---|
212 | public static boolean checkOk(Document doc) { |
---|
213 | if (doc.getRootElement().getAttributeValue("stat").equals("ok")) { |
---|
214 | return true; |
---|
215 | } else { |
---|
216 | LOG.error("Resultat : " + doc.getRootElement().getAttributeValue("stat") + "\nDocument retourné : \n" |
---|
217 | + Outil.documentToString(doc)); |
---|
218 | return false; |
---|
219 | } |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * Exception to string |
---|
225 | * @param aThrowable exception |
---|
226 | * @return l'exception en string |
---|
227 | */ |
---|
228 | public static String getStackTrace(Throwable aThrowable) { |
---|
229 | final Writer result = new StringWriter(); |
---|
230 | final PrintWriter printWriter = new PrintWriter(result); |
---|
231 | aThrowable.printStackTrace(printWriter); |
---|
232 | return result.toString(); |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * Function that splits a file |
---|
237 | * @param fichier the file to split |
---|
238 | * @param size the size of the resulting chunks |
---|
239 | * @return the list of files |
---|
240 | * @throws IOException |
---|
241 | */ |
---|
242 | //feature:0001827 |
---|
243 | public static ArrayList<File> splitFile(File fichier, int size) throws IOException { |
---|
244 | FileInputStream fis = new FileInputStream(fichier); |
---|
245 | byte buffer[] = new byte[size]; |
---|
246 | ArrayList<File> listFichiers = new ArrayList<File>(); |
---|
247 | int count = 0; |
---|
248 | while (true) { |
---|
249 | int i = fis.read(buffer, 0, size); |
---|
250 | if (i == -1) |
---|
251 | break; |
---|
252 | File file = new File(System.getProperty("java.io.tmpdir") + "/tempcut" + count); |
---|
253 | listFichiers.add(file); |
---|
254 | FileOutputStream fos = new FileOutputStream(file); |
---|
255 | fos.write(buffer, 0, i); |
---|
256 | fos.flush(); |
---|
257 | fos.close(); |
---|
258 | |
---|
259 | ++count; |
---|
260 | } |
---|
261 | return listFichiers; |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * Function used to put the exif and iptc metadata from one image to another |
---|
266 | * @param enriched original image where the metadata comes from |
---|
267 | * @param naked image where to put metadata |
---|
268 | * @return enriched image |
---|
269 | * @throws Exception |
---|
270 | */ |
---|
271 | public static byte[] enrich(byte[] enriched, byte[] naked) throws Exception { |
---|
272 | |
---|
273 | // read IPTC metadata from the original enriched image |
---|
274 | IImageMetadata metadata = Sanselan.getMetadata(enriched); |
---|
275 | JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; |
---|
276 | JpegPhotoshopMetadata photoshopMetadata = jpegMetadata.getPhotoshop(); |
---|
277 | if (photoshopMetadata == null) { |
---|
278 | return naked; |
---|
279 | } |
---|
280 | |
---|
281 | PhotoshopApp13Data data = photoshopMetadata.photoshopApp13Data; |
---|
282 | |
---|
283 | // read the EXIF metadata from the parsed JPEG metadata |
---|
284 | TiffOutputSet outputSet = jpegMetadata.getExif().getOutputSet(); |
---|
285 | |
---|
286 | // enrich the naked byte[] with EXIF metadata |
---|
287 | ExifRewriter writer = new ExifRewriter(); |
---|
288 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
---|
289 | writer.updateExifMetadataLossless(naked, outputStream, outputSet); |
---|
290 | |
---|
291 | // enrich the partially clothed byte[] with IPTC metadata |
---|
292 | InputStream src = new ByteArrayInputStream(outputStream.toByteArray()); |
---|
293 | ByteArrayOutputStream dest = new ByteArrayOutputStream(); |
---|
294 | new JpegIptcRewriter().writeIPTC(src, dest, data); |
---|
295 | |
---|
296 | // return the fully clothed image as a byte[] |
---|
297 | return dest.toByteArray(); |
---|
298 | } |
---|
299 | |
---|
300 | public static void byteToFile(String fichier, byte[] bytes) throws IOException { |
---|
301 | FileOutputStream fos = new FileOutputStream(fichier); |
---|
302 | fos.write(bytes); |
---|
303 | fos.flush(); |
---|
304 | fos.close(); |
---|
305 | |
---|
306 | } |
---|
307 | } |
---|