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 java.io.IOException;
    19
    20  import org.jboss.netty.buffer.ChannelBuffer;
    21  import org.jboss.netty.buffer.ChannelBuffers;
    22  import org.jboss.netty.handler.codec.http.HttpConstants;
    23
    24  /**
    25   * Disk implementation of Attributes
    26   */
    27  public class DiskAttribute extends AbstractDiskHttpData implements Attribute {
    28      public static String baseDirectory;
    29
    30      public static boolean deleteOnExitTemporaryFile = true;
    31
    32      public static String prefix = "Attr_";
    33
    34      public static String postfix = ".att";
    35
    36      /**
    37       * Constructor used for huge Attribute
    38       * @param name
    39       */
    40      public DiskAttribute(String name) {
    41          super(name, HttpConstants.DEFAULT_CHARSET, 0);
    42      }
    43      /**
    44       *
    45       * @param name
    46       * @param value
    47       * @throws NullPointerException
    48       * @throws IllegalArgumentException
    49       * @throws IOException
    50       */
    51      public DiskAttribute(String name, String value) throws IOException {
    52          super(name, HttpConstants.DEFAULT_CHARSET, 0); // Attribute have no default size
    53          setValue(value);
    54      }
    55
    56      public HttpDataType getHttpDataType() {
    57          return HttpDataType.Attribute;
    58      }
    59
    60      public String getValue() throws IOException {
    61          byte [] bytes = get();
    62          return new String(bytes, charset.name());
    63      }
    64
    65      public void setValue(String value) throws IOException {
    66          if (value == null) {
    67              throw new NullPointerException("value");
    68          }
    69          byte [] bytes = value.getBytes(charset.name());
    70          ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes);
    71          if (definedSize > 0) {
    72              definedSize = buffer.readableBytes();
    73          }
    74          setContent(buffer);
    75      }
    76
    77      @Override
    78      public void addContent(ChannelBuffer buffer, boolean last) throws IOException {
    79          int localsize = buffer.readableBytes();
    80          if (definedSize > 0 && definedSize < size + localsize) {
    81              definedSize = size + localsize;
    82          }
    83          super.addContent(buffer, last);
    84      }
    85      @Override
    86      public int hashCode() {
    87          return getName().hashCode();
    88      }
    89
    90      @Override
    91      public boolean equals(Object o) {
    92          if (!(o instanceof Attribute)) {
    93              return false;
    94          }
    95          Attribute attribute = (Attribute) o;
    96          return getName().equalsIgnoreCase(attribute.getName());
    97      }
    98
    99      public int compareTo(InterfaceHttpData arg0) {
    100         if (!(arg0 instanceof Attribute)) {
    101             throw new ClassCastException("Cannot compare " + getHttpDataType() +
    102                     " with " + arg0.getHttpDataType());
    103         }
    104         return compareTo((Attribute) arg0);
    105     }
    106
    107     public int compareTo(Attribute o) {
    108         return getName().compareToIgnoreCase(o.getName());
    109     }
    110
    111     @Override
    112     public String toString() {
    113         try {
    114             return getName() + "=" + getValue();
    115         } catch (IOException e) {
    116             return getName() + "=IoException";
    117         }
    118     }
    119
    120     @Override
    121     protected boolean deleteOnExit() {
    122         return deleteOnExitTemporaryFile;
    123     }
    124
    125     @Override
    126     protected String getBaseDirectory() {
    127         return baseDirectory;
    128     }
    129
    130     @Override
    131     protected String getDiskFilename() {
    132         return getName() + postfix;
    133     }
    134
    135     @Override
    136     protected String getPostfix() {
    137         return postfix;
    138     }
    139
    140     @Override
    141     protected String getPrefix() {
    142         return prefix;
    143     }
    144 }