ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.org.jvnet.mimepull; ohair@286: ohair@286: import java.io.File; alanb@368: import java.util.logging.Level; alanb@368: import java.util.logging.Logger; ohair@286: ohair@286: /** ohair@286: * Configuration for MIME message parsing and storing. ohair@286: * ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: public class MIMEConfig { ohair@286: ohair@286: private static final int DEFAULT_CHUNK_SIZE = 8192; ohair@286: private static final long DEFAULT_MEMORY_THRESHOLD = 1048576L; ohair@286: private static final String DEFAULT_FILE_PREFIX = "MIME"; ohair@286: alanb@368: private static final Logger LOGGER = Logger.getLogger(MIMEConfig.class.getName()); alanb@368: ohair@286: // Parses the entire message eagerly ohair@286: boolean parseEagerly; ohair@286: ohair@286: // Approximate Chunk size ohair@286: int chunkSize; ohair@286: ohair@286: // Maximum in-memory data per attachment ohair@286: long memoryThreshold; ohair@286: ohair@286: // temp Dir to store large files ohair@286: File tempDir; ohair@286: String prefix; ohair@286: String suffix; ohair@286: ohair@286: private MIMEConfig(boolean parseEagerly, int chunkSize, ohair@286: long inMemoryThreshold, String dir, String prefix, String suffix) { ohair@286: this.parseEagerly = parseEagerly; ohair@286: this.chunkSize = chunkSize; ohair@286: this.memoryThreshold = inMemoryThreshold; ohair@286: this.prefix = prefix; ohair@286: this.suffix = suffix; ohair@286: setDir(dir); ohair@286: } ohair@286: ohair@286: public MIMEConfig() { ohair@286: this(false, DEFAULT_CHUNK_SIZE, DEFAULT_MEMORY_THRESHOLD, null, ohair@286: DEFAULT_FILE_PREFIX, null); ohair@286: } ohair@286: ohair@286: boolean isParseEagerly() { ohair@286: return parseEagerly; ohair@286: } ohair@286: ohair@286: public void setParseEagerly(boolean parseEagerly) { ohair@286: this.parseEagerly = parseEagerly; ohair@286: } ohair@286: ohair@286: int getChunkSize() { ohair@286: return chunkSize; ohair@286: } ohair@286: ohair@286: void setChunkSize(int chunkSize) { ohair@286: this.chunkSize = chunkSize; ohair@286: } ohair@286: ohair@286: long getMemoryThreshold() { ohair@286: return memoryThreshold; ohair@286: } ohair@286: ohair@286: /** ohair@286: * If the attachment is greater than the threshold, it is ohair@286: * written to the disk. ohair@286: * ohair@286: * @param memoryThreshold no of bytes per attachment ohair@286: * if -1, then the whole attachment is kept in memory ohair@286: */ ohair@286: public void setMemoryThreshold(long memoryThreshold) { ohair@286: this.memoryThreshold = memoryThreshold; ohair@286: } ohair@286: ohair@286: boolean isOnlyMemory() { ohair@286: return memoryThreshold == -1L; ohair@286: } ohair@286: ohair@286: File getTempDir() { ohair@286: return tempDir; ohair@286: } ohair@286: ohair@286: String getTempFilePrefix() { ohair@286: return prefix; ohair@286: } ohair@286: ohair@286: String getTempFileSuffix() { ohair@286: return suffix; ohair@286: } ohair@286: ohair@286: /** ohair@286: * @param dir ohair@286: */ alanb@368: public final void setDir(String dir) { ohair@286: if (tempDir == null && dir != null && !dir.equals("")) { ohair@286: tempDir = new File(dir); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Validates if it can create temporary files. Otherwise, it stores ohair@286: * attachment contents in memory. ohair@286: */ ohair@286: public void validate() { ohair@286: if (!isOnlyMemory()) { ohair@286: try { ohair@286: File tempFile = (tempDir == null) ohair@286: ? File.createTempFile(prefix, suffix) ohair@286: : File.createTempFile(prefix, suffix, tempDir); alanb@368: boolean deleted = tempFile.delete(); alanb@368: if (!deleted) { alanb@368: if (LOGGER.isLoggable(Level.INFO)) { alanb@368: LOGGER.log(Level.INFO, "File {0} was not deleted", tempFile.getAbsolutePath()); alanb@368: } alanb@368: } ohair@286: } catch(Exception ioe) { ohair@286: memoryThreshold = -1L; // whole attachment will be in-memory ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: }