Subversion Repositories XServices

Rev

Rev 46 | Rev 54 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | 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.impl;

import java.io.File;
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.BrutexNamespaces;
import net.brutex.xservices.util.RunTask;
import net.brutex.xservices.util.UnRarTask;
import net.brutex.xservices.ws.ArchiveService;

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 = BrutexNamespaces.WS_XSERVICES,
                endpointInterface="net.brutex.xservices.ws.ArchiveService",
                serviceName = "ArchiveService")
public class ArchiveServiceImpl implements ArchiveService {


    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#bzip2(net.brutex.xservices.types.FileResource, java.lang.String)
         */
    @Override
        @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));
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#bzip2FromArchive(net.brutex.xservices.types.ArchiveResource, java.lang.String)
         */
    @Override
        @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));
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#gzip(net.brutex.xservices.types.FileResource, java.lang.String)
         */
    @Override
        @WebMethod(operationName = WS_OPERATION_GZIP, action = WS_OPERATION_GZIP)
    public ReturnCode gzip(@WebParam(name = WS_PARAM_SOURCEFILE) FileResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
        return gzip(src, new File(file));
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#gzipFromArchive(net.brutex.xservices.types.ArchiveResource, java.lang.String)
         */
    @Override
        @WebMethod(operationName = WS_OPERATION_GZIP_ARCHIVE, action = WS_OPERATION_GZIP_ARCHIVE)
    public ReturnCode gzipFromArchive(@WebParam(name = WS_PARAM_SOURCEARCHIVE) ArchiveResource src,
            @WebParam(name = WS_PARAM_DESTFILE) String file) {
        return gzip(src, new File(file));
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#gunzip(java.lang.String, java.lang.String)
         */
    @Override
        @WebMethod(operationName = WS_OPERATION_GUNZIP, action = WS_OPERATION_GUNZIP)
    public ReturnCode 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);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#bunzip2(java.lang.String, java.lang.String)
         */
    @Override
        @WebMethod(operationName = WS_OPERATION_BUNZIP2)
    public ReturnCode 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);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#gunzipFromURL(java.lang.String, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "gunzipFromURL")
    public ReturnCode 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);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#bunzip2FromURL(java.lang.String, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "bunzip2FromURL")
    public ReturnCode 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);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#zip(net.brutex.xservices.types.FileResource, java.lang.String, boolean, java.lang.String, int)
         */
    @Override
        @WebMethod(operationName = "zip")
    public ReturnCode 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);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#zipFromArchive(net.brutex.xservices.types.ArchiveResource, java.lang.String, boolean, java.lang.String, int)
         */
    @Override
        @WebMethod(operationName = "zipFromArchive")
    public ReturnCode 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);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#unzip(java.lang.String, java.lang.String, boolean, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "unzip")
    public ReturnCode 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);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#unrar(java.lang.String, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "unrar")
    public ReturnCode unrar(@WebParam(name = WS_PARAM_SOURCEFILE_STRING) String src,
            @WebParam(name = WS_PARAM_DESTDIR) String dest) {
        return unrar(new File(src), new File(dest));
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.ArchiveService#untar(java.lang.String, java.lang.String, boolean, net.brutex.xservices.types.CompressionType)
         */
    @Override
        @WebMethod(operationName = "untar")
    public ReturnCode 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);

        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode 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);
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode 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);
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode 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);
        }
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode 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);
        }
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode 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);
        }
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode 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);
        return runner.postTask();
    }

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