001 /* 002 NGramJ - n-gram based text classification 003 Copyright (C) 2001 Frank S. Nestel (frank at spieleck.de) 004 005 This program is free software; you can redistribute it and/or modify 006 it under the terms of the GNU Lesser General Public License as published 007 by the Free Software Foundation; either version 2.1 of the License, or 008 (at your option) any later version. 009 010 This program 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 Lesser General Public License 016 along with this program (lesser.txt); if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 018 */ 019 020 package de.spieleck.app.ngramj.servlet; 021 022 import java.io.*; 023 import javax.servlet.*; 024 import javax.servlet.http.*; 025 026 import de.spieleck.app.ngramj.*; 027 import de.spieleck.app.ngramj.phoner.*; 028 import de.spieleck.app.ngramj.lm.LMDataProfile; 029 030 /** 031 * Mini-Application to create a "useful" word from 032 * a phonenumber. 033 */ 034 public class PhoneServlet 035 extends HttpServlet 036 { 037 protected String basePath = "WEB-INF/lm"; 038 protected String forcedReferer = null; 039 040 public PhoneServlet() { } 041 042 public void init() 043 { 044 String h; 045 h = getInitParameter("basePath"); 046 if ( h != null ) 047 basePath = h; 048 basePath = getServletConfig() 049 .getServletContext() 050 .getRealPath(basePath)+"/"; 051 forcedReferer = getInitParameter("forcedReferer"); 052 053 System.err.println("bp="+basePath); 054 } 055 056 public void doPost(HttpServletRequest req, HttpServletResponse res) 057 throws IOException 058 { 059 doGet(req,res); 060 } 061 062 public void doGet(HttpServletRequest req, HttpServletResponse res) 063 throws IOException 064 { 065 res.setContentType("text/plain"); 066 PrintWriter out = res.getWriter(); 067 if ( forcedReferer != null ) 068 { 069 String referer = req.getHeader("Referer"); 070 if ( !forcedReferer.equals(referer) ) 071 { 072 out.println("Sorry! You came from "+referer+"."); 073 out.println("This page is costly in terms of CPU time."); 074 out.println("You can only access via "+forcedReferer+"."); 075 return; 076 } 077 } 078 String tel = req.getParameter("tel"); 079 if ( tel.length() >= 9 ) 080 { 081 out.println("Sorry! Your number is too long."); 082 out.println("The current implementation is costly in terms of CPU time."); 083 return; 084 } 085 String lang = req.getParameter("lang"); 086 System.out.println("Using language resource "+lang+" on <"+tel+">("+tel.length()+")."); 087 try 088 { 089 File ifi = new File(basePath+lang); 090 InputStream in = new FileInputStream(ifi); 091 IterableProfile prof = new LMDataProfile(lang, in); 092 // 093 Phoner p = new Phoner(prof, tel); 094 out.println("Using language resource "+lang); 095 p.show(out); 096 } 097 catch ( Exception e ) 098 { 099 out.println("Error: "+e); 100 } 101 } 102 }