View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.xml.internal.org.jvnet.mimepull;
27  
28  import java.io.File;
29  import java.util.logging.Level;
30  import java.util.logging.Logger;
31  
32  /**
33   * Configuration for MIME message parsing and storing.
34   *
35   * @author Jitendra Kotamraju
36   */
37  public class MIMEConfig {
38  
39      private static final int DEFAULT_CHUNK_SIZE = 8192;
40      private static final long DEFAULT_MEMORY_THRESHOLD = 1048576L;
41      private static final String DEFAULT_FILE_PREFIX = "MIME";
42  
43      private static final Logger LOGGER = Logger.getLogger(MIMEConfig.class.getName());
44  
45      // Parses the entire message eagerly
46      boolean parseEagerly;
47  
48      // Approximate Chunk size
49      int chunkSize;
50  
51      // Maximum in-memory data per attachment
52      long memoryThreshold;
53  
54      // temp Dir to store large files
55      File tempDir;
56      String prefix;
57      String suffix;
58  
59      private MIMEConfig(boolean parseEagerly, int chunkSize,
60                         long inMemoryThreshold, String dir, String prefix, String suffix) {
61          this.parseEagerly = parseEagerly;
62          this.chunkSize = chunkSize;
63          this.memoryThreshold = inMemoryThreshold;
64          this.prefix = prefix;
65          this.suffix = suffix;
66          setDir(dir);
67      }
68  
69      public MIMEConfig() {
70          this(false, DEFAULT_CHUNK_SIZE, DEFAULT_MEMORY_THRESHOLD, null,
71                  DEFAULT_FILE_PREFIX, null);
72      }
73  
74      boolean isParseEagerly() {
75          return parseEagerly;
76      }
77  
78      public void setParseEagerly(boolean parseEagerly) {
79          this.parseEagerly = parseEagerly;
80      }
81  
82      int getChunkSize() {
83          return chunkSize;
84      }
85  
86      void setChunkSize(int chunkSize) {
87          this.chunkSize = chunkSize;
88      }
89  
90      long getMemoryThreshold() {
91          return memoryThreshold;
92      }
93  
94      /**
95       * If the attachment is greater than the threshold, it is
96       * written to the disk.
97       *
98       * @param memoryThreshold no of bytes per attachment
99       *        if -1, then the whole attachment is kept in memory
100      */
101     public void setMemoryThreshold(long memoryThreshold) {
102         this.memoryThreshold = memoryThreshold;
103     }
104 
105     boolean isOnlyMemory() {
106         return memoryThreshold == -1L;
107     }
108 
109     File getTempDir() {
110         return tempDir;
111     }
112 
113     String getTempFilePrefix() {
114         return prefix;
115     }
116 
117     String getTempFileSuffix() {
118         return suffix;
119     }
120 
121     /**
122      * @param dir
123      */
124     public final void setDir(String dir) {
125         if (tempDir == null && dir != null && !dir.equals("")) {
126             tempDir = new File(dir);
127         }
128     }
129 
130     /**
131      * Validates if it can create temporary files. Otherwise, it stores
132      * attachment contents in memory.
133      */
134     public void validate() {
135         if (!isOnlyMemory()) {
136             try {
137                 File tempFile = (tempDir == null)
138                         ? File.createTempFile(prefix, suffix)
139                         : File.createTempFile(prefix, suffix, tempDir);
140                 boolean deleted = tempFile.delete();
141                 if (!deleted) {
142                     if (LOGGER.isLoggable(Level.INFO)) {
143                         LOGGER.log(Level.INFO, "File {0} was not deleted", tempFile.getAbsolutePath());
144                     }
145                 }
146             } catch(Exception ioe) {
147                 memoryThreshold = -1L;      // whole attachment will be in-memory
148             }
149         }
150     }
151 
152 }