Web based NIP Verification with Token
/*
##########################
#
# Skrypcik pozwala na weryfikacje numeru w bazie, po podaniu
# prawidlowego numerku podanego na obrazku z klasy IMG
# dodatkowo dokonuje weryfikacji poprawnosci numeru NIP
# na podstawie ostatniej cyfry kontrolnej
#
########### Maciej Stopa
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
*/
package nip;
import java.io.*;
import java.util.Random;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
import java.util.Calendar;
public class NIP extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType ("text/html");
PrintWriter out = response.getWriter();
out.write("<html>\r\n");
out.write("<meta HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=ISO-8859-2\">\r\n");
out.write("<body text=\"black\" bgcolor=\"white\" background=\"images/tlo.gif\" link=\"black\" vlink=\"#666666\" alink=\"black\">\r\n");
out.write("\r\n");
out.write("<table align=\"center\" border=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">\r\n");
out.write("<tr>\r\n");
out.write(" <td><img src=\"images/header.png\"></td>\r\n");
out.write("</tr>\r\n");
out.write("<tr bgcolor=\"white\">\r\n");
out.write(" <td>\r\n");
out.write("\r\n");
int found=0;
out.println("Tutaj mozesz sprawdzic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. Podawane tu informacje aktualizowane sa </br>codziennie,");
out.println("wiec jesli nie ma w bazie wnioskow tego numeru NIP, nie oznacza to ze nie zostanie on przetworzony.</br></br>");
String nip=request.getParameter("nip");
String token=request.getParameter("token");
String enteredtoken=request.getParameter("enteredtoken");
if (nip!=null)
if (nip!="")
{
Date now = Calendar.getInstance().getTime();
int ok=1;
try {
BufferedWriter log = new BufferedWriter(new FileWriter("c:/nip/nip.log", true));
log.write(now+": Requested NIP: "+nip+"\n");
log.close();
} catch (IOException e) {
ok=0;
}
try {
int intA = Integer.valueOf(nip);
} catch(NumberFormatException e) {
out.println("<strong>Numer NIP moze zawierac jedynie cyfry, wprowadz ponownie</strong></br></br>");
ok=0;
}
if (nip.length()>10)
{
out.println("<strong>Numer NIP zbyt dlugi, wprowadz ponownie</strong></br></br>");
ok=0;
}
if (nip.length()<8)
{
out.println("<strong>Numer NIP zbyt krotki, wprowadz ponownie</strong></br></br>");
ok=0;
}
if (enteredtoken=="")
{
out.println("<strong>Nie wprowadziles numeru z obrazka, wprowadz ponownie</strong></br></br>");
ok=0;
} else
if (99999999-(Integer.valueOf(token)*3)!=Integer.valueOf(enteredtoken))
{
out.println("<strong>Wprowadziles bledny numer z obrazka, wprowadz ponownie</strong></br></br>");
ok=0;
}
if (ok==1)
{
File inFile = new File("c:/nip/nip_data.csv");
BufferedReader reader = new BufferedReader(new FileReader(inFile));
String line = null;
while ((line = reader.readLine()) != null)
{
if (line.contains(nip))
{
String[] elementy=line.split(";");
out.write("<strong>Status wniosku: "+elementy[1]+"</strong></br></br>\n");
found=1;
}
}
reader.close(); // Close to unlock.
if (found==0)
{
out.write("<strong>Nie znaleziono wniosku w bazie.</strong></br></br>");
}
}
} else
{
out.println("<strong>Nie podales numeru NIP, wprowadz ponownie</strong></br></br>");
}
out.println("<form action=\"nip\" method=\"post\">Wprowadz numer NIP dla interesujacego cie wniosku:");
out.println("<INPUT type=\"text\" value=\"");
if (nip==null)
{
out.println("0000000000");
} else
if (nip=="")
{
out.println("0000000000");
} else
{
out.println(request.getParameter("nip"));
}
out.println("\" name=\"nip\" maxlength=\"10\" size=\"10\"></br>");
out.println("Wpisz numer podany obok");
Random generator = new Random();
int numer = generator.nextInt(99999999/3);
out.println("<img src=\"img?token="+numer+"\">\n<INPUT type=\"text\" value=\"");
out.println("\" name=\"enteredtoken\" maxlength=\"8\" size=\"8\">");
out.println("<INPUT type=\"HIDDEN\" value=\""+numer+"\" name=\"token\">");
out.println("<INPUT type=\"submit\"></form></br><a href=\"http://xxxxxxxxxxxxxxxxxxxxx/\">Strona glowna</a>");
out.write("\r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" </td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write(" <td><img src=\"images/tail.png\"></td>\r\n");
out.write("</tr>\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("</table>\r\n");
out.write("\r\n");
out.write("</body>\r\n");
out.write("\r\n");
out.write("</html>");
}
}