Subversion Repositories XServices

Rev

Rev 12 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12 Rev 46
1
/*
1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
3
 *
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with 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
6
 *   You may obtain a copy of the License at
7
 *
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
14
 *   limitations under the License.
15
 */
15
 */
16
package net.brutex.xservices.ws;
16
package net.brutex.xservices.ws;
17
 
-
 
18
import java.io.File;
-
 
19
import java.util.Map;
17
 
20
import javax.jws.WebMethod;
18
import javax.jws.WebMethod;
21
import javax.jws.WebParam;
19
import javax.jws.WebParam;
22
import javax.jws.WebService;
20
import javax.jws.WebService;
23
import net.brutex.xservices.types.ArchiveResource;
21
import net.brutex.xservices.types.ArchiveResource;
24
import net.brutex.xservices.types.CompressionType;
22
import net.brutex.xservices.types.CompressionType;
25
import net.brutex.xservices.types.FileResource;
23
import net.brutex.xservices.types.FileResource;
26
import net.brutex.xservices.types.ResourceInterface;
-
 
-
 
24
 
27
import net.brutex.xservices.types.ReturnCode;
25
import net.brutex.xservices.types.ReturnCode;
28
import net.brutex.xservices.util.RunTask;
26
import net.brutex.xservices.util.BrutexNamespaces;
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;
-
 
-
 
27
 
37
 
28
 
38
/**
29
/**
39
 *
30
 *
40
 * @author Brian Rosenberger, bru@brutex.de
31
 * @author Brian Rosenberger, bru@brutex.de
41
 */
32
 */
42
@WebService(targetNamespace = "http://ws.xservices.brutex.net", name = "ArchiveService")
33
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES)
43
public class ArchiveService {
34
public interface ArchiveService {
44
 
35
 
45
    public static final String WS_OPERATION_BZIP2 = "bzip2";
36
    public static final String WS_OPERATION_BZIP2 = "bzip2";
46
    public static final String WS_OPERATION_BZIP2_ARCHIVE = "bzip2FromArchive";
37
    public static final String WS_OPERATION_BZIP2_ARCHIVE = "bzip2FromArchive";
47
    public static final String WS_OPERATION_GZIP = "gzip";
38
    public static final String WS_OPERATION_GZIP = "gzip";
48
    public static final String WS_OPERATION_GZIP_ARCHIVE = "gzipFromArchive";
39
    public static final String WS_OPERATION_GZIP_ARCHIVE = "gzipFromArchive";
49
    public static final String WS_OPERATION_UNZIP = "unzip";
40
    public static final String WS_OPERATION_UNZIP = "unzip";
50
    public static final String WS_OPERATION_GUNZIP = "gunzip";
41
    public static final String WS_OPERATION_GUNZIP = "gunzip";
51
    public static final String WS_OPERATION_BUNZIP2 = "bunzip2";
42
    public static final String WS_OPERATION_BUNZIP2 = "bunzip2";
52
    public static final String WS_PARAM_SOURCEFILE = "source";
43
    public static final String WS_PARAM_SOURCEFILE = "source";
53
    public static final String WS_PARAM_SOURCEFILE_STRING = "srcfile";
44
    public static final String WS_PARAM_SOURCEFILE_STRING = "srcfile";
54
    public static final String WS_PARAM_SOURCEURL = "srcurl";
45
    public static final String WS_PARAM_SOURCEURL = "srcurl";
55
    public static final String WS_PARAM_SOURCEARCHIVE = "archivesource";
46
    public static final String WS_PARAM_SOURCEARCHIVE = "archivesource";
56
    public static final String WS_PARAM_DESTFILE = "destfile";
47
    public static final String WS_PARAM_DESTFILE = "destfile";
57
    public static final String WS_PARAM_DESTDIR = "destdir";
48
    public static final String WS_PARAM_DESTDIR = "destdir";
58
    public static final String WS_PARAM_ENCODING = "encoding";
49
    public static final String WS_PARAM_ENCODING = "encoding";
59
    public static final String WS_PARAM_OVERWRITE = "overwrite";
50
    public static final String WS_PARAM_OVERWRITE = "overwrite";
60
 
51
 
61
    @WebMethod(operationName = WS_OPERATION_BZIP2, action = WS_OPERATION_BZIP2)
52
    @WebMethod(operationName = WS_OPERATION_BZIP2, action = WS_OPERATION_BZIP2)
62
    public ReturnCode bzip2(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
53
    public ReturnCode bzip2(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
63
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
54
            @WebParam(name = WS_PARAM_DESTFILE) String file);
64
        return bzip(src, new File(file));
-
 
65
    }
-
 
66
 
55
 
67
    @WebMethod(operationName = WS_OPERATION_BZIP2_ARCHIVE, action = WS_OPERATION_BZIP2_ARCHIVE)
56
    @WebMethod(operationName = WS_OPERATION_BZIP2_ARCHIVE, action = WS_OPERATION_BZIP2_ARCHIVE)
68
    public ReturnCode bzip2FromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
57
    public ReturnCode bzip2FromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
69
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
58
            @WebParam(name = WS_PARAM_DESTFILE) String file);
70
        return bzip(src, new File(file));
-
 
71
    }
-
 
72
 
59
 
73
    @WebMethod(operationName = WS_OPERATION_GZIP, action = WS_OPERATION_GZIP)
60
    @WebMethod(operationName = WS_OPERATION_GZIP, action = WS_OPERATION_GZIP)
74
    public ReturnCode gzip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
61
    public ReturnCode gzip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
75
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
62
            @WebParam(name = WS_PARAM_DESTFILE) String file);
76
        return gzip(src, new File(file));
-
 
77
    }
-
 
78
 
63
 
79
    @WebMethod(operationName = WS_OPERATION_GZIP_ARCHIVE, action = WS_OPERATION_GZIP_ARCHIVE)
64
    @WebMethod(operationName = WS_OPERATION_GZIP_ARCHIVE, action = WS_OPERATION_GZIP_ARCHIVE)
80
    public ReturnCode gzipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
65
    public ReturnCode gzipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
81
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
66
            @WebParam(name = WS_PARAM_DESTFILE) String file);
82
        return gzip(src, new File(file));
-
 
83
    }
-
 
84
 
67
 
85
    @WebMethod(operationName = WS_OPERATION_GUNZIP, action = WS_OPERATION_GUNZIP)
68
    @WebMethod(operationName = WS_OPERATION_GUNZIP, action = WS_OPERATION_GUNZIP)
86
    public ReturnCode gunzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
69
    public ReturnCode gunzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
87
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
70
            @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
 
71
 
95
    @WebMethod(operationName = WS_OPERATION_BUNZIP2)
72
    @WebMethod(operationName = WS_OPERATION_BUNZIP2)
96
    public ReturnCode bunzip2(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
73
    public ReturnCode bunzip2(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
97
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
74
            @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
 
75
 
105
    @WebMethod(operationName = "gunzipFromURL")
76
    @WebMethod(operationName = "gunzipFromURL")
106
    public ReturnCode gunzipFromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
77
    public ReturnCode gunzipFromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
107
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
78
            @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
 
79
 
115
    @WebMethod(operationName = "bunzip2FromURL")
80
    @WebMethod(operationName = "bunzip2FromURL")
116
    public ReturnCode bunzip2FromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
81
    public ReturnCode bunzip2FromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
117
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
82
            @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
 
83
 
125
    @WebMethod(operationName = "zip")
84
    @WebMethod(operationName = "zip")
126
    public ReturnCode zip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
85
    public ReturnCode zip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
127
            @WebParam(name = WS_PARAM_DESTFILE) String file,
86
            @WebParam(name = WS_PARAM_DESTFILE) String file,
128
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
87
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
129
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
88
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
130
            @WebParam(name = "compresslevel") int level) {
89
            @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
 
90
 
140
    @WebMethod(operationName = "zipFromArchive")
91
    @WebMethod(operationName = "zipFromArchive")
141
    public ReturnCode zipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
92
    public ReturnCode zipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
142
            @WebParam(name = WS_PARAM_DESTFILE) String file,
93
            @WebParam(name = WS_PARAM_DESTFILE) String file,
143
            @WebParam(name = WS_PARAM_OVERWRITE) boolean update,
94
            @WebParam(name = WS_PARAM_OVERWRITE) boolean update,
144
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
95
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
145
            @WebParam(name = "compresslevel") int level) {
96
            @WebParam(name = "compresslevel") int level);
146
        return zip(src, new File(file), encoding, !update, level);
-
 
147
    }
-
 
148
 
97
 
149
    @WebMethod(operationName = "unzip")
98
    @WebMethod(operationName = "unzip")
150
    public ReturnCode unzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
99
    public ReturnCode unzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
151
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
100
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
152
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
101
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
153
            @WebParam(name = WS_PARAM_ENCODING) String encoding) {
102
            @WebParam(name = WS_PARAM_ENCODING) String encoding);
154
        return unzip(new File(src), new File(dest), overwrite, encoding);
-
 
155
    }
-
 
156
 
103
 
157
    @WebMethod(operationName = "unrar")
104
    @WebMethod(operationName = "unrar")
158
    public ReturnCode unrar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
105
    public ReturnCode unrar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
159
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
106
            @WebParam(name = WS_PARAM_DESTDIR) String dest);
160
        return unrar(new File(src), new File(dest));
-
 
161
    }
-
 
162
 
107
 
163
    @WebMethod(operationName = "untar")
108
    @WebMethod(operationName = "untar")
164
    public ReturnCode untar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
109
    public ReturnCode untar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
165
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
110
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
166
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
111
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
167
            @WebParam(name = "compression") CompressionType compression) {
112
            @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)
-
 
184
    private ReturnCode bzip(ResourceInterface src, File dst) {
-
 
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
 
-
 
194
        return runner.postTask();
-
 
195
    }
-
 
196
 
-
 
197
    @WebMethod(exclude = true)
-
 
198
    private ReturnCode gzip(ResourceInterface src, File dst) {
-
 
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);
-
 
207
        return runner.postTask();
-
 
208
    }
-
 
209
 
-
 
210
    @WebMethod(exclude = true)
-
 
211
    private ReturnCode zip(ResourceInterface src, File dst, String encoding, boolean update, int compresslevel) {
-
 
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);
-
 
222
        return runner.postTask();
-
 
223
    }
-
 
224
 
-
 
225
    @WebMethod(exclude = true)
-
 
226
    private ReturnCode GUnzip(ResourceInterface src, File dst) {
-
 
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
        }
-
 
234
        return runner.postTask();
-
 
235
    }
-
 
236
 
-
 
237
    @WebMethod(exclude = true)
-
 
238
    private ReturnCode BUnzip2(ResourceInterface src, File dst) {
-
 
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
        }
-
 
246
        return runner.postTask();
-
 
247
    }
-
 
248
 
-
 
249
    @WebMethod(exclude = true)
-
 
250
    private ReturnCode unzip(File src, File dest, boolean overwrite, String encoding) {
-
 
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
        }
-
 
260
        return runner.postTask();
-
 
261
    }
-
 
262
 
-
 
263
    @WebMethod(exclude = true)
-
 
264
    private ReturnCode untar(File src, File dest, boolean overwrite, Untar.UntarCompressionMethod compression) {
-
 
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);
-
 
272
        return runner.postTask();
-
 
273
    }
-
 
274
 
-
 
275
    @WebMethod(exclude = true)
-
 
276
    private ReturnCode unrar(File src, File dst) {
-
 
277
        UnRarTask unrar = new UnRarTask();
-
 
278
        unrar.setTaskName("UnRar");
-
 
279
        RunTask runner = new RunTask(unrar);
-
 
280
        unrar.setSrc(src);
-
 
281
        unrar.setDst(dst);
-
 
282
        return runner.postTask();
-
 
283
    }
-
 
284
}
113
}