src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEConfig.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEConfig.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,148 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.org.jvnet.mimepull;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.io.IOException;
    1.33 +
    1.34 +/**
    1.35 + * Configuration for MIME message parsing and storing.
    1.36 + *
    1.37 + * @author Jitendra Kotamraju
    1.38 + */
    1.39 +public class MIMEConfig {
    1.40 +
    1.41 +    private static final int DEFAULT_CHUNK_SIZE = 8192;
    1.42 +    private static final long DEFAULT_MEMORY_THRESHOLD = 1048576L;
    1.43 +    private static final String DEFAULT_FILE_PREFIX = "MIME";
    1.44 +
    1.45 +    // Parses the entire message eagerly
    1.46 +    boolean parseEagerly;
    1.47 +
    1.48 +    // Approximate Chunk size
    1.49 +    int chunkSize;
    1.50 +
    1.51 +    // Maximum in-memory data per attachment
    1.52 +    long memoryThreshold;
    1.53 +
    1.54 +    // Do not store to disk
    1.55 +    boolean onlyMemory;
    1.56 +
    1.57 +    // temp Dir to store large files
    1.58 +    File tempDir;
    1.59 +    String prefix;
    1.60 +    String suffix;
    1.61 +
    1.62 +
    1.63 +    private MIMEConfig(boolean parseEagerly, int chunkSize,
    1.64 +                       long inMemoryThreshold, String dir, String prefix, String suffix) {
    1.65 +        this.parseEagerly = parseEagerly;
    1.66 +        this.chunkSize = chunkSize;
    1.67 +        this.memoryThreshold = inMemoryThreshold;
    1.68 +        this.prefix = prefix;
    1.69 +        this.suffix = suffix;
    1.70 +        setDir(dir);
    1.71 +    }
    1.72 +
    1.73 +    public MIMEConfig() {
    1.74 +        this(false, DEFAULT_CHUNK_SIZE, DEFAULT_MEMORY_THRESHOLD, null,
    1.75 +                DEFAULT_FILE_PREFIX, null);
    1.76 +    }
    1.77 +
    1.78 +    boolean isParseEagerly() {
    1.79 +        return parseEagerly;
    1.80 +    }
    1.81 +
    1.82 +    public void setParseEagerly(boolean parseEagerly) {
    1.83 +        this.parseEagerly = parseEagerly;
    1.84 +    }
    1.85 +
    1.86 +    int getChunkSize() {
    1.87 +        return chunkSize;
    1.88 +    }
    1.89 +
    1.90 +    void setChunkSize(int chunkSize) {
    1.91 +        this.chunkSize = chunkSize;
    1.92 +    }
    1.93 +
    1.94 +    long getMemoryThreshold() {
    1.95 +        return memoryThreshold;
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * If the attachment is greater than the threshold, it is
   1.100 +     * written to the disk.
   1.101 +     *
   1.102 +     * @param memoryThreshold no of bytes per attachment
   1.103 +     *        if -1, then the whole attachment is kept in memory
   1.104 +     */
   1.105 +    public void setMemoryThreshold(long memoryThreshold) {
   1.106 +        this.memoryThreshold = memoryThreshold;
   1.107 +    }
   1.108 +
   1.109 +    boolean isOnlyMemory() {
   1.110 +        return memoryThreshold == -1L;
   1.111 +    }
   1.112 +
   1.113 +    File getTempDir() {
   1.114 +        return tempDir;
   1.115 +    }
   1.116 +
   1.117 +    String getTempFilePrefix() {
   1.118 +        return prefix;
   1.119 +    }
   1.120 +
   1.121 +    String getTempFileSuffix() {
   1.122 +        return suffix;
   1.123 +    }
   1.124 +
   1.125 +    /**
   1.126 +     * @param dir
   1.127 +     */
   1.128 +    public void setDir(String dir) {
   1.129 +        if (tempDir == null && dir != null && !dir.equals("")) {
   1.130 +            tempDir = new File(dir);
   1.131 +        }
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * Validates if it can create temporary files. Otherwise, it stores
   1.136 +     * attachment contents in memory.
   1.137 +     */
   1.138 +    public void validate() {
   1.139 +        if (!isOnlyMemory()) {
   1.140 +            try {
   1.141 +                File tempFile = (tempDir == null)
   1.142 +                        ? File.createTempFile(prefix, suffix)
   1.143 +                        : File.createTempFile(prefix, suffix, tempDir);
   1.144 +                tempFile.delete();
   1.145 +            } catch(Exception ioe) {
   1.146 +                memoryThreshold = -1L;      // whole attachment will be in-memory
   1.147 +            }
   1.148 +        }
   1.149 +    }
   1.150 +
   1.151 +}

mercurial