www.pudn.com > sxg.rar > GatewayGetRequestFactory.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.
 */

/**
 	Provides static method getGatewayGetRequest for converting a 
 	request-string (a XPath-expression) into the accordant application's data structure
 	GatewayGetRequest
 
 	@author Jens Mueller
 */

import java.util.StringTokenizer;
import java.util.regex.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.dom4j.xpath.DefaultXPath;
import org.dom4j.InvalidXPathException;

public class GatewayGetRequestFactory
{
	
	/**
	 	converts the request-string into an instance of GatewayGetRequest  
	 	@param req   the request-string 	 	@return GatewayGetRequest 	 	@throws MalformedRequestStringException 	 */
	public static GatewayGetRequest getGatewayGetRequest( String req ) 
						throws MalformedRequestStringException
	{
		GatewayGetRequest gwReq;

/*		
		try
		{
			DefaultXPath defXPath = new DefaultXPath( req );
		}
		catch ( InvalidXPathException e )
		{
			throw new MalformedRequestStringException ( "not a valid XPath" );
		}
*/
		
		// check rootelement
		StringTokenizer strtok = new StringTokenizer( req, "/" );
		if ( strtok.countTokens() > 0 )
		{
			String token = strtok.nextToken();
			if ( token.equals( GatewayConf.GET_ROOTELEMENT ) )
			{
				gwReq = new GatewayDataRequest(); 
			}
			else
			{
				throw new MalformedRequestStringException( "not a valid Request-String. Specify \""
																					+ GatewayConf.GET_ROOTELEMENT
																					+ "\" as rootelement" );
			}
		}
		else
		{
			throw new MalformedRequestStringException("not a valid Request-String");
		}
		
		// build context
		String[] contexts = req.split( GatewayConf.GET_AGENTELEMENT );

		// starting j = 1 to jump prefix /snmp-data/
		for ( int j = 1; j < contexts.length; j++ )
		{
			try
			{
				gwReq.getContextReqVector().add( getContextRequest( contexts[ j ] ) );
			}
			catch ( MalformedRequestStringException e )
			{
				throw new MalformedRequestStringException( e.getMessage() );
			}
		}
		return gwReq;
	}
	
	
	
	private static ContextGetRequest getContextRequest( String contextRequestString )
						throws MalformedRequestStringException
	{		
		contextRequestString = contextRequestString.trim();
		
		if ( contextRequestString.charAt( 0 ) != '[' )
		{
			throw new MalformedRequestStringException( 	"not a valid Request-String: " + 
																				contextRequestString );				
		} 
		
		int i = 0;
		ContextGetRequest conReq = new ContextGetRequest();
			 
		while ( i < contextRequestString.length() && contextRequestString.charAt( i ) != ']' )
		{
			i++;
		}
		String contextString = contextRequestString.substring( 1, i );
			
		StringTokenizer contextAttrTok = new StringTokenizer( contextString, " " );
		boolean matched = false;
		while ( contextAttrTok.hasMoreTokens() )
		{
			matched = false;
			String contextAttrStr = contextAttrTok.nextToken();
			
			if ( !contextAttrStr.trim().toUpperCase().equals( "AND" ) )
			{			
			StringTokenizer contextAttrStrTok = new StringTokenizer( contextAttrStr, "=" );
			if ( contextAttrStrTok.countTokens() != 2 )
			{
				throw new MalformedRequestStringException( 	"not a valid Request-String," 
																					+ " context-attribute "
																					+ contextAttrStr
																					+ " invalid" );
			}
			
			String AttrValue = "";
			String Attribute = contextAttrStrTok.nextToken();

			if ( Attribute.equals( GatewayConf.GET_IPADDR ) )
			{
				matched = true;
				AttrValue = contextAttrStrTok.nextToken();
				if ( AttrValue.charAt( 0 ) == '\"' && AttrValue.charAt( AttrValue.length()-1 ) == '\"' )
				{
					conReq.setIPaddr( AttrValue.substring( 1, AttrValue.length()-1 ) );
				}
				else
				{
					throw new MalformedRequestStringException(   "not a valid Request-String " 																					  + " ipaddr-attribute " + AttrValue
																					  + " invalid" );
				}
			}

			if ( Attribute.equals( GatewayConf.GET_HOSTNAME ) )
			{
				matched = true;
				AttrValue = contextAttrStrTok.nextToken();
				if ( AttrValue.charAt( 0 ) == '\"' && AttrValue.charAt( AttrValue.length()-1 ) == '\"' )
				{
					try
					{
						conReq.setInetAddress( InetAddress.getByName( 
																		AttrValue.substring( 1, AttrValue.length() - 1 )) );
					}
					catch (Exception e)
					{
						throw new MalformedRequestStringException( "not a valid Request-String,"
																							+ " resolving"
																							+ " hostname returned: "
																							+ e.getMessage() );
					}
					catch (Throwable th)
					{
						throw new GatewayError( "Error while resolving hostname: "
																+ AttrValue.substring( 1, AttrValue.length() - 1 ) );
					}
				}
				else
				{
					throw new MalformedRequestStringException( "not a valid Request-String,"
																						+ " hostname-attribute "
																						+ AttrValue
																						+ " invalid" );
				}
			}
			
			if ( Attribute.equals( GatewayConf.GET_PORT ) )
			{
				matched = true;
				AttrValue = contextAttrStrTok.nextToken();
				if ( AttrValue.charAt( 0 ) == '\"' && AttrValue.charAt( AttrValue.length()-1 ) == '\"' )
				{
					try
					{
						int port = Integer.parseInt( AttrValue.substring( 1, AttrValue.length() - 1 ) );
						if ( port < 1 || port > 65535 )
							throw new MalformedRequestStringException(  "invalid port value" );
						conReq.setPort( port );
					}
					catch (Exception e)
					{
						throw new MalformedRequestStringException(  "not a valid Request-String,"
																							+ " port-attribute "
																							+ AttrValue
																							+ " invalid" ); 
					}
				}
				else
				{
					throw new MalformedRequestStringException( "not a valid Request-String,"
																						+ " port-attribute "
																						+ AttrValue
																						+ " invalid" );
				}
			}

			if ( Attribute.equals( GatewayConf.GET_COMM_STR ) )
			{
				matched = true;
				AttrValue = contextAttrStrTok.nextToken();
				if ( AttrValue.charAt( 0 ) == '\"' && AttrValue.charAt( AttrValue.length() - 1 ) == '\"' )
				{
					conReq.setCommString( AttrValue.substring( 1, AttrValue.length() - 1 ) );
				}
				else
				{
					throw new MalformedRequestStringException( "not a valid Request-String,"
																						+ " community-attribute "
																						+ AttrValue
																						+ " invalid" );
				}
			}

			if ( Attribute.equals( GatewayConf.GET_CACHING ) )
			{
				matched = true;
				AttrValue = contextAttrStrTok.nextToken();
				if ( AttrValue.charAt( 0 ) == '\"' && AttrValue.charAt( AttrValue.length() - 1 ) == '\"' )
				{
					String cachingRequest = AttrValue.substring( 1, AttrValue.length() - 1 );
					if ( cachingRequest.toUpperCase().equals("NO") )
					{
						conReq.setCaching( false );
					}
					else if ( ! cachingRequest.toUpperCase().equals("YES") )
					{
						throw new MalformedRequestStringException( "not a valid Request-String."
																							+ " Value of caching attribute \""
																							+ cachingRequest +"\" has to be"
																							+ "\"yes\" or \"no\"" );
					}
				}
				else
				{
					throw new MalformedRequestStringException( "not a valid Request-String,"
																						+ " context-attribute "
																						+ AttrValue
																						+ " invalid" );
				}
			}
			
			if ( !matched )
			{
				throw new MalformedRequestStringException( "not a valid Request-String,"
																					+ " invalid attribute "
																					+ Attribute );
			}	
		}
		}
			
		//check ipaddr <-> hostname
		if ( ( conReq.getIPaddr() == null && conReq.getInetAddress() == null ) )
		{
			throw new MalformedRequestStringException( "not a valid Request-String. Host"
																				+ " has to be specified." );
		}
		else if( conReq.getIPaddr() == null )
		{
			conReq.setIPaddr( conReq.getInetAddress().getHostAddress() );
		}

		else if( conReq.getInetAddress() == null )
		{
			byte[] ipAddrBytes = new byte[ 4 ];
			StringTokenizer ipStringTok = new StringTokenizer( conReq.getIPaddr(), "." );
			if( ipStringTok.countTokens() != 4 )
				throw new MalformedRequestStringException( "not a valid Request-String,"
																					+ conReq.getIPaddr()
																					+ " not a valid IP-Address" );
			else
			{
				try
				{
					for( int ipcnt = 0; i < 4; i++ )
						ipAddrBytes[ ipcnt ] = Byte.parseByte( ipStringTok.nextToken() );
				}
				catch( Exception e )
				{
					throw new MalformedRequestStringException( "not a valid Request-String,"
																						+ conReq.getIPaddr()
																						+ " not a valid IP-Address" );
				}
			}
			try
			{
				conReq.setInetAddress( InetAddress.getByAddress( ipAddrBytes ) );
			}
			catch( UnknownHostException e )
			{
				throw new MalformedRequestStringException( "not a valid Request-String,"
																					+ conReq.getIPaddr()
																					+ " not a valid IP-Address" );
			}
		}
	
		
		/* XPath */
		String reqXPath = contextRequestString.substring( i + 1 );
		try
		{
			DefaultXPath defXPath = new DefaultXPath( reqXPath );
			conReq.setXPath( reqXPath );
		}
		catch ( InvalidXPathException e )
		{
			try
			{
				reqXPath = reqXPath.trim();

				if ( reqXPath.charAt( reqXPath.length() - 1 ) == '|' || 
			 	     reqXPath.charAt( reqXPath.length() - 1 ) == ')' )
				{
					reqXPath = reqXPath.substring( 0, reqXPath.length() - 1 ); 
					//8.1.03 reqXPath = reqXPath.substring( 0, reqXPath.length() - 2 ); 
				}
				
				DefaultXPath defXPath = new DefaultXPath( reqXPath );
				conReq.setXPath( reqXPath );
			}
			catch ( InvalidXPathException e1 )
			{
				throw new MalformedRequestStringException( "not a valid Request-String. \"" 
																					+ reqXPath
																					+ "\" is not a valid XPath." );
			}
		}		
		return conReq;
	}

}