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 35KB

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