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 Lesser 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 Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser 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; 027 028 import java.io.BufferedReader; 029 import java.io.IOException; 030 import java.io.PrintWriter; 031 import java.io.Reader; 032 import java.io.Writer; 033 import java.util.List; 034 035 import org.apache.commons.fileupload.FileItem; 036 import org.apache.commons.fileupload.FileUploadException; 037 import org.apache.log4j.Logger; 038 039 import com.softwarementors.extjs.djn.api.Registry; 040 import com.softwarementors.extjs.djn.config.GlobalConfiguration; 041 import com.softwarementors.extjs.djn.router.dispatcher.Dispatcher; 042 import com.softwarementors.extjs.djn.router.processor.RequestException; 043 import com.softwarementors.extjs.djn.router.processor.poll.PollRequestProcessor; 044 import com.softwarementors.extjs.djn.router.processor.standard.form.simple.SimpleFormPostRequestProcessor; 045 import com.softwarementors.extjs.djn.router.processor.standard.form.upload.UploadFormPostRequestProcessor; 046 import com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor; 047 048 import edu.umd.cs.findbugs.annotations.NonNull; 049 050 public class RequestRouter { 051 @NonNull 052 static final Logger logger = Logger.getLogger( RequestRouter.class ); 053 054 private Registry registry; 055 private Dispatcher dispatcher; 056 private GlobalConfiguration globalConfiguration; 057 058 public RequestRouter( Registry registry, GlobalConfiguration globalConfiguration, Dispatcher dispatcher ) { 059 assert registry != null; 060 assert globalConfiguration != null; 061 assert dispatcher != null; 062 063 this.registry = registry; 064 this.dispatcher = dispatcher; 065 this.globalConfiguration = globalConfiguration; 066 } 067 068 public void processSimpleFormPostRequest(Reader reader, Writer writer) throws IOException { 069 new SimpleFormPostRequestProcessor( this.registry, this.dispatcher, this.globalConfiguration).process( reader, writer ); 070 } 071 072 public UploadFormPostRequestProcessor createUploadFromProcessor() { 073 return new UploadFormPostRequestProcessor( this.registry, this.dispatcher, this.globalConfiguration); 074 } 075 076 public void processUploadFormPostRequest(UploadFormPostRequestProcessor processor, List<FileItem> fileItems, Writer writer ) throws IOException { 077 assert processor != null; 078 079 processor.process( fileItems, writer ); 080 } 081 082 public void processJsonRequest(Reader reader, Writer writer) throws IOException { 083 new JsonRequestProcessor(this.registry, this.dispatcher, this.globalConfiguration).process(reader, writer); 084 } 085 086 public void processPollRequest(Reader reader, Writer writer, String pathInfo) throws IOException { 087 new PollRequestProcessor(this.registry, this.dispatcher, this.globalConfiguration).process(reader, writer, pathInfo); 088 } 089 090 public void handleFileUploadException(UploadFormPostRequestProcessor processor, FileUploadException e) { 091 assert e != null; 092 093 processor.handleFileUploadException(e); 094 } 095 096 public static final String SOURCE_NAME_PREFIX= "/src"; 097 098 public void processSourceRequest(BufferedReader reader, PrintWriter writer, String pathInfo) { 099 assert reader != null; 100 assert writer != null; 101 assert pathInfo != null; 102 103 int lastIndex = pathInfo.lastIndexOf(SOURCE_NAME_PREFIX); 104 int position = lastIndex + SOURCE_NAME_PREFIX.length(); 105 String sourceName = pathInfo.substring(position + 1); 106 if( !this.registry.hasSource(sourceName)) { 107 RequestException ex = RequestException.forSourceNotFound(sourceName); 108 logger.error(sourceName); 109 throw ex; 110 } 111 String code = this.registry.getSource(sourceName); 112 assert code != null; 113 writer.append( code ); 114 } 115 116 public static boolean isSourceRequest(String pathInfo) { 117 assert pathInfo != null; 118 119 int lastIndex = pathInfo.lastIndexOf(SOURCE_NAME_PREFIX); 120 boolean isSource = lastIndex >= 0; 121 return isSource; 122 } 123 124 }