|
1 /* |
|
2 * Copyright (c) 1997, 2010, 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.io.IOException; |
|
30 |
|
31 /** |
|
32 * Configuration for MIME message parsing and storing. |
|
33 * |
|
34 * @author Jitendra Kotamraju |
|
35 */ |
|
36 public class MIMEConfig { |
|
37 |
|
38 private static final int DEFAULT_CHUNK_SIZE = 8192; |
|
39 private static final long DEFAULT_MEMORY_THRESHOLD = 1048576L; |
|
40 private static final String DEFAULT_FILE_PREFIX = "MIME"; |
|
41 |
|
42 // Parses the entire message eagerly |
|
43 boolean parseEagerly; |
|
44 |
|
45 // Approximate Chunk size |
|
46 int chunkSize; |
|
47 |
|
48 // Maximum in-memory data per attachment |
|
49 long memoryThreshold; |
|
50 |
|
51 // Do not store to disk |
|
52 boolean onlyMemory; |
|
53 |
|
54 // temp Dir to store large files |
|
55 File tempDir; |
|
56 String prefix; |
|
57 String suffix; |
|
58 |
|
59 |
|
60 private MIMEConfig(boolean parseEagerly, int chunkSize, |
|
61 long inMemoryThreshold, String dir, String prefix, String suffix) { |
|
62 this.parseEagerly = parseEagerly; |
|
63 this.chunkSize = chunkSize; |
|
64 this.memoryThreshold = inMemoryThreshold; |
|
65 this.prefix = prefix; |
|
66 this.suffix = suffix; |
|
67 setDir(dir); |
|
68 } |
|
69 |
|
70 public MIMEConfig() { |
|
71 this(false, DEFAULT_CHUNK_SIZE, DEFAULT_MEMORY_THRESHOLD, null, |
|
72 DEFAULT_FILE_PREFIX, null); |
|
73 } |
|
74 |
|
75 boolean isParseEagerly() { |
|
76 return parseEagerly; |
|
77 } |
|
78 |
|
79 public void setParseEagerly(boolean parseEagerly) { |
|
80 this.parseEagerly = parseEagerly; |
|
81 } |
|
82 |
|
83 int getChunkSize() { |
|
84 return chunkSize; |
|
85 } |
|
86 |
|
87 void setChunkSize(int chunkSize) { |
|
88 this.chunkSize = chunkSize; |
|
89 } |
|
90 |
|
91 long getMemoryThreshold() { |
|
92 return memoryThreshold; |
|
93 } |
|
94 |
|
95 /** |
|
96 * If the attachment is greater than the threshold, it is |
|
97 * written to the disk. |
|
98 * |
|
99 * @param memoryThreshold no of bytes per attachment |
|
100 * if -1, then the whole attachment is kept in memory |
|
101 */ |
|
102 public void setMemoryThreshold(long memoryThreshold) { |
|
103 this.memoryThreshold = memoryThreshold; |
|
104 } |
|
105 |
|
106 boolean isOnlyMemory() { |
|
107 return memoryThreshold == -1L; |
|
108 } |
|
109 |
|
110 File getTempDir() { |
|
111 return tempDir; |
|
112 } |
|
113 |
|
114 String getTempFilePrefix() { |
|
115 return prefix; |
|
116 } |
|
117 |
|
118 String getTempFileSuffix() { |
|
119 return suffix; |
|
120 } |
|
121 |
|
122 /** |
|
123 * @param dir |
|
124 */ |
|
125 public void setDir(String dir) { |
|
126 if (tempDir == null && dir != null && !dir.equals("")) { |
|
127 tempDir = new File(dir); |
|
128 } |
|
129 } |
|
130 |
|
131 /** |
|
132 * Validates if it can create temporary files. Otherwise, it stores |
|
133 * attachment contents in memory. |
|
134 */ |
|
135 public void validate() { |
|
136 if (!isOnlyMemory()) { |
|
137 try { |
|
138 File tempFile = (tempDir == null) |
|
139 ? File.createTempFile(prefix, suffix) |
|
140 : File.createTempFile(prefix, suffix, tempDir); |
|
141 tempFile.delete(); |
|
142 } catch(Exception ioe) { |
|
143 memoryThreshold = -1L; // whole attachment will be in-memory |
|
144 } |
|
145 } |
|
146 } |
|
147 |
|
148 } |