<repo_name>arif034/Chat-Application<file_sep>/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Chat</groupId>
  <artifactId>Project</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>
		<spring.version>4.0.7.RELEASE</spring.version>
		<!-- <spring.version>3.2.1.RELEASE</spring.version> -->
		<spring.data.version>1.7.0.RELEASE</spring.data.version>
		<!-- <spring.data.version>1.3.0.RELEASE</spring.data.version> -->

		<thymeleaf.version>2.1.3.RELEASE</thymeleaf.version>
		<hsqldb.version>2.3.2</hsqldb.version>
		<mysql.version>5.1.33</mysql.version>

		<java.version>1.7</java.version>
	</properties>
	
	<dependencies>
	<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>net.sf.flexjson</groupId>
    <artifactId>flexjson</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>io.socket</groupId>
    <artifactId>socket.io-client</artifactId>
    <version>1.0.0</version>
</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.11.3</version>
</dependency>
		
		
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
            <version>0.34</version>
        </dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf-spring4</artifactId>
			<version>${thymeleaf.version}</version>
			<scope>compile</scope>
		</dependency>

		<!-- JPA -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>${spring.data.version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate.javax.persistence</groupId>
			<artifactId>hibernate-jpa-2.0-api</artifactId>
			<version>1.0.1.Final</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>4.1.9.Final</version>
			<scope>runtime</scope>
		</dependency>

		<!-- <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> 
			<version>${hsqldb.version}</version> </dependency> -->

		<dependency>
   		  <groupId>mysql</groupId>
    	  <artifactId>mysql-connector-java</artifactId>
    	  <version>8.0.21</version>
		</dependency>

	</dependencies>
	
  
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
      <plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<url>http://192.168.59.103/manager/text</url>
					<server>RemoteTomcatServer</server>
					<path>/</path>
				</configuration>
			</plugin>
    </plugins>
  </build>
</project><file_sep>/WebContent/WEB-INF/queries/mycreateInsertUser.sql
CREATE USER 'arif'@'localhost' IDENTIFIED BY 'arif';
GRANT ALL PRIVILEGES ON * . * TO 'arif'@'localhost';

USE `chat_application`;
select * from user;
ALTER TABLE new_table
RENAME TO user;
SELECT IF( EXISTS(
             SELECT *
             FROM user
             WHERE username ='arif' AND password ='<PASSWORD>' ), 1, 0);
select if(exists(select * from user where username = 'arif' and password = <PASSWORD>'),1,0);
insert into user 
VALUES (2,'arif2','#Plotter2', '<EMAIL>', 'India');
INSERT INTO user
VALUES (3,'arif3','#Plotter2', '<EMAIL>', 'India');

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(45) DEFAULT NULL,
  `password` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `region` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;<file_sep>/src/com/chat/app/controller/UserController.java
package com.chat.app.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.chat.app.dto.UserDTO;
import com.chat.app.model.User;
import com.chat.app.service.UserService;

@RestController("")
public class UserController
{
	private static Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/validate", produces = "application/json; charset=UTF-8")
	@ResponseBody
	public ResponseEntity<Boolean> validateUser(@RequestBody String json)
	{
		Boolean bool = new Boolean(true);
		UserDTO userDTO = UserDTO.fromJsonToUserDTO(json);
		User user = new User();
		user.setUsername(userDTO.getUsername());
		user.setPassword(<PASSWORD>());
		if(userService.validateUser(user.getUsername(), user.getPassword()))
			bool = true;
		else
			bool = false;
		return new ResponseEntity<Boolean>(bool, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/fetch", produces = "application/json; charset=UTF-8")
	@ResponseBody
	public User fetchUserById(@RequestParam(value = "id", required = true) Long id)
	{
		User user = userService.fetchUser(id);
		return user;
	}
	
	
	@RequestMapping(value = "/save", produces = "application/json; charset=UTF-8")
	@ResponseBody
	public void saveNewUser( @RequestBody String json)
	{
		try
		{
			UserDTO userDTO = UserDTO.fromJsonToUserDTO(json);
			User user = new User();
			user.setUsername(userDTO.getUsername());
			user.setPassword(<PASSWORD>());
			user.setEmail(userDTO.getEmail());
			user.setRegion(userDTO.getRegion());
			userService.saveNewUser(user);
		}catch(Exception e)
		{
			logger.error("Error occured while regestering entry into database",e);
			throw e;
		}
	}
	
	@RequestMapping(value = "/createnewuser", produces = "application/json; charset=UTF-8")
	@ResponseBody
	public ResponseEntity<Boolean> createNewUser( @RequestBody String json)
	{
			Boolean bool = new Boolean(true);
			UserDTO userDTO = UserDTO.fromJsonToUserDTO(json);
			User user = new User();
			user.setFirstname(userDTO.getFirstname());
			user.setLastname(userDTO.getLastname());
			user.setUsername(userDTO.getUsername());
			user.setPassword(<PASSWORD>());
			user.setEmail(userDTO.getEmail());
			user.setRegion(userDTO.getRegion());
			userService.saveNewUser(user);
			bool = true;
			return new ResponseEntity<Boolean>(bool, HttpStatus.OK);
	}
}<file_sep>/src/com/chat/app/controller/SampleClass2.java
package com.chat.app.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;

import com.chat.app.dto.MessageDTO;

public class SampleClass2 {

	public static void main(String[] args) throws IOException {
		
	System.out.println("Server is started");
	ServerSocket serverSocket = new ServerSocket(8083);
	System.out.println("Server is waitting for client request");
	Socket s = serverSocket.accept();
	BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
	String message = br.readLine();
	System.out.println("S: Client Data ");
	String messages = "messaage2";
	
	OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
	PrintWriter out = new PrintWriter(os);
	out.println(messages);
	os.flush();
	System.out.println("S : data sent from Server to Client ");
	String ssa = "String here";
	ArrayList<String> al = (ArrayList<String>) Arrays.asList("Hi ","My name is"," Arif");
	}
}
<file_sep>/src/com/chat/app/controller/ChatClientController.java
package com.chat.app.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.chat.app.dto.MessageDTO;
import com.chat.app.dto.UserDTO;
import com.chat.app.model.User;

@RestController("")
public class ChatClientController {
	
	private static Logger logger = Logger.getLogger(UserController.class);
	
	@RequestMapping(value = "/server", produces = "application/json; charset=UTF-8")
	@ResponseBody
	public String takeNewMessage( @RequestBody String json) throws Exception
	{
		ServerSocket serverSocket = new ServerSocket(8083);
		Socket s = serverSocket.accept();
		MessageDTO messageDTO = MessageDTO.fromJsonToMessageDTO(json);
		String messages = messageDTO.getMessage();
		BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		String message = br.readLine();		
		OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
		PrintWriter out = new PrintWriter(os);
		out.println(messages);
		os.flush();
		String outString = " S : Message from Client  : " + message;
		return outString;
	}
	
	
	
	@RequestMapping(value = "/client", produces = "application/json; charset=UTF-8")
	@ResponseBody
	public String takeNewMessages( @RequestBody String json) throws Exception
	{
		String ipAddress = "localhost";
		int port = 8083;
		String message = "";
		String message2 = "";
		Socket s = new Socket(ipAddress, port);
		MessageDTO messageDTO = MessageDTO.fromJsonToMessageDTO(json);
		message = messageDTO.getMessage();
		OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
		PrintWriter out = new PrintWriter(os);
		out.println(message);
		os.flush();
		BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		message2 = br.readLine();
		String outString = " C : Message from Server  : " + message2;
		return outString;
		}
}
