Hi I'm using eclips indigo for developing my project and I'm getting error in my package - like this
Multiple markers at this line
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from
required .class files
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from
required .class files
Does anyone know why I'm getting error when I import Hashmap,util.map,common.collect.map in my java class
package info.sample.project;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.common.collect.Maps;
#ServerEndpoint("/chat")
public class SocketServer {
// set to store all the live sessions
private static final Set<Session> sessions = Collections
.synchronizedSet(new HashSet<Session>());
// Mapping between session and person name
private static final HashMap<String, String> nameSessionPair = new HashMap<String, String>();
private JSONUtils jsonUtils = new JSONUtils();
// Getting query params
public static Map<String, String> getQueryMap(String query) {
Map<String, String> map = Maps.newHashMap();
if (query != null) {
String[] params = query.split("&");
for (String param : params) {
String[] nameval = param.split("=");
map.put(nameval[0], nameval[1]);
}
}
return map;
}
/**
* Called when a socket connection opened
* */
#OnOpen
public void onOpen(Session session) {
System.out.println(session.getId() + " has opened a connection");
Map<String, String> queryParams = getQueryMap(session.getQueryString());
String name = "";
if (queryParams.containsKey("name")) {
// Getting client name via query param
name = queryParams.get("name");
try {
name = URLDecoder.decode(name, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// Mapping client name and session id
nameSessionPair.put(session.getId(), name);
}
// Adding session to session list
sessions.add(session);
try {
// Sending session id to the client that just connected
session.getBasicRemote().sendText(
jsonUtils.getClientDetailsJson(session.getId(),
"Your session details"));
} catch (IOException e) {
e.printStackTrace();
}
// Notifying all the clients about new person joined
sendMessageToAll(session.getId(), name, " joined conversation!", true,
false);
}
/**
* method called when new message received from any client
*
* #param message
* JSON message from client
* */
#OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message from " + session.getId() + ": " + message);
String msg = null;
// Parsing the json and getting message
try {
JSONObject jObj = new JSONObject(message);
msg = jObj.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
// Sending the message to all clients
sendMessageToAll(session.getId(), nameSessionPair.get(session.getId()),
msg, false, false);
}
/**
* Method called when a connection is closed
* */
#OnClose
public void onClose(Session session) {
System.out.println("Session " + session.getId() + " has ended");
// Getting the client name that exited
String name = nameSessionPair.get(session.getId());
// removing the session from sessions list
sessions.remove(session);
// Notifying all the clients about person exit
sendMessageToAll(session.getId(), name, " left conversation!", false,
true);
}
/**
* Method to send message to all clients
*
* #param sessionId
* #param message
* message to be sent to clients
* #param isNewClient
* flag to identify that message is about new person joined
* #param isExit
* flag to identify that a person left the conversation
* */
private void sendMessageToAll(String sessionId, String name,
String message, boolean isNewClient, boolean isExit) {
// Looping through all the sessions and sending the message individually
for (Session s : sessions) {
String json = null;
// Checking if the message is about new client joined
if (isNewClient) {
json = jsonUtils.getNewClientJson(sessionId, name, message,
sessions.size());
} else if (isExit) {
// Checking if the person left the conversation
json = jsonUtils.getClientExitJson(sessionId, name, message,
sessions.size());
} else {
// Normal chat conversation message
json = jsonUtils
.getSendAllMessageJson(sessionId, name, message);
}
try {
System.out.println("Sending Message To: " + sessionId + ", "
+ json);
s.getBasicRemote().sendText(json);
} catch (IOException e) {
System.out.println("error in sending. " + s.getId() + ", "
+ e.getMessage());
e.printStackTrace();
}
}
}
}
Try importing java.util.Map.Entry as well.
Related
I'm trying to UNSUBSCRIBE durable subscribers from TOPICS.
My app is a kind of social network : each user is a topic for other users. So, each time a user is doing something, his friends are notified. Of course, a subscriber may unsubscribe from a topic, wanting to receive notifications about a user no more.
Each time I'm trying to unsubscribe a subscriber from a topic, I've got an error telling me that : "javax.jms.JMSException: Durable consumer is in use"
Here are my 2 classes, the SENDER one and the RECEIVER one. Can someone tell me what I'm doing wrong ??
SENDER Class :
package com.citizenweb.classes;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageFormatException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.ObjectMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import com.citizenweb.interfaces.PostIF;
import com.citizenweb.interfaces.UserIF;
public class Sender {
private ActiveMQConnectionFactory factory = null;
private ActiveMQConnection connection = null;
private ActiveMQSession session = null;
private Destination destination = null;
private MessageProducer producer = null;
public Sender() {
}
public void connect(){
try{
factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
// TODO Mécanisme de sécurité d'ActiveMQ à rétablir en production
factory.setTrustAllPackages(true);
connection = (ActiveMQConnection) factory.createConnection();
connection.start();
session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e){
e.printStackTrace();
}
}
public void sendPost(UserIF user,PostIF post) {
if(session==null){connect();}
try {
destination = session.createTopic(user.toString());
producer = session.createProducer(destination);
ObjectMessage postMessage = session.createObjectMessage();
postMessage.setObject(post);
producer.send(postMessage);
System.out.println("\n SENDER Object message sent");
} catch (MessageFormatException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
public void sendInformation(UserIF user,String info){
if(session==null){connect();}
try {
destination = session.createTopic(user.toString());
producer = session.createProducer(destination);
TextMessage infoMessage = session.createTextMessage();
infoMessage.setText(info);
producer.send(infoMessage);
System.out.println("\n SENDER Information message sent");
} catch (JMSException e) {
e.printStackTrace();
}
}
/**
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
UserIF u1, u2, u3;
String[] nom = new String[5];
String[] prenom = new String[5];
String[] login = new String[5];
String[] password = new String[5];
Date[] naiss = new Date[5];
String[] mail = new String[5];
for (int i = 0; i < 5; i++) {
nom[i] = "nom_" + i;
prenom[i] = "prenom_" + i;
login[i] = "login_" + i;
password[i] = "password_" + i;
naiss[i] = new Date();
mail[i] = "mail_" + i;
}
System.out.println("\n SENDER AFFECTATION DES NOMS");
u1 = new User(nom[0], prenom[0], login[0], password[0], naiss[0], mail[0]);
u2 = new User(nom[1], prenom[1], login[1], password[1], naiss[1], mail[1]);
u3 = new User(nom[2], prenom[2], login[2], password[2], naiss[2], mail[2]);
Sender sender = new Sender();
sender.sendInformation(u1, "U1 notification");
sender.sendInformation(u2, "U2 notification");
sender.sendInformation(u3, "U3 notification");
//PostIF post = new Post("Mon Post","Ceci est mon message",u1,u1,"Classe Sender",((User) u1).getIdUser(),0);
//sender.sendPost(user, post);
sender.session.close();
sender.connection.close();
}
}
RECEIVER Class :
package com.citizenweb.classes;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.broker.region.Destination;
import com.citizenweb.interfaces.PostIF;
import com.citizenweb.interfaces.UserIF;
import com.citizenweb.classes.Post;
public class Receiver implements MessageListener, Serializable {
private static final long serialVersionUID = 1L;
private ActiveMQConnectionFactory factory = null;
private ActiveMQConnection connection = null;
private ActiveMQSession session = null;
private Topic destination = null;
private MessageConsumer consumer = null;
UserIF userTopic = new User();
UserIF userSubscriber = new User();
List<Message> listeMsg = new ArrayList<Message>();
public Receiver(UserIF subscriber) {
this.userSubscriber = subscriber;
}
public void connect() {
try {
factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
// TODO Mécanisme de sécurité d'ActiveMQ à rétablir en production
factory.setTrustAllPackages(true);
connection = (ActiveMQConnection) factory.createConnection();
// ClientID :
// https://qnalist.com/questions/2068823/create-durable-topic-subscriber
connection.setClientID(userSubscriber.toString());
connection.start();
session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void receiveMessage(UserIF topic) {
try {
if (session == null) {
connect();
}
destination = session.createTopic(topic.toString());
String nomAbonnement = topic.toString() + "->" + userSubscriber.toString();
//String nomAbonnement = userSubscriber.toString();
consumer = session.createDurableSubscriber(destination, nomAbonnement);
consumer.setMessageListener(this);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void unsubscribe(UserIF topic) {
try {
if (session == null) {
connect();
}
System.out.println("\n RECEIVER Désinscription du topic " + topic.toString());
//consumer.close();
String nomAbonnement = topic.toString() + "->" + userSubscriber.toString();
//String nomAbonnement = userSubscriber.toString();
System.out.println("\n RECEIVER Abonnement à clore = " + nomAbonnement);
session.unsubscribe(nomAbonnement);
System.out.println("\n RECEIVER " + userSubscriber.toString() + " s'est désinscrit de " + nomAbonnement);
} catch (JMSException e) {
e.printStackTrace();
}
}
#Override
public void onMessage(Message message) {
System.out.println("\n RECEIVER OnMessage triggered for " + userSubscriber.toString());
listeMsg.add(message);
System.out.println("\n RECEIVER Nombre de messages reçus par " + userSubscriber + " = " + listeMsg.size());
String classe = message.getClass().getSimpleName();
System.out.println("\n RECEIVER Classe de message : " + classe);
try {
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
System.out.println("\n RECEIVER Information : " + text.getText());
}
if (message instanceof ObjectMessage) {
System.out.println("\n RECEIVER ObjectMessage");
ObjectMessage oMessage = (ObjectMessage) message;
if (oMessage.getObject() instanceof PostIF) {
PostIF post = (PostIF) oMessage.getObject();
String s = ((Post) post).getCorpsMessage();
System.out.println("\n RECEIVER Post : " + s);
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws JMSException {
/*
* EACH USER IS A TOPIC FOR OTHER USERS
* WHATEVER A USER DOES RESULTS IN A NOTIFICATION TO SUBSCRIBERS
*/
//CREATE USER
UserIF u1, u2, u3;
String[] nom = new String[5];
String[] prenom = new String[5];
String[] login = new String[5];
String[] password = new String[5];
Date[] naiss = new Date[5];
String[] mail = new String[5];
for (int i = 0; i < 5; i++) {
nom[i] = "nom_" + i;
prenom[i] = "prenom_" + i;
login[i] = "login_" + i;
password[i] = "password_" + i;
naiss[i] = new Date();
mail[i] = "mail_" + i;
}
u1 = new User(nom[0], prenom[0], login[0], password[0], naiss[0], mail[0]);
u2 = new User(nom[1], prenom[1], login[1], password[1], naiss[1], mail[1]);
u3 = new User(nom[2], prenom[2], login[2], password[2], naiss[2], mail[2]);
/*
* MAKE EACH USER A SUBSCRIBER
*/
Receiver receiver1 = new Receiver(u1);
Receiver receiver2 = new Receiver(u2);
Receiver receiver3 = new Receiver(u3);
/*
* PUT A MESSAGE LISTENER FOR EACH USER
*/
receiver1.receiveMessage(u2);
receiver1.receiveMessage(u3);
receiver2.receiveMessage(u1);
receiver2.receiveMessage(u3);
receiver3.receiveMessage(u1);
receiver3.receiveMessage(u2);
/*
* CALL THE SENDER CLASS TO SEND MESSAGES
*/
try {
Sender.main(args);
} catch (Exception e1) {
e1.printStackTrace();
}
/*
* A SLEEP TO HAVE ENOUGH TIME TO LOOK AT THE ACTIVEMQ CONSOLE
* CAN BE REMOVE
*/
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
/*
* UNSUBSCRIBE SUBSCRIBERS FROM TOPICS
*/
receiver1.unsubscribe(u2);
receiver1.unsubscribe(u3);
receiver2.unsubscribe(u1);
receiver2.unsubscribe(u3);
receiver3.unsubscribe(u1);
receiver3.unsubscribe(u2);
}
}
Each connection needs a unique ClientID : connection.setClientID("clientID");
My mistake was to misunderstand this unicity for a given client.
When a client subscribes to a Topic, there is one connection for this Topic. So, for a given client subscribed to 3 topics (for instance), 3 ClientID are needed because 3 connections are needed.
A ClientID has to be unique because it identifies one connection of one client for one topic.
That's why I had so many JMSException telling that "Durable Consumer was In Use" when I wanted to end its subscription.
You can only unsubscribe a durable subscription if there is no active subscriber currently consuming from it. It looks like your code creates several subscriptions and does not stop the consumers so of course the unsubscribe will fail, if you close down the consumers and then do the unsubscribe you should get the result you are looking for.
An example of durable subscription unsubscribe is here.
Here below is my client side script for WebSockets. In this code I have defined a WebSockets object with IP address and port.
Client Script:
var webSocket =
new WebSocket('ws://localhost:8080/Spring4JSONHandling/websocket');
webSocket.onmessage = function processMessage(message)
{
var jsonData = JSON.parse(message.data);
jsonforlogout = jsonData;
//var user=JSON.parse(username.data);
console.log(jsonData); //to print
if (jsonData.message != null){
var msgAdd = JSON.stringify(jsonData.message);
msgAdd = msgAdd.replace(/[&\/\\#,+()$~%.'"*?<>{}]/g, '');
alert("3"+msgAdd);
var xyz = msgAdd.split(" : ");
var logged = '${me}';
alert("logged" + logged);
alert("xyz[0]" + xyz[0]);
if (logged == xyz[0]){
alert("1");
$("#chat" + xyz[0]).chatbox("option", "boxManager").addMsg(xyz[0], xyz[1]);
} else{
alert("2");
$("#chat" + xyz[0]).chatbox("option", "boxManager").addMsg(xyz[0], xyz[1]);
}
}
/* if(jsonData.message=='close'){websocket.close();} */
if (jsonData.users != null)
{
document.getElementById("chatusers").innerHTML = "";
var loggedInUser = '${me}';
var i = 0;
while (i < jsonData.users.length)
{
var onlineAdd = JSON.stringify(jsonData.users[i]);
onlineAdd = onlineAdd.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
if (loggedInUser != onlineAdd){
var clone = "<a href='#' onclick=\"clickFun('" + onlineAdd.trim() + "');\" class='usr' data-chat-id='chat" + onlineAdd + "' data-chat-fname='" + onlineAdd + "' data-chat-lname='" + onlineAdd + "'"
+ " data-chat-status='online' data-chat-alertmsg='' data-chat-alertshow='true' data-rel='popover-hover' data-placement='right' data-html='true'"
+ " data-content=\"<div class='usr-card'>"
+ "<img src='Spring4JSONHandling/resources/img/1.png' alt='Jessica Dolof'>"
+ "<div class='usr-card-content'>"
+ "<h3>Jessica Dolof</h3>"
+ "<p>Sales Administrator</p>"
+ "</div>"
+ "</div>\"><i></i>" + onlineAdd + "</a>";
$('#chatusers').append(clone);
}
i++;
}
}
//or(i = 0; i < responselist.data.length; i++) {
/* var i=0;
while(i<jsonData.users.length)
{
var comboitem = document.createElement("option");
comboitem.text = jsonData.users[i];//"utsav";//
comboitem.value = jsonData.users[i];//"10";
usercombo.options.add(comboitem);
i++;
} */
}
function sendMessage(txt) {
//alert(messageText.value+ " h1");
webSocket.send(txt);
// messageText.value = "";
}
window.onbeforeunload = function () {
webSocket.onclose = function() {};
webSocket.close();
};
Here is the code for the server end point. In this code I have defined a WebSockets object with a server endpoint.
Server Code:
package com.outbottle.controllers;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
#ServerEndpoint("/websocket")
public class WebSocketTest {
static Set<Session> chatroomUsers= Collections.synchronizedSet(new HashSet<Session>());
private final static HashMap<String, WebSocketTest> sockets = new HashMap<>();
private String myUniqueId;
private String getMyUniqueId() {
// unique ID from this class' hash code
return Integer.toHexString(this.hashCode());
}
#OnOpen
public void handleOpen(Session userSession) throws IOException {
chatroomUsers.add(userSession);
System.out.println("user added"); //user added
this.myUniqueId = this.getMyUniqueId();
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+ myUniqueId);
WebSocketTest.sockets.put(this.myUniqueId, this);
System.out.println("###################################"+sockets);
}
#OnMessage
public void handleMessage(String message, Session userSession) throws IOException{
String username= (String) userSession.getUserProperties().get("username");
Iterator<Session> itr=chatroomUsers.iterator();
if(username==null){
userSession.getUserProperties().put("username", message);
userSession.getBasicRemote().sendText(buildJsonMessageData("System","You are now connected as "+message));
while (itr.hasNext()) (itr.next()).getBasicRemote().sendText(buildJsonUsersData()); }
else {
while (itr.hasNext()) {
itr.next().getBasicRemote().sendText(buildJsonMessageData(username,message));
}
}
}
#OnClose
public void handleClose(Session userSession) throws IOException {
// TODO Auto-generated method stub
System.out.println("user logout");
chatroomUsers.remove(userSession);
Iterator<Session> itr = chatroomUsers.iterator();
while (itr.hasNext()) (itr.next()).getBasicRemote().sendText(buildJsonUsersData());
}
private String buildJsonUsersData() {
Iterator<String> itr= getUserNames().iterator();
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
while (itr.hasNext()) jsonArrayBuilder.add((String)itr.next());
return Json.createObjectBuilder().add("users", jsonArrayBuilder).build().toString();
}
private String buildJsonMessageData(String username, String message)
{
JsonObject jsonObject=Json.createObjectBuilder().add("message", username+" : "+ message).build();
StringWriter stringWriter= new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(stringWriter)){
jsonWriter.write(jsonObject);
}
return stringWriter.toString();
}
private Set<String> getUserNames()
{
HashSet<String> returnSet = new HashSet<String>();
Iterator<Session> itr= chatroomUsers.iterator();
System.out.println("######################");
while (itr.hasNext())
{
returnSet.add(itr.next().getUserProperties().get("username").toString());
}
return returnSet;
}
}
The problem is when I send a message to the user on a different IP, all users with the same name get the message. I need to send the message to a particular session only.
It looks like you are iterating through all the sessions chatroomUsers in the handleMessage function.
If you only want to send the message to a particular user/session, do you not need to check for your username property here?
I've recently starting working on a java webapp (JSP / Servlet) that was developed by the internal developer of a company.
This app randomly doesn't return data, and inspecting the log I found some NullPointerExceptions related to the classes' member variable which holds the database connection. Following the stack trace it seems that a second thread closes the connection after it ended its task leaving the first thread without a connection.
By the needs of the company the app uses different databases, one which rules appdata, and others which contain data the app has to retrieve. So every class attached to the main servlet may connect to one or more databases depending on the task it has to accomplish.
I'm not familiar with JavaEE but giving a look at the database connection class, I see nothing which protect threads from conflicting each other.
Which is the correct way to handle such connections?
This is the code of the Database handler:
package it.metmi.mmasgis.utils;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class DBManager
{
private String szDatabase;
private String szUsername;
private String szPassword;
private String szError;
private Connection db;
private boolean bConnected;
private Logger logger;
public DBManager(String szDBName)
{
this(szDBName, "", "");
}
public DBManager(String szDBName, String szName, String szPass)
{
szDatabase = szDBName;
szUsername = szName;
szPassword = szPass;
bConnected = false;
szError = "";
logger = LogManager.getFormatterLogger(DBManager.class.getName());
}
public boolean connect()
{
logger.entry();
try {
Class.forName("com.mysql.jdbc.Driver");
if(!szDatabase.isEmpty())
{
String szCon = "jdbc:mysql://localhost/" + szDatabase;
if(!szUsername.isEmpty())
{
szCon += "?user=" + szUsername;
if(!szPassword.isEmpty())
szCon += "&password=" + szPassword;
}
db = DriverManager.getConnection(szCon);
bConnected = true;
} else {
logger.error("No database name!!");
System.exit(0);
}
} catch(SQLException | ClassNotFoundException e) {
szError = e.getMessage();
e.printStackTrace();
logger.error("Can't connect: %s", e);
}
return logger.exit(bConnected);
}
public void disconnect()
{
logger.entry();
try {
db.close();
bConnected = false;
} catch(SQLException e) {
e.printStackTrace();
logger.error("Can't disconnect: %s", e);
}
logger.exit();
}
public boolean isConnected()
{
return bConnected;
}
public String getError()
{
return szError;
}
public ArrayList<HashMap<String,String>> query(String szQuery)
{
logger.entry(szQuery);
ArrayList<HashMap<String,String>> aResults = new ArrayList<HashMap<String,String>>();
int iCols = 0;
try {
Statement stmt = db.createStatement();
logger.info("Query: %s", szQuery);
ResultSet rs = stmt.executeQuery(szQuery);
ResultSetMetaData rsmd = rs.getMetaData();
iCols = rsmd.getColumnCount();
while(rs.next())
{
HashMap<String,String> pv = new HashMap<String,String>();
for(int i = 0; i < iCols; i++)
{
String szCol = rsmd.getColumnLabel(i + 1);
String szVal = rs.getString(i + 1);
pv.put(szCol, szVal);
}
aResults.add(pv);
}
rs.close();
stmt.close();
} catch(SQLException e) {
e.printStackTrace();
szError = e.getMessage();
logger.error("Error executing query: %s", e);
}
return logger.exit(aResults);
}
public boolean update(String szQuery)
{
logger.entry(szQuery);
boolean bResult = false;
try {
Statement stmt = db.createStatement();
logger.info("Query: %s", szQuery);
stmt.executeUpdate(szQuery);
bResult = true;
stmt.close();
} catch(SQLException e) {
e.printStackTrace();
szError = e.getMessage();
bResult = false;
logger.error("Error executing query: %s", e);
}
return logger.exit(bResult);
}
}
The class Task which all the servlet classes are based on, is a simple abstract class:
package it.metmi.mmasgis.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class Task
{
public abstract void doTask(HttpServletRequest request, HttpServletResponse response);
}
The class which throws NullPointerExceptions it this one, during the invocation of db.disconnect(). This class is called rapidly via AJAX 4 or 5 times from the interface written in JS.
package it.metmi.mmasgis.servlet.params;
import it.metmi.mmasgis.servlet.Task;
import it.metmi.mmasgis.utils.Const;
import it.metmi.mmasgis.utils.DBManager;
import it.metmi.mmasgis.utils.Query;
import it.metmi.mmasgis.utils.Utility;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ClassType extends Task
{
private DBManager db = null;
private Logger logger = LogManager.getFormatterLogger(ClassType.class.getName());
#Override
public void doTask(HttpServletRequest request, HttpServletResponse response)
{
logger.entry(request, response);
String szCensimento = Utility.getParameter(request, "censimento");
String szCategoria = Utility.getParameter(request, "category");
ArrayList<HashMap<String,String>> aClasses = new ArrayList<HashMap<String,String>>();
PrintWriter out = null;
logger.debug("Census: %s", szCensimento);
logger.debug("Category: %s", szCategoria);
db = new DBManager(szCensimento, Const.DB_USER, Const.DB_PASS);
if(db.connect())
{
String szQuery = String.format(Query.classes, szCategoria, szCategoria);
aClasses = db.query(szQuery);
db.disconnect();
}
try {
out = response.getWriter();
jsonEncode(aClasses, out);
} catch(IOException e) {
e.printStackTrace();
logger.error("Failed to encode JSON: %s", e);
}
logger.exit();
}
private void jsonEncode(ArrayList<HashMap<String,String>> aData, PrintWriter out)
{
HashMap<String,Object> result = new HashMap<String,Object>();
result.put("results", aData);
result.put("success", true);
Gson gson = new GsonBuilder().create();
gson.toJson(result, out);
}
}
If the webapp would use only one database, it could be rewritten as a Singleton, but in this way I have no idea on how to handle different connections for different databases.
How can avoid these exceptions?
The problem was that the connection object was declared as member.
Moving the variable inside the methods resolved.
I've created users and assigned them different roles using wso2im. Using these I managed to restrict access to a .jsp file so roles seem to be working right.
The problem lies when I need to display different things to different roles within the same JSP (For instance, role AAA can do xxx and yyy, role BBB can do zzz), I'm trying to check for roles using request.isUserInRole("role") but it always returns null, both when trying from the .jsp itself and the servlet that handles authentication.
Finally managed to get it to work. Getting the roles with the servlet and storing them in a cookie. Neither safe nor pretty but does the job:
package foo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.um.ws.api.stub.RemoteUserStoreManagerServiceStub;
/**
* Servlet implementation class LoginServlet
*/
#WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String basicAuthUserID = "admin";
private final String basicAuthPassword = "admin";
private final String serverUrl = "https://localhost:9444/services/";
private RemoteUserStoreManagerServiceStub stub = null;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// get request parameters for userID and password
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
try {
if(authenticate(user,pwd)){
HttpSession session = request.getSession();
session.setAttribute("user", user);
//setting session to expiry in 30 mins
session.setMaxInactiveInterval(30*60);
Cookie userName = new Cookie("user", user);
userName.setMaxAge(30*60);
String[] roles = getRoleListOfUser(user);
String rolesTodos = null;
for (String s: roles){
if (!s.equals("Internal/everyone")) {
if (rolesTodos == null){
rolesTodos = s;
} else {
//System.out.println("Rol: " + s);
rolesTodos = rolesTodos + "," + s;
}
}
}
//System.out.println("Roles: " + rolesTodos);
Cookie rolesCookie = new Cookie("roles", rolesTodos);
rolesCookie.setMaxAge(30*60);
response.addCookie(userName);
response.addCookie(rolesCookie);
response.sendRedirect("index.jsp");
}else{
RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");
PrintWriter out= response.getWriter();
out.println("<font color=red>Either user name or password is wrong.</font>");
rd.include(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean authenticate(String userName, Object credential) throws Exception {
if (!(credential instanceof String)) {
throw new Exception("Unsupported type of password");
}
try {
if(stub == null) {
stub = new RemoteUserStoreManagerServiceStub(null, serverUrl
+ "RemoteUserStoreManagerService");
HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator();
basicAuth.setUsername(basicAuthUserID);
basicAuth.setPassword(basicAuthPassword);
basicAuth.setPreemptiveAuthentication(true);
final Options clientOptions = stub._getServiceClient().getOptions();
clientOptions.setProperty(HTTPConstants.AUTHENTICATE, basicAuth);
stub._getServiceClient().setOptions(clientOptions);
}
return stub.authenticate(userName, (String) credential);
} catch (Exception e) {
handleException(e.getMessage(), e);
}
return false;
}
private String[] handleException(String msg, Exception e) throws Exception {
System.out.println(e.getMessage() + e);
throw new Exception(msg, e);
}
public String[] getRoleListOfUser(String userName) throws Exception {
try {
return stub.getRoleListOfUser(userName);
} catch (Exception e) {
System.out.println(e.getMessage() + e);
}
return null;
}
}
I would like to know how I could call phonegap plugin from java, for example to call the javascript plugin that would desire: "cordova.exec(successCallback, failureCallback, 'CallListPlugin', 'list', params)"
But if I call it from the MainActivity? without going through the browser?
Regards!
CallListPlugin.java
package com.leafcut.ctrac;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.EmptyStackException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.text.format.DateFormat;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
/**
* Grab call log data
*
* #author James Hornitzky
*/
public class CallListPlugin extends CordovaPlugin {
/** List Action */
private static final String ACTION = "list";
private static final String CONTACT_ACTION = "contact";
private static final String SHOW_ACTION = "show";
private static final String TAG = "CallListPlugin";
/*
* (non-Javadoc)
*
* #see com.phonegap.api.Plugin#execute(java.lang.String,
* org.json.JSONArray, java.lang.String)
*/
#Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
boolean actionValid = true;
Log.d(TAG, "Plugin Called");
if (ACTION.equals(action)) {
try {
int limit = -1;
//obtain date to limit by
if (!data.isNull(0)) {
String d = data.getString(0);
Log.d(TAG, "Time period is: " + d);
if (d.equals("week"))
limit = -7;
else if (d.equals("month"))
limit = -30;
else if (d.equals("all"))
limit = -1000000; // LOL
}
//turn this into a date
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_YEAR, limit);
Date limitDate = calendar.getTime();
String limiter = String.valueOf(limitDate.getTime());
//now do required search
JSONObject callInfo = getCallListing(limiter);
Log.d(TAG, "Returning " + callInfo.toString());
// result = new PluginResult(Status.OK, callInfo);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, callInfo);
callbackContext.sendPluginResult(pluginResult);
callbackContext.success();
} catch (JSONException jsonEx) {
Log.d(TAG, "Got JSON Exception " + jsonEx.getMessage());
callbackContext.error(jsonEx.getMessage());
actionValid = false;
}
} else if (SHOW_ACTION.equals(action)) {
try {
if (!data.isNull(0)) {
viewContact(data.getString(0));
}
} catch (JSONException jsonEx) {
Log.d(TAG, "Got JSON Exception " + jsonEx.getMessage());
// result = new PluginResult(Status.JSON_EXCEPTION);
callbackContext.error(jsonEx.getMessage());
actionValid = false;
} catch (Exception e) {}
} else if (CONTACT_ACTION.equals(action)) {
try {
String contactInfo = getContactNameFromNumber(data.getString(0));
Log.d(TAG, "Returning " + contactInfo.toString());
// result = new PluginResult(Status.OK, contactInfo);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, contactInfo);
callbackContext.sendPluginResult(pluginResult);
callbackContext.success();
} catch (JSONException jsonEx) {
Log.d(TAG, "Got JSON Exception " + jsonEx.getMessage());
// result = new PluginResult(Status.JSON_EXCEPTION);
callbackContext.error(jsonEx.getMessage());
actionValid = false;
}
} else {
// result = new PluginResult(Status.INVALID_ACTION);
actionValid = false;
Log.d(TAG, "Invalid action : " + action + " passed");
}
// return result;
return actionValid;
}
/**
* Gets the Directory listing for file, in JSON format
*
* #param file
* The file for which we want to do directory listing
* #return JSONObject representation of directory list. e.g
* {"filename":"/sdcard"
* ,"isdir":true,"children":[{"filename":"a.txt"
* ,"isdir":false},{...}]}
* #throws JSONException
*/
private JSONObject getCallListing(String period) throws JSONException {
JSONObject callLog = new JSONObject();
String[] strFields = {
android.provider.CallLog.Calls.DATE,
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.TYPE,
android.provider.CallLog.Calls.DURATION,
android.provider.CallLog.Calls.NEW,
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.CACHED_NUMBER_TYPE,
android.provider.CallLog.Calls.CACHED_NUMBER_LABEL };
try {
Cursor callLogCursor = cordova.getActivity().getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
strFields,
CallLog.Calls.DATE + ">?",
new String[] {period},
android.provider.CallLog.Calls.DEFAULT_SORT_ORDER);
int callCount = callLogCursor.getCount();
if (callCount > 0) {
JSONObject callLogItem = new JSONObject();
JSONArray callLogItems = new JSONArray();
callLogCursor.moveToFirst();
do {
callLogItem.put("date", callLogCursor.getLong(0));
callLogItem.put("number", callLogCursor.getString(1));
callLogItem.put("type", callLogCursor.getInt(2));
callLogItem.put("duration", callLogCursor.getLong(3));
callLogItem.put("new", callLogCursor.getInt(4));
callLogItem.put("cachedName", callLogCursor.getString(5));
callLogItem.put("cachedNumberType", callLogCursor.getInt(6));
//callLogItem.put("name", getContactNameFromNumber(callLogCursor.getString(1))); //grab name too
callLogItems.put(callLogItem);
callLogItem = new JSONObject();
} while (callLogCursor.moveToNext());
callLog.put("rows", callLogItems);
}
callLogCursor.close();
} catch (Exception e) {
Log.d("CallLog_Plugin",
" ERROR : SQL to get cursor: ERROR " + e.getMessage());
}
return callLog;
}
/**
* Show contact data based on id
* #param number
*/
private void viewContact(String number) {
Intent i = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
Uri.parse(String.format("tel: %s", number)));
this.cordova.getActivity().startActivity(i);
}
/**
* Util method to grab name based on number
*
*/
private String getContactNameFromNumber(String number) {
// define the columns I want the query to return
String[] projection = new String[] { Contacts.Phones.DISPLAY_NAME, Contacts.Phones.NUMBER };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));
// query time
Cursor c = cordova.getActivity().getContentResolver().query(contactUri, projection, null, null, null);
// if the query returns 1 or more results
// return the first result
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
c.deactivate();
return name;
}
// return the original number if no match was found
return number;
}
}
You would want to import the package above into your main Java file with:
import com.leafcut.ctrac.CallListPlugin;
After that you should have access to any of the public methods of CallListPlugin, example:
CallListPlugin.execute(String some_string, JSONArray some_array, CallbackContext some_context);