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      @Deprecated
33      protected final List<String> value = new ArrayList<String>();
34      private final Charset charset;
35  
36      @Deprecated
37      public InternalAttribute() {
38         this(CharsetUtil.UTF_8);
39      }
40  
41      @Deprecated
42      public InternalAttribute(Charset charset) {
43          this.charset = charset;
44      }
45  
46      public HttpDataType getHttpDataType() {
47          return HttpDataType.InternalAttribute;
48      }
49  
50      @Deprecated
51      public List<String> getValue() {
52          return value;
53      }
54  
55      public void addValue(String value) {
56          if (value == null) {
57              throw new NullPointerException("value");
58          }
59          this.value.add(value);
60      }
61  
62      public void addValue(String value, int rank) {
63          if (value == null) {
64              throw new NullPointerException("value");
65          }
66          this.value.add(rank, value);
67      }
68  
69      public void setValue(String value, int rank) {
70          if (value == null) {
71              throw new NullPointerException("value");
72          }
73          this.value.set(rank, value);
74      }
75  
76      @Override
77      public int hashCode() {
78          return getName().hashCode();
79      }
80  
81      @Override
82      public boolean equals(Object o) {
83          if (!(o instanceof Attribute)) {
84              return false;
85          }
86          Attribute attribute = (Attribute) o;
87          return getName().equalsIgnoreCase(attribute.getName());
88      }
89  
90      public int compareTo(InterfaceHttpData o) {
91          if (!(o instanceof InternalAttribute)) {
92              throw new ClassCastException("Cannot compare " + getHttpDataType() +
93                      " with " + o.getHttpDataType());
94          }
95          return compareTo((InternalAttribute) o);
96      }
97  
98      public int compareTo(InternalAttribute o) {
99          return getName().compareToIgnoreCase(o.getName());
100     }
101 
102     public int size() {
103         int size = 0;
104         for (String elt : value) {
105             try {
106                 size += elt.getBytes(charset.name()).length;
107             } catch (UnsupportedEncodingException e) {
108                 throw new RuntimeException(e);
109             }
110         }
111         return size;
112     }
113     @Override
114     public String toString() {
115         StringBuilder result = new StringBuilder();
116         for (String elt : value) {
117             result.append(elt);
118         }
119         return result.toString();
120     }
121 
122     public ChannelBuffer toChannelBuffer() {
123         ChannelBuffer[] buffers = new ChannelBuffer[value.size()];
124         for (int i = 0; i < buffers.length; i++) {
125             buffers[i] = ChannelBuffers.copiedBuffer(value.get(i), charset);
126         }
127         return ChannelBuffers.wrappedBuffer(buffers);
128     }
129 
130     public String getName() {
131         return "InternalAttribute";
132     }
133 }