use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.
the class DeleteWorkgroup method addStageInformation.
@Override
protected void addStageInformation(SessionData data, Element command) {
DataForm form = new DataForm(DataForm.Type.form);
form.setTitle("Delete workgroup");
form.addInstruction("Fill out this form to delete a workgroup.");
FormField field = form.addField();
field.setType(FormField.Type.hidden);
field.setVariable("FORM_TYPE");
field.addValue("http://jabber.org/protocol/admin");
field = form.addField();
field.setType(FormField.Type.jid_single);
field.setLabel("Workgroup's JID");
field.setVariable("workgroup");
field.setRequired(true);
// Add the form to the command
command.add(form.getElement());
}
use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.
the class FormManager method getDataForm.
public DataForm getDataForm(Workgroup workgroup) {
DbProperties props = workgroup.getProperties();
String context = "jive.dataform.wg";
String form = props.getProperty(context);
if (form != null) {
XStream xstream = new XStream();
xstream.setClassLoader(this.getClass().getClassLoader());
try {
return (DataForm) xstream.fromXML(form);
} catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
return null;
}
use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.
the class IQChatSearchHandler method handleIQ.
public void handleIQ(IQ packet) {
try {
// Check that the sender of this IQ is an agent
workgroupManager.getAgentManager().getAgent(packet.getFrom());
Element iq = packet.getChildElement();
IQ reply = IQ.createResultIQ(packet);
if (iq.elements().isEmpty()) {
reply.setChildElement(iq.createCopy());
// Send the search form to the agent
reply.addExtension(searchForm.createCopy());
workgroupManager.send(reply);
} else {
// Send the result of the search to the agent
Date startDate = null;
Date endDate = null;
Collection<Workgroup> workgroups = WorkgroupManager.getInstance().getWorkgroups();
JID agentJID = null;
String queryString = null;
// Get the search parameters from the completed form
DataForm submitedForm = (DataForm) packet.getExtension(DataForm.ELEMENT_NAME, DataForm.NAMESPACE);
for (FormField field : submitedForm.getFields()) {
if ("date/start".equals(field.getVariable())) {
try {
startDate = DataForm.parseDate(field.getValues().get(0));
} catch (ParseException e) {
Log.debug("Invalid startDate " + field.getValues().get(0), e);
}
} else if ("date/end".equals(field.getVariable())) {
try {
endDate = DataForm.parseDate(field.getValues().get(0));
} catch (ParseException e) {
Log.debug("Invalid endDate " + field.getValues().get(0), e);
}
} else if ("workgroups".equals(field.getVariable())) {
if (!field.getValues().isEmpty()) {
workgroups = new ArrayList<Workgroup>();
for (String value : field.getValues()) {
try {
workgroups.add(WorkgroupManager.getInstance().getWorkgroup(new JID(value)));
} catch (UserNotFoundException e) {
Log.debug("Invalid workgroup JID " + value, e);
}
}
} else {
// Search in all the workgroups since no one was specified
workgroups = WorkgroupManager.getInstance().getWorkgroups();
}
} else if ("agent".equals(field.getVariable())) {
agentJID = new JID(field.getValues().get(0));
} else if ("queryString".equals(field.getVariable())) {
queryString = field.getValues().get(0);
}
}
// Build the response
DataForm searchResults = resultForm.createCopy();
// Perform the search
for (Workgroup workgroup : workgroups) {
ChatSearch search = new ChatSearch(workgroup, startDate, endDate, agentJID, queryString);
for (QueryResult result : search.getResults()) {
Map<String, Object> fields = new LinkedHashMap<String, Object>();
fields.put("workgroup", result.getWorkgroup().getJID().toBareJID());
fields.put("sessionID", result.getSessionID());
fields.put("startDate", result.getStartDate());
fields.put("agentJIDs", result.getAgentJIDs());
fields.put("relevance", result.getRelevance());
// Add Metadata
Map<String, String> metadata = getMetadataMap(result.getSessionID());
if (metadata.containsKey("question")) {
fields.put("question", metadata.get("question"));
}
if (metadata.containsKey("email")) {
fields.put("email", metadata.get("email"));
}
if (metadata.containsKey("username")) {
fields.put("username", metadata.get("username"));
}
searchResults.addItemFields(fields);
}
}
reply.setChildElement(iq.getName(), iq.getNamespaceURI());
reply.addExtension(searchResults);
workgroupManager.send(reply);
}
} catch (AgentNotFoundException e) {
IQ reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.not_authorized));
workgroupManager.send(reply);
}
}
use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.
the class RegistrationHandler method setRegistrationForm.
/**
* Handles a IQ-register 'set' request, which is to be interpreted as a
* request to create a new registration.
*
* @param packet the IQ-register 'set' stanza.
* @throws UnauthorizedException if the user isn't allowed to register.
*/
private void setRegistrationForm(IQ packet) throws UnauthorizedException {
final JID from = packet.getFrom();
final boolean registered;
Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType);
if (registrations.iterator().hasNext()) {
registered = true;
} else {
registered = false;
}
if (!registered && !parent.permissionManager.hasAccess(from)) {
// registered.
throw new UnauthorizedException(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyacls", "kraken"));
}
// Parse the input variables
String username = null;
String password = null;
String nickname = null;
try {
if (packet.getChildElement().element("x") != null) {
final DataForm form = new DataForm(packet.getChildElement().element("x"));
final List<FormField> fields = form.getFields();
for (final FormField field : fields) {
final String var = field.getVariable();
if (var.equals("username")) {
username = field.getValues().get(0);
} else if (var.equals("password")) {
password = field.getValues().get(0);
} else if (var.equals("nick")) {
nickname = field.getValues().get(0);
}
}
}
}// specific subclasses instead).
catch (Exception ex) {
// No with data form apparently
Log.info("Most likely, no dataform was present " + "in the IQ-register request.", ex);
}
// input variables could also exist in the non-extended elements
final Element userEl = packet.getChildElement().element("username");
final Element passEl = packet.getChildElement().element("password");
final Element nickEl = packet.getChildElement().element("nick");
if (userEl != null) {
username = userEl.getTextTrim();
}
if (passEl != null) {
password = passEl.getTextTrim();
}
if (nickEl != null) {
nickname = nickEl.getTextTrim();
}
username = (username == null || username.equals("")) ? null : username;
password = (password == null || password.equals("")) ? null : password;
nickname = (nickname == null || nickname.equals("")) ? null : nickname;
// verify that we've got wat we need.
if (username == null || (parent.isPasswordRequired() && password == null) || (parent.isNicknameRequired() && nickname == null)) {
// Invalid information from stanza, lets yell.
Log.info("Cannot process IQ register request, as it " + "fails to provide all data that's required: " + packet.toXML());
final IQ result = IQ.createResultIQ(packet);
result.setError(Condition.bad_request);
parent.sendPacket(result);
return;
}
// Check if the client supports our proprietary 'rosterless' mode.
final boolean rosterlessMode;
final Element x = packet.getChildElement().element("x");
if (x != null && x.getNamespaceURI() != null && x.getNamespaceURI().equals(NameSpace.IQ_GATEWAY_REGISTER)) {
rosterlessMode = true;
Log.info("Registering " + packet.getFrom() + " as " + username + " in rosterless mode.");
} else {
rosterlessMode = false;
Log.info("Registering " + packet.getFrom() + " as " + username + " (without making use of rosterless mode).");
}
// Here's where the true magic lies: create the registration!
try {
addNewRegistration(from, username, password, nickname, rosterlessMode);
registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType);
Registration registration = registrations.iterator().next();
TransportSession session = parent.registrationLoggedIn(registration, from, PresenceType.available, "", -1);
session.setRegistrationPacket(packet);
session.detachSession();
parent.getSessionManager().storeSession(from, session);
//final IQ result = IQ.createResultIQ(packet);
// I believe this shouldn't be included. Leaving it around just in
// case.
// Element response =
// DocumentHelper.createElement(QName.get("query", IQ_REGISTER));
// result.setChildElement(response);
//parent.sendPacket(result);
} catch (UserNotFoundException e) {
Log.warn("Someone attempted to register with the gateway " + "who is not registered with the server: " + from);
final IQ eresult = IQ.createResultIQ(packet);
eresult.setError(Condition.forbidden);
parent.sendPacket(eresult);
final Message em = new Message();
em.setType(Message.Type.error);
em.setTo(packet.getFrom());
em.setFrom(packet.getTo());
em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniednoacct", "kraken"));
parent.sendPacket(em);
} catch (IllegalAccessException e) {
Log.warn("Someone who is not a user of this server " + "tried to register with the transport: " + from);
final IQ eresult = IQ.createResultIQ(packet);
eresult.setError(Condition.forbidden);
parent.sendPacket(eresult);
final Message em = new Message();
em.setType(Message.Type.error);
em.setTo(packet.getFrom());
em.setFrom(packet.getTo());
em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyhost", "kraken"));
parent.sendPacket(em);
} catch (IllegalArgumentException e) {
Log.warn("Someone attempted to register with the " + "gateway with an invalid username: " + from);
final IQ eresult = IQ.createResultIQ(packet);
eresult.setError(Condition.bad_request);
parent.sendPacket(eresult);
final Message em = new Message();
em.setType(Message.Type.error);
em.setTo(packet.getFrom());
em.setFrom(packet.getTo());
em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbadusername", "kraken"));
parent.sendPacket(em);
}
}
use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.
the class RegistrationHandler method getRegistrationForm.
/**
* Handles a IQ-register 'get' request, which is to be interpreted as a
* request for a registration form template. The template will be prefilled
* with data, if the requestee has a current registration with the gateway.
*
* @param packet the IQ-register 'get' stanza.
* @throws UnauthorizedException if the user is not allowed to make use of the gateway.
*/
private void getRegistrationForm(IQ packet) throws UnauthorizedException {
final JID from = packet.getFrom();
final IQ result = IQ.createResultIQ(packet);
// search for existing registrations
String curUsername = null;
String curPassword = null;
String curNickname = null;
Boolean registered = false;
final Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType);
if (registrations.iterator().hasNext()) {
Registration registration = registrations.iterator().next();
curUsername = registration.getUsername();
curPassword = registration.getPassword();
curNickname = registration.getNickname();
registered = true;
}
// Verify that the user is allowed to make use of the gateway.
if (!registered && !parent.permissionManager.hasAccess(from)) {
// registered.
throw new UnauthorizedException(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyacls", "kraken"));
}
// generate a template registration form.
final Element response = DocumentHelper.createElement(QName.get("query", NameSpace.IQ_REGISTER));
final DataForm form = new DataForm(DataForm.Type.form);
form.addInstruction(parent.getTerminologyRegistration());
final FormField usernameField = form.addField();
usernameField.setLabel(parent.getTerminologyUsername());
usernameField.setVariable("username");
usernameField.setType(FormField.Type.text_single);
if (curUsername != null) {
usernameField.addValue(curUsername);
}
final FormField passwordField = form.addField();
passwordField.setLabel(parent.getTerminologyPassword());
passwordField.setVariable("password");
passwordField.setType(FormField.Type.text_private);
if (curPassword != null) {
passwordField.addValue(curPassword);
}
final String nicknameTerm = parent.getTerminologyNickname();
if (nicknameTerm != null) {
FormField nicknameField = form.addField();
nicknameField.setLabel(nicknameTerm);
nicknameField.setVariable("nick");
nicknameField.setType(FormField.Type.text_single);
if (curNickname != null) {
nicknameField.addValue(curNickname);
}
}
response.add(form.getElement());
response.addElement("instructions").addText(parent.getTerminologyRegistration());
// exists.
if (registered) {
response.addElement("registered");
response.addElement("username").addText(curUsername);
if (curPassword == null) {
response.addElement("password");
} else {
response.addElement("password").addText(curPassword);
}
if (nicknameTerm != null) {
if (curNickname == null) {
response.addElement("nick");
} else {
response.addElement("nick").addText(curNickname);
}
}
} else {
response.addElement("username");
response.addElement("password");
if (nicknameTerm != null) {
response.addElement("nick");
}
}
// Add special indicator for rosterless gateway handling.
response.addElement(QName.get("x", NameSpace.IQ_GATEWAY_REGISTER));
result.setChildElement(response);
parent.sendPacket(result);
}
Aggregations