001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/control/RPCWebEvent.java $
002    
003    package org.deegree.enterprise.control;
004    
005    import java.io.BufferedReader;
006    import java.io.StringReader;
007    
008    import javax.servlet.ServletRequest;
009    import javax.servlet.http.HttpServletRequest;
010    
011    /**
012     * @author  Administrator
013     */
014    
015    public class RPCWebEvent extends WebEvent {
016    
017        /**
018         * 
019         */
020        private static final long serialVersionUID = 1L;
021        /**
022         * 
023         */
024        private RPCMethodCall mc = null;
025    
026        
027        /** Creates a new instance of RPCWebEvent */
028        public RPCWebEvent(HttpServletRequest request) {
029            super( request );
030        }
031        
032        /** Creates a new instance of RPCWebEvent */
033        public RPCWebEvent(HttpServletRequest request, RPCMethodCall mc) {
034            super( request );
035            this.mc = mc;
036        }
037        
038        /** Creates a new instance of RPCWebEvent */
039        public RPCWebEvent(FormEvent parent, RPCMethodCall mc) {
040            super( (HttpServletRequest)parent.getSource() );
041            this.mc = mc;
042        }
043        
044        /**
045         * returns the the RPC methodcall extracted from the <tt>HttpServletRequest</tt>
046         * passed to the first constructor.
047         */
048        public RPCMethodCall getRPCMethodCall() {
049            if ( mc == null ) {
050                try {
051                    mc = getMethodCall( (ServletRequest)this.getSource() );
052                } catch (Exception e) {
053                    e.printStackTrace();
054                }
055            }
056            return mc;
057        }
058        
059        /**
060         * extracts the RPC method call from the 
061         * @param request
062         * @throws RPCException
063         */
064        private RPCMethodCall getMethodCall(ServletRequest request ) throws RPCException {
065            
066            StringBuffer sb = new StringBuffer(1000);
067            try {
068                BufferedReader br = request.getReader();
069                String line = null;
070                while ( (line = br.readLine() ) != null ) {
071                    sb.append( line );
072                }
073                br.close();
074            } catch (Exception e) {
075                throw new RPCException( "Error reading stream from servlet\n" + e.toString() );
076            }
077            
078            String s = sb.toString();
079            int pos1 = s.indexOf( "<methodCall>" );
080            int pos2 = s.indexOf( "</methodCall>" );
081            if ( pos1 < 0 ) {
082                throw new RPCException( "request doesn't contain a RPC methodCall" );
083            }
084            s = s.substring( pos1, pos2 + 13 );
085            
086            StringReader reader = new StringReader( s );
087            RPCMethodCall mc = RPCFactory.createRPCMethodCall( reader );
088            
089            return mc;
090        }
091    }