View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
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  package net.sf.exorcist.core;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.security.MessageDigest;
25  import java.security.NoSuchAlgorithmException;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.apache.commons.codec.binary.Hex;
31  import org.apache.commons.io.CopyUtils;
32  import org.apache.commons.io.IOUtils;
33  
34  import net.sf.exorcist.api.ContentState;
35  
36  public class FileContentState implements ContentState {
37  
38      private static final int BUFFER_SIZE = 4096;
39  
40      private File content;
41  
42      private Map attachments;
43  
44      public FileContentState() {
45          content = null;
46          attachments = new HashMap();
47      }
48  
49      public InputStream getContent() throws IOException {
50          if (content == null) {
51              return new ByteArrayInputStream(new byte[] {});
52          } else {
53              return new FileInputStream(content);
54          }
55      }
56  
57      public void setContent(InputStream input) throws IOException {
58          if (content == null) {
59              content = File.createTempFile("exorcist", ".xml");
60              content.deleteOnExit();
61          }
62          FileOutputStream output = new FileOutputStream(content);
63          try {
64              CopyUtils.copy(input, output);
65          } finally {
66              IOUtils.closeQuietly(output);
67          }
68      }
69  
70      public String addAttachment(InputStream data) throws IOException {
71          File file = File.createTempFile("exorcist", ".bin");
72          file.deleteOnExit();
73          FileOutputStream output = new FileOutputStream(file);
74          try {
75              MessageDigest digest = MessageDigest.getInstance("SHA-1");
76              byte[] buffer = new byte[BUFFER_SIZE];
77              for (int n = data.read(buffer); n != -1; n = data.read(buffer)) {
78                  digest.update(buffer, 0, n);
79                  output.write(buffer, 0, n);
80              }
81              String hash = new String(Hex.encodeHex(digest.digest()));
82              File old = (File) attachments.get(hash);
83              if (old != null) {
84                  old.delete();
85              }
86              attachments.put(hash, file);
87              return hash;
88          } catch (NoSuchAlgorithmException e) {
89              throw new IOException("SHA-1 not available: " + e.getMessage());
90          } finally {
91              IOUtils.closeQuietly(output);
92          }
93      }
94  
95      public Collection getAttachmentHashes() {
96          return attachments.keySet();
97      }
98  
99      public InputStream getAttachment(String hash)
100             throws IOException, IllegalArgumentException {
101         File file = (File) attachments.get(hash);
102         if (file != null) {
103             return new FileInputStream(file);
104         } else {
105             throw new IllegalArgumentException("Attachment not found: " + hash);
106         }
107     }
108 
109     public void removeAttachment(String hash) throws IllegalArgumentException {
110         File file = (File) attachments.remove(hash);
111         if (file != null) {
112             file.delete();
113         } else {
114             throw new IllegalArgumentException("Attachment not found: " + hash);
115         }
116     }
117 
118 }