Subversion Repositories XServices

Rev

Rev 6 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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