View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http.multipart;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.handler.codec.http.HttpConstants;
20  
21  import java.io.IOException;
22  import java.nio.charset.Charset;
23  
24  /**
25   * Mixed implementation using both in Memory and in File with a limit of size
26   */
27  public class MixedAttribute extends AbstractMixedHttpData<Attribute> implements Attribute {
28      public MixedAttribute(String name, long limitSize) {
29          this(name, limitSize, HttpConstants.DEFAULT_CHARSET);
30      }
31  
32      public MixedAttribute(String name, long definedSize, long limitSize) {
33          this(name, definedSize, limitSize, HttpConstants.DEFAULT_CHARSET);
34      }
35  
36      public MixedAttribute(String name, long limitSize, Charset charset) {
37          this(name, limitSize, charset, DiskAttribute.baseDirectory, DiskAttribute.deleteOnExitTemporaryFile);
38      }
39  
40      public MixedAttribute(String name, long limitSize, Charset charset, String baseDir, boolean deleteOnExit) {
41          this(name, 0, limitSize, charset, baseDir, deleteOnExit);
42      }
43  
44      public MixedAttribute(String name, long definedSize, long limitSize, Charset charset) {
45          this(name, definedSize, limitSize, charset,
46                  DiskAttribute.baseDirectory, DiskAttribute.deleteOnExitTemporaryFile);
47      }
48  
49      public MixedAttribute(String name, long definedSize, long limitSize, Charset charset,
50                            String baseDir, boolean deleteOnExit) {
51          super(limitSize, baseDir, deleteOnExit,
52                  new MemoryAttribute(name, definedSize, charset));
53      }
54  
55      public MixedAttribute(String name, String value, long limitSize) {
56          this(name, value, limitSize, HttpConstants.DEFAULT_CHARSET,
57                  DiskAttribute.baseDirectory, DiskFileUpload.deleteOnExitTemporaryFile);
58      }
59  
60      public MixedAttribute(String name, String value, long limitSize, Charset charset) {
61          this(name, value, limitSize, charset,
62                  DiskAttribute.baseDirectory, DiskFileUpload.deleteOnExitTemporaryFile);
63      }
64  
65      private static Attribute makeInitialAttributeFromValue(String name, String value, long limitSize, Charset charset,
66                                                             String baseDir, boolean deleteOnExit) {
67          if (value.length() > limitSize) {
68              try {
69                  return new DiskAttribute(name, value, charset, baseDir, deleteOnExit);
70              } catch (IOException e) {
71                  // revert to Memory mode
72                  try {
73                      return new MemoryAttribute(name, value, charset);
74                  } catch (IOException ignore) {
75                      throw new IllegalArgumentException(e);
76                  }
77              }
78          } else {
79              try {
80                  return new MemoryAttribute(name, value, charset);
81              } catch (IOException e) {
82                  throw new IllegalArgumentException(e);
83              }
84          }
85      }
86  
87      public MixedAttribute(String name, String value, long limitSize, Charset charset,
88                            String baseDir, boolean deleteOnExit) {
89          super(limitSize, baseDir, deleteOnExit,
90                  makeInitialAttributeFromValue(name, value, limitSize, charset, baseDir, deleteOnExit));
91      }
92  
93      @Override
94      public String getValue() throws IOException {
95          return wrapped.getValue();
96      }
97  
98      @Override
99      public void setValue(String value) throws IOException {
100         wrapped.setValue(value);
101     }
102 
103     @Override
104     Attribute makeDiskData() {
105         DiskAttribute diskAttribute = new DiskAttribute(getName(), definedLength(), baseDir, deleteOnExit);
106         diskAttribute.setMaxSize(getMaxSize());
107         return diskAttribute;
108     }
109 
110     @Override
111     public Attribute copy() {
112         // for binary compatibility
113         return super.copy();
114     }
115 
116     @Override
117     public Attribute duplicate() {
118         // for binary compatibility
119         return super.duplicate();
120     }
121 
122     @Override
123     public Attribute replace(ByteBuf content) {
124         // for binary compatibility
125         return super.replace(content);
126     }
127 
128     @Override
129     public Attribute retain() {
130         // for binary compatibility
131         return super.retain();
132     }
133 
134     @Override
135     public Attribute retain(int increment) {
136         // for binary compatibility
137         return super.retain(increment);
138     }
139 
140     @Override
141     public Attribute retainedDuplicate() {
142         // for binary compatibility
143         return super.retainedDuplicate();
144     }
145 
146     @Override
147     public Attribute touch() {
148         // for binary compatibility
149         return super.touch();
150     }
151 
152     @Override
153     public Attribute touch(Object hint) {
154         // for binary compatibility
155         return super.touch(hint);
156     }
157 }