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 * DirectJNgine is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with DirectJNgine. If not, see <http://www.gnu.org/licenses/>. 017 * 018 * This software uses the ExtJs library (http://extjs.com), which is 019 * distributed under the GPL v3 license (see http://extjs.com/license). 020 */ 021 022 package com.softwarementors.extjs.djn.router; 023 024 import java.io.IOException; 025 import java.io.Reader; 026 import java.io.Writer; 027 import java.util.Map; 028 029 import org.apache.commons.io.IOUtils; 030 import org.apache.log4j.Logger; 031 032 import com.softwarementors.extjs.djn.StringUtils; 033 import com.softwarementors.extjs.djn.config.RegisteredPollMethod; 034 import com.softwarementors.extjs.djn.config.Registry; 035 036 public class PollRequestProcessor extends RequestProcessorBase { 037 038 private static Logger logger = Logger.getLogger(PollRequestProcessor.class); 039 public static final String PATHINFO_POLL_PREFIX = "/poll/"; 040 041 protected PollRequestProcessor(Registry registry, Dispatcher dispatcher, boolean debug) { 042 super(registry, dispatcher, debug); 043 } 044 045 private String getEventName(String pathInfo) { 046 assert !StringUtils.isEmpty(pathInfo); 047 048 return pathInfo.replace( PATHINFO_POLL_PREFIX, ""); 049 } 050 051 public void process(Reader reader, Writer writer, String pathInfo) throws IOException { 052 assert !StringUtils.isEmpty(pathInfo); 053 054 String requestString = IOUtils.toString(reader); 055 String eventName = getEventName(pathInfo); 056 logger.debug( "request=>" + requestString + ". Event name='" + eventName + "'"); 057 058 RegisteredPollMethod method = getRegistry().getPollEvent( eventName ); 059 if( method == null ) { 060 RequestException ex = RequestException.forPollEventNotFound(eventName); 061 logger.error( ex.getMessage(), ex ); 062 throw ex; 063 } 064 065 ResponseBase response; 066 try { 067 Map<String, String> requestParameters = getDecodedRequestParameters(requestString); 068 Object[] parameters = new Object[] { requestParameters }; 069 Object result = dispatch(method.getOwningClass(), method.getMethod(), parameters); 070 response = new PollResponse( eventName, result ); 071 } 072 catch( Throwable t ) { 073 Throwable reportedException = getExceptionToReport(t); 074 String message = getExceptionMessage(reportedException); 075 String where = getExceptionWhere(reportedException); 076 response = new PollErrorResponse( message, where); 077 } 078 StringBuilder result = new StringBuilder(); 079 appendIndividualResponseJsonString(response, result); 080 081 String resultString = result.toString(); 082 writer.write( resultString ); 083 logger.debug( "response=>" + resultString ); 084 } 085 }