Subversion Repositories XServices

Rev

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