Subversion Repositories XServices

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

/*
 *   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;

import java.io.File;
import java.util.Map;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import net.brutex.xservices.types.ArchiveResource;
import net.brutex.xservices.types.FileResource;
import net.brutex.xservices.types.FileSetResource;
import net.brutex.xservices.types.ResourceInterface;
import net.brutex.xservices.util.RunTask;
import org.apache.tools.ant.taskdefs.Basename;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Echo;
import org.apache.tools.ant.taskdefs.LoadResource;
import org.apache.tools.ant.types.FileSet;

/**
 *
 * @author Brian Rosenberger, bru@brutex.de
 */
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="FileService")
public class FileService {

    @WebMethod(operationName = "basename")
    public String basename(@WebParam(name = "file") String filename,
            @WebParam(name = "suffix") String suffix) {
        return basename(new File(filename), suffix);
    }

    @WebMethod(operationName = "copy")
    public void 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 {
        try {
            copy(src, new File(todir), plm, overwrite, encoding);
        } catch (Exception ex) {
            throw new XServicesFault(ex.getMessage(), ex);
        }
    }

    @WebMethod(operationName = "loadResource")
    public String loadRes(@WebParam(name = "resource") FileResource res,
            @WebParam(name = "encoding") String encoding) {
        if (encoding == null || encoding.equals("")) {
            encoding = System.getProperty("file.encoding");
        }
        return loadResource(res, encoding);
    }

    @WebMethod(operationName = "loadResourceFromArchive")
    public String loadResFromArchive(@WebParam(name = "archiveresource") ArchiveResource res,
            @WebParam(name = "encoding") String encoding) {
        if (encoding == null || encoding.equals("")) {
            encoding = System.getProperty("file.encoding");
        }
        return loadResource(res, encoding);
    }

    @WebMethod(operationName = "echoToFile")
    public String 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);
    }

    @WebMethod(exclude = true)
    private String basename(File file,
            String suffix) {
        Basename basename = new Basename();
        RunTask runner = new RunTask(basename);
        basename.setFile(file);
        if (suffix != null && !suffix.equals("")) {
            basename.setSuffix(suffix);
        }
        basename.setProperty("basename.value");
        Map<String, String> result = runner.postTask();
        return result.get("basename.value");
    }
        @WebMethod(exclude = true)
    private String loadResource(ResourceInterface src, String encoding) {
        LoadResource lr = new LoadResource();
        lr.setTaskName("LoadResource");
        RunTask runner = new RunTask(lr);
        lr.addConfigured(src.getAntResource(lr.getProject()));
        lr.setEncoding(encoding);
        System.out.println("Using encoding: " + encoding);
        lr.setProperty("LoadResource.out");
        Map<String, String> result = runner.postTask();
        return result.get("LoadResource.out");
    }

    @WebMethod(exclude = true)
    private String 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);
        Map<String, String> result = runTask.postTask();
        return "complete";
    }

    @WebMethod(exclude=true)
    private void 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.getAntFileSet(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"));
        }

        Map<String, String> result = runner.postTask();
    }
}