View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.fileupload.api.job;
23  
24  import org.opencastproject.mediapackage.MediaPackage;
25  import org.opencastproject.mediapackage.MediaPackageElementFlavor;
26  
27  import java.util.UUID;
28  
29  import javax.xml.bind.annotation.XmlAccessType;
30  import javax.xml.bind.annotation.XmlAccessorType;
31  import javax.xml.bind.annotation.XmlAttribute;
32  import javax.xml.bind.annotation.XmlElement;
33  import javax.xml.bind.annotation.XmlEnum;
34  import javax.xml.bind.annotation.XmlEnumValue;
35  import javax.xml.bind.annotation.XmlRootElement;
36  import javax.xml.bind.annotation.XmlType;
37  
38  /**
39   * A Class representing the information about an upload job.
40   *
41   */
42  @XmlType(name = "uploadjob", namespace = "http://fileupload.opencastproject.org")
43  @XmlRootElement(name = "uploadjob", namespace = "http://fileupload.opencastproject.org")
44  @XmlAccessorType(XmlAccessType.NONE)
45  public class FileUploadJob {
46  
47    @XmlEnum
48    public enum JobState { // states an upload job can be in
49      @XmlEnumValue("READY") READY,
50      @XmlEnumValue("INPROGRESS") INPROGRESS,
51      @XmlEnumValue("FINALIZING") FINALIZING,
52      @XmlEnumValue("COMPLETE") COMPLETE
53    }
54  
55    @XmlAttribute()
56    private String id; // this jobs identifier
57    @XmlAttribute()
58    private JobState state = JobState.READY; // this jobs state
59    private long modified;  // time of last modification
60    @XmlElement(name = "payload")
61    private Payload payload; // information about this jobs payload
62    @XmlElement(name = "chunksize")
63    private int chunksize = -1; // size of the chunks that are tranfered
64    @XmlElement(name = "chunks-total")
65    private long chunksTotal = 1; // total number of chunks the upload consists of
66    @XmlElement(name = "current-chunk")
67    private Chunk currentChunk = new Chunk(); // information about the current chunk
68  
69    public FileUploadJob() {
70      this.id = UUID.randomUUID().toString();
71      this.modified = System.currentTimeMillis();
72      this.payload = new Payload("unknown", -1, null, null);
73    }
74  
75    public FileUploadJob(
76        String filename,
77        long filesize,
78        int chunksize,
79        MediaPackage mp,
80        MediaPackageElementFlavor flavor
81    ) {
82      this.id = UUID.randomUUID().toString();
83      this.modified = System.currentTimeMillis();
84      this.chunksize = chunksize;
85      if (chunksize == -1) {           // indicates ordinary HTTP upload
86        chunksTotal = 1;               // ..so we have only one chunk
87      } else {                         // chunked upload
88        chunksTotal = filesize / chunksize;  // compute number of chunks
89        if (filesize % chunksize != 0) {     // if file size is not a multiple of chunk size
90          chunksTotal++;                     // ..add one chunk for the rest
91        }
92      }
93      this.payload = new Payload(filename, filesize, mp, flavor);
94    }
95  
96    public String getId() {
97      return id;
98    }
99  
100   public synchronized JobState getState() {
101     return this.state;
102   }
103 
104   public synchronized void setState(JobState state) {
105     setLastModified(System.currentTimeMillis());
106     this.state = state;
107   }
108 
109   public void setLastModified(long time) {
110     this.modified = time;
111   }
112 
113   public long lastModified() {
114     return this.modified;
115   }
116 
117   public Payload getPayload() {
118     return this.payload;
119   }
120 
121   public int getChunksize() {
122     return chunksize;
123   }
124 
125   public long getChunksTotal() {
126     return chunksTotal;
127   }
128 
129   public Chunk getCurrentChunk() {
130     return currentChunk;
131   }
132 
133   public void setCurrentChunk(Chunk currentChunk) {
134     setLastModified(System.currentTimeMillis());
135     this.currentChunk = currentChunk;
136   }
137 
138   @Override
139   public String toString() {
140     StringBuilder sb = new StringBuilder().append("FileUploadJob(id=").append(this.id).append(", filename=")
141             .append(this.payload.getFilename()).append(")");
142     return sb.toString();
143   }
144 }