FileService.java
package com.training.ws;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
@MTOM
@WebService
public interface FilesService {
@WebMethod
Collection<Id> getFileIds();
@WebMethod
byte[] downloadFile(String id) throws IOException;
@WebMethod
String uploadFile(byte[] bytes) throws FileNotFoundException, IOException;
@WebMethod
void deleteFile(String id);
}
FileServiceImpl.java
package com.training.ws;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
@MTOM
@WebService(endpointInterface = "com.training.ws.FilesService", serviceName = "FilesService")
public class FilesServiceImpl implements FilesService {
private static int lastId = 0;
private Map<String, File> files = new HashMap<String, File>();
public Collection<Id> getFileIds() {
return transformToIdObject();
}
public byte[] downloadFile(String id) throws IOException {
File file = files.get(id);
return readContentIntoBytes(file);
}
public String uploadFile(byte[] bytes) throws FileNotFoundException,
IOException {
String generatedId = String.valueOf(++lastId);
System.out.println("read file: " + generatedId);
File file = File.createTempFile("myfile", ".txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(bytes);
fileOutputStream.close();
files.put(generatedId, file);
readFile(file);
return generatedId;
}
public void deleteFile(String id) {
files.remove(id);
}
private Collection<Id> transformToIdObject() {
return Collections2.transform(files.keySet(),
new Function<String, Id>() {
public Id apply(String id) {
return new Id(id);
}
});
}
private void readFile(File file) throws FileNotFoundException, IOException {
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
fileReader.close();
}
private byte[] readContentIntoBytes(File file) throws IOException {
byte[] bFile = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
return bFile;
}
}
Id.java
package com.training.ws;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Id {
private String id;
public Id() {
}
public Id(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.training</groupId>
<artifactId>jax_ws_files</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
</project>