1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http.multipart;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelException;
20 import io.netty.handler.codec.http.HttpHeaderNames;
21 import io.netty.handler.codec.http.HttpHeaderValues;
22 import io.netty.util.internal.ObjectUtil;
23
24 import java.io.IOException;
25 import java.nio.charset.Charset;
26
27
28
29
30
31
32 public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUpload {
33
34 private String filename;
35
36 private String contentType;
37
38 private String contentTransferEncoding;
39
40 public MemoryFileUpload(String name, String filename, String contentType,
41 String contentTransferEncoding, Charset charset, long size) {
42 super(name, charset, size);
43 setFilename(filename);
44 setContentType(contentType);
45 setContentTransferEncoding(contentTransferEncoding);
46 }
47
48 @Override
49 public HttpDataType getHttpDataType() {
50 return HttpDataType.FileUpload;
51 }
52
53 @Override
54 public String getFilename() {
55 return filename;
56 }
57
58 @Override
59 public void setFilename(String filename) {
60 this.filename = ObjectUtil.checkNotNull(filename, "filename");
61 }
62
63 @Override
64 public int hashCode() {
65 return FileUploadUtil.hashCode(this);
66 }
67
68 @Override
69 public boolean equals(Object o) {
70 return o instanceof FileUpload && FileUploadUtil.equals(this, (FileUpload) o);
71 }
72
73 @Override
74 public int compareTo(InterfaceHttpData o) {
75 if (!(o instanceof FileUpload)) {
76 throw new ClassCastException("Cannot compare " + getHttpDataType() +
77 " with " + o.getHttpDataType());
78 }
79 return compareTo((FileUpload) o);
80 }
81
82 public int compareTo(FileUpload o) {
83 return FileUploadUtil.compareTo(this, o);
84 }
85
86 @Override
87 public void setContentType(String contentType) {
88 this.contentType = ObjectUtil.checkNotNull(contentType, "contentType");
89 }
90
91 @Override
92 public String getContentType() {
93 return contentType;
94 }
95
96 @Override
97 public String getContentTransferEncoding() {
98 return contentTransferEncoding;
99 }
100
101 @Override
102 public void setContentTransferEncoding(String contentTransferEncoding) {
103 this.contentTransferEncoding = contentTransferEncoding;
104 }
105
106 @Override
107 public String toString() {
108 return HttpHeaderNames.CONTENT_DISPOSITION + ": " +
109 HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() +
110 "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" +
111 HttpHeaderNames.CONTENT_TYPE + ": " + contentType +
112 (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset().name() + "\r\n" : "\r\n") +
113 HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" +
114 "Completed: " + isCompleted() +
115 "\r\nIsInMemory: " + isInMemory();
116 }
117
118 @Override
119 public FileUpload copy() {
120 final ByteBuf content = content();
121 return replace(content != null ? content.copy() : content);
122 }
123
124 @Override
125 public FileUpload duplicate() {
126 final ByteBuf content = content();
127 return replace(content != null ? content.duplicate() : content);
128 }
129
130 @Override
131 public FileUpload retainedDuplicate() {
132 ByteBuf content = content();
133 if (content != null) {
134 content = content.retainedDuplicate();
135 boolean success = false;
136 try {
137 FileUpload duplicate = replace(content);
138 success = true;
139 return duplicate;
140 } finally {
141 if (!success) {
142 content.release();
143 }
144 }
145 } else {
146 return replace(null);
147 }
148 }
149
150 @Override
151 public FileUpload replace(ByteBuf content) {
152 MemoryFileUpload upload = new MemoryFileUpload(
153 getName(), getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
154 if (content != null) {
155 try {
156 upload.setContent(content);
157 } catch (IOException e) {
158 throw new ChannelException(e);
159 }
160 }
161 upload.setCompleted(isCompleted());
162 return upload;
163 }
164
165 @Override
166 public FileUpload retain() {
167 super.retain();
168 return this;
169 }
170
171 @Override
172 public FileUpload retain(int increment) {
173 super.retain(increment);
174 return this;
175 }
176
177 @Override
178 public FileUpload touch() {
179 super.touch();
180 return this;
181 }
182
183 @Override
184 public FileUpload touch(Object hint) {
185 super.touch(hint);
186 return this;
187 }
188 }