Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 brianR 1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.xservices.ws;
17
 
18
import java.io.File;
19
import javax.jws.WebMethod;
20
import javax.jws.WebParam;
21
import javax.jws.WebService;
22
import net.brutex.xservices.types.ArchiveResource;
23
import net.brutex.xservices.types.FileResource;
24
import net.brutex.xservices.types.FileSetResource;
25
import net.brutex.xservices.types.ResourceInterface;
12 brianR 26
import net.brutex.xservices.types.ReturnCode;
6 brianR 27
import net.brutex.xservices.util.RunTask;
28
import org.apache.tools.ant.taskdefs.Basename;
29
import org.apache.tools.ant.taskdefs.Copy;
30
import org.apache.tools.ant.taskdefs.Echo;
31
import org.apache.tools.ant.taskdefs.LoadResource;
12 brianR 32
import org.apache.tools.ant.taskdefs.optional.unix.Chgrp;
33
import org.apache.tools.ant.taskdefs.optional.unix.Chown;
6 brianR 34
import org.apache.tools.ant.types.FileSet;
35
 
36
/**
37
 *
38
 * @author Brian Rosenberger, bru@brutex.de
39
 */
12 brianR 40
@WebService(targetNamespace = "http://ws.xservices.brutex.net", name = "FileService")
6 brianR 41
public class FileService {
42
 
43
    @WebMethod(operationName = "basename")
12 brianR 44
    public ReturnCode basename(@WebParam(name = "file") String filename,
6 brianR 45
            @WebParam(name = "suffix") String suffix) {
46
        return basename(new File(filename), suffix);
47
    }
48
 
49
    @WebMethod(operationName = "copy")
12 brianR 50
    public ReturnCode copy(@WebParam(name = "fileset") FileSetResource src,
51
            @WebParam(name = "todir") String todir,
52
            @WebParam(name = "preservelastmodified") boolean plm,
53
            @WebParam(name = "overwrite") boolean overwrite,
54
            @WebParam(name = "encoding") String encoding)
55
            throws XServicesFault {
56
        return copy(src, new File(todir), plm, overwrite, encoding);
6 brianR 57
    }
58
 
59
    @WebMethod(operationName = "loadResource")
12 brianR 60
    public ReturnCode loadRes(@WebParam(name = "resource") FileResource res,
6 brianR 61
            @WebParam(name = "encoding") String encoding) {
62
        if (encoding == null || encoding.equals("")) {
63
            encoding = System.getProperty("file.encoding");
64
        }
65
        return loadResource(res, encoding);
66
    }
67
 
68
    @WebMethod(operationName = "loadResourceFromArchive")
12 brianR 69
    public ReturnCode loadResFromArchive(@WebParam(name = "archiveresource") ArchiveResource res,
6 brianR 70
            @WebParam(name = "encoding") String encoding) {
71
        if (encoding == null || encoding.equals("")) {
72
            encoding = System.getProperty("file.encoding");
73
        }
74
        return loadResource(res, encoding);
75
    }
76
 
77
    @WebMethod(operationName = "echoToFile")
12 brianR 78
    public ReturnCode echo2file(@WebParam(name = "message") String message,
79
            @WebParam(name = "file") String file, @WebParam(name = "encoding") String encoding,
80
            @WebParam(name = "append") boolean append) {
6 brianR 81
        return echo(message, new File(file), encoding, append);
82
    }
83
 
12 brianR 84
    @WebMethod(operationName = "changeOwner")
85
    public ReturnCode changeOwner(@WebParam(name = "fileset") FileSetResource res,
86
            @WebParam(name = "owner") String owner) {
87
        return chown(res, owner);
88
    }
89
 
90
    @WebMethod(operationName = "changeGroup")
91
    public ReturnCode changeGroup(@WebParam(name = "fileset") FileSetResource res,
92
            @WebParam(name = "group") String group) {
93
        return chgrp(res, group);
94
    }
95
 
6 brianR 96
    @WebMethod(exclude = true)
12 brianR 97
    private ReturnCode basename(File file,
6 brianR 98
            String suffix) {
99
        Basename basename = new Basename();
100
        RunTask runner = new RunTask(basename);
101
        basename.setFile(file);
102
        if (suffix != null && !suffix.equals("")) {
103
            basename.setSuffix(suffix);
104
        }
105
        basename.setProperty("basename.value");
12 brianR 106
        return runner.postTask();
6 brianR 107
    }
12 brianR 108
 
109
    @WebMethod(exclude = true)
110
    private ReturnCode loadResource(ResourceInterface src, String encoding) {
6 brianR 111
        LoadResource lr = new LoadResource();
112
        lr.setTaskName("LoadResource");
113
        RunTask runner = new RunTask(lr);
114
        lr.addConfigured(src.getAntResource(lr.getProject()));
115
        lr.setEncoding(encoding);
116
        System.out.println("Using encoding: " + encoding);
117
        lr.setProperty("LoadResource.out");
12 brianR 118
        return runner.postTask();
6 brianR 119
    }
120
 
121
    @WebMethod(exclude = true)
12 brianR 122
    private ReturnCode echo(String msg, File file, String encoding, boolean append) {
6 brianR 123
        Echo echo = new Echo();
124
        echo.setTaskName("toFile");
125
        RunTask runTask = new RunTask(echo);
126
        echo.addText(msg);
127
        echo.setEncoding(encoding);
128
        echo.setFile(file);
129
        echo.setAppend(append);
12 brianR 130
        return runTask.postTask();
6 brianR 131
    }
132
 
12 brianR 133
    @WebMethod(exclude = true)
134
    private ReturnCode copy(FileSetResource src, File dst, boolean preservelastmodified,
6 brianR 135
            boolean overwrite, String encoding) {
136
        Copy copy = new Copy();
137
        copy.setTaskName("Copy");
138
        RunTask runner = new RunTask(copy);
139
        FileSet set = src.getAntFileSet(copy.getProject());
140
        copy.add(set);
141
 
12 brianR 142
        if (dst.isDirectory()) {
143
            copy.setTodir(dst);
144
        }
145
        if (dst.isFile()) {
146
            copy.setTofile(dst);
147
        }
6 brianR 148
        copy.setOverwrite(overwrite);
149
        copy.setPreserveLastModified(preservelastmodified);
12 brianR 150
        if (encoding != null && !encoding.equals("")) {
6 brianR 151
            copy.setOutputEncoding(encoding);
152
        } else {
153
            copy.setOutputEncoding(System.getProperty("file.encoding"));
154
        }
155
 
12 brianR 156
        return runner.postTask();
6 brianR 157
    }
12 brianR 158
 
159
    @WebMethod(exclude = true)
160
    private ReturnCode chown(FileSetResource src, String owner) {
161
        Chown chown = new Chown();
162
        chown.setTaskName("Chown");
163
        RunTask runner = new RunTask(chown);
164
        chown.setOwner(owner);
165
        FileSet set = src.getAntFileSet(chown.getProject());
166
        chown.add(set);
167
        return runner.postTask();
168
    }
169
 
170
    @WebMethod(exclude = true)
171
    private ReturnCode chgrp(FileSetResource src, String group) {
172
        Chgrp chgrp = new Chgrp();
173
        chgrp.setTaskName("Chgrp");
174
        RunTask runner = new RunTask(chgrp);
175
        chgrp.setGroup(group);
176
        FileSet set = src.getAntFileSet(chgrp.getProject());
177
        chgrp.add(set);
178
        return runner.postTask();
179
    }
6 brianR 180
}