www.pudn.com > sxg.rar > GatewayClient.java
/*
* Copyright (c) 2003 Jens Mueller
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
A simple commandline test client for the SNMP-XML-Gateway
@author Jens Mueller
*/
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.StringTokenizer;
import org.dom4j.io.SAXReader;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.xml.sax.InputSource;
public class GatewayClient
{
private static String encodeRequestString( String requestString )
{
StringTokenizer strtok = new StringTokenizer( requestString, "=" );
String result = "";
while ( strtok.hasMoreTokens() )
{
result += strtok.nextToken();
if ( strtok.hasMoreTokens() )
result += "%3D";
}
strtok = new StringTokenizer( result, "\"" );
result = "";
while ( strtok.hasMoreTokens() )
{
result += strtok.nextToken();
if ( strtok.hasMoreTokens() )
result += "%22";
}
strtok = new StringTokenizer( result, " " );
result = "";
while ( strtok.hasMoreTokens() )
{
result += strtok.nextToken();
if ( strtok.hasMoreTokens() )
result += "%20";
}
return result;
}
private static void performGet( String gatewayUrlString, String requestString )
{
SAXReader reader = new SAXReader();
HttpURLConnection conn = null;
reader.setValidation( false );
Document document = null;
try
{
URL requestUrl = new URL( gatewayUrlString+"?get=" +
encodeRequestString( requestString ) );
conn = ( HttpURLConnection )requestUrl.openConnection();
InputStream bytestream = conn.getInputStream();
InputStreamReader streamReader = new InputStreamReader( bytestream );
InputSource insrc = new InputSource( streamReader );
document = reader.read( insrc );
conn.disconnect();
}
catch( IOException e )
{
if ( conn != null )
{
try
{
System.out.println( "the gateway responded: "+conn.getResponseCode() +
" - "+conn.getResponseMessage() );
}
catch ( IOException ex )
{
System.out.println( "onnection to gateway failed");
}
}
else
{
System.out.println( "connection to gateway failed");
}
}
catch( DocumentException e )
{
System.out.println( "the gateway returned an unreadable XML-document");
}
try
{
OutputFormat outputformat = new OutputFormat( "", false );
XMLWriter xmlWriter = new XMLWriter( System.out, outputformat );
xmlWriter.write( document );
System.out.print( "\n" );
}
catch( Exception e )
{
System.out.println( "no data to write" );
}
}
private static void performSet( String gatewayUrlString, String path )
{
SAXReader reader = new SAXReader();
reader.setValidation( false );
HttpURLConnection conn = null;
Document document = null;
FileInputStream fInStr = null;
InputStreamReader strReader = null;
BufferedReader buffReader = null;
String xmlInputString = "";
try
{
fInStr = new FileInputStream( path );
strReader = new InputStreamReader( fInStr );
buffReader = new BufferedReader( strReader );
String currLine;
while( true )
{
currLine = buffReader.readLine();
if( currLine != null )
{
currLine = currLine.trim();
if( ! currLine.equals( "" ) )
xmlInputString += currLine.trim();
}
else
break;
}
}
catch( Exception e )
{
System.out.println( "file "+path+" not found" );
System.out.println( "no request sent" );
return;
}
try
{
StringReader stringReader = new StringReader( xmlInputString );
//strReader = new InputStreamReader( stringReader );
InputSource insrc = new InputSource( stringReader );
document = reader.read( insrc );
}
catch( Exception e )
{
System.out.println( "file "+path+" is not a valid XML-document" );
System.out.println( "no request sent" );
return;
}
try
{
//encoding
StringTokenizer strtok = new StringTokenizer( xmlInputString, "=" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%3D";
}
strtok = new StringTokenizer( xmlInputString, "\"" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%22";
}
strtok = new StringTokenizer( xmlInputString, " " );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%20";
}
strtok = new StringTokenizer( xmlInputString, "<" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%3C";
}
strtok = new StringTokenizer( xmlInputString, ">" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%3E";
}
strtok = new StringTokenizer( xmlInputString, "#" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%23";
}
strtok = new StringTokenizer( xmlInputString, "&" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%26";
}
strtok = new StringTokenizer( xmlInputString, ";" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%3B";
}
strtok = new StringTokenizer( xmlInputString, "/" );
xmlInputString = "";
while ( strtok.hasMoreTokens() )
{
xmlInputString+= strtok.nextToken();
if ( strtok.hasMoreTokens() )
xmlInputString+= "%2F";
}
xmlInputString = "%3C" + xmlInputString + "%3E";
URL requestUrl = new URL( gatewayUrlString );
conn = ( HttpURLConnection )requestUrl.openConnection();
conn.setDoOutput( true );
conn.setRequestMethod( "POST" );
conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
// Grows if necessary
ByteArrayOutputStream byteStream = new ByteArrayOutputStream( 512 );
PrintWriter out = new PrintWriter( byteStream, true );
out.print( "set="+xmlInputString );
out.flush();
// POST requests are required to have Content-Length
String lengthString = String.valueOf( byteStream.size() );
conn.setRequestProperty( "Content-Length", lengthString );
byteStream.writeTo( conn.getOutputStream() );
conn.connect();
}
catch( IllegalArgumentException e )
{
System.out.println( e.toString() );
}
catch( IOException e )
{
System.out.println( "connection to gateway failed");
}
try
{
System.out.println( "gateway responded: "+conn.getResponseCode()+
" - "+conn.getResponseMessage() );
}
catch( IOException e )
{
System.out.println("error in response");
}
}
public static void main(String[] args)
{
if ( args.length != 3 || ( args.length == 1 &&
( args[0].equals("-h") ) || ( args[0].equals("--h") ) ||
( args[0].equals("--h")) || ( args[0].equals("--help") ) ) )
{
System.out.println("usage: java GatewayClient url method");
System.out.println(" url: URL of XML-SNMP-Gateway used");
System.out.println(" method: one of");
System.out.println(" \"get\" requeststring");
System.out.println(" \"set\" file");
System.out.println("");
System.out.println("GatewayClient version 1.0, Copyright (C) 2003 Jens Mueller");
System.out.println("This software comes with ABSOLUTELY NO WARRANTY.");
System.out.println("This is free software, and you are welcome to redistribute");
System.out.println(" it under certain conditions; refer to the GNU GPL for details");
}
else
{
URL gatewayUrl = null;
String gatewayUrlString = "";
try
{
gatewayUrlString = args[ 0 ];
gatewayUrl = new URL( gatewayUrlString );
}
catch ( MalformedURLException e )
{
System.out.println( args[ 0 ] + " is not a valid url" );
return;
}
String requestType = args[ 1 ];
if ( requestType.equals( "get" ) )
{
String requestString = args[ 2 ];
performGet( gatewayUrlString, requestString );
}
else if ( requestType.equals( "set" ) )
{
String setFile = args[ 2 ];
performSet( gatewayUrlString, setFile );
}
else
{
System.out.println( "method has to be \"set\" or \"get\"");
return;
}
}
}
}