Subversion Repositories XServices

Rev

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