Subversion Repositories XServices

Rev

Go to most recent revision | 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
 */
46 brianR 16
package net.brutex.xservices.ws.impl;
6 brianR 17
 
63 brianR 18
import java.io.BufferedInputStream;
6 brianR 19
import java.io.File;
54 brianR 20
import java.io.FileInputStream;
21
import java.io.FileNotFoundException;
22
import java.io.FileOutputStream;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.io.UnsupportedEncodingException;
64 brianR 26
import java.util.Iterator;
54 brianR 27
import java.util.List;
28
 
29
import javax.activation.DataHandler;
6 brianR 30
import javax.jws.WebMethod;
31
import javax.jws.WebParam;
32
import javax.jws.WebService;
54 brianR 33
 
34
import net.brutex.xservices.types.AntProperty;
6 brianR 35
import net.brutex.xservices.types.ArchiveResource;
54 brianR 36
import net.brutex.xservices.types.AttachmentType;
6 brianR 37
import net.brutex.xservices.types.FileResource;
38
import net.brutex.xservices.types.FileSetResource;
63 brianR 39
import net.brutex.xservices.types.ReplacePattern;
6 brianR 40
import net.brutex.xservices.types.ResourceInterface;
12 brianR 41
import net.brutex.xservices.types.ReturnCode;
46 brianR 42
import net.brutex.xservices.util.BrutexNamespaces;
6 brianR 43
import net.brutex.xservices.util.RunTask;
46 brianR 44
import net.brutex.xservices.ws.FileService;
45
import net.brutex.xservices.ws.XServicesFault;
46
 
54 brianR 47
import org.apache.commons.codec.binary.Base64;
48
import org.apache.commons.codec.binary.Base64InputStream;
49
import org.apache.cxf.aegis.type.mtom.StreamDataSource;
50
import org.apache.tools.ant.BuildException;
6 brianR 51
import org.apache.tools.ant.taskdefs.Basename;
14 brianR 52
import org.apache.tools.ant.taskdefs.Chmod;
6 brianR 53
import org.apache.tools.ant.taskdefs.Copy;
54
import org.apache.tools.ant.taskdefs.Echo;
55
import org.apache.tools.ant.taskdefs.LoadResource;
63 brianR 56
import org.apache.tools.ant.taskdefs.Replace;
57
import org.apache.tools.ant.taskdefs.optional.ReplaceRegExp;
12 brianR 58
import org.apache.tools.ant.taskdefs.optional.unix.Chgrp;
59
import org.apache.tools.ant.taskdefs.optional.unix.Chown;
6 brianR 60
import org.apache.tools.ant.types.FileSet;
64 brianR 61
import org.apache.tools.ant.types.Resource;
62
import org.apache.tools.ant.types.ResourceCollection;
6 brianR 63
 
64
/**
54 brianR 65
 *
6 brianR 66
 * @author Brian Rosenberger, bru@brutex.de
67
 */
54 brianR 68
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.FileService", serviceName = "FileService")
46 brianR 69
public class FileServiceImpl implements FileService {
6 brianR 70
 
54 brianR 71
	/*
72
	 * (non-Javadoc)
73
	 *
74
	 * @see net.brutex.xservices.ws.impl.FileService#basename(java.lang.String,
75
	 * java.lang.String)
46 brianR 76
	 */
54 brianR 77
	@Override
46 brianR 78
	@WebMethod(operationName = "basename")
64 brianR 79
	public String basename(@WebParam(name = "file") String filename,
54 brianR 80
			@WebParam(name = "suffix") String suffix) {
64 brianR 81
		final String BASENAME_VALUE = "basename.value";
82
		Basename basename = new Basename();
83
		RunTask runner = new RunTask(basename);
84
		basename.setFile(new File(filename));
85
		if (suffix != null && !suffix.equals("")) {
86
			basename.setSuffix(suffix);
87
		}
88
		basename.setProperty("BASENAME_VALUE");
89
		ReturnCode r = runner.postTask();
90
		return r.getProperty("BASENAME_VALUE");
54 brianR 91
	}
6 brianR 92
 
63 brianR 93
	@WebMethod(operationName = "replaceInFile")
94
	public ReturnCode replaceInFile(
95
			@WebParam(name = "file") FileResource res,
96
			@WebParam(name = "search") String search,
97
			@WebParam(name = "replace") String replace) {
98
		ReturnCode r = null;
99
		Replace rep = new Replace();
100
		rep.setTaskName("Replace");
101
		RunTask runner = new RunTask(rep);
102
		rep.addConfigured(res.getAntResource(rep.getProject()));
103
		rep.setToken(search);
104
		rep.setValue(replace);
105
		r = runner.postTask();
106
		return r;
107
	}
108
 
109
	@WebMethod(operationName = "replaceInFile2")
110
	public ReturnCode replaceInFile2(
111
			@WebParam(name = "file") FileResource res,
112
			@WebParam(name = "patternList") List<ReplacePattern> patternList) {
113
		ReturnCode r = null;
114
		for (ReplacePattern pat : patternList) {
115
			Replace rep = new Replace();
116
			rep.setTaskName("Replace");
117
			RunTask runner = new RunTask(rep);
118
			rep.addConfigured(res.getAntResource(rep.getProject()));
119
			rep.setToken(pat.search);
120
			rep.setValue(pat.replace);
121
			r = runner.postTask();
122
		}
123
		return r;
124
	}
125
 
126
	@WebMethod(operationName = "replaceInFileRegEx")
127
	public ReturnCode replaceInFileRegEx(
128
			@WebParam(name = "file") FileResource res,
129
			@WebParam(name = "search") String search,
130
			@WebParam(name = "replace") String replace,
131
			@WebParam(name = "flags") String flags) {
132
		ReplaceRegExp rep = new ReplaceRegExp();
133
		rep.setTaskName("ReplaceRegExp");
134
		RunTask runner = new RunTask(rep);
135
		File infile = new File(res.uri);
136
		rep.setFile(infile);
137
		rep.setMatch(search);
138
		rep.setReplace(replace);
139
		rep.setFlags(flags);
140
		ReturnCode r = runner.postTask();
141
		return r;
142
	}
143
 
144
 
54 brianR 145
	/*
146
	 * (non-Javadoc)
147
	 *
148
	 * @see
149
	 * net.brutex.xservices.ws.impl.FileService#base64Encode(net.brutex.xservices
150
	 * .types.FileSetResource)
46 brianR 151
	 */
54 brianR 152
	@Override
153
	@WebMethod(operationName = "downloadFile")
154
	public AttachmentType downloadFile(@WebParam(name = "file") FileResource res) {
155
		InputStream is = null;
156
		try {
157
			is = res.getAntResource(null).getInputStream();
158
		} catch (IOException e) {
159
			// TODO Auto-generated catch block
160
			e.printStackTrace();
161
		}
162
		DataHandler h = new DataHandler(new StreamDataSource(
163
				"application/binary", is));
164
		AttachmentType t = new AttachmentType();
165
		t.setContent(h);
60 brianR 166
		t.setFilename(res.getAntResource(null).getName());
54 brianR 167
		return t;
168
	}
169
 
170
	/*
171
	 * (non-Javadoc)
172
	 *
173
	 * @see
174
	 * net.brutex.xservices.ws.impl.FileService#base64Decode(net.brutex.xservices
175
	 * .types.AttachmentType)
176
	 */
177
	@Override
178
	@WebMethod(operationName = "uploadFile")
179
	public String uploadFile(@WebParam(name = "file") AttachmentType file) {
180
		DataHandler h =  file.getContent();
181
		File f = new File(file.getFilename());
182
		FileOutputStream fout;
183
		try {
184
			fout = new FileOutputStream(f);
185
			/*
186
			 * InputStream in = h.getInputStream();
187
			int b;
188
			while( (b=in.read())!= -1 ) {
189
				fout.write(b);
190
			}
191
			*/
192
			h.writeTo(fout);
193
			fout.flush();
194
			fout.close();
195
			//in.close();
196
		} catch (FileNotFoundException e) {
197
			throw new BuildException(e);
198
		} catch (IOException e) {
199
			throw new BuildException(e);
200
		}
201
		return file.getFilename();
202
	}
203
 
204
	/*
205
	 * (non-Javadoc)
206
	 *
207
	 * @see
208
	 * net.brutex.xservices.ws.impl.FileService#copy(net.brutex.xservices.types
209
	 * .FileSetResource, java.lang.String, boolean, boolean, java.lang.String)
210
	 */
211
	@Override
46 brianR 212
	@WebMethod(operationName = "copy")
54 brianR 213
	public ReturnCode copy(@WebParam(name = "fileset") FileSetResource src,
214
			@WebParam(name = "todir") String todir,
215
			@WebParam(name = "preservelastmodified") boolean plm,
216
			@WebParam(name = "overwrite") boolean overwrite,
217
			@WebParam(name = "encoding") String encoding) throws XServicesFault {
218
		return copy(src, new File(todir), plm, overwrite, encoding);
219
	}
64 brianR 220
 
221
	@Override
222
	public ReturnCode copyFile(String fromFile,
223
								String tofile,
224
								boolean overwrite) throws XServicesFault {
225
		Copy copy = new Copy();
226
		copy.setTaskName("Copy");
227
		RunTask runner = new RunTask(copy);
228
		File f = new File(fromFile);
229
		if( ! f.isFile() ) throw new XServicesFault("File '"+fromFile+"' not found.");
230
		copy.setFile( new File( fromFile ));
231
		copy.setTofile(new File(tofile));
232
		copy.setOverwrite(overwrite);
233
		return runner.postTask();
234
	}
6 brianR 235
 
54 brianR 236
	/*
237
	 * (non-Javadoc)
238
	 *
239
	 * @see
240
	 * net.brutex.xservices.ws.impl.FileService#loadRes(net.brutex.xservices
241
	 * .types.FileResource, java.lang.String)
46 brianR 242
	 */
54 brianR 243
	@Override
46 brianR 244
	@WebMethod(operationName = "loadResource")
64 brianR 245
	public String loadRes(@WebParam(name = "resource") FileResource res,
54 brianR 246
			@WebParam(name = "encoding") String encoding) {
247
		if (encoding == null || encoding.equals("")) {
248
			encoding = System.getProperty("file.encoding");
249
		}
64 brianR 250
		LoadResource lr = new LoadResource();
251
		lr.setTaskName("LoadResource");
252
		RunTask runner = new RunTask(lr);
253
		lr.addConfigured(res.getAntResource(lr.getProject()));
254
		lr.setEncoding(encoding);
255
		System.out.println("Using encoding: " + encoding);
256
		lr.setProperty("LoadResource.out");
257
		ReturnCode r = runner.postTask();
258
		return r.getProperty("LoadResource.out");
54 brianR 259
	}
6 brianR 260
 
54 brianR 261
	/*
262
	 * (non-Javadoc)
263
	 *
264
	 * @see
265
	 * net.brutex.xservices.ws.impl.FileService#loadResFromArchive(net.brutex
266
	 * .xservices.types.ArchiveResource, java.lang.String)
46 brianR 267
	 */
54 brianR 268
	@Override
46 brianR 269
	@WebMethod(operationName = "loadResourceFromArchive")
64 brianR 270
	public String loadResFromArchive(
54 brianR 271
			@WebParam(name = "archiveresource") ArchiveResource res,
272
			@WebParam(name = "encoding") String encoding) {
273
		if (encoding == null || encoding.equals("")) {
274
			encoding = System.getProperty("file.encoding");
275
		}
64 brianR 276
		LoadResource lr = new LoadResource();
277
		lr.setTaskName("LoadResource");
278
		RunTask runner = new RunTask(lr);
279
		lr.addConfigured(res.getAntResource(lr.getProject()));
280
		lr.setEncoding(encoding);
281
		System.out.println("Using encoding: " + encoding);
282
		lr.setProperty("LoadResource.out");
283
		ReturnCode r = runner.postTask();
284
		return r.getProperty("LoadResource.out");
285
 
54 brianR 286
	}
6 brianR 287
 
54 brianR 288
	/*
289
	 * (non-Javadoc)
290
	 *
291
	 * @see net.brutex.xservices.ws.impl.FileService#echo2file(java.lang.String,
292
	 * java.lang.String, java.lang.String, boolean)
46 brianR 293
	 */
54 brianR 294
	@Override
46 brianR 295
	@WebMethod(operationName = "echoToFile")
54 brianR 296
	public ReturnCode echo2file(@WebParam(name = "message") String message,
297
			@WebParam(name = "file") String file,
298
			@WebParam(name = "encoding") String encoding,
299
			@WebParam(name = "append") boolean append) {
300
		return echo(message, new File(file), encoding, append);
301
	}
6 brianR 302
 
54 brianR 303
	/*
304
	 * (non-Javadoc)
305
	 *
306
	 * @see
307
	 * net.brutex.xservices.ws.impl.FileService#changeOwner(net.brutex.xservices
308
	 * .types.FileSetResource, java.lang.String)
46 brianR 309
	 */
54 brianR 310
	@Override
46 brianR 311
	@WebMethod(operationName = "changeOwner")
54 brianR 312
	public ReturnCode changeOwner(
313
			@WebParam(name = "fileset") FileSetResource res,
314
			@WebParam(name = "owner") String owner) {
315
		return chown(res, owner);
316
	}
12 brianR 317
 
54 brianR 318
	/*
319
	 * (non-Javadoc)
320
	 *
321
	 * @see
322
	 * net.brutex.xservices.ws.impl.FileService#changeGroup(net.brutex.xservices
323
	 * .types.FileSetResource, java.lang.String)
46 brianR 324
	 */
54 brianR 325
	@Override
46 brianR 326
	@WebMethod(operationName = "changeGroup")
54 brianR 327
	public ReturnCode changeGroup(
328
			@WebParam(name = "fileset") FileSetResource res,
329
			@WebParam(name = "group") String group) {
330
		return chgrp(res, group);
331
	}
12 brianR 332
 
54 brianR 333
	/*
334
	 * (non-Javadoc)
335
	 *
336
	 * @see
337
	 * net.brutex.xservices.ws.impl.FileService#changeMode(net.brutex.xservices
338
	 * .types.FileSetResource, java.lang.String)
46 brianR 339
	 */
54 brianR 340
	@Override
46 brianR 341
	@WebMethod(operationName = "changeMode")
54 brianR 342
	public ReturnCode changeMode(
343
			@WebParam(name = "fileset") FileSetResource res,
344
			@WebParam(name = "permissions") String perm) {
345
		return chmod(res, perm);
346
	}
14 brianR 347
 
54 brianR 348
	@WebMethod(exclude = true)
349
	private ReturnCode echo(String msg, File file, String encoding,
350
			boolean append) {
351
		Echo echo = new Echo();
352
		echo.setTaskName("toFile");
353
		RunTask runTask = new RunTask(echo);
354
		echo.addText(msg);
355
		echo.setEncoding(encoding);
356
		echo.setFile(file);
357
		echo.setAppend(append);
358
		return runTask.postTask();
359
	}
6 brianR 360
 
54 brianR 361
	@WebMethod(exclude = true)
362
	private ReturnCode copy(FileSetResource src, File dst,
363
			boolean preservelastmodified, boolean overwrite, String encoding) {
364
		Copy copy = new Copy();
365
		copy.setTaskName("Copy");
366
		RunTask runner = new RunTask(copy);
367
		FileSet set = src.getAntResource(copy.getProject());
368
		copy.add(set);
6 brianR 369
 
54 brianR 370
		if (dst.isDirectory()) {
371
			copy.setTodir(dst);
372
		}
373
		if (dst.isFile()) {
374
			copy.setTofile(dst);
375
		}
376
		copy.setOverwrite(overwrite);
377
		copy.setPreserveLastModified(preservelastmodified);
378
		if (encoding != null && !encoding.equals("")) {
379
			copy.setOutputEncoding(encoding);
380
		} else {
381
			copy.setOutputEncoding(System.getProperty("file.encoding"));
382
		}
6 brianR 383
 
54 brianR 384
		return runner.postTask();
385
	}
12 brianR 386
 
54 brianR 387
	@WebMethod(exclude = true)
388
	private ReturnCode chown(FileSetResource src, String owner) {
389
		Chown chown = new Chown();
390
		chown.setTaskName("Chown");
391
		RunTask runner = new RunTask(chown);
392
		chown.setOwner(owner);
393
		FileSet set = src.getAntResource(chown.getProject());
394
		chown.add(set);
395
		chown.setMaxParallel(300);
396
		return runner.postTask();
397
	}
12 brianR 398
 
54 brianR 399
	@WebMethod(exclude = true)
400
	private ReturnCode chgrp(FileSetResource src, String group) {
401
		Chgrp chgrp = new Chgrp();
402
		chgrp.setTaskName("Chgrp");
403
		RunTask runner = new RunTask(chgrp);
404
		chgrp.setGroup(group);
405
		FileSet set = src.getAntResource(chgrp.getProject());
406
		chgrp.add(set);
407
		chgrp.setMaxParallel(300);
408
		return runner.postTask();
409
	}
14 brianR 410
 
54 brianR 411
	@WebMethod(exclude = true)
412
	private ReturnCode chmod(FileSetResource src, String perm) {
413
		Chmod chmod = new Chmod();
414
		chmod.setTaskName("Chmod");
415
		RunTask runner = new RunTask(chmod);
416
		FileSet set = src.getAntResource(chmod.getProject());
417
		chmod.add(set);
418
		chmod.setMaxParallel(300);
419
		chmod.setPerm(perm);
420
		chmod.setVerbose(true);
421
		return runner.postTask();
422
	}
14 brianR 423
 
6 brianR 424
}