ws

JAX-WS files client

  • App.java
  • pom.xml

source

App.java

package com.training.jax_ws_files_client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.FileWriter;
import java.io.IOException;
import java.util.List;

import com.training.ws.FileNotFoundException_Exception;
import com.training.ws.FilesService;
import com.training.ws.FilesService_Service;
import com.training.ws.IOException_Exception;
import com.training.ws.Id;

public class App {

    public static void main(String[] args)
            throws FileNotFoundException_Exception, IOException_Exception,
            IOException {
        App app = new App();
        FilesService_Service service = new FilesService_Service();
        FilesService port = service.getFilesServiceImplPort();
        app.removeAllFiles(port);

        List<Id> fileIds = port.getFileIds();
        app.showIds(fileIds);

        File file1 = app.createFile("text1");
        File file2 = app.createFile("text2");
        File file3 = app.createFile("text3");

        app.uploadFile(port, file1);
        app.uploadFile(port, file2);
        app.uploadFile(port, file3);

        fileIds = port.getFileIds();
        app.showIds(fileIds);

        app.downloadFiles(port, fileIds);
    }

    private void removeAllFiles(FilesService port) {
        System.out.println("Remove all data.");
        for (Id id : port.getFileIds()) {
            port.deleteFile(id.getId());
        }
    }

    private static File downloadFile(FilesService port, String fileId)
            throws IOException, IOException_Exception {
        byte[] fileBytes = port.downloadFile(fileId);

        File file = File.createTempFile("myfile", ".txt");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream outputStream = new BufferedOutputStream(fos);
            outputStream.write(fileBytes);
            outputStream.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
        return file;
    }

    private void uploadFile(FilesService port, File file)
            throws FileNotFoundException_Exception, IOException_Exception {
        System.out.println("Upload file " + file.getName());
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream inputStream = new BufferedInputStream(fis);
            byte[] imageBytes = new byte[(int) file.length()];
            inputStream.read(imageBytes);

            port.uploadFile(imageBytes);

            inputStream.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }

    private void downloadFiles(FilesService port, List<Id> fileIds)
            throws IOException, IOException_Exception {
        for (Id id : fileIds) {
            File file = downloadFile(port, id.getId());
            readFile(file);
        }
    }

    private File createFile(String text) throws IOException {
        File file = File.createTempFile(text + "_", ".txt");
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(text);
        fileWriter.close();
        return file;
    }

    private void showIds(List<Id> fileIds) {
        System.out.print("Ids: ");
        for (Id id : fileIds) {
            System.out.print(id.getId() + ",");
        }
        System.out.println("");
    }

    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();
    }
}

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.training</groupId>
    <artifactId>jax_ws_files_client</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jax-ws-commons</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                        <configuration>
                            <wsdlUrls>
                                <wsdlUrl>http://localhost:9080/jax_ws_files/FilesService?wsdl</wsdlUrl>
                            </wsdlUrls>
                            <packageName></packageName>
                            <wsdlLocation>http://localhost:9080/jax_ws_files/FilesService?wsdl</wsdlLocation>
                        </configuration>
                        <id>wsimport-generate-service</id>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>javax.xml</groupId>
                        <artifactId>webservices-api</artifactId>
                        <version>2.0</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir>
                    <xnocompile>true</xnocompile>
                    <verbose>true</verbose>
                    <extension>true</extension>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>