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

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