KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > upload > FlashUpload


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  * Carsten Woelk [cwoelk at neteye dot de]
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.riotfamily.forms.element.upload;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.riotfamily.common.util.FlashInfo;
35 import org.riotfamily.forms.ContentElement;
36 import org.riotfamily.forms.EditorBinder;
37 import org.riotfamily.forms.Element;
38 import org.riotfamily.forms.ErrorUtils;
39 import org.riotfamily.forms.element.TemplateElement;
40 import org.springframework.util.FileCopyUtils;
41
42 /**
43  * Specialized FileUpload element for flash uploads.
44  * @author Carsten Woelk [cwoelk at neteye dot de]
45  * @since 6.5
46  */

47 public class FlashUpload extends FileUpload {
48
49     public static final String JavaDoc FLASH_CONTENT_TYPE = "application/x-shockwave-flash";
50
51     private String JavaDoc widthProperty;
52
53     private String JavaDoc heightProperty;
54
55     private String JavaDoc versionProperty;
56
57     private boolean isValidSwf = false;
58
59     private int width;
60
61     private int height;
62
63     private int version;
64
65
66     public FlashUpload() {
67         super();
68     }
69
70     protected void destroy() {
71         super.destroy();
72     }
73
74
75     public boolean isPreviewAvailable() {
76         return true;
77     }
78
79     protected Element createPreviewElement() {
80         return new PreviewElement();
81     }
82
83
84     public String JavaDoc getContentType() {
85         return FLASH_CONTENT_TYPE;
86     }
87
88
89     public String JavaDoc getWidthProperty() {
90         return this.widthProperty;
91     }
92
93     public void setWidthProperty(String JavaDoc widthProperty) {
94         this.widthProperty = widthProperty;
95     }
96
97     public String JavaDoc getHeightProperty() {
98         return this.heightProperty;
99     }
100
101     public void setHeightProperty(String JavaDoc heightProperty) {
102         this.heightProperty = heightProperty;
103     }
104
105     public String JavaDoc getVersionProperty() {
106         return versionProperty;
107     }
108
109     public void setVersionProperty(String JavaDoc versionProperty) {
110         this.versionProperty = versionProperty;
111     }
112
113
114     public boolean isValidSwf() {
115         return this.isValidSwf;
116     }
117
118     public int getWidth() {
119         return this.width;
120     }
121
122     public int getHeight() {
123         return this.height;
124     }
125
126     public int getVersion() {
127         return this.version;
128     }
129
130
131     protected void validateFile(File JavaDoc file) {
132         try {
133             parseFlashMovie(file);
134             if (!isValidSwf) {
135                 ErrorUtils.reject(this, "flash.invalidFormat");
136             }
137         }
138         catch (IOException JavaDoc e) {
139             ErrorUtils.reject(this, "unexpected");
140         }
141     }
142
143     public void setValue(Object JavaDoc value) {
144         super.setValue(value);
145         if (value == null) {
146             return;
147         }
148
149         try {
150             parseFlashMovie(getFile());
151             EditorBinder editorBinder = getEditorBinding().getEditorBinder();
152             if (widthProperty != null) {
153                 width = getIntegerProperty(editorBinder, widthProperty);
154             }
155             if (heightProperty != null) {
156                 height = getIntegerProperty(editorBinder, heightProperty);
157             }
158             if (versionProperty != null) {
159                 version = getIntegerProperty(editorBinder, versionProperty);;
160             }
161         }
162         catch (IOException JavaDoc e) {
163             isValidSwf = false;
164         }
165     }
166
167     private static int getIntegerProperty(EditorBinder editorBinder, String JavaDoc propertyName) {
168         int result = 0;
169         Object JavaDoc value = editorBinder.getPropertyValue(propertyName);
170         if (value != null && value instanceof Integer JavaDoc) {
171             result = ((Integer JavaDoc) value).intValue();
172         }
173         return result;
174     }
175
176     public Object JavaDoc getValue() {
177         EditorBinder editorBinder = getEditorBinding().getEditorBinder();
178         setIntegerProperty(editorBinder, widthProperty, width);
179         setIntegerProperty(editorBinder, heightProperty, height);
180         setIntegerProperty(editorBinder, versionProperty, version);
181         return super.getValue();
182     }
183
184     private static void setIntegerProperty(EditorBinder editorBinder, String JavaDoc propertyName, int value) {
185         if (value != 0) {
186             if (propertyName != null) {
187                 editorBinder.setPropertyValue(propertyName, new Integer JavaDoc(value));
188             }
189         }
190     }
191
192     protected void parseFlashMovie(File JavaDoc file) throws IOException JavaDoc {
193         FlashInfo flashInfo = new FlashInfo(file);
194         isValidSwf = flashInfo.isValid();
195         width = flashInfo.getWidth();
196         height = flashInfo.getHeight();
197         version = flashInfo.getVersion();
198     }
199
200     public class PreviewElement extends TemplateElement
201             implements ContentElement {
202
203         public PreviewElement() {
204             setAttribute("flash", FlashUpload.this);
205         }
206
207         public void handleContentRequest(HttpServletRequest JavaDoc request,
208                 HttpServletResponse JavaDoc response) throws IOException JavaDoc {
209
210             File JavaDoc file = getFile();
211             if (file != null && file.exists()) {
212                 response.setDateHeader("Expires", 0);
213                 response.setHeader("Content-Type", getContentType());
214
215                 response.setContentLength(getSize().intValue());
216                 FileCopyUtils.copy(new FileInputStream JavaDoc(file),
217                         response.getOutputStream());
218             }
219             else {
220                 response.sendError(HttpServletResponse.SC_NO_CONTENT);
221             }
222         }
223
224         public String JavaDoc getDownloadUrl() {
225             return getFormContext().getContentUrl(this);
226         }
227
228     }
229
230 }
231
Popular Tags