Subversion Repositories XServices

Rev

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