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