You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Upload.java 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * * Copyright 2011 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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 under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import java.io.OutputStream;
  18. import java.io.Serializable;
  19. import java.lang.reflect.Method;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.LinkedHashSet;
  23. import java.util.Map;
  24. import com.vaadin.server.NoInputStreamException;
  25. import com.vaadin.server.NoOutputStreamException;
  26. import com.vaadin.server.PaintException;
  27. import com.vaadin.server.PaintTarget;
  28. import com.vaadin.server.StreamVariable.StreamingProgressEvent;
  29. /**
  30. * Component for uploading files from client to server.
  31. *
  32. * <p>
  33. * The visible component consists of a file name input box and a browse button
  34. * and an upload submit button to start uploading.
  35. *
  36. * <p>
  37. * The Upload component needs a java.io.OutputStream to write the uploaded data.
  38. * You need to implement the Upload.Receiver interface and return the output
  39. * stream in the receiveUpload() method.
  40. *
  41. * <p>
  42. * You can get an event regarding starting (StartedEvent), progress
  43. * (ProgressEvent), and finishing (FinishedEvent) of upload by implementing
  44. * StartedListener, ProgressListener, and FinishedListener, respectively. The
  45. * FinishedListener is called for both failed and succeeded uploads. If you wish
  46. * to separate between these two cases, you can use SucceededListener
  47. * (SucceededEvenet) and FailedListener (FailedEvent).
  48. *
  49. * <p>
  50. * The upload component does not itself show upload progress, but you can use
  51. * the ProgressIndicator for providing progress feedback by implementing
  52. * ProgressListener and updating the indicator in updateProgress().
  53. *
  54. * <p>
  55. * Setting upload component immediate initiates the upload as soon as a file is
  56. * selected, instead of the common pattern of file selection field and upload
  57. * button.
  58. *
  59. * <p>
  60. * Note! Because of browser dependent implementations of <input type="file">
  61. * element, setting size for Upload component is not supported. For some
  62. * browsers setting size may work to some extend.
  63. *
  64. * @author Vaadin Ltd.
  65. * @since 3.0
  66. */
  67. @SuppressWarnings("serial")
  68. public class Upload extends AbstractComponent implements Component.Focusable,
  69. LegacyComponent {
  70. /**
  71. * Should the field be focused on next repaint?
  72. */
  73. private final boolean focus = false;
  74. /**
  75. * The tab order number of this field.
  76. */
  77. private int tabIndex = 0;
  78. /**
  79. * The output of the upload is redirected to this receiver.
  80. */
  81. private Receiver receiver;
  82. private boolean isUploading;
  83. private long contentLength = -1;
  84. private int totalBytes;
  85. private String buttonCaption = "Upload";
  86. /**
  87. * ProgressListeners to which information about progress is sent during
  88. * upload
  89. */
  90. private LinkedHashSet<ProgressListener> progressListeners;
  91. private boolean interrupted = false;
  92. private boolean notStarted;
  93. private int nextid;
  94. /**
  95. * Flag to indicate that submitting file has been requested.
  96. */
  97. private boolean forceSubmit;
  98. /**
  99. * Creates a new instance of Upload.
  100. *
  101. * The receiver must be set before performing an upload.
  102. */
  103. public Upload() {
  104. }
  105. public Upload(String caption, Receiver uploadReceiver) {
  106. setCaption(caption);
  107. receiver = uploadReceiver;
  108. }
  109. /**
  110. * Invoked when the value of a variable has changed.
  111. *
  112. * @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object,
  113. * java.util.Map)
  114. */
  115. @Override
  116. public void changeVariables(Object source, Map<String, Object> variables) {
  117. if (variables.containsKey("pollForStart")) {
  118. int id = (Integer) variables.get("pollForStart");
  119. if (!isUploading && id == nextid) {
  120. notStarted = true;
  121. markAsDirty();
  122. } else {
  123. }
  124. }
  125. }
  126. /**
  127. * Paints the content of this component.
  128. *
  129. * @param target
  130. * Target to paint the content on.
  131. * @throws PaintException
  132. * if the paint operation failed.
  133. */
  134. @Override
  135. public void paintContent(PaintTarget target) throws PaintException {
  136. if (notStarted) {
  137. target.addAttribute("notStarted", true);
  138. notStarted = false;
  139. return;
  140. }
  141. if (forceSubmit) {
  142. target.addAttribute("forceSubmit", true);
  143. forceSubmit = true;
  144. return;
  145. }
  146. // The field should be focused
  147. if (focus) {
  148. target.addAttribute("focus", true);
  149. }
  150. // The tab ordering number
  151. if (tabIndex >= 0) {
  152. target.addAttribute("tabindex", tabIndex);
  153. }
  154. target.addAttribute("state", isUploading);
  155. if (buttonCaption != null) {
  156. target.addAttribute("buttoncaption", buttonCaption);
  157. }
  158. target.addAttribute("nextid", nextid);
  159. // Post file to this strean variable
  160. target.addVariable(this, "action", getStreamVariable());
  161. }
  162. /**
  163. * Interface that must be implemented by the upload receivers to provide the
  164. * Upload component an output stream to write the uploaded data.
  165. *
  166. * @author Vaadin Ltd.
  167. * @since 3.0
  168. */
  169. public interface Receiver extends Serializable {
  170. /**
  171. * Invoked when a new upload arrives.
  172. *
  173. * @param filename
  174. * the desired filename of the upload, usually as specified
  175. * by the client.
  176. * @param mimeType
  177. * the MIME type of the uploaded file.
  178. * @return Stream to which the uploaded file should be written.
  179. */
  180. public OutputStream receiveUpload(String filename, String mimeType);
  181. }
  182. /* Upload events */
  183. private static final Method UPLOAD_FINISHED_METHOD;
  184. private static final Method UPLOAD_FAILED_METHOD;
  185. private static final Method UPLOAD_SUCCEEDED_METHOD;
  186. private static final Method UPLOAD_STARTED_METHOD;
  187. static {
  188. try {
  189. UPLOAD_FINISHED_METHOD = FinishedListener.class.getDeclaredMethod(
  190. "uploadFinished", new Class[] { FinishedEvent.class });
  191. UPLOAD_FAILED_METHOD = FailedListener.class.getDeclaredMethod(
  192. "uploadFailed", new Class[] { FailedEvent.class });
  193. UPLOAD_STARTED_METHOD = StartedListener.class.getDeclaredMethod(
  194. "uploadStarted", new Class[] { StartedEvent.class });
  195. UPLOAD_SUCCEEDED_METHOD = SucceededListener.class
  196. .getDeclaredMethod("uploadSucceeded",
  197. new Class[] { SucceededEvent.class });
  198. } catch (final java.lang.NoSuchMethodException e) {
  199. // This should never happen
  200. throw new java.lang.RuntimeException(
  201. "Internal error finding methods in Upload");
  202. }
  203. }
  204. /**
  205. * Upload.FinishedEvent is sent when the upload receives a file, regardless
  206. * of whether the reception was successful or failed. If you wish to
  207. * distinguish between the two cases, use either SucceededEvent or
  208. * FailedEvent, which are both subclasses of the FinishedEvent.
  209. *
  210. * @author Vaadin Ltd.
  211. * @since 3.0
  212. */
  213. public static class FinishedEvent extends Component.Event {
  214. /**
  215. * Length of the received file.
  216. */
  217. private final long length;
  218. /**
  219. * MIME type of the received file.
  220. */
  221. private final String type;
  222. /**
  223. * Received file name.
  224. */
  225. private final String filename;
  226. /**
  227. *
  228. * @param source
  229. * the source of the file.
  230. * @param filename
  231. * the received file name.
  232. * @param MIMEType
  233. * the MIME type of the received file.
  234. * @param length
  235. * the length of the received file.
  236. */
  237. public FinishedEvent(Upload source, String filename, String MIMEType,
  238. long length) {
  239. super(source);
  240. type = MIMEType;
  241. this.filename = filename;
  242. this.length = length;
  243. }
  244. /**
  245. * Uploads where the event occurred.
  246. *
  247. * @return the Source of the event.
  248. */
  249. public Upload getUpload() {
  250. return (Upload) getSource();
  251. }
  252. /**
  253. * Gets the file name.
  254. *
  255. * @return the filename.
  256. */
  257. public String getFilename() {
  258. return filename;
  259. }
  260. /**
  261. * Gets the MIME Type of the file.
  262. *
  263. * @return the MIME type.
  264. */
  265. public String getMIMEType() {
  266. return type;
  267. }
  268. /**
  269. * Gets the length of the file.
  270. *
  271. * @return the length.
  272. */
  273. public long getLength() {
  274. return length;
  275. }
  276. }
  277. /**
  278. * Upload.FailedEvent event is sent when the upload is received, but the
  279. * reception is interrupted for some reason.
  280. *
  281. * @author Vaadin Ltd.
  282. * @since 3.0
  283. */
  284. public static class FailedEvent extends FinishedEvent {
  285. private Exception reason = null;
  286. /**
  287. *
  288. * @param source
  289. * @param filename
  290. * @param MIMEType
  291. * @param length
  292. * @param exception
  293. */
  294. public FailedEvent(Upload source, String filename, String MIMEType,
  295. long length, Exception reason) {
  296. this(source, filename, MIMEType, length);
  297. this.reason = reason;
  298. }
  299. /**
  300. *
  301. * @param source
  302. * @param filename
  303. * @param MIMEType
  304. * @param length
  305. * @param exception
  306. */
  307. public FailedEvent(Upload source, String filename, String MIMEType,
  308. long length) {
  309. super(source, filename, MIMEType, length);
  310. }
  311. /**
  312. * Gets the exception that caused the failure.
  313. *
  314. * @return the exception that caused the failure, null if n/a
  315. */
  316. public Exception getReason() {
  317. return reason;
  318. }
  319. }
  320. /**
  321. * FailedEvent that indicates that an output stream could not be obtained.
  322. */
  323. public static class NoOutputStreamEvent extends FailedEvent {
  324. /**
  325. *
  326. * @param source
  327. * @param filename
  328. * @param MIMEType
  329. * @param length
  330. */
  331. public NoOutputStreamEvent(Upload source, String filename,
  332. String MIMEType, long length) {
  333. super(source, filename, MIMEType, length);
  334. }
  335. }
  336. /**
  337. * FailedEvent that indicates that an input stream could not be obtained.
  338. */
  339. public static class NoInputStreamEvent extends FailedEvent {
  340. /**
  341. *
  342. * @param source
  343. * @param filename
  344. * @param MIMEType
  345. * @param length
  346. */
  347. public NoInputStreamEvent(Upload source, String filename,
  348. String MIMEType, long length) {
  349. super(source, filename, MIMEType, length);
  350. }
  351. }
  352. /**
  353. * Upload.SucceededEvent event is sent when the upload is received
  354. * successfully.
  355. *
  356. * @author Vaadin Ltd.
  357. * @since 3.0
  358. */
  359. public static class SucceededEvent extends FinishedEvent {
  360. /**
  361. *
  362. * @param source
  363. * @param filename
  364. * @param MIMEType
  365. * @param length
  366. */
  367. public SucceededEvent(Upload source, String filename, String MIMEType,
  368. long length) {
  369. super(source, filename, MIMEType, length);
  370. }
  371. }
  372. /**
  373. * Upload.StartedEvent event is sent when the upload is started to received.
  374. *
  375. * @author Vaadin Ltd.
  376. * @since 5.0
  377. */
  378. public static class StartedEvent extends Component.Event {
  379. private final String filename;
  380. private final String type;
  381. /**
  382. * Length of the received file.
  383. */
  384. private final long length;
  385. /**
  386. *
  387. * @param source
  388. * @param filename
  389. * @param MIMEType
  390. * @param length
  391. */
  392. public StartedEvent(Upload source, String filename, String MIMEType,
  393. long contentLength) {
  394. super(source);
  395. this.filename = filename;
  396. type = MIMEType;
  397. length = contentLength;
  398. }
  399. /**
  400. * Uploads where the event occurred.
  401. *
  402. * @return the Source of the event.
  403. */
  404. public Upload getUpload() {
  405. return (Upload) getSource();
  406. }
  407. /**
  408. * Gets the file name.
  409. *
  410. * @return the filename.
  411. */
  412. public String getFilename() {
  413. return filename;
  414. }
  415. /**
  416. * Gets the MIME Type of the file.
  417. *
  418. * @return the MIME type.
  419. */
  420. public String getMIMEType() {
  421. return type;
  422. }
  423. /**
  424. * @return the length of the file that is being uploaded
  425. */
  426. public long getContentLength() {
  427. return length;
  428. }
  429. }
  430. /**
  431. * Receives the events when the upload starts.
  432. *
  433. * @author Vaadin Ltd.
  434. * @since 5.0
  435. */
  436. public interface StartedListener extends Serializable {
  437. /**
  438. * Upload has started.
  439. *
  440. * @param event
  441. * the Upload started event.
  442. */
  443. public void uploadStarted(StartedEvent event);
  444. }
  445. /**
  446. * Receives the events when the uploads are ready.
  447. *
  448. * @author Vaadin Ltd.
  449. * @since 3.0
  450. */
  451. public interface FinishedListener extends Serializable {
  452. /**
  453. * Upload has finished.
  454. *
  455. * @param event
  456. * the Upload finished event.
  457. */
  458. public void uploadFinished(FinishedEvent event);
  459. }
  460. /**
  461. * Receives events when the uploads are finished, but unsuccessful.
  462. *
  463. * @author Vaadin Ltd.
  464. * @since 3.0
  465. */
  466. public interface FailedListener extends Serializable {
  467. /**
  468. * Upload has finished unsuccessfully.
  469. *
  470. * @param event
  471. * the Upload failed event.
  472. */
  473. public void uploadFailed(FailedEvent event);
  474. }
  475. /**
  476. * Receives events when the uploads are successfully finished.
  477. *
  478. * @author Vaadin Ltd.
  479. * @since 3.0
  480. */
  481. public interface SucceededListener extends Serializable {
  482. /**
  483. * Upload successfull..
  484. *
  485. * @param event
  486. * the Upload successfull event.
  487. */
  488. public void uploadSucceeded(SucceededEvent event);
  489. }
  490. /**
  491. * Adds the upload started event listener.
  492. *
  493. * @param listener
  494. * the Listener to be added.
  495. */
  496. public void addStartedListener(StartedListener listener) {
  497. addListener(StartedEvent.class, listener, UPLOAD_STARTED_METHOD);
  498. }
  499. /**
  500. * @deprecated As of 7.0, replaced by
  501. * {@link #addStartedListener(StartedListener)}
  502. **/
  503. @Deprecated
  504. public void addListener(StartedListener listener) {
  505. addStartedListener(listener);
  506. }
  507. /**
  508. * Removes the upload started event listener.
  509. *
  510. * @param listener
  511. * the Listener to be removed.
  512. */
  513. public void removeStartedListener(StartedListener listener) {
  514. removeListener(StartedEvent.class, listener, UPLOAD_STARTED_METHOD);
  515. }
  516. /**
  517. * @deprecated As of 7.0, replaced by
  518. * {@link #removeStartedListener(StartedListener)}
  519. **/
  520. @Deprecated
  521. public void removeListener(StartedListener listener) {
  522. removeStartedListener(listener);
  523. }
  524. /**
  525. * Adds the upload received event listener.
  526. *
  527. * @param listener
  528. * the Listener to be added.
  529. */
  530. public void addFinishedListener(FinishedListener listener) {
  531. addListener(FinishedEvent.class, listener, UPLOAD_FINISHED_METHOD);
  532. }
  533. /**
  534. * @deprecated As of 7.0, replaced by
  535. * {@link #addFinishedListener(FinishedListener)}
  536. **/
  537. @Deprecated
  538. public void addListener(FinishedListener listener) {
  539. addFinishedListener(listener);
  540. }
  541. /**
  542. * Removes the upload received event listener.
  543. *
  544. * @param listener
  545. * the Listener to be removed.
  546. */
  547. public void removeFinishedListener(FinishedListener listener) {
  548. removeListener(FinishedEvent.class, listener, UPLOAD_FINISHED_METHOD);
  549. }
  550. /**
  551. * @deprecated As of 7.0, replaced by
  552. * {@link #removeFinishedListener(FinishedListener)}
  553. **/
  554. @Deprecated
  555. public void removeListener(FinishedListener listener) {
  556. removeFinishedListener(listener);
  557. }
  558. /**
  559. * Adds the upload interrupted event listener.
  560. *
  561. * @param listener
  562. * the Listener to be added.
  563. */
  564. public void addFailedListener(FailedListener listener) {
  565. addListener(FailedEvent.class, listener, UPLOAD_FAILED_METHOD);
  566. }
  567. /**
  568. * @deprecated As of 7.0, replaced by
  569. * {@link #addFailedListener(FailedListener)}
  570. **/
  571. @Deprecated
  572. public void addListener(FailedListener listener) {
  573. addFailedListener(listener);
  574. }
  575. /**
  576. * Removes the upload interrupted event listener.
  577. *
  578. * @param listener
  579. * the Listener to be removed.
  580. */
  581. public void removeFailedListener(FailedListener listener) {
  582. removeListener(FailedEvent.class, listener, UPLOAD_FAILED_METHOD);
  583. }
  584. /**
  585. * @deprecated As of 7.0, replaced by
  586. * {@link #removeFailedListener(FailedListener)}
  587. **/
  588. @Deprecated
  589. public void removeListener(FailedListener listener) {
  590. removeFailedListener(listener);
  591. }
  592. /**
  593. * Adds the upload success event listener.
  594. *
  595. * @param listener
  596. * the Listener to be added.
  597. */
  598. public void addSucceededListener(SucceededListener listener) {
  599. addListener(SucceededEvent.class, listener, UPLOAD_SUCCEEDED_METHOD);
  600. }
  601. /**
  602. * @deprecated As of 7.0, replaced by
  603. * {@link #addSucceededListener(SucceededListener)}
  604. **/
  605. @Deprecated
  606. public void addListener(SucceededListener listener) {
  607. addSucceededListener(listener);
  608. }
  609. /**
  610. * Removes the upload success event listener.
  611. *
  612. * @param listener
  613. * the Listener to be removed.
  614. */
  615. public void removeSucceededListener(SucceededListener listener) {
  616. removeListener(SucceededEvent.class, listener, UPLOAD_SUCCEEDED_METHOD);
  617. }
  618. /**
  619. * @deprecated As of 7.0, replaced by
  620. * {@link #removeSucceededListener(SucceededListener)}
  621. **/
  622. @Deprecated
  623. public void removeListener(SucceededListener listener) {
  624. removeSucceededListener(listener);
  625. }
  626. /**
  627. * Adds the upload success event listener.
  628. *
  629. * @param listener
  630. * the Listener to be added.
  631. */
  632. public void addProgressListener(ProgressListener listener) {
  633. if (progressListeners == null) {
  634. progressListeners = new LinkedHashSet<ProgressListener>();
  635. }
  636. progressListeners.add(listener);
  637. }
  638. /**
  639. * @deprecated As of 7.0, replaced by
  640. * {@link #addProgressListener(ProgressListener)}
  641. **/
  642. @Deprecated
  643. public void addListener(ProgressListener listener) {
  644. addProgressListener(listener);
  645. }
  646. /**
  647. * Removes the upload success event listener.
  648. *
  649. * @param listener
  650. * the Listener to be removed.
  651. */
  652. public void removeProgressListener(ProgressListener listener) {
  653. if (progressListeners != null) {
  654. progressListeners.remove(listener);
  655. }
  656. }
  657. /**
  658. * @deprecated As of 7.0, replaced by
  659. * {@link #removeProgressListener(ProgressListener)}
  660. **/
  661. @Deprecated
  662. public void removeListener(ProgressListener listener) {
  663. removeProgressListener(listener);
  664. }
  665. /**
  666. * Emit upload received event.
  667. *
  668. * @param filename
  669. * @param MIMEType
  670. * @param length
  671. */
  672. protected void fireStarted(String filename, String MIMEType) {
  673. fireEvent(new Upload.StartedEvent(this, filename, MIMEType,
  674. contentLength));
  675. }
  676. /**
  677. * Emits the upload failed event.
  678. *
  679. * @param filename
  680. * @param MIMEType
  681. * @param length
  682. */
  683. protected void fireUploadInterrupted(String filename, String MIMEType,
  684. long length) {
  685. fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length));
  686. }
  687. protected void fireNoInputStream(String filename, String MIMEType,
  688. long length) {
  689. fireEvent(new Upload.NoInputStreamEvent(this, filename, MIMEType,
  690. length));
  691. }
  692. protected void fireNoOutputStream(String filename, String MIMEType,
  693. long length) {
  694. fireEvent(new Upload.NoOutputStreamEvent(this, filename, MIMEType,
  695. length));
  696. }
  697. protected void fireUploadInterrupted(String filename, String MIMEType,
  698. long length, Exception e) {
  699. fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length, e));
  700. }
  701. /**
  702. * Emits the upload success event.
  703. *
  704. * @param filename
  705. * @param MIMEType
  706. * @param length
  707. *
  708. */
  709. protected void fireUploadSuccess(String filename, String MIMEType,
  710. long length) {
  711. fireEvent(new Upload.SucceededEvent(this, filename, MIMEType, length));
  712. }
  713. /**
  714. * Emits the progress event.
  715. *
  716. * @param totalBytes
  717. * bytes received so far
  718. * @param contentLength
  719. * actual size of the file being uploaded, if known
  720. *
  721. */
  722. protected void fireUpdateProgress(long totalBytes, long contentLength) {
  723. // this is implemented differently than other listeners to maintain
  724. // backwards compatibility
  725. if (progressListeners != null) {
  726. for (Iterator<ProgressListener> it = progressListeners.iterator(); it
  727. .hasNext();) {
  728. ProgressListener l = it.next();
  729. l.updateProgress(totalBytes, contentLength);
  730. }
  731. }
  732. }
  733. /**
  734. * Returns the current receiver.
  735. *
  736. * @return the StreamVariable.
  737. */
  738. public Receiver getReceiver() {
  739. return receiver;
  740. }
  741. /**
  742. * Sets the receiver.
  743. *
  744. * @param receiver
  745. * the receiver to set.
  746. */
  747. public void setReceiver(Receiver receiver) {
  748. this.receiver = receiver;
  749. }
  750. /**
  751. * {@inheritDoc}
  752. */
  753. @Override
  754. public void focus() {
  755. super.focus();
  756. }
  757. /**
  758. * Gets the Tabulator index of this Focusable component.
  759. *
  760. * @see com.vaadin.ui.Component.Focusable#getTabIndex()
  761. */
  762. @Override
  763. public int getTabIndex() {
  764. return tabIndex;
  765. }
  766. /**
  767. * Sets the Tabulator index of this Focusable component.
  768. *
  769. * @see com.vaadin.ui.Component.Focusable#setTabIndex(int)
  770. */
  771. @Override
  772. public void setTabIndex(int tabIndex) {
  773. this.tabIndex = tabIndex;
  774. }
  775. /**
  776. * Go into upload state. This is to prevent double uploading on same
  777. * component.
  778. *
  779. * Warning: this is an internal method used by the framework and should not
  780. * be used by user of the Upload component. Using it results in the Upload
  781. * component going in wrong state and not working. It is currently public
  782. * because it is used by another class.
  783. */
  784. public void startUpload() {
  785. if (isUploading) {
  786. throw new IllegalStateException("uploading already started");
  787. }
  788. isUploading = true;
  789. nextid++;
  790. }
  791. /**
  792. * Interrupts the upload currently being received. The interruption will be
  793. * done by the receiving tread so this method will return immediately and
  794. * the actual interrupt will happen a bit later.
  795. */
  796. public void interruptUpload() {
  797. if (isUploading) {
  798. interrupted = true;
  799. }
  800. }
  801. /**
  802. * Go into state where new uploading can begin.
  803. *
  804. * Warning: this is an internal method used by the framework and should not
  805. * be used by user of the Upload component.
  806. */
  807. private void endUpload() {
  808. isUploading = false;
  809. contentLength = -1;
  810. interrupted = false;
  811. markAsDirty();
  812. }
  813. public boolean isUploading() {
  814. return isUploading;
  815. }
  816. /**
  817. * Gets read bytes of the file currently being uploaded.
  818. *
  819. * @return bytes
  820. */
  821. public long getBytesRead() {
  822. return totalBytes;
  823. }
  824. /**
  825. * Returns size of file currently being uploaded. Value sane only during
  826. * upload.
  827. *
  828. * @return size in bytes
  829. */
  830. public long getUploadSize() {
  831. return contentLength;
  832. }
  833. /**
  834. * ProgressListener receives events to track progress of upload.
  835. */
  836. public interface ProgressListener extends Serializable {
  837. /**
  838. * Updates progress to listener
  839. *
  840. * @param readBytes
  841. * bytes transferred
  842. * @param contentLength
  843. * total size of file currently being uploaded, -1 if unknown
  844. */
  845. public void updateProgress(long readBytes, long contentLength);
  846. }
  847. /**
  848. * @return String to be rendered into button that fires uploading
  849. */
  850. public String getButtonCaption() {
  851. return buttonCaption;
  852. }
  853. /**
  854. * In addition to the actual file chooser, upload components have button
  855. * that starts actual upload progress. This method is used to set text in
  856. * that button.
  857. * <p>
  858. * In case the button text is set to null, the button is hidden. In this
  859. * case developer must explicitly initiate the upload process with
  860. * {@link #submitUpload()}.
  861. * <p>
  862. * In case the Upload is used in immediate mode using
  863. * {@link #setImmediate(boolean)}, the file choose (html input with type
  864. * "file") is hidden and only the button with this text is shown.
  865. * <p>
  866. *
  867. * <p>
  868. * <strong>Note</strong> the string given is set as is to the button. HTML
  869. * formatting is not stripped. Be sure to properly validate your value
  870. * according to your needs.
  871. *
  872. * @param buttonCaption
  873. * text for upload components button.
  874. */
  875. public void setButtonCaption(String buttonCaption) {
  876. this.buttonCaption = buttonCaption;
  877. markAsDirty();
  878. }
  879. /**
  880. * Forces the upload the send selected file to the server.
  881. * <p>
  882. * In case developer wants to use this feature, he/she will most probably
  883. * want to hide the uploads internal submit button by setting its caption to
  884. * null with {@link #setButtonCaption(String)} method.
  885. * <p>
  886. * Note, that the upload runs asynchronous. Developer should use normal
  887. * upload listeners to trac the process of upload. If the field is empty
  888. * uploaded the file name will be empty string and file length 0 in the
  889. * upload finished event.
  890. * <p>
  891. * Also note, that the developer should not remove or modify the upload in
  892. * the same user transaction where the upload submit is requested. The
  893. * upload may safely be hidden or removed once the upload started event is
  894. * fired.
  895. */
  896. public void submitUpload() {
  897. markAsDirty();
  898. forceSubmit = true;
  899. }
  900. @Override
  901. public void markAsDirty() {
  902. forceSubmit = false;
  903. super.markAsDirty();
  904. }
  905. /*
  906. * Handle to terminal via Upload monitors and controls the upload during it
  907. * is being streamed.
  908. */
  909. private com.vaadin.server.StreamVariable streamVariable;
  910. protected com.vaadin.server.StreamVariable getStreamVariable() {
  911. if (streamVariable == null) {
  912. streamVariable = new com.vaadin.server.StreamVariable() {
  913. private StreamingStartEvent lastStartedEvent;
  914. @Override
  915. public boolean listenProgress() {
  916. return (progressListeners != null && !progressListeners
  917. .isEmpty());
  918. }
  919. @Override
  920. public void onProgress(StreamingProgressEvent event) {
  921. fireUpdateProgress(event.getBytesReceived(),
  922. event.getContentLength());
  923. }
  924. @Override
  925. public boolean isInterrupted() {
  926. return interrupted;
  927. }
  928. @Override
  929. public OutputStream getOutputStream() {
  930. OutputStream receiveUpload = receiver.receiveUpload(
  931. lastStartedEvent.getFileName(),
  932. lastStartedEvent.getMimeType());
  933. lastStartedEvent = null;
  934. return receiveUpload;
  935. }
  936. @Override
  937. public void streamingStarted(StreamingStartEvent event) {
  938. startUpload();
  939. contentLength = event.getContentLength();
  940. fireStarted(event.getFileName(), event.getMimeType());
  941. lastStartedEvent = event;
  942. }
  943. @Override
  944. public void streamingFinished(StreamingEndEvent event) {
  945. fireUploadSuccess(event.getFileName(), event.getMimeType(),
  946. event.getContentLength());
  947. endUpload();
  948. markAsDirty();
  949. }
  950. @Override
  951. public void streamingFailed(StreamingErrorEvent event) {
  952. Exception exception = event.getException();
  953. if (exception instanceof NoInputStreamException) {
  954. fireNoInputStream(event.getFileName(),
  955. event.getMimeType(), 0);
  956. } else if (exception instanceof NoOutputStreamException) {
  957. fireNoOutputStream(event.getFileName(),
  958. event.getMimeType(), 0);
  959. } else {
  960. fireUploadInterrupted(event.getFileName(),
  961. event.getMimeType(), 0, exception);
  962. }
  963. endUpload();
  964. }
  965. };
  966. }
  967. return streamVariable;
  968. }
  969. @Override
  970. public java.util.Collection<?> getListeners(java.lang.Class<?> eventType) {
  971. if (StreamingProgressEvent.class.isAssignableFrom(eventType)) {
  972. if (progressListeners == null) {
  973. return Collections.EMPTY_LIST;
  974. } else {
  975. return Collections.unmodifiableCollection(progressListeners);
  976. }
  977. }
  978. return super.getListeners(eventType);
  979. };
  980. }