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
 
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
 
6 brianR 30
import net.brutex.xservices.types.ArchiveResource;
54 brianR 31
import net.brutex.xservices.types.AttachmentType;
6 brianR 32
import net.brutex.xservices.types.FileResource;
33
import net.brutex.xservices.types.FileSetResource;
63 brianR 34
import net.brutex.xservices.types.ReplacePattern;
12 brianR 35
import net.brutex.xservices.types.ReturnCode;
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,
287
			boolean append) throws XServicesFault {
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);
295
		try {
296
			if (!f.canWrite())
297
 
298
				throw new XServicesFault("Cannot write to file: "
299
						+ f.getCanonicalPath());
300
 
301
			echo.setFile(f);
302
			echo.setAppend(append);
303
			ReturnCode c = runTask.postTask();
304
			return c;
305
		} catch (BuildException e) {
306
			throw new XServicesFault("Error in echo2file.", e);
307
		} catch (IOException e) {
308
			throw new XServicesFault("Cannot write to file.", e);
309
		}
54 brianR 310
	}
6 brianR 311
 
54 brianR 312
	/*
313
	 * (non-Javadoc)
314
	 *
315
	 * @see
316
	 * net.brutex.xservices.ws.impl.FileService#changeOwner(net.brutex.xservices
317
	 * .types.FileSetResource, java.lang.String)
46 brianR 318
	 */
70 brianR 319
 
65 brianR 320
	public ReturnCode changeOwner(FileSetResource res, String owner) {
321
		Chown chown = new Chown();
322
		chown.setTaskName("Chown");
323
		RunTask runner = new RunTask(chown);
324
		chown.setOwner(owner);
325
		FileSet set = res.getAntResource(chown.getProject());
326
		chown.add(set);
327
		chown.setMaxParallel(300);
328
		return runner.postTask();
54 brianR 329
	}
12 brianR 330
 
54 brianR 331
	/*
332
	 * (non-Javadoc)
333
	 *
334
	 * @see
335
	 * net.brutex.xservices.ws.impl.FileService#changeGroup(net.brutex.xservices
336
	 * .types.FileSetResource, java.lang.String)
46 brianR 337
	 */
70 brianR 338
 
65 brianR 339
	public ReturnCode changeGroup(FileSetResource res, String group) {
340
		Chgrp chgrp = new Chgrp();
341
		chgrp.setTaskName("Chgrp");
342
		RunTask runner = new RunTask(chgrp);
343
		chgrp.setGroup(group);
344
		FileSet set = res.getAntResource(chgrp.getProject());
345
		chgrp.add(set);
346
		chgrp.setMaxParallel(300);
347
		return runner.postTask();
54 brianR 348
	}
12 brianR 349
 
54 brianR 350
	/*
351
	 * (non-Javadoc)
352
	 *
353
	 * @see
354
	 * net.brutex.xservices.ws.impl.FileService#changeMode(net.brutex.xservices
355
	 * .types.FileSetResource, java.lang.String)
46 brianR 356
	 */
70 brianR 357
 
65 brianR 358
	public ReturnCode changeMode(FileSetResource res, String perm) {
54 brianR 359
		Chmod chmod = new Chmod();
360
		chmod.setTaskName("Chmod");
361
		RunTask runner = new RunTask(chmod);
65 brianR 362
		FileSet set = res.getAntResource(chmod.getProject());
54 brianR 363
		chmod.add(set);
364
		chmod.setMaxParallel(300);
365
		chmod.setPerm(perm);
366
		chmod.setVerbose(true);
367
		return runner.postTask();
368
	}
14 brianR 369
 
6 brianR 370
}