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
 
17
package net.brutex.xservices.ws;
18
 
19
import java.io.File;
20
import java.util.Map;
21
import javax.jws.WebMethod;
22
import javax.jws.WebParam;
23
import javax.jws.WebService;
24
import net.brutex.xservices.types.ArchiveResource;
25
import net.brutex.xservices.types.FileResource;
26
import net.brutex.xservices.types.FileSetResource;
27
import net.brutex.xservices.types.ResourceInterface;
28
import net.brutex.xservices.util.RunTask;
29
import org.apache.tools.ant.taskdefs.Basename;
30
import org.apache.tools.ant.taskdefs.Copy;
31
import org.apache.tools.ant.taskdefs.Echo;
32
import org.apache.tools.ant.taskdefs.LoadResource;
33
import org.apache.tools.ant.types.FileSet;
34
 
35
/**
36
 *
37
 * @author Brian Rosenberger, bru@brutex.de
38
 */
39
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="FileService")
40
public class FileService {
41
 
42
    @WebMethod(operationName = "basename")
43
    public String basename(@WebParam(name = "file") String filename,
44
            @WebParam(name = "suffix") String suffix) {
45
        return basename(new File(filename), suffix);
46
    }
47
 
48
    @WebMethod(operationName = "copy")
49
    public void copy(@WebParam(name="fileset") FileSetResource src,
50
            @WebParam(name="todir") String todir,
51
            @WebParam(name="preservelastmodified") boolean plm,
52
            @WebParam(name="overwrite") boolean overwrite,
53
            @WebParam(name="encoding") String encoding)
54
    throws XServicesFault {
55
        try {
56
            copy(src, new File(todir), plm, overwrite, encoding);
57
        } catch (Exception ex) {
58
            throw new XServicesFault(ex.getMessage(), ex);
59
        }
60
    }
61
 
62
    @WebMethod(operationName = "loadResource")
63
    public String loadRes(@WebParam(name = "resource") FileResource res,
64
            @WebParam(name = "encoding") String encoding) {
65
        if (encoding == null || encoding.equals("")) {
66
            encoding = System.getProperty("file.encoding");
67
        }
68
        return loadResource(res, encoding);
69
    }
70
 
71
    @WebMethod(operationName = "loadResourceFromArchive")
72
    public String loadResFromArchive(@WebParam(name = "archiveresource") ArchiveResource res,
73
            @WebParam(name = "encoding") String encoding) {
74
        if (encoding == null || encoding.equals("")) {
75
            encoding = System.getProperty("file.encoding");
76
        }
77
        return loadResource(res, encoding);
78
    }
79
 
80
    @WebMethod(operationName = "echoToFile")
81
    public String echo2file(@WebParam(name="message") String message,
82
            @WebParam(name="file")String file, @WebParam(name="encoding") String encoding,
83
            @WebParam(name="append")boolean append) {
84
        return echo(message, new File(file), encoding, append);
85
    }
86
 
87
    @WebMethod(exclude = true)
88
    private String basename(File file,
89
            String suffix) {
90
        Basename basename = new Basename();
91
        RunTask runner = new RunTask(basename);
92
        basename.setFile(file);
93
        if (suffix != null && !suffix.equals("")) {
94
            basename.setSuffix(suffix);
95
        }
96
        basename.setProperty("basename.value");
97
        Map<String, String> result = runner.postTask();
98
        return result.get("basename.value");
99
    }
100
        @WebMethod(exclude = true)
101
    private String loadResource(ResourceInterface src, String encoding) {
102
        LoadResource lr = new LoadResource();
103
        lr.setTaskName("LoadResource");
104
        RunTask runner = new RunTask(lr);
105
        lr.addConfigured(src.getAntResource(lr.getProject()));
106
        lr.setEncoding(encoding);
107
        System.out.println("Using encoding: " + encoding);
108
        lr.setProperty("LoadResource.out");
109
        Map<String, String> result = runner.postTask();
110
        return result.get("LoadResource.out");
111
    }
112
 
113
    @WebMethod(exclude = true)
114
    private String echo(String msg, File file, String encoding, boolean append) {
115
        Echo echo = new Echo();
116
        echo.setTaskName("toFile");
117
        RunTask runTask = new RunTask(echo);
118
        echo.addText(msg);
119
        echo.setEncoding(encoding);
120
        echo.setFile(file);
121
        echo.setAppend(append);
122
        Map<String, String> result = runTask.postTask();
123
        return "complete";
124
    }
125
 
126
    @WebMethod(exclude=true)
127
    private void copy(FileSetResource src, File dst, boolean preservelastmodified,
128
            boolean overwrite, String encoding) {
129
        Copy copy = new Copy();
130
        copy.setTaskName("Copy");
131
        RunTask runner = new RunTask(copy);
132
        FileSet set = src.getAntFileSet(copy.getProject());
133
        copy.add(set);
134
 
135
        if(dst.isDirectory()) copy.setTodir(dst);
136
        if(dst.isFile()) copy.setTofile(dst);
137
        copy.setOverwrite(overwrite);
138
        copy.setPreserveLastModified(preservelastmodified);
139
        if(encoding!=null && !encoding.equals("") ) {
140
            copy.setOutputEncoding(encoding);
141
        } else {
142
            copy.setOutputEncoding(System.getProperty("file.encoding"));
143
        }
144
 
145
        Map<String, String> result = runner.postTask();
146
    }
147
}