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.GlobalConfiguration; 034 import com.softwarementors.extjs.djn.config.RegisteredPollMethod; 035 import com.softwarementors.extjs.djn.config.Registry; 036 037 public class PollRequestProcessor extends RequestProcessorBase { 038 039 private static Logger logger = Logger.getLogger(PollRequestProcessor.class); 040 public static final String PATHINFO_POLL_PREFIX = "/poll/"; 041 042 protected PollRequestProcessor(Registry registry, Dispatcher dispatcher, GlobalConfiguration globalConfiguration) { 043 super(registry, dispatcher, globalConfiguration); 044 } 045 046 private String getEventName(String pathInfo) { 047 assert !StringUtils.isEmpty(pathInfo); 048 049 return pathInfo.replace( PATHINFO_POLL_PREFIX, ""); 050 } 051 052 public void process(Reader reader, Writer writer, String pathInfo) throws IOException { 053 assert !StringUtils.isEmpty(pathInfo); 054 055 String requestString = IOUtils.toString(reader); 056 String eventName = getEventName(pathInfo); 057 if( logger.isDebugEnabled() ) { 058 logger.debug( "Request data (POLL)=>" + requestString + " Event name='" + eventName + "'"); 059 } 060 061 RegisteredPollMethod method = getRegistry().getPollEvent( eventName ); 062 if( method == null ) { 063 RequestException ex = RequestException.forPollEventNotFound(eventName); 064 logger.error( ex.getMessage(), ex ); 065 throw ex; 066 } 067 068 ResponseBase response; 069 try { 070 Map<String, String> requestParameters = getDecodedRequestParameters(requestString); 071 Object[] parameters = new Object[] { requestParameters }; 072 Object result = dispatch(method.getOwningClass(), method.getMethod(), parameters); 073 response = new PollResponse( eventName, result ); 074 } 075 catch( Throwable t ) { 076 Throwable reportedException = getExceptionToReport(t); 077 String message = getExceptionMessage(reportedException); 078 String where = getExceptionWhere(reportedException); 079 response = new PollErrorResponse( message, where); 080 081 logger.error( "(Controlled) server error: " + t.getMessage() + " for Poll Event '" + eventName + "'", t); 082 } 083 StringBuilder result = new StringBuilder(); 084 appendIndividualResponseJsonString(response, result); 085 086 String resultString = result.toString(); 087 writer.write( resultString ); 088 if( logger.isDebugEnabled() ) { 089 logger.debug( "Response data (POLL)=>" + resultString ); 090 } 091 } 092 }