2011-01-29 19:42:06 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2010 Brian Rosenberger (Brutex Network)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package net.brutex.xservices.ws.impl;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2011-04-20 16:59:34 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import javax.activation.DataHandler;
|
2011-01-29 19:42:06 +00:00
|
|
|
import javax.jws.WebMethod;
|
|
|
|
|
import javax.jws.WebParam;
|
|
|
|
|
import javax.jws.WebService;
|
2011-04-20 16:59:34 +00:00
|
|
|
|
2011-01-29 19:42:06 +00:00
|
|
|
import net.brutex.xservices.types.ArchiveResource;
|
2011-04-20 16:59:34 +00:00
|
|
|
import net.brutex.xservices.types.AttachmentType;
|
2011-01-29 19:42:06 +00:00
|
|
|
import net.brutex.xservices.types.FileResource;
|
|
|
|
|
import net.brutex.xservices.types.FileSetResource;
|
2011-05-03 14:10:04 +00:00
|
|
|
import net.brutex.xservices.types.ReplacePattern;
|
2011-01-29 19:42:06 +00:00
|
|
|
import net.brutex.xservices.types.ReturnCode;
|
|
|
|
|
import net.brutex.xservices.util.BrutexNamespaces;
|
|
|
|
|
import net.brutex.xservices.util.RunTask;
|
|
|
|
|
import net.brutex.xservices.ws.FileService;
|
|
|
|
|
import net.brutex.xservices.ws.XServicesFault;
|
|
|
|
|
|
2011-04-20 16:59:34 +00:00
|
|
|
import org.apache.cxf.aegis.type.mtom.StreamDataSource;
|
|
|
|
|
import org.apache.tools.ant.BuildException;
|
2011-01-29 19:42:06 +00:00
|
|
|
import org.apache.tools.ant.taskdefs.Basename;
|
|
|
|
|
import org.apache.tools.ant.taskdefs.Chmod;
|
|
|
|
|
import org.apache.tools.ant.taskdefs.Copy;
|
|
|
|
|
import org.apache.tools.ant.taskdefs.Echo;
|
|
|
|
|
import org.apache.tools.ant.taskdefs.LoadResource;
|
2011-05-03 14:10:04 +00:00
|
|
|
import org.apache.tools.ant.taskdefs.Replace;
|
|
|
|
|
import org.apache.tools.ant.taskdefs.optional.ReplaceRegExp;
|
2011-01-29 19:42:06 +00:00
|
|
|
import org.apache.tools.ant.taskdefs.optional.unix.Chgrp;
|
|
|
|
|
import org.apache.tools.ant.taskdefs.optional.unix.Chown;
|
|
|
|
|
import org.apache.tools.ant.types.FileSet;
|
|
|
|
|
|
|
|
|
|
/**
|
2011-04-20 16:59:34 +00:00
|
|
|
*
|
2011-01-29 19:42:06 +00:00
|
|
|
* @author Brian Rosenberger, bru@brutex.de
|
|
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.FileService", serviceName = "FileService")
|
2011-01-29 19:42:06 +00:00
|
|
|
public class FileServiceImpl implements FileService {
|
|
|
|
|
|
2011-04-20 16:59:34 +00:00
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see net.brutex.xservices.ws.impl.FileService#basename(java.lang.String,
|
|
|
|
|
* java.lang.String)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public String basename(String filename, String suffix) {
|
2011-05-19 15:28:13 +00:00
|
|
|
final String BASENAME_VALUE = "basename.value";
|
|
|
|
|
Basename basename = new Basename();
|
|
|
|
|
RunTask runner = new RunTask(basename);
|
|
|
|
|
basename.setFile(new File(filename));
|
|
|
|
|
if (suffix != null && !suffix.equals("")) {
|
|
|
|
|
basename.setSuffix(suffix);
|
|
|
|
|
}
|
2011-05-20 17:38:12 +00:00
|
|
|
basename.setProperty(BASENAME_VALUE);
|
2011-05-19 15:28:13 +00:00
|
|
|
ReturnCode r = runner.postTask();
|
2011-05-20 17:38:12 +00:00
|
|
|
return r.getProperty(BASENAME_VALUE);
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-20 17:38:12 +00:00
|
|
|
public ReturnCode replaceInFile(FileResource res, String search,
|
|
|
|
|
String replace) throws XServicesFault {
|
2011-05-03 14:10:04 +00:00
|
|
|
ReturnCode r = null;
|
|
|
|
|
Replace rep = new Replace();
|
|
|
|
|
rep.setTaskName("Replace");
|
|
|
|
|
RunTask runner = new RunTask(rep);
|
|
|
|
|
rep.addConfigured(res.getAntResource(rep.getProject()));
|
|
|
|
|
rep.setToken(search);
|
|
|
|
|
rep.setValue(replace);
|
2011-05-20 17:38:12 +00:00
|
|
|
try {
|
|
|
|
|
r = runner.postTask();
|
|
|
|
|
} catch (BuildException e) {
|
|
|
|
|
throw new XServicesFault(e);
|
|
|
|
|
}
|
2011-05-03 14:10:04 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
2011-05-20 17:38:12 +00:00
|
|
|
|
|
|
|
|
public ReturnCode replaceInFile2(FileResource res,
|
|
|
|
|
List<ReplacePattern> patternList) throws XServicesFault {
|
2011-05-03 14:10:04 +00:00
|
|
|
ReturnCode r = null;
|
|
|
|
|
for (ReplacePattern pat : patternList) {
|
|
|
|
|
Replace rep = new Replace();
|
|
|
|
|
rep.setTaskName("Replace");
|
|
|
|
|
RunTask runner = new RunTask(rep);
|
|
|
|
|
rep.addConfigured(res.getAntResource(rep.getProject()));
|
|
|
|
|
rep.setToken(pat.search);
|
|
|
|
|
rep.setValue(pat.replace);
|
2011-05-20 17:38:12 +00:00
|
|
|
try {
|
|
|
|
|
r = runner.postTask();
|
|
|
|
|
} catch (BuildException e) {
|
|
|
|
|
throw new XServicesFault(e);
|
|
|
|
|
}
|
2011-05-03 14:10:04 +00:00
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2011-05-20 17:38:12 +00:00
|
|
|
|
|
|
|
|
public ReturnCode replaceInFileRegEx(FileResource res, String search,
|
|
|
|
|
String replace, String flags) throws XServicesFault {
|
2011-05-03 14:10:04 +00:00
|
|
|
ReplaceRegExp rep = new ReplaceRegExp();
|
|
|
|
|
rep.setTaskName("ReplaceRegExp");
|
|
|
|
|
RunTask runner = new RunTask(rep);
|
|
|
|
|
File infile = new File(res.uri);
|
|
|
|
|
rep.setFile(infile);
|
|
|
|
|
rep.setMatch(search);
|
|
|
|
|
rep.setReplace(replace);
|
|
|
|
|
rep.setFlags(flags);
|
2011-05-20 17:38:12 +00:00
|
|
|
try {
|
|
|
|
|
ReturnCode r = runner.postTask();
|
|
|
|
|
return r;
|
|
|
|
|
} catch (BuildException e) {
|
|
|
|
|
throw new XServicesFault(e);
|
|
|
|
|
}
|
2011-05-03 14:10:04 +00:00
|
|
|
}
|
2011-05-20 17:38:12 +00:00
|
|
|
|
2011-04-20 16:59:34 +00:00
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#base64Encode(net.brutex.xservices
|
|
|
|
|
* .types.FileSetResource)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public AttachmentType downloadFile(FileResource res) throws XServicesFault {
|
2011-04-20 16:59:34 +00:00
|
|
|
InputStream is = null;
|
|
|
|
|
try {
|
|
|
|
|
is = res.getAntResource(null).getInputStream();
|
2011-05-20 17:38:12 +00:00
|
|
|
StreamDataSource ssource = new StreamDataSource(
|
|
|
|
|
"application/binary", is);
|
|
|
|
|
DataHandler h = new DataHandler(ssource);
|
|
|
|
|
AttachmentType t = new AttachmentType();
|
|
|
|
|
t.setContent(h);
|
|
|
|
|
t.setFilename(res.getAntResource(null).getName());
|
|
|
|
|
return t;
|
2011-04-20 16:59:34 +00:00
|
|
|
} catch (IOException e) {
|
2011-05-20 17:38:12 +00:00
|
|
|
throw new XServicesFault(e);
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-01-29 19:42:06 +00:00
|
|
|
|
2011-04-20 16:59:34 +00:00
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#base64Decode(net.brutex.xservices
|
|
|
|
|
* .types.AttachmentType)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public String uploadFile(AttachmentType file) throws XServicesFault {
|
|
|
|
|
DataHandler h = file.getContent();
|
2011-04-20 16:59:34 +00:00
|
|
|
File f = new File(file.getFilename());
|
|
|
|
|
FileOutputStream fout;
|
|
|
|
|
try {
|
|
|
|
|
fout = new FileOutputStream(f);
|
|
|
|
|
h.writeTo(fout);
|
|
|
|
|
fout.flush();
|
|
|
|
|
fout.close();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
2011-05-20 17:38:12 +00:00
|
|
|
throw new XServicesFault(e);
|
2011-04-20 16:59:34 +00:00
|
|
|
} catch (IOException e) {
|
2011-05-20 17:38:12 +00:00
|
|
|
throw new XServicesFault(e);
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
|
|
|
|
return file.getFilename();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#copy(net.brutex.xservices.types
|
|
|
|
|
* .FileSetResource, java.lang.String, boolean, boolean, java.lang.String)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public ReturnCode copy(FileSetResource src, String todir, boolean plm,
|
|
|
|
|
boolean overwrite, String encoding) throws XServicesFault {
|
|
|
|
|
Copy copy = new Copy();
|
|
|
|
|
copy.setTaskName("Copy");
|
|
|
|
|
RunTask runner = new RunTask(copy);
|
|
|
|
|
FileSet set = src.getAntResource(copy.getProject());
|
|
|
|
|
copy.add(set);
|
|
|
|
|
File dst = new File(todir);
|
|
|
|
|
if (dst.isDirectory()) {
|
|
|
|
|
copy.setTodir(dst);
|
|
|
|
|
}
|
|
|
|
|
if (dst.isFile()) {
|
|
|
|
|
copy.setTofile(dst);
|
|
|
|
|
}
|
|
|
|
|
copy.setOverwrite(overwrite);
|
|
|
|
|
copy.setPreserveLastModified(plm);
|
|
|
|
|
if (encoding != null && !encoding.equals("")) {
|
|
|
|
|
copy.setOutputEncoding(encoding);
|
|
|
|
|
} else {
|
|
|
|
|
copy.setOutputEncoding(System.getProperty("file.encoding"));
|
|
|
|
|
}
|
|
|
|
|
return runner.postTask();
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
2011-05-20 17:38:12 +00:00
|
|
|
|
2011-05-19 15:28:13 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public ReturnCode copyFile(String fromFile, String tofile, boolean overwrite)
|
|
|
|
|
throws XServicesFault {
|
2011-05-19 15:28:13 +00:00
|
|
|
Copy copy = new Copy();
|
|
|
|
|
copy.setTaskName("Copy");
|
|
|
|
|
RunTask runner = new RunTask(copy);
|
|
|
|
|
File f = new File(fromFile);
|
2011-05-20 17:38:12 +00:00
|
|
|
if (!f.isFile())
|
|
|
|
|
throw new XServicesFault("File '" + fromFile + "' not found.");
|
|
|
|
|
copy.setFile(new File(fromFile));
|
2011-05-19 15:28:13 +00:00
|
|
|
copy.setTofile(new File(tofile));
|
|
|
|
|
copy.setOverwrite(overwrite);
|
|
|
|
|
return runner.postTask();
|
|
|
|
|
}
|
2011-04-20 16:59:34 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#loadRes(net.brutex.xservices
|
|
|
|
|
* .types.FileResource, java.lang.String)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public String loadRes(FileResource res, String encoding)
|
|
|
|
|
throws XServicesFault {
|
2011-04-20 16:59:34 +00:00
|
|
|
if (encoding == null || encoding.equals("")) {
|
|
|
|
|
encoding = System.getProperty("file.encoding");
|
|
|
|
|
}
|
2011-05-19 15:28:13 +00:00
|
|
|
LoadResource lr = new LoadResource();
|
|
|
|
|
lr.setTaskName("LoadResource");
|
|
|
|
|
RunTask runner = new RunTask(lr);
|
|
|
|
|
lr.addConfigured(res.getAntResource(lr.getProject()));
|
|
|
|
|
lr.setEncoding(encoding);
|
|
|
|
|
System.out.println("Using encoding: " + encoding);
|
|
|
|
|
lr.setProperty("LoadResource.out");
|
|
|
|
|
ReturnCode r = runner.postTask();
|
|
|
|
|
return r.getProperty("LoadResource.out");
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#loadResFromArchive(net.brutex
|
|
|
|
|
* .xservices.types.ArchiveResource, java.lang.String)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public String loadResFromArchive(ArchiveResource res, String encoding) {
|
2011-04-20 16:59:34 +00:00
|
|
|
if (encoding == null || encoding.equals("")) {
|
|
|
|
|
encoding = System.getProperty("file.encoding");
|
|
|
|
|
}
|
2011-05-19 15:28:13 +00:00
|
|
|
LoadResource lr = new LoadResource();
|
|
|
|
|
lr.setTaskName("LoadResource");
|
|
|
|
|
RunTask runner = new RunTask(lr);
|
|
|
|
|
lr.addConfigured(res.getAntResource(lr.getProject()));
|
|
|
|
|
lr.setEncoding(encoding);
|
|
|
|
|
System.out.println("Using encoding: " + encoding);
|
|
|
|
|
lr.setProperty("LoadResource.out");
|
|
|
|
|
ReturnCode r = runner.postTask();
|
|
|
|
|
return r.getProperty("LoadResource.out");
|
2011-05-20 17:38:12 +00:00
|
|
|
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see net.brutex.xservices.ws.impl.FileService#echo2file(java.lang.String,
|
|
|
|
|
* java.lang.String, java.lang.String, boolean)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public ReturnCode echo2file(String message, String file, String encoding,
|
|
|
|
|
boolean append) throws XServicesFault {
|
|
|
|
|
|
|
|
|
|
Echo echo = new Echo();
|
|
|
|
|
echo.setTaskName("toFile");
|
|
|
|
|
RunTask runTask = new RunTask(echo);
|
|
|
|
|
echo.addText(message);
|
|
|
|
|
echo.setEncoding(encoding);
|
|
|
|
|
File f = new File(file);
|
|
|
|
|
try {
|
|
|
|
|
if (!f.canWrite())
|
|
|
|
|
|
|
|
|
|
throw new XServicesFault("Cannot write to file: "
|
|
|
|
|
+ f.getCanonicalPath());
|
|
|
|
|
|
|
|
|
|
echo.setFile(f);
|
|
|
|
|
echo.setAppend(append);
|
|
|
|
|
ReturnCode c = runTask.postTask();
|
|
|
|
|
return c;
|
|
|
|
|
} catch (BuildException e) {
|
|
|
|
|
throw new XServicesFault("Error in echo2file.", e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new XServicesFault("Cannot write to file.", e);
|
|
|
|
|
}
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#changeOwner(net.brutex.xservices
|
|
|
|
|
* .types.FileSetResource, java.lang.String)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public ReturnCode changeOwner(FileSetResource res, String owner) {
|
|
|
|
|
Chown chown = new Chown();
|
|
|
|
|
chown.setTaskName("Chown");
|
|
|
|
|
RunTask runner = new RunTask(chown);
|
|
|
|
|
chown.setOwner(owner);
|
|
|
|
|
FileSet set = res.getAntResource(chown.getProject());
|
|
|
|
|
chown.add(set);
|
|
|
|
|
chown.setMaxParallel(300);
|
|
|
|
|
return runner.postTask();
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
2011-01-29 19:42:06 +00:00
|
|
|
|
2011-04-20 16:59:34 +00:00
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#changeGroup(net.brutex.xservices
|
|
|
|
|
* .types.FileSetResource, java.lang.String)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public ReturnCode changeGroup(FileSetResource res, String group) {
|
|
|
|
|
Chgrp chgrp = new Chgrp();
|
|
|
|
|
chgrp.setTaskName("Chgrp");
|
|
|
|
|
RunTask runner = new RunTask(chgrp);
|
|
|
|
|
chgrp.setGroup(group);
|
|
|
|
|
FileSet set = res.getAntResource(chgrp.getProject());
|
|
|
|
|
chgrp.add(set);
|
|
|
|
|
chgrp.setMaxParallel(300);
|
|
|
|
|
return runner.postTask();
|
2011-04-20 16:59:34 +00:00
|
|
|
}
|
2011-01-29 19:42:06 +00:00
|
|
|
|
2011-04-20 16:59:34 +00:00
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see
|
|
|
|
|
* net.brutex.xservices.ws.impl.FileService#changeMode(net.brutex.xservices
|
|
|
|
|
* .types.FileSetResource, java.lang.String)
|
2011-01-29 19:42:06 +00:00
|
|
|
*/
|
2011-04-20 16:59:34 +00:00
|
|
|
@Override
|
2011-05-20 17:38:12 +00:00
|
|
|
public ReturnCode changeMode(FileSetResource res, String perm) {
|
2011-04-20 16:59:34 +00:00
|
|
|
Chmod chmod = new Chmod();
|
|
|
|
|
chmod.setTaskName("Chmod");
|
|
|
|
|
RunTask runner = new RunTask(chmod);
|
2011-05-20 17:38:12 +00:00
|
|
|
FileSet set = res.getAntResource(chmod.getProject());
|
2011-04-20 16:59:34 +00:00
|
|
|
chmod.add(set);
|
|
|
|
|
chmod.setMaxParallel(300);
|
|
|
|
|
chmod.setPerm(perm);
|
|
|
|
|
chmod.setVerbose(true);
|
|
|
|
|
return runner.postTask();
|
|
|
|
|
}
|
2011-01-29 19:42:06 +00:00
|
|
|
|
|
|
|
|
}
|