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