Subversion Repositories XServices

Rev

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