|
| 1 | +/* |
| 2 | + Copyright 2013 KU Leuven Research and Development - iMinds - Distrinet |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of 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, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +
|
| 16 | + Administrative Contact: [email protected] |
| 17 | + Technical Contact: [email protected] |
| 18 | +*/ |
| 19 | + |
| 20 | +package drm.taskworker.workers; |
| 21 | + |
| 22 | +import static drm.taskworker.config.Config.cfg; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.List; |
| 26 | +import java.util.logging.Level; |
| 27 | + |
| 28 | +import com.github.sardine.DavResource; |
| 29 | +import com.github.sardine.Sardine; |
| 30 | +import com.github.sardine.SardineFactory; |
| 31 | + |
| 32 | +import drm.taskworker.Worker; |
| 33 | +import drm.taskworker.tasks.ParameterFoundException; |
| 34 | +import drm.taskworker.tasks.Task; |
| 35 | +import drm.taskworker.tasks.TaskResult; |
| 36 | + |
| 37 | +/** |
| 38 | + * Collect all files in a workflow and zip them when an end of workflow task |
| 39 | + * is received. |
| 40 | + * |
| 41 | + * @author Bart Vanbrabant <[email protected]> |
| 42 | + */ |
| 43 | +public class WebdavArchiveWorker extends Worker { |
| 44 | + /** |
| 45 | + * Creates a new work with the name blob-to-cache |
| 46 | + */ |
| 47 | + public WebdavArchiveWorker(String workerName) { |
| 48 | + super(workerName); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Archive the result of the previous task |
| 53 | + */ |
| 54 | + public TaskResult work(Task task) { |
| 55 | + logger.info("Archiving file"); |
| 56 | + TaskResult result = new TaskResult(); |
| 57 | + |
| 58 | + byte[] fileData = null; |
| 59 | + |
| 60 | + try { |
| 61 | + fileData = (byte[])task.getParam("arg0"); |
| 62 | + } catch (ParameterFoundException e) { |
| 63 | + return result.setResult(TaskResult.Result.ARGUMENT_ERROR); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + // WebDAV URL: |
| 68 | + final String baseUrl = cfg().getProperty("taskworker.archive.url"); |
| 69 | + if (baseUrl == null) { |
| 70 | + logger.log(Level.SEVERE, "No base url configured to upload result to."); |
| 71 | + return result.setResult(TaskResult.Result.ARGUMENT_ERROR); |
| 72 | + } |
| 73 | + |
| 74 | + Sardine sardine = SardineFactory.begin(cfg().getProperty("taskworker.archive.username", "admin"), |
| 75 | + cfg().getProperty("taskworker.archive.password", "admin")); |
| 76 | + List<DavResource> resources = sardine.list(baseUrl); |
| 77 | + |
| 78 | + sardine.put(baseUrl + task.getJobId().toString(), fileData, "application/zip"); |
| 79 | + |
| 80 | + Task newTask = new Task(task, this.getNextWorker(task.getJobId())); |
| 81 | + result.addNextTask(newTask); |
| 82 | + |
| 83 | + } catch (IOException e) { |
| 84 | + result.setResult(TaskResult.Result.EXCEPTION); |
| 85 | + result.setException(e); |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + return result.setResult(TaskResult.Result.SUCCESS); |
| 90 | + } |
| 91 | +} |
0 commit comments