/* Facebook Login Servlet - Shiboleth Identity Provider integrator with Facebook * Copyright (C) 2012 Poznan Supercomputing and Networking Center - Application Department * * contact: adamski@man.poznan.pl * * This program is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. * * This program 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. You should have received a copy of * the GNU General Public License along with this program. * If not, see . * * * Poznan Supercomputing and Networking Center - Application Department, * ul.Dąbrowskiego 79a, 60-529 Poznań, Poland. (+48 61) 858-20-72 * http://apps.man.poznan.pl/ * * This application was developed as a part of the SYNAT project * http://www.synat.pl/en * * Poznan Supercomputing and Networking Center, hereby disclaims all copyright interest in * the Facebook Login Servlet written by Jakub "Erwin" Aftowicz. (erwin37@gmail.com) * * Marcin Adamski , 21 MAY 2012 (adamski@man.poznan.pl) */ package pl.psnc.synat.idp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XMLParser { public static FBConfig getConf(String url) throws Exception { final FBConfig Conf= new FBConfig(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean SqlConf = false; boolean SqlBlackConf = false; boolean Driver = false; boolean DataBase = false; boolean User = false; boolean Pass = false; boolean Stmt = false; boolean Redirect = false; boolean Insert=false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("FacebookServletConfiguration")) { Conf.setAPP_SECRET(attributes.getValue("APP_SECRET")); Conf.setAPP_ID( attributes.getValue("APP_ID") ); } if (qName.equalsIgnoreCase("DataBaseConfiguration")) { SqlConf = true; if (attributes.getValue("isUsed").compareTo("true")==0) Conf.setDatabaseUsed(true); else Conf.setDatabaseUsed(false); } if (qName.equalsIgnoreCase("BlackListConfiguration")) { SqlBlackConf = true; if (attributes.getValue("isUsed").compareTo("true")==0) Conf.setBlackListUsed(true); else Conf.setBlackListUsed(false); } if (qName.equalsIgnoreCase("Parameter")) { if(attributes.getValue("default")!=null){ Conf.addParameter(attributes.getValue("name"), attributes.getValue("default")); } else{ Conf.addParameter(attributes.getValue("name")); } } if (qName.equalsIgnoreCase("FbParameter")){ if(attributes.getValue("numberInStatement")!=null){ if(attributes.getValue("numberInBlackListStatement")!=null){ if(attributes.getValue("numberInInsertStatement")!=null){ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return"), attributes.getValue("numberInStatement"), attributes.getValue("numberInBlackListStatement"), attributes.getValue("numberInInsertStatement")); } else{ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return"), attributes.getValue("numberInStatement"), attributes.getValue("numberInBlackListStatement"), "0" ); } } else{ if(attributes.getValue("numberInInsertStatement")!=null){ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return"), attributes.getValue("numberInStatement"), "0", attributes.getValue("numberInInsertStatement")); } else{ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return"), attributes.getValue("numberInStatement"), "0", "0"); } } } else { if(attributes.getValue("numberInBlackListStatement")!=null){ if(attributes.getValue("numberInInsertStatement")!=null){ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return"), "0", attributes.getValue("numberInBlackListStatement"), attributes.getValue("numberInInsertStatement")); } else{ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return"), "0", attributes.getValue("numberInBlackListStatement"), "0" ); } } else{ if(attributes.getValue("numberInInsertStatement")!=null){ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return"), "0", "0", attributes.getValue("numberInInsertStatement")); } else{ Conf.addFbParameter(attributes.getValue("name"), attributes.getValue("return")); } } } } if (qName.equalsIgnoreCase("Driver")) { Driver = true; } if (qName.equalsIgnoreCase("DataBaseName")) { DataBase = true; } if (qName.equalsIgnoreCase("UserName")) { User = true; } if (qName.equalsIgnoreCase("Password")) { Pass = true; } if (qName.equalsIgnoreCase("Statement")) { Stmt = true; } if (qName.equalsIgnoreCase("RedirectAddress")) { Redirect = true; } if (qName.equalsIgnoreCase("InsertStatement")) { Insert = true; } } public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("DataBaseConfiguration")) { SqlConf = false; } if (qName.equalsIgnoreCase("BlackListConfiguration")) { SqlBlackConf = false; } if (qName.equalsIgnoreCase("Statement")) { Stmt = false; } } public void characters(char ch[], int start, int length) throws SAXException { if (Driver) { if(SqlConf){ Conf.setSqlDriver(new String(ch, start, length)); } else if (SqlBlackConf){ Conf.setBlackListDriver(new String(ch, start, length)); } Driver = false; } if (DataBase) { if(SqlConf){ Conf.setSqlDataBaseName(new String(ch, start, length)); } else if (SqlBlackConf){ Conf.setBlackListDataBaseName(new String(ch, start, length)); } DataBase = false; } if (User) { if(SqlConf){ Conf.setSqlUserName(new String(ch, start, length)); } else if (SqlBlackConf){ Conf.setBlackListUserName(new String(ch, start, length)); } User = false; } if (Pass) { if(SqlConf){ Conf.setSqlPassword(new String(ch, start, length)); Conf.setPasswordUsed(true); } else if (SqlBlackConf){ Conf.setBlackListPassword(new String(ch, start, length)); Conf.setBlackListPasswordUsed(true); } Pass = false; } if (Stmt) { if(SqlConf){ if(Conf.getSqlStatement()!=null) Conf.setSqlStatement(Conf.getSqlStatement() + new String(ch, start, length)); else Conf.setSqlStatement(new String(ch, start, length)); } else if (SqlBlackConf){ Conf.setBlackListStatement(new String(ch, start, length)); } //Stmt = false; } if (Redirect) { if (SqlBlackConf){ Conf.setBlackListRedirectAddress(new String(ch, start, length)); } Redirect = false; } if (Insert) { Conf.setSqlInsertStatement(new String(ch, start, length)); Insert = false; } } }; String url2; if (url.startsWith("file:")) url2=url.substring("file:".length(), url.length()); else url2=url; File file = new File(url2); InputStream inputStream= new FileInputStream(file); Reader reader = new InputStreamReader(inputStream,"UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); saxParser.parse(is, handler); // System.out.println(Conf.toString()); return Conf; } catch (FileNotFoundException e){ throw new FileNotFoundException("Unable to find and open " + url); } catch (Exception e) { throw e; } } }