Subversion Repositories XServices

Rev

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