001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.extend;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.InputStream;
020:
021: /**
022: * The result of a DWR query is normally a set of name/value pairs unless we are
023: * doing file-upload in which case there is more information with each field.
024: * This class replaces the value part of the set of name/value pairs to
025: * provide access to the extra information.
026: * @author Lance Semmens [uklance at gmail dot com]
027: */
028: public class FormField {
029: /**
030: * Standard ctor for the normal non file-upload case
031: * @param value The string value
032: */
033: public FormField(String value) {
034: this .name = null;
035: this .mimeType = null;
036: this .file = false;
037: this .bytes = value.getBytes();
038: this .string = value;
039: }
040:
041: /**
042: * Ctor for when we are in the special file-upload case
043: * @param name The file name
044: * @param mimeType The mime type sent by the browser
045: * @param bytes The bytes sent by the browser
046: */
047: public FormField(String name, String mimeType, byte[] bytes) {
048: this .name = name;
049: this .mimeType = mimeType;
050: this .file = true;
051: this .bytes = bytes;
052: this .string = null;
053: }
054:
055: /**
056: * Returns the content type passed by the browser or null if not defined.
057: * @return The content type passed by the browser or null if not defined.
058: */
059: public String getMimeType() {
060: return mimeType;
061: }
062:
063: /**
064: * Returns an InputStream that can be used to retrieve the contents of the file.
065: * @return An InputStream that can be used to retrieve the contents of the file.
066: */
067: public InputStream getInputStream() {
068: return new ByteArrayInputStream(bytes);
069: }
070:
071: /**
072: * Returns the original filename in the client's file-system, as provided by
073: * the browser (or other client software).
074: * In most cases, this will be the base file name, without path information.
075: * However, some clients, such as the Opera browser, do include path
076: * information.
077: * @return The original filename in the client's file-system.
078: */
079: public String getName() {
080: return name;
081: }
082:
083: /**
084: * Returns the contents of the file item as a String.
085: */
086: public String getString() {
087: if (string == null && bytes != null) {
088: string = new String(bytes);
089: }
090: return string;
091: }
092:
093: /**
094: * Determines whether or not a FormField instance represents a simple form
095: * field.
096: * @return true for an uploaded file; false for a simple form field.
097: */
098: public boolean isFile() {
099: return file;
100: }
101:
102: /* (non-Javadoc)
103: * @see java.lang.Object#toString()
104: */
105: @Override
106: public String toString() {
107: if (file) {
108: return "FormField: "
109: + ("byteCount=" + (bytes == null ? 0 : bytes.length));
110: } else {
111: return "FormField: " + getString();
112: }
113: }
114:
115: /* (non-Javadoc)
116: * @see java.lang.Object#hashCode()
117: */
118: @Override
119: public int hashCode() {
120: int hash = 0;
121: if (file) {
122: if (mimeType != null) {
123: hash += mimeType.hashCode();
124: }
125:
126: if (name != null) {
127: hash += name.hashCode();
128: }
129: }
130:
131: hash += getString().hashCode();
132: return hash;
133: }
134:
135: /* (non-Javadoc)
136: * @see java.lang.Object#equals(java.lang.Object)
137: */
138: @Override
139: public boolean equals(Object obj) {
140: if (obj == this ) {
141: return true;
142: }
143:
144: if (!(obj instanceof FormField)) {
145: return false;
146: }
147:
148: FormField that = (FormField) obj;
149:
150: if (this .file != that.file) {
151: return false;
152: }
153:
154: if (this .file) {
155: if (!equals(this .mimeType, that.mimeType)) {
156: return false;
157: }
158:
159: if (!equals(this .name, that.name)) {
160: return false;
161: }
162: }
163:
164: if (this .bytes.length != that.bytes.length) {
165: return false;
166: }
167:
168: for (int i = 0; i < this .bytes.length; ++i) {
169: if (this .bytes[i] != that.bytes[i]) {
170: return false;
171: }
172: }
173:
174: return true;
175: }
176:
177: private boolean equals(Object o1, Object o2) {
178: if (o1 == null) {
179: return o2 == null;
180: }
181: return o1.equals(o2);
182: }
183:
184: private boolean file;
185: private byte[] bytes;
186: private String name;
187: private String mimeType;
188: private String string;
189: }
|