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