if ((id.equals(session.getDomain())
|| id.equals(session.getUserId()))
&& packet.getFrom().equals(session.getConnectionId())) {
Authorization result = Authorization.NOT_AUTHORIZED;
Element request = packet.getElement();
StanzaType type = packet.getType();
switch (type) {
case set:
// Is it registration cancel request?
Element elem = request.findChild("/iq/query/remove");
if (elem != null) {
// Yes this is registration cancel request
// According to JEP-0077 there must not be any
// more subelemets apart from <remove/>
elem = request.findChild("/iq/query");
if (elem.getChildren().size() > 1) {
result = Authorization.BAD_REQUEST;
} else {
try {
result = session.unregister(packet.getElemFrom());
results.offer(packet.okResult((String)null, 0));
results.offer(Command.CLOSE.getPacket(session.getDomain(),
session.getConnectionId(), StanzaType.set,
session.nextStanzaId()));
} catch (NotAuthorizedException e) {
results.offer(Authorization.NOT_AUTHORIZED.getResponseMessage(packet,
"You must authorize session first.", true));
} // end of try-catch
}
} else {
// No, so assuming this is registration of new
// user or change registration details for existing user
String user_name = request.getChildCData("/iq/query/username");
String password = request.getChildCData("/iq/query/password");
String email = request.getChildCData("/iq/query/email");
result = session.register(user_name, password, email);
if (result == Authorization.AUTHORIZED) {
results.offer(result.getResponseMessage(packet, null, false));
} else {
results.offer(result.getResponseMessage(packet,
"Unsuccessful registration attempt", true));
}
}
break;
case get:
results.offer(packet.okResult(
"<instructions>" +
"Choose a user name and password for use with this service." +
"Please provide also your e-mail address." +
"</instructions>" +
"<username/>" +
"<password/>" +
"<email/>", 1));
break;
case result:
// It might be a registration request from transport for example...
Element elem_res = packet.getElement().clone();
Packet pack_res = new Packet(elem_res);
pack_res.setTo(session.getConnectionId());
results.offer(pack_res);
break;
default:
results.offer(Authorization.BAD_REQUEST.getResponseMessage(packet,
"Message type is incorrect", true));
break;
} // end of switch (type)
} else {
if (id.equals(session.getUserId())) {
// It might be a registration request from transport for example...
Element elem_res = packet.getElement().clone();
Packet pack_res = new Packet(elem_res);
pack_res.setTo(session.getConnectionId());
results.offer(pack_res);
} else {
Element result = packet.getElement().clone();
results.offer(new Packet(result));
}
}
} catch (NotAuthorizedException e) {
results.offer(Authorization.NOT_AUTHORIZED.getResponseMessage(packet,