Subversion Repositories XServices

Rev

Rev 10 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 brianR 1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
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
 
17
package net.brutex.xservices.ws;
18
 
19
import java.io.File;
20
import java.util.Map;
21
import javax.jws.WebMethod;
22
import javax.jws.WebParam;
23
import javax.jws.WebService;
24
import net.brutex.xservices.types.ArchiveResource;
25
import net.brutex.xservices.types.CompressionType;
26
import net.brutex.xservices.types.FileResource;
27
import net.brutex.xservices.types.ResourceInterface;
10 brianR 28
import net.brutex.xservices.types.ReturnCode;
6 brianR 29
import net.brutex.xservices.util.RunTask;
30
import net.brutex.xservices.util.UnRarTask;
31
import org.apache.tools.ant.taskdefs.BUnzip2;
32
import org.apache.tools.ant.taskdefs.BZip2;
33
import org.apache.tools.ant.taskdefs.Expand;
34
import org.apache.tools.ant.taskdefs.GUnzip;
35
import org.apache.tools.ant.taskdefs.GZip;
36
import org.apache.tools.ant.taskdefs.Untar;
37
import org.apache.tools.ant.taskdefs.Zip;
38
 
39
/**
40
 *
41
 * @author Brian Rosenberger, bru@brutex.de
42
 */
43
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="ArchiveService")
44
public class ArchiveService {
45
 
46
    public static final String WS_OPERATION_BZIP2 = "bzip2";
47
    public static final String WS_OPERATION_BZIP2_ARCHIVE = "bzip2FromArchive";
48
    public static final String WS_OPERATION_GZIP = "gzip";
49
    public static final String WS_OPERATION_GZIP_ARCHIVE = "gzipFromArchive";
50
    public static final String WS_OPERATION_UNZIP = "unzip";
51
    public static final String WS_OPERATION_GUNZIP = "gunzip";
52
    public static final String WS_OPERATION_BUNZIP2 = "bunzip2";
53
 
54
    public static final String WS_PARAM_SOURCEFILE = "source";
55
    public static final String WS_PARAM_SOURCEFILE_STRING = "srcfile";
56
    public static final String WS_PARAM_SOURCEURL = "srcurl";
57
    public static final String WS_PARAM_SOURCEARCHIVE = "archivesource";
58
    public static final String WS_PARAM_DESTFILE = "destfile";
59
    public static final String WS_PARAM_DESTDIR = "destdir";
60
 
61
    public static final String WS_PARAM_ENCODING = "encoding";
62
    public static final String WS_PARAM_OVERWRITE= "overwrite";
63
 
64
    @WebMethod(operationName = WS_OPERATION_BZIP2, action=WS_OPERATION_BZIP2)
10 brianR 65
    public ReturnCode bzip2(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
6 brianR 66
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
67
        return bzip(src, new File(file));
68
    }
69
 
70
    @WebMethod(operationName = WS_OPERATION_BZIP2_ARCHIVE, action=WS_OPERATION_BZIP2_ARCHIVE)
10 brianR 71
    public ReturnCode bzip2FromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
6 brianR 72
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
73
        return bzip(src, new File(file));
74
    }
75
 
76
    @WebMethod(operationName = WS_OPERATION_GZIP, action=WS_OPERATION_GZIP)
77
    public String gzip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
78
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
79
        return gzip(src, new File(file));
80
    }
81
 
82
    @WebMethod(operationName = WS_OPERATION_GZIP_ARCHIVE, action=WS_OPERATION_GZIP_ARCHIVE)
83
    public String gzipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
84
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
85
        return gzip(src, new File(file));
86
    }
87
 
88
    @WebMethod(operationName = WS_OPERATION_GUNZIP, action=WS_OPERATION_GUNZIP)
89
    public String gunzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
90
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
91
        File target = null;
92
        if (!dest.equals("") && dest != null) {
93
            target = new File(dest);
94
        }
95
        return GUnzip(new FileResource(FileResource.Type.FILE, src), target);
96
    }
97
 
98
    @WebMethod(operationName = WS_OPERATION_BUNZIP2)
99
    public String bunzip2(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
100
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
101
        File target = null;
102
        if (!dest.equals("") && dest != null) {
103
            target = new File(dest);
104
        }
105
        return BUnzip2(new FileResource(FileResource.Type.FILE, src), target);
106
    }
107
 
108
    @WebMethod(operationName = "gunzipFromURL")
109
    public String gunzipFromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
110
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
111
        File target = null;
112
        if (!dest.equals("") && dest != null) {
113
            target = new File(dest);
114
        }
115
        return GUnzip(new FileResource(FileResource.Type.URL, src), target);
116
    }
117
 
118
    @WebMethod(operationName = "bunzip2FromURL")
119
    public String bunzip2FromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
120
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
121
        File target = null;
122
        if (!dest.equals("") && dest != null) {
123
            target = new File(dest);
124
        }
125
        return BUnzip2(new FileResource(FileResource.Type.URL, src), target);
126
    }
127
 
128
    @WebMethod(operationName = "zip")
129
    public String zip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
130
            @WebParam(name = WS_PARAM_DESTFILE) String file,
131
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
132
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
133
            @WebParam(name = "compresslevel") int level) {
134
        if (level > 9) {
135
            level = 9;
136
        }
137
        if (level < 0) {
138
            level = 0;
139
        }
140
        return zip(src, new File(file), encoding, !overwrite, level);
141
    }
142
 
143
    @WebMethod(operationName = "zipFromArchive")
144
    public String zipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
145
            @WebParam(name = WS_PARAM_DESTFILE) String file,
146
            @WebParam(name = WS_PARAM_OVERWRITE) boolean update,
147
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
148
            @WebParam(name = "compresslevel") int level) {
149
        return zip(src, new File(file), encoding, !update, level);
150
    }
151
 
152
 
153
    @WebMethod(operationName = "unzip")
154
    public String unzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
155
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
156
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
157
            @WebParam(name = WS_PARAM_ENCODING) String encoding) {
158
        return unzip(new File(src), new File(dest), overwrite, encoding);
159
    }
160
 
161
    @WebMethod(operationName = "unrar")
162
    public String unrar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
163
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
164
        return unrar(new File(src), new File(dest));
165
    }
166
 
167
    @WebMethod(operationName = "untar")
168
    public String untar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
169
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
170
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
171
            @WebParam(name = "compression") CompressionType compression) {
172
        Untar.UntarCompressionMethod c = new Untar.UntarCompressionMethod();
173
        switch (compression) {
174
            case GZIP:
175
                c.setValue("gzip");
176
                break;
177
            case BZIP2:
178
                c.setValue("bzip2");
179
                break;
180
            default:
181
                c.setValue("none");
182
                break;
183
        }
184
        return untar(new File(src), new File(dest), overwrite, c);
185
    }
186
 
187
    @WebMethod(exclude = true)
10 brianR 188
    private ReturnCode bzip(ResourceInterface src, File dst) {
6 brianR 189
        if (dst.exists() && dst.isFile()) {
190
            dst.delete();
191
        }
192
        BZip2 bzip = new BZip2();
193
        bzip.setTaskName("BZip2");
194
        RunTask runner = new RunTask(bzip);
195
        bzip.setSrcResource(src.getAntResource(bzip.getProject()));
196
        bzip.setDestfile(dst);
197
 
198
        Map<String, String> result = runner.postTask();
11 brianR 199
        return new ReturnCode(0,result.get("System.stdOut"),result.get("System.stdErr"));
6 brianR 200
    }
201
 
202
    @WebMethod(exclude = true)
203
    private String gzip(ResourceInterface src, File dst) {
204
        if (dst.exists() && dst.isFile()) {
205
            dst.delete();
206
        }
207
        GZip gzip = new GZip();
208
        gzip.setTaskName("GZip");
209
        RunTask runner = new RunTask(gzip);
210
        gzip.addConfigured(src.getAntResource(gzip.getProject()));
211
        gzip.setDestfile(dst);
212
        Map<String, String> result = runner.postTask();
213
        return "complete";
214
    }
215
 
216
 
217
 
218
    @WebMethod(exclude = true)
219
    private String zip(ResourceInterface src, File dst, String encoding, boolean update, int compresslevel) {
220
        Zip zip = new Zip();
221
        zip.setTaskName("Zip");
222
        RunTask runner = new RunTask(zip);
223
        zip.add(src.getAntResource(zip.getProject()));
224
        zip.setDestFile(dst);
225
        if (encoding != null && !encoding.equals("")) {
226
            zip.setEncoding(encoding);
227
        }
228
        zip.setUpdate(update);
229
        zip.setLevel(compresslevel);
230
        Map<String, String> result = runner.postTask();
231
        return "complete";
232
    }
233
 
234
    @WebMethod(exclude = true)
235
    private String GUnzip(ResourceInterface src, File dst) {
236
        GUnzip uz = new GUnzip();
237
        uz.setTaskName("GUnzip");
238
        RunTask runner = new RunTask(uz);
239
        uz.setSrcResource(src.getAntResource(uz.getProject()));
240
        if (dst != null) {
241
            uz.setDest(dst);
242
        }
243
        Map<String, String> result = runner.postTask();
244
        return "complete";
245
    }
246
 
247
    @WebMethod(exclude = true)
248
    private String BUnzip2(ResourceInterface src, File dst) {
249
        BUnzip2 uz = new BUnzip2();
250
        uz.setTaskName("BUnzip2");
251
        RunTask runner = new RunTask(uz);
252
        uz.setSrcResource(src.getAntResource(uz.getProject()));
253
        if (dst != null) {
254
            uz.setDest(dst);
255
        }
256
        Map<String, String> result = runner.postTask();
257
        return "complete";
258
    }
259
 
260
    @WebMethod(exclude = true)
261
    private String unzip(File src, File dest, boolean overwrite, String encoding) {
262
        Expand unzip = new Expand();
263
        unzip.setTaskName("UnZip");
264
        RunTask runner = new RunTask(unzip);
265
        unzip.setSrc(src);
266
        unzip.setDest(dest);
267
        unzip.setOverwrite(overwrite);
268
        if (encoding != null && !encoding.equals("")) {
269
            unzip.setEncoding(encoding);
270
        }
271
 
272
        Map<String, String> result = runner.postTask();
273
        return "complete";
274
    }
275
 
276
    @WebMethod(exclude = true)
277
    private String untar(File src, File dest, boolean overwrite, Untar.UntarCompressionMethod compression) {
278
        Untar unzip = new Untar();
279
        unzip.setTaskName("Untar");
280
        RunTask runner = new RunTask(unzip);
281
        unzip.setSrc(src);
282
        unzip.setDest(dest);
283
        unzip.setOverwrite(overwrite);
284
        unzip.setCompression(compression);
285
        Map<String, String> result = runner.postTask();
286
        return "complete";
287
    }
288
 
289
    @WebMethod(exclude = true)
290
    private String unrar(File src, File dst) {
291
        UnRarTask unrar = new UnRarTask();
292
        unrar.setTaskName("UnRar");
293
        RunTask runner = new RunTask(unrar);
294
        unrar.setSrc(src);
295
        unrar.setDst(dst);
296
        Map<String, String> result = runner.postTask();
297
        return "complete";
298
    }
299
 
300
 
301
}