Subversion Repositories XServices

Rev

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