package com.ticktac.utils;

import java.io.IOException;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import com.ticktac.business.User;
import com.ticktac.business.Message;

public class SendMessageRequestHandler implements MessageRequestHandler {

	@Override
	public String handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		return null;
	}

	@Override
	public String handleRequest(HttpServletRequest request, HttpServletResponse response, EntityManager em,
			UserTransaction tr) throws ServletException, IOException {
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * Main message request handler
	 * @see com.ticktac.utils.MessageRequestHandler#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.jms.ConnectionFactory, javax.jms.Queue)
	 */
	/*@Override
	public String handleRequest(HttpServletRequest request, HttpServletResponse response, ConnectionFactory cf,
			Queue queue) throws ServletException, IOException {
		String view = "notfound.html";
		try {
			Connection connection = cf.createConnection();
			Session session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
			MessageProducer messageProducer = session.createProducer(queue);
			
			Message message = session.createTextMessage(request.getParameter("message")); // Always a TextMessage
			// The ID of the User as the receiver (Should be always 1 if the logged in user is not the administrator)
			message.setIntProperty("receiver_id", 1);
			// The ID of the User as the sender
			message.setIntProperty("sender_id", ((User)request.getSession().getAttribute("userBean")).getId());
			// The Fullname of the User as the sender 
			message.setStringProperty("sender_name", ((User)request.getSession().getAttribute("userBean")).getName() + " " +
					((User)request.getSession().getAttribute("userBean")).getSurname());
			
			messageProducer.send(message);
			
			messageProducer.close();
			session.close();
			connection.close();
			
			request.setAttribute("messageSent", 1);
			view = "sendmessage.jsp";
		} catch(Exception e) {
			request.setAttribute("messageSent", 0);
			e.printStackTrace();
		}
		
		return view;
	}*/
	
	public String handleRequest(HttpServletRequest request, HttpServletResponse response, ConnectionFactory cf,
			Queue queue) throws ServletException, IOException {
		String view = "notfound.html";
		
		Message message = new Message(((User)request.getSession().getAttribute("userBean")).getId(), "Administrator", 1, request.getParameter("message"));
		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client.target("http://localhost:8082").path("sendmessage");
		Message result = webTarget.request("application/json").accept("application/json").
				post(Entity.entity(message, MediaType.APPLICATION_JSON), Message.class);

		request.setAttribute("messageSent", 1);
		view = "sendmessage.jsp";
		
		return view;
	}
}

@@ -1104,1022 +1104,292 @@
 %0A%09%0A%09
-/*%0A%09 * (non-Javadoc)%0A%09 * Main message request handler%0A%09 * @see com.ticktac.utils.MessageRequestHandler#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.jms.ConnectionFactory, javax.jms.Queue)%0A%09 */%0A%09/*@Override%0A%09public String handleRequest(HttpServletRequest request, HttpServletResponse response, ConnectionFactory cf,%0A%09%09%09Queue queue) throws ServletException, IOException %7B%0A%09%09String view = %22notfound.html%22;%0A%09%09try %7B%0A%09%09%09Connection connection = cf.createConnection();%0A%09%09%09Session session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);%0A%09%09%09MessageProducer messageProducer = session.createProducer(queue);%0A%09%09%09%0A%09%09%09Message message = session.createTextMessage(request.getParameter(%22message%22)); // Always a TextMessage%0A%09%09%09// The ID of the User as the receiver (Should be always 1 if the logged in user is not the administrator)%0A%09%09%09message.setIntProperty(%22receiver_id%22, 1);%0A%09%09%09// The ID of the User as the sender%0A%09%09%09message.setIntProperty(%22sender_id%22, 
+public String handleRequest(HttpServletRequest request, HttpServletResponse response, ConnectionFactory cf,%0A%09%09%09Queue queue) throws ServletException, IOException %7B%0A%09%09String view = %22notfound.html%22;%0A%09%09%0A%09%09// Create the message with the required parameters%0A%09%09Message message = new Message(
 ((Us
@@ -1449,101 +1449,14 @@
 Id()
-);
+,
 %0A%09%09%09
-// The Fullname of the User as the sender %0A%09%09%09message.setStringProperty(%22sender_name%22, 
+%09
 ((Us
@@ -1526,14 +1526,9 @@
  %22 +
-%0A%09%09%09%09%09
+ 
 ((Us
@@ -1593,633 +1593,14 @@
 me()
-);%0A%09%09%09%0A%09%09%09messageProducer.send(message);%0A%09%09%09%0A%09%09%09messageProducer.close();%0A%09%09%09session.close();%0A%09%09%09connection.close();%0A%09%09%09%0A%09%09%09request.setAttribute(%22messageSent%22, 1);%0A%09%09%09view = %22sendmessage.jsp%22;%0A%09%09%7D catch(Exception e) %7B%0A%09%09%09request.setAttribute(%22messageSent%22, 0);%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A%09%09return view;%0A%09%7D*/%0A%09%0A%09public String handleRequest(HttpServletRequest request, HttpServletResponse response, ConnectionFactory cf,%0A%09%09%09Queue queue) throws ServletException, IOException %7B%0A%09%09String view = %22notfound.html%22;%0A%09%09%0A%09%09Message message = new Message(((User)request.getSession().getAttribute(%22userBean%22)).getId(), %22Administrator%22, 
+,%0A%09%09%09%09
 1, r
@@ -1632,16 +1632,19 @@
 age%22));%0A
+%09%09%0A
 %09%09Client

Remove unecessary comments