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    *   http://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 org.jboss.netty.handler.codec.http.multipart;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.buffer.ChannelBuffers;
20  import org.jboss.netty.util.CharsetUtil;
21  
22  import java.io.UnsupportedEncodingException;
23  import java.nio.charset.Charset;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * This Attribute is only for Encoder use to insert special command between object if needed
29   * (like Multipart Mixed mode)
30   */
31  public class InternalAttribute implements InterfaceHttpData {
32      protected final List<String> value = new ArrayList<String>();
33      private final Charset charset;
34  
35      @Deprecated
36      public InternalAttribute() {
37         this(CharsetUtil.UTF_8);
38      }
39  
40      public InternalAttribute(Charset charset) {
41          this.charset = charset;
42      }
43  
44      public HttpDataType getHttpDataType() {
45          return HttpDataType.InternalAttribute;
46      }
47  
48      @Deprecated
49      public List<String> getValue() {
50          return value;
51      }
52  
53      public void addValue(String value) {
54          if (value == null) {
55              throw new NullPointerException("value");
56          }
57          this.value.add(value);
58      }
59  
60      public void addValue(String value, int rank) {
61          if (value == null) {
62              throw new NullPointerException("value");
63          }
64          this.value.add(rank, value);
65      }
66  
67      public void setValue(String value, int rank) {
68          if (value == null) {
69              throw new NullPointerException("value");
70          }
71          this.value.set(rank, value);
72      }
73  
74      @Override
75      public int hashCode() {
76          return getName().hashCode();
77      }
78  
79      @Override
80      public boolean equals(Object o) {
81          if (!(o instanceof Attribute)) {
82              return false;
83          }
84          Attribute attribute = (Attribute) o;
85          return getName().equalsIgnoreCase(attribute.getName());
86      }
87  
88      public int compareTo(InterfaceHttpData o) {
89          if (!(o instanceof InternalAttribute)) {
90              throw new ClassCastException("Cannot compare " + getHttpDataType() +
91                      " with " + o.getHttpDataType());
92          }
93          return compareTo((InternalAttribute) o);
94      }
95  
96      public int compareTo(InternalAttribute o) {
97          return getName().compareToIgnoreCase(o.getName());
98      }
99  
100     public int size() {
101         int size = 0;
102         for (String elt : value) {
103             try {
104                 size += elt.getBytes(charset.name()).length;
105             } catch (UnsupportedEncodingException e) {
106                 throw new RuntimeException(e);
107             }
108         }
109         return size;
110     }
111     @Override
112     public String toString() {
113         StringBuilder result = new StringBuilder();
114         for (String elt : value) {
115             result.append(elt);
116         }
117         return result.toString();
118     }
119 
120     public ChannelBuffer toChannelBuffer() {
121         ChannelBuffer[] buffers = new ChannelBuffer[value.size()];
122         for (int i = 0; i < buffers.length; i++) {
123             buffers[i] = ChannelBuffers.copiedBuffer(value.get(i), charset);
124         }
125         return ChannelBuffers.wrappedBuffer(buffers);
126     }
127 
128     public String getName() {
129         return "InternalAttribute";
130     }
131 }