Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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