Subversion Repositories XServices

Rev

Rev 10 | 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.CompressionType;
import net.brutex.xservices.types.FileResource;
import net.brutex.xservices.types.ResourceInterface;
import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.util.RunTask;
import net.brutex.xservices.util.UnRarTask;
import org.apache.tools.ant.taskdefs.BUnzip2;
import org.apache.tools.ant.taskdefs.BZip2;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.GUnzip;
import org.apache.tools.ant.taskdefs.GZip;
import org.apache.tools.ant.taskdefs.Untar;
import org.apache.tools.ant.taskdefs.Zip;

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

    public static final String WS_OPERATION_BZIP2 = "bzip2";
    public static final String WS_OPERATION_BZIP2_ARCHIVE = "bzip2FromArchive";
    public static final String WS_OPERATION_GZIP = "gzip";
    public static final String WS_OPERATION_GZIP_ARCHIVE = "gzipFromArchive";
    public static final String WS_OPERATION_UNZIP = "unzip";
    public static final String WS_OPERATION_GUNZIP = "gunzip";
    public static final String WS_OPERATION_BUNZIP2 = "bunzip2";

    public static final String WS_PARAM_SOURCEFILE = "source";
    public static final String WS_PARAM_SOURCEFILE_STRING = "srcfile";
    public static final String WS_PARAM_SOURCEURL = "srcurl";
    public static final String WS_PARAM_SOURCEARCHIVE = "archivesource";
    public static final String WS_PARAM_DESTFILE = "destfile";
    public static final String WS_PARAM_DESTDIR = "destdir";

    public static final String WS_PARAM_ENCODING = "encoding";
    public static final String WS_PARAM_OVERWRITE= "overwrite";

    @WebMethod(operationName = WS_OPERATION_BZIP2, action=WS_OPERATION_BZIP2)
    public ReturnCode bzip2(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
        return bzip(src, new File(file));
    }

    @WebMethod(operationName = WS_OPERATION_BZIP2_ARCHIVE, action=WS_OPERATION_BZIP2_ARCHIVE)
    public ReturnCode bzip2FromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
        return bzip(src, new File(file));
    }

    @WebMethod(operationName = WS_OPERATION_GZIP, action=WS_OPERATION_GZIP)
    public String gzip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
        return gzip(src, new File(file));
    }

    @WebMethod(operationName = WS_OPERATION_GZIP_ARCHIVE, action=WS_OPERATION_GZIP_ARCHIVE)
    public String gzipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
        return gzip(src, new File(file));
    }

    @WebMethod(operationName = WS_OPERATION_GUNZIP, action=WS_OPERATION_GUNZIP)
    public String gunzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
        File target = null;
        if (!dest.equals("") && dest != null) {
            target = new File(dest);
        }
        return GUnzip(new FileResource(FileResource.Type.FILE, src), target);
    }

    @WebMethod(operationName = WS_OPERATION_BUNZIP2)
    public String bunzip2(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
        File target = null;
        if (!dest.equals("") && dest != null) {
            target = new File(dest);
        }
        return BUnzip2(new FileResource(FileResource.Type.FILE, src), target);
    }

    @WebMethod(operationName = "gunzipFromURL")
    public String gunzipFromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
        File target = null;
        if (!dest.equals("") && dest != null) {
            target = new File(dest);
        }
        return GUnzip(new FileResource(FileResource.Type.URL, src), target);
    }

    @WebMethod(operationName = "bunzip2FromURL")
    public String bunzip2FromURL(@WebParam(name = WS_PARAM_SOURCEURL) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
        File target = null;
        if (!dest.equals("") && dest != null) {
            target = new File(dest);
        }
        return BUnzip2(new FileResource(FileResource.Type.URL, src), target);
    }

    @WebMethod(operationName = "zip")
    public String zip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file,
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
            @WebParam(name = "compresslevel") int level) {
        if (level > 9) {
            level = 9;
        }
        if (level < 0) {
            level = 0;
        }
        return zip(src, new File(file), encoding, !overwrite, level);
    }

    @WebMethod(operationName = "zipFromArchive")
    public String zipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file,
            @WebParam(name = WS_PARAM_OVERWRITE) boolean update,
            @WebParam(name = WS_PARAM_ENCODING) String encoding,
            @WebParam(name = "compresslevel") int level) {
        return zip(src, new File(file), encoding, !update, level);
    }


    @WebMethod(operationName = "unzip")
    public String unzip(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
            @WebParam(name = WS_PARAM_ENCODING) String encoding) {
        return unzip(new File(src), new File(dest), overwrite, encoding);
    }

    @WebMethod(operationName = "unrar")
    public String unrar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
        return unrar(new File(src), new File(dest));
    }

    @WebMethod(operationName = "untar")
    public String untar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest,
            @WebParam(name = WS_PARAM_OVERWRITE) boolean overwrite,
            @WebParam(name = "compression") CompressionType compression) {
        Untar.UntarCompressionMethod c = new Untar.UntarCompressionMethod();
        switch (compression) {
            case GZIP:
                c.setValue("gzip");
                break;
            case BZIP2:
                c.setValue("bzip2");
                break;
            default:
                c.setValue("none");
                break;
        }
        return untar(new File(src), new File(dest), overwrite, c);
    }

    @WebMethod(exclude = true)
    private ReturnCode bzip(ResourceInterface src, File dst) {
        if (dst.exists() && dst.isFile()) {
            dst.delete();
        }
        BZip2 bzip = new BZip2();
        bzip.setTaskName("BZip2");
        RunTask runner = new RunTask(bzip);
        bzip.setSrcResource(src.getAntResource(bzip.getProject()));
        bzip.setDestfile(dst);

        Map<String, String> result = runner.postTask();
        return new ReturnCode(0,result.get("System.stdOut"),result.get("System.stdErr"));
    }

    @WebMethod(exclude = true)
    private String gzip(ResourceInterface src, File dst) {
        if (dst.exists() && dst.isFile()) {
            dst.delete();
        }
        GZip gzip = new GZip();
        gzip.setTaskName("GZip");
        RunTask runner = new RunTask(gzip);
        gzip.addConfigured(src.getAntResource(gzip.getProject()));
        gzip.setDestfile(dst);
        Map<String, String> result = runner.postTask();
        return "complete";
    }



    @WebMethod(exclude = true)
    private String zip(ResourceInterface src, File dst, String encoding, boolean update, int compresslevel) {
        Zip zip = new Zip();
        zip.setTaskName("Zip");
        RunTask runner = new RunTask(zip);
        zip.add(src.getAntResource(zip.getProject()));
        zip.setDestFile(dst);
        if (encoding != null && !encoding.equals("")) {
            zip.setEncoding(encoding);
        }
        zip.setUpdate(update);
        zip.setLevel(compresslevel);
        Map<String, String> result = runner.postTask();
        return "complete";
    }

    @WebMethod(exclude = true)
    private String GUnzip(ResourceInterface src, File dst) {
        GUnzip uz = new GUnzip();
        uz.setTaskName("GUnzip");
        RunTask runner = new RunTask(uz);
        uz.setSrcResource(src.getAntResource(uz.getProject()));
        if (dst != null) {
            uz.setDest(dst);
        }
        Map<String, String> result = runner.postTask();
        return "complete";
    }

    @WebMethod(exclude = true)
    private String BUnzip2(ResourceInterface src, File dst) {
        BUnzip2 uz = new BUnzip2();
        uz.setTaskName("BUnzip2");
        RunTask runner = new RunTask(uz);
        uz.setSrcResource(src.getAntResource(uz.getProject()));
        if (dst != null) {
            uz.setDest(dst);
        }
        Map<String, String> result = runner.postTask();
        return "complete";
    }

    @WebMethod(exclude = true)
    private String unzip(File src, File dest, boolean overwrite, String encoding) {
        Expand unzip = new Expand();
        unzip.setTaskName("UnZip");
        RunTask runner = new RunTask(unzip);
        unzip.setSrc(src);
        unzip.setDest(dest);
        unzip.setOverwrite(overwrite);
        if (encoding != null && !encoding.equals("")) {
            unzip.setEncoding(encoding);
        }

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

    @WebMethod(exclude = true)
    private String untar(File src, File dest, boolean overwrite, Untar.UntarCompressionMethod compression) {
        Untar unzip = new Untar();
        unzip.setTaskName("Untar");
        RunTask runner = new RunTask(unzip);
        unzip.setSrc(src);
        unzip.setDest(dest);
        unzip.setOverwrite(overwrite);
        unzip.setCompression(compression);
        Map<String, String> result = runner.postTask();
        return "complete";
    }

    @WebMethod(exclude = true)
    private String unrar(File src, File dst) {
        UnRarTask unrar = new UnRarTask();
        unrar.setTaskName("UnRar");
        RunTask runner = new RunTask(unrar);
        unrar.setSrc(src);
        unrar.setDst(dst);
        Map<String, String> result = runner.postTask();
        return "complete";
    }

   
}