Package sos.spooler.jobs

Source Code of sos.spooler.jobs.Test_web_service

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
// $Id$
/**
*
* @author Joacim Zschimmer
* @version $Revision$  $Date$
* Created on 19.01.2006
*
*/
package sos.spooler.jobs;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import sos.spooler.jobs.Web_service_forwarder.Order_exception;



/**
* @author Joacim Zschimmer
* @version $Revision: 3946 $
*/


public class Test_web_service  extends sos.spooler.Job_impl
{
    //private String              current_url_string = null;
    public final String encoding = "utf-8"; //"iso-8859-1";
           

    //---------------------------------------------------------------------------------------------
   
    static String string_from_reader( Reader reader throws IOException
    {
        StringBuffer result = new StringBuffer( 10000 );
        char[]       buffer = new char[ 1024 ];
       
        while(true)
        {
            int len = reader.read( buffer );
            if( len <= 0 break;
            result.append( buffer, 0, len );
        }
       
        return result.toString();
    }
   
    //---------------------------------------------------------------------------------------------
   
    public Test_web_service()  throws Exception
    {
    }
   
    //---------------------------------------------------------------------------------------------

    public boolean spooler_process()  throws Exception
    {
        Document document                = dom_from_xml( (String)spooler_task.order().payload() );
        Element  service_request_element = document.getDocumentElement();
        Element  content_element         = null;
       
        if( service_request_element == null throw new Exception( "<service_request> fehlt" );
       
        URL url = new URL( service_request_element.getAttribute( "url" ) );
       
        for( Node node = service_request_element.getFirstChild();; node = node.getNextSibling() )
        {
            if( node.getNodeType() == Node.ELEMENT_NODE  &&  node.getNodeName().equals( "content" ) )
            {
                content_element = (Element)node;
                break;
            }
        }

        if( content_element == null throw new Exception( "<content> fehlt" );

        //URL url = new URL( "http://localhost:" + spooler.tcp_port() + "/webdienst" );
           

        HttpURLConnection http_connection = null;
       
        try
        {
            spooler_log.info( "url=" + url );
           
            URLConnection connection = url.openConnection();
            if( !( connection instanceof HttpURLConnection ) )  throw new Exception( "Nur HTTP-URLs werden akzeptiert: url=" + url );

            http_connection = (HttpURLConnection)connection;
            http_connection.setRequestMethod( "POST" );
            http_connection.setRequestProperty( "Content-Type", "text/xml; charset=" + encoding )//TODO scheduler_http.cxx muss charset verstehen!
            http_connection.setDoOutput( true );
            http_connection.connect();

           
            OutputStream        output_stream = http_connection.getOutputStream();
            //OutputStreamWriter  writer        = new OutputStreamWriter( output_stream, encoding );

            Element data_element = get_data_element( service_request_element );
            new XMLSerializer( output_stream, new OutputFormat( service_request_element.getOwnerDocument(), encoding, false ) )
            .serialize( data_element );
           
            //writer.write( "<?xml version='1.0'?>\n" );
            //writer.write( "<my_request>bla äöü </my_request>\n" );
            //writer.flush();
            output_stream.flush();
           

            int response_code = http_connection.getResponseCode();
           
            if( response_code != HttpURLConnection.HTTP_OK
             && response_code != HttpURLConnection.HTTP_ACCEPTED )
                throw new Exception( http_connection.getResponseMessage() );
           
            //Object response = http_connection.getContent();
            spooler_log.info( string_from_reader( new InputStreamReader( http_connection.getInputStream(), encoding ) ) );       
        }
        finally
        {
            if( http_connection != null http_connection.disconnect();
        }

        return false;
    }
   
    //---------------------------------------------------------------------------------------------
   
    private static Document dom_from_xml( String xml throws Exception
    {
        DOMParser parser = new DOMParser();
        parser.parse( new org.xml.sax.InputSource( new StringReader( xml ) ) );
           
        return parser.getDocument();
    }

    //-----------------------------------------------------------------------------------------
   
    Element get_data_element( Element service_request_element throws Exception
    {
        Element content_element = element_after_comment_and_empty_text( service_request_element.getFirstChild() );
        if( content_element == null throw new Exception( "<service_request> ohne <content>" );
       
        Element data_element = element_after_comment_and_empty_text( content_element.getFirstChild() );
        if( data_element == null throw new Exception( "<content> ist leer" );
        if( element_after_comment_and_empty_text( data_element.getNextSibling() ) != null throw new Exception( "<content> enthält mehr als einen Knoten" );
       
        //spooler_log.debug3( "data_node=" + data_node );
       
        return data_element;
    }       
   
    //-----------------------------------------------------------------------------------------
   
    Element element_after_comment_and_empty_text( Node node throws Order_exception
    {
        for(; node != null; node = node.getNextSibling() )
        {
            if( node.getNodeType() == Node.COMMENT_NODE continue;

            if( ( node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE )
                &&  node.getNodeValue().trim().equals( "" ) )  continue;
           
            break;
        }
       
        if( node != null  &&  node.getNodeType() != Node.ELEMENT_NODE throw new Order_exception( "Unzulässiger Knoten: " + node );
       
        return (Element)node;
    }

    //---------------------------------------------------------------------------------------------
}
TOP

Related Classes of sos.spooler.jobs.Test_web_service

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.