/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package demo.hw.client;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public final class Get {

    private Get() {
    }

    public static void main(String[] args) throws Exception {
        // Sent HTTP GET request to invoke sayHi
        String target = "http://localhost:9000/SoapContext/SoapPort/sayHi";
        URL url = new URL(target);
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.connect();
        System.out.println("Invoking server through HTTP GET to invoke sayHi");

        InputStream in = httpConnection.getInputStream();
        try {
            StreamSource source = new StreamSource(in);
            printSource(source);
        } finally {
            if (in != null) {
                in.close();
            }
        }

        // Sent HTTP GET request to invoke greetMe FAULT
        target = "http://localhost:9000/SoapContext/SoapPort/greetMe/me/CXF";
        url = new URL(target);
        httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.connect();
        System.out.println("Invoking server through HTTP GET to invoke greetMe");

        try {
            in = httpConnection.getInputStream();
            try {
                StreamSource source = new StreamSource(in);
                printSource(source);
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } catch (Exception e) {
            System.err.println("GreetMe Fault: " + e.getMessage());
        }
        InputStream err = httpConnection.getErrorStream();
        if (err != null) {
            try {
                StreamSource source = new StreamSource(err);
                printSource(source);
            } finally {
                err.close();
            }
        }

        // Sent HTTP GET request to invoke greetMe
        target = "http://localhost:9000/SoapContext/SoapPort/greetMe/requestType/CXF";
        url = new URL(target);
        httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.connect();
        System.out.println("Invoking server through HTTP GET to invoke greetMe");

        in = httpConnection.getInputStream();
        try {
            StreamSource source = new StreamSource(in);
            printSource(source);
        } finally {
            if (in != null) {
                in.close();
            }
        }

        // Sent HTTP GET request to invoke pingMe
        target = "http://localhost:9000/SoapContext/SoapPort/pingMe";
        url = new URL(target);
        httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.connect();
        System.out.println("Invoking server through HTTP GET to invoke pingMe");

        try {
            in = httpConnection.getInputStream();
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            System.out.println("PingMe fault raised");
        }
        err = httpConnection.getErrorStream();
        if (err != null) {
            try {
                StreamSource source = new StreamSource(err);
                printSource(source);
            } finally {
                err.close();
            }
        }
    }

    private static void printSource(Source source) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            StreamResult sr = new StreamResult(bos);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
            Transformer transformer = transformerFactory.newTransformer();
            Properties oprops = new Properties();
            oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperties(oprops);
            transformer.transform(source, sr);
            System.out.println();
            System.out.println("**** Response ******");
            System.out.println();
            System.out.println(bos.toString());
            bos.close();
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
