001    /*
002     * Copyright © 2008, 2009 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * Commercial use is permitted to the extent that the code/component(s)
011     * do NOT become part of another Open Source or Commercially developed
012     * licensed development library or toolkit without explicit permission.
013     *
014     * DirectJNgine is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
021     * 
022     * This software uses the ExtJs library (http://extjs.com), which is 
023     * distributed under the GPL v3 license (see http://extjs.com/license).
024     */
025    
026    package com.softwarementors.extjs.djn.router.processor.poll;
027    
028    import java.io.IOException;
029    import java.io.Reader;
030    import java.io.Writer;
031    import java.util.Map;
032    
033    import org.apache.commons.io.IOUtils;
034    import org.apache.log4j.Logger;
035    
036    import com.softwarementors.extjs.djn.StringUtils;
037    import com.softwarementors.extjs.djn.api.RegisteredPollMethod;
038    import com.softwarementors.extjs.djn.api.Registry;
039    import com.softwarementors.extjs.djn.config.GlobalConfiguration;
040    import com.softwarementors.extjs.djn.router.dispatcher.Dispatcher;
041    import com.softwarementors.extjs.djn.router.processor.RequestException;
042    import com.softwarementors.extjs.djn.router.processor.RequestProcessorBase;
043    import com.softwarementors.extjs.djn.router.processor.ResponseBase;
044    
045    public class PollRequestProcessor extends RequestProcessorBase {
046    
047      private static Logger logger = Logger.getLogger(PollRequestProcessor.class);
048      public static final String PATHINFO_POLL_PREFIX = "/poll/"; 
049      
050      public PollRequestProcessor(Registry registry, Dispatcher dispatcher, GlobalConfiguration globalConfiguration) {
051        super(registry, dispatcher, globalConfiguration);
052      }
053    
054      private String getEventName(String pathInfo) {
055        assert !StringUtils.isEmpty(pathInfo);
056        
057        return pathInfo.replace( PATHINFO_POLL_PREFIX, "");
058      }
059      
060      public void process(Reader reader, Writer writer, String pathInfo) throws IOException {
061        assert !StringUtils.isEmpty(pathInfo);
062      
063        String requestString = IOUtils.toString(reader);
064        String eventName = getEventName(pathInfo);
065        if( logger.isDebugEnabled() ) {
066          logger.debug( "Request data (POLL)=>" + requestString + " Event name='" + eventName + "'");
067        }
068        
069        RegisteredPollMethod method = getRegistry().getPollMethod( eventName );
070        if( method == null ) {
071          RequestException ex = RequestException.forPollEventNotFound(eventName);
072          logger.error( ex.getMessage(), ex );
073          throw ex;
074        }
075        
076        ResponseBase response;
077        try {
078          Map<String, String> requestParameters = getDecodedRequestParameters(requestString);
079          Object[] parameters = new Object[] { requestParameters };
080          Object result = dispatch(method.getOwningClass(), method.getMethod(), parameters);
081          response = new PollSuccessResponse( eventName, result );
082        }
083        catch( Throwable t ) {
084          Throwable reportedException = getExceptionToReport(t);
085          String message = getExceptionMessage(reportedException);
086          String where = getExceptionWhere(reportedException);
087          response = new PollErrorResponse( message, where);
088          
089          logger.error( "(Controlled) server error: " + t.getMessage() + " for Poll Event '" + eventName + "'", t); 
090        }  
091        StringBuilder result = new StringBuilder();
092        appendIndividualResponseJsonString(response, result);
093        
094        String resultString = result.toString();
095        writer.write( resultString );
096        if( logger.isDebugEnabled() ) {
097          logger.debug( "Response data (POLL)=>" + resultString );
098        }
099      }
100    }