Guest User

Untitled

a guest
Jul 12th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. package org.me.asocial.server;
  2.  
  3. import java.io.File;
  4. import javax.jws.WebService;
  5. import javax.jws.WebMethod;
  6. import javax.jws.WebParam;
  7. import java.sql.*;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11.  
  12. import javax.xml.parsers.DocumentBuilder;
  13. import javax.xml.parsers.DocumentBuilderFactory;
  14. import javax.xml.parsers.ParserConfigurationException;
  15. import javax.xml.transform.Transformer;
  16. import javax.xml.transform.TransformerException;
  17. import javax.xml.transform.TransformerFactory;
  18. import javax.xml.transform.dom.DOMSource;
  19. import javax.xml.transform.stream.StreamResult;
  20.  
  21. import org.w3c.dom.Attr;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.Element;
  24.  
  25. //@author Andrea
  26.  
  27. // **********************************************************
  28. // ************************ CLASSES *************************
  29. // **********************************************************
  30.  
  31. class Database
  32. {
  33.     Connection con;
  34.     PreparedStatement pst;
  35.     ResultSet rs;
  36.     Database()
  37.     {
  38.         try{
  39.             Class.forName("com.mysql.jdbc.Driver");
  40.             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/asocial_db","asocial_god","asocial");
  41.            }
  42.         catch (Exception e)
  43.         {
  44.             System.out.println(e);
  45.         }
  46.     }
  47.  
  48.     protected String checkLogin(String usr,String pwd)
  49.     {
  50.         try {
  51.                     String query="SELECT * FROM user_authentication WHERE username=? AND password=?";
  52.                     pst=con.prepareStatement(query);
  53.                     pst.setString(1, usr);
  54.                     pst.setString(2, pwd);
  55.                     rs=pst.executeQuery();
  56.                     if(rs.next())
  57.                     {
  58.             return "Login effettuato!";
  59.                     } else {
  60.             return "Login errato!";
  61.                     }
  62.         } catch (Exception e) {
  63.                     return "Errore: " + e;
  64.         }
  65.         }
  66.        
  67.         protected String sendPost(int userID, String postTitle, String postBody)
  68.         {
  69.                 try {
  70.                     String query="INSERT INTO `asocial_db`.`posts` (`post_id`, `post_date`, `post_title`, `post_body`, `user_id`)"
  71.                                 + "VALUES (NULL, CURRENT_TIMESTAMP,?,?,1)";
  72.                     pst=con.prepareStatement(query);
  73.                     pst.setString(1, postTitle);
  74.                     pst.setString(2, postBody);                    
  75.                     pst.executeUpdate();
  76.                 } catch (Exception e){
  77.                     return "Errore! " + e;
  78.                 }
  79.           return "Post inserito!";  
  80.         }
  81.        
  82.         protected ResultSet getPost()
  83.         {
  84.                 try {
  85.                     String query="SELECT * FROM posts";
  86.                     pst=con.prepareStatement(query);
  87.                     rs=pst.executeQuery();
  88.                 } catch (Exception e) {
  89.                     System.out.println("Errore: " + e);
  90.                     return null;
  91.                 }
  92.             return rs;
  93.         }
  94. }
  95. class WriteXMLFile
  96. {
  97.         String post_repo_xml="C:\\file.xml";
  98.  
  99.     protected void getXML() {
  100.                 Database db = new Database();
  101.                 ResultSet xrs=db.getPost();
  102.                
  103.             try {
  104.         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  105.         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  106.                  
  107.                 Document doc = docBuilder.newDocument();
  108.                 Element post_root = doc.createElement("post_root");
  109.                 doc.appendChild(post_root);
  110.                
  111.                 while (xrs.next()) {
  112.                     String ptitle = xrs.getString("post_title");
  113.                     String pbody = xrs.getString("post_body");
  114.                     Timestamp pdate = xrs.getTimestamp("post_date");
  115.                     int pid = xrs.getInt("post_id");
  116.                     int uid = xrs.getInt("user_id");
  117.  
  118.  
  119.                     Element post = doc.createElement("post");
  120.                     post_root.appendChild(post);
  121.  
  122.                     Attr post_id = doc.createAttribute("post_id");
  123.                     post_id.setValue(""+pid); // CACATA
  124.                     post.setAttributeNode(post_id);
  125.  
  126.                     Attr user_id = doc.createAttribute("user_id");
  127.                     user_id.setValue(""+uid); // CACATA
  128.                     post.setAttributeNode(user_id);
  129.  
  130.                     Element post_date = doc.createElement("post_date");
  131.                     post_date.appendChild(doc.createTextNode(""+pdate)); // CACATA
  132.                     post.appendChild(post_date);
  133.  
  134.                     Element post_title = doc.createElement("post_title");
  135.                     post_title.appendChild(doc.createTextNode(ptitle));
  136.                     post.appendChild(post_title);
  137.  
  138.                     Element post_body = doc.createElement("post_body");
  139.                     post_body.appendChild(doc.createTextNode(pbody));
  140.                     post.appendChild(post_body);
  141.                 }
  142.  
  143.         // Scrive nel file XML
  144.         TransformerFactory transformerFactory = TransformerFactory.newInstance();
  145.         Transformer transformer = transformerFactory.newTransformer();
  146.         DOMSource source = new DOMSource(doc);
  147.         StreamResult result = new StreamResult(new File(post_repo_xml));
  148.        
  149.         transformer.transform(source, result);
  150.  
  151.         System.out.println("File XML creato.");
  152.  
  153.       } catch (SQLException ex) {
  154.                 System.out.println(ex);
  155.         } catch (ParserConfigurationException pce) {
  156.               System.out.println(pce);
  157.       } catch (TransformerException tfe) {
  158.               System.out.println(tfe);
  159.       }
  160.     }
  161. }
  162.  
  163.  
  164. // **********************************************************
  165. // *************** WEB SERVICE ASocialService ***************
  166. // **********************************************************
  167.  
  168. @WebService(serviceName = "ASocialService")
  169. public class ASocialService {
  170.    
  171.     @WebMethod(operationName = "hello")
  172.     public String hello(@WebParam(name = "name") String txt) {
  173.         return "Hello " + txt + " !";
  174.     }
  175.  
  176.     @WebMethod(operationName = "loginRequest")
  177.     public String loginRequest(@WebParam(name = "username") String username, @WebParam(name = "password") String password) {
  178.         //TODO: Authorization Checks
  179.         Database db = new Database();
  180.         return db.checkLogin(username,password);
  181.     }
  182.  
  183.     @WebMethod(operationName = "setPost")
  184.     public String sendPost(@WebParam(name = "userID") int userID, @WebParam(name = "postTitle") String postTitle, @WebParam(name = "postBody") String postBody) {
  185.         //TODO: Authorization Checks
  186.         Database db = new Database();
  187.         String res=db.sendPost(userID, postTitle, postBody);
  188.         WriteXMLFile xml = new WriteXMLFile();
  189.         xml.getXML();
  190.         return res;
  191.     }
  192. }
Add Comment
Please, Sign In to add comment