Subversion Repositories XServices

Compare Revisions

Ignore whitespace Rev 64 → Rev 65

/xservices/trunk/src/java/net/brutex/xservices/ws/impl/FileServiceImpl.java
15,15 → 15,11
*/
package net.brutex.xservices.ws.impl;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
 
import javax.activation.DataHandler;
31,13 → 27,11
import javax.jws.WebParam;
import javax.jws.WebService;
 
import net.brutex.xservices.types.AntProperty;
import net.brutex.xservices.types.ArchiveResource;
import net.brutex.xservices.types.AttachmentType;
import net.brutex.xservices.types.FileResource;
import net.brutex.xservices.types.FileSetResource;
import net.brutex.xservices.types.ReplacePattern;
import net.brutex.xservices.types.ResourceInterface;
import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.util.BrutexNamespaces;
import net.brutex.xservices.util.RunTask;
44,8 → 38,6
import net.brutex.xservices.ws.FileService;
import net.brutex.xservices.ws.XServicesFault;
 
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.cxf.aegis.type.mtom.StreamDataSource;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Basename;
58,8 → 50,6
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;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
 
/**
*
75,9 → 65,7
* java.lang.String)
*/
@Override
@WebMethod(operationName = "basename")
public String basename(@WebParam(name = "file") String filename,
@WebParam(name = "suffix") String suffix) {
public String basename(String filename, String suffix) {
final String BASENAME_VALUE = "basename.value";
Basename basename = new Basename();
RunTask runner = new RunTask(basename);
85,16 → 73,13
if (suffix != null && !suffix.equals("")) {
basename.setSuffix(suffix);
}
basename.setProperty("BASENAME_VALUE");
basename.setProperty(BASENAME_VALUE);
ReturnCode r = runner.postTask();
return r.getProperty("BASENAME_VALUE");
return r.getProperty(BASENAME_VALUE);
}
 
@WebMethod(operationName = "replaceInFile")
public ReturnCode replaceInFile(
@WebParam(name = "file") FileResource res,
@WebParam(name = "search") String search,
@WebParam(name = "replace") String replace) {
public ReturnCode replaceInFile(FileResource res, String search,
String replace) throws XServicesFault {
ReturnCode r = null;
Replace rep = new Replace();
rep.setTaskName("Replace");
102,14 → 87,16
rep.addConfigured(res.getAntResource(rep.getProject()));
rep.setToken(search);
rep.setValue(replace);
r = runner.postTask();
try {
r = runner.postTask();
} catch (BuildException e) {
throw new XServicesFault(e);
}
return r;
}
@WebMethod(operationName = "replaceInFile2")
public ReturnCode replaceInFile2(
@WebParam(name = "file") FileResource res,
@WebParam(name = "patternList") List<ReplacePattern> patternList) {
 
public ReturnCode replaceInFile2(FileResource res,
List<ReplacePattern> patternList) throws XServicesFault {
ReturnCode r = null;
for (ReplacePattern pat : patternList) {
Replace rep = new Replace();
118,17 → 105,17
rep.addConfigured(res.getAntResource(rep.getProject()));
rep.setToken(pat.search);
rep.setValue(pat.replace);
r = runner.postTask();
try {
r = runner.postTask();
} catch (BuildException e) {
throw new XServicesFault(e);
}
}
return r;
}
@WebMethod(operationName = "replaceInFileRegEx")
public ReturnCode replaceInFileRegEx(
@WebParam(name = "file") FileResource res,
@WebParam(name = "search") String search,
@WebParam(name = "replace") String replace,
@WebParam(name = "flags") String flags) {
 
public ReturnCode replaceInFileRegEx(FileResource res, String search,
String replace, String flags) throws XServicesFault {
ReplaceRegExp rep = new ReplaceRegExp();
rep.setTaskName("ReplaceRegExp");
RunTask runner = new RunTask(rep);
137,11 → 124,14
rep.setMatch(search);
rep.setReplace(replace);
rep.setFlags(flags);
ReturnCode r = runner.postTask();
return r;
try {
ReturnCode r = runner.postTask();
return r;
} catch (BuildException e) {
throw new XServicesFault(e);
}
}
 
/*
* (non-Javadoc)
*
150,21 → 140,20
* .types.FileSetResource)
*/
@Override
@WebMethod(operationName = "downloadFile")
public AttachmentType downloadFile(@WebParam(name = "file") FileResource res) {
public AttachmentType downloadFile(FileResource res) throws XServicesFault {
InputStream is = null;
try {
is = res.getAntResource(null).getInputStream();
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;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new XServicesFault(e);
}
DataHandler h = new DataHandler(new StreamDataSource(
"application/binary", is));
AttachmentType t = new AttachmentType();
t.setContent(h);
t.setFilename(res.getAntResource(null).getName());
return t;
}
 
/*
175,28 → 164,19
* .types.AttachmentType)
*/
@Override
@WebMethod(operationName = "uploadFile")
public String uploadFile(@WebParam(name = "file") AttachmentType file) {
DataHandler h = file.getContent();
public String uploadFile(AttachmentType file) throws XServicesFault {
DataHandler h = file.getContent();
File f = new File(file.getFilename());
FileOutputStream fout;
try {
fout = new FileOutputStream(f);
/*
* InputStream in = h.getInputStream();
int b;
while( (b=in.read())!= -1 ) {
fout.write(b);
}
*/
h.writeTo(fout);
fout.flush();
fout.close();
//in.close();
} catch (FileNotFoundException e) {
throw new BuildException(e);
throw new XServicesFault(e);
} catch (IOException e) {
throw new BuildException(e);
throw new XServicesFault(e);
}
return file.getFilename();
}
209,25 → 189,40
* .FileSetResource, java.lang.String, boolean, boolean, java.lang.String)
*/
@Override
@WebMethod(operationName = "copy")
public ReturnCode copy(@WebParam(name = "fileset") FileSetResource src,
@WebParam(name = "todir") String todir,
@WebParam(name = "preservelastmodified") boolean plm,
@WebParam(name = "overwrite") boolean overwrite,
@WebParam(name = "encoding") String encoding) throws XServicesFault {
return copy(src, new File(todir), plm, overwrite, encoding);
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();
}
 
@Override
public ReturnCode copyFile(String fromFile,
String tofile,
boolean overwrite) throws XServicesFault {
public ReturnCode copyFile(String fromFile, String tofile, boolean overwrite)
throws XServicesFault {
Copy copy = new Copy();
copy.setTaskName("Copy");
RunTask runner = new RunTask(copy);
File f = new File(fromFile);
if( ! f.isFile() ) throw new XServicesFault("File '"+fromFile+"' not found.");
copy.setFile( new File( fromFile ));
if (!f.isFile())
throw new XServicesFault("File '" + fromFile + "' not found.");
copy.setFile(new File(fromFile));
copy.setTofile(new File(tofile));
copy.setOverwrite(overwrite);
return runner.postTask();
241,9 → 236,8
* .types.FileResource, java.lang.String)
*/
@Override
@WebMethod(operationName = "loadResource")
public String loadRes(@WebParam(name = "resource") FileResource res,
@WebParam(name = "encoding") String encoding) {
public String loadRes(FileResource res, String encoding)
throws XServicesFault {
if (encoding == null || encoding.equals("")) {
encoding = System.getProperty("file.encoding");
}
266,10 → 260,7
* .xservices.types.ArchiveResource, java.lang.String)
*/
@Override
@WebMethod(operationName = "loadResourceFromArchive")
public String loadResFromArchive(
@WebParam(name = "archiveresource") ArchiveResource res,
@WebParam(name = "encoding") String encoding) {
public String loadResFromArchive(ArchiveResource res, String encoding) {
if (encoding == null || encoding.equals("")) {
encoding = System.getProperty("file.encoding");
}
282,7 → 273,7
lr.setProperty("LoadResource.out");
ReturnCode r = runner.postTask();
return r.getProperty("LoadResource.out");
 
}
 
/*
292,12 → 283,30
* java.lang.String, java.lang.String, boolean)
*/
@Override
@WebMethod(operationName = "echoToFile")
public ReturnCode echo2file(@WebParam(name = "message") String message,
@WebParam(name = "file") String file,
@WebParam(name = "encoding") String encoding,
@WebParam(name = "append") boolean append) {
return echo(message, new File(file), encoding, append);
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);
}
}
 
/*
308,11 → 317,15
* .types.FileSetResource, java.lang.String)
*/
@Override
@WebMethod(operationName = "changeOwner")
public ReturnCode changeOwner(
@WebParam(name = "fileset") FileSetResource res,
@WebParam(name = "owner") String owner) {
return chown(res, owner);
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();
}
 
/*
323,11 → 336,15
* .types.FileSetResource, java.lang.String)
*/
@Override
@WebMethod(operationName = "changeGroup")
public ReturnCode changeGroup(
@WebParam(name = "fileset") FileSetResource res,
@WebParam(name = "group") String group) {
return chgrp(res, group);
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();
}
 
/*
338,82 → 355,11
* .types.FileSetResource, java.lang.String)
*/
@Override
@WebMethod(operationName = "changeMode")
public ReturnCode changeMode(
@WebParam(name = "fileset") FileSetResource res,
@WebParam(name = "permissions") String perm) {
return chmod(res, perm);
}
 
@WebMethod(exclude = true)
private ReturnCode echo(String msg, File file, String encoding,
boolean append) {
Echo echo = new Echo();
echo.setTaskName("toFile");
RunTask runTask = new RunTask(echo);
echo.addText(msg);
echo.setEncoding(encoding);
echo.setFile(file);
echo.setAppend(append);
return runTask.postTask();
}
 
@WebMethod(exclude = true)
private ReturnCode copy(FileSetResource src, File dst,
boolean preservelastmodified, boolean overwrite, String encoding) {
Copy copy = new Copy();
copy.setTaskName("Copy");
RunTask runner = new RunTask(copy);
FileSet set = src.getAntResource(copy.getProject());
copy.add(set);
 
if (dst.isDirectory()) {
copy.setTodir(dst);
}
if (dst.isFile()) {
copy.setTofile(dst);
}
copy.setOverwrite(overwrite);
copy.setPreserveLastModified(preservelastmodified);
if (encoding != null && !encoding.equals("")) {
copy.setOutputEncoding(encoding);
} else {
copy.setOutputEncoding(System.getProperty("file.encoding"));
}
 
return runner.postTask();
}
 
@WebMethod(exclude = true)
private ReturnCode chown(FileSetResource src, String owner) {
Chown chown = new Chown();
chown.setTaskName("Chown");
RunTask runner = new RunTask(chown);
chown.setOwner(owner);
FileSet set = src.getAntResource(chown.getProject());
chown.add(set);
chown.setMaxParallel(300);
return runner.postTask();
}
 
@WebMethod(exclude = true)
private ReturnCode chgrp(FileSetResource src, String group) {
Chgrp chgrp = new Chgrp();
chgrp.setTaskName("Chgrp");
RunTask runner = new RunTask(chgrp);
chgrp.setGroup(group);
FileSet set = src.getAntResource(chgrp.getProject());
chgrp.add(set);
chgrp.setMaxParallel(300);
return runner.postTask();
}
 
@WebMethod(exclude = true)
private ReturnCode chmod(FileSetResource src, String perm) {
public ReturnCode changeMode(FileSetResource res, String perm) {
Chmod chmod = new Chmod();
chmod.setTaskName("Chmod");
RunTask runner = new RunTask(chmod);
FileSet set = src.getAntResource(chmod.getProject());
FileSet set = res.getAntResource(chmod.getProject());
chmod.add(set);
chmod.setMaxParallel(300);
chmod.setPerm(perm);