Subversion Repositories XServices

Rev

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