Subversion Repositories XServices

Rev

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