1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-12 05:51:08 +01:00

Prefix subprojects with 'smack-'

instead of using the old baseName=smack appendix=project.name approach,
we are now going convention over configuration and renaming the
subprojects directories to the proper name.

Having a prefix is actually very helpful, because the resulting
libraries will be named like the subproject. And a core-4.0.0-rc1.jar is
not as explicit about what it actually *is* as a
smack-core-4.0.0-rc1.jar.

SMACK-265
This commit is contained in:
Florian Schmaus 2014-04-28 19:27:53 +02:00
parent b6fb1f3743
commit 91fd15ad86
758 changed files with 42 additions and 42 deletions

View file

@ -0,0 +1,273 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.search;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jivesoftware.smackx.xdata.packet.DataForm.Item;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Represents a set of data results returned as part of a search. The report is structured
* in columns and rows.
*
* @author Gaston Dombiak
*/
public class ReportedData {
private List<Column> columns = new ArrayList<Column>();
private List<Row> rows = new ArrayList<Row>();
private String title = "";
/**
* Returns a new ReportedData if the packet is used for reporting data and includes an
* extension that matches the elementName and namespace "x","jabber:x:data".
*
* @param packet the packet used for reporting data.
*/
public static ReportedData getReportedDataFrom(Packet packet) {
// Check if the packet includes the DataForm extension
PacketExtension packetExtension = packet.getExtension("x","jabber:x:data");
if (packetExtension != null) {
// Check if the existing DataForm is a result of a search
DataForm dataForm = (DataForm) packetExtension;
if (dataForm.getReportedData() != null)
return new ReportedData(dataForm);
}
// Otherwise return null
return null;
}
/**
* Creates a new ReportedData based on the returned dataForm from a search
*(namespace "jabber:iq:search").
*
* @param dataForm the dataForm returned from a search (namespace "jabber:iq:search").
*/
private ReportedData(DataForm dataForm) {
// Add the columns to the report based on the reported data fields
for (FormField field : dataForm.getReportedData().getFields()) {
columns.add(new Column(field.getLabel(), field.getVariable(), field.getType()));
}
// Add the rows to the report based on the form's items
for (Item item : dataForm.getItems()) {
List<Field> fieldList = new ArrayList<Field>(columns.size());
for (FormField field : item.getFields()) {
// The field is created with all the values of the data form's field
List<String> values = new ArrayList<String>();
for (String value : field.getValues()) {
values.add(value);
}
fieldList.add(new Field(field.getVariable(), values));
}
rows.add(new Row(fieldList));
}
// Set the report's title
this.title = dataForm.getTitle();
}
public ReportedData(){
// Allow for model creation of ReportedData.
}
/**
* Adds a new <code>Row</code>.
* @param row the new row to add.
*/
public void addRow(Row row){
rows.add(row);
}
/**
* Adds a new <code>Column</code>
* @param column the column to add.
*/
public void addColumn(Column column){
columns.add(column);
}
/**
* Returns a List of the rows returned from a search.
*
* @return a List of the rows returned from a search.
*/
public List<Row> getRows() {
return Collections.unmodifiableList(new ArrayList<Row>(rows));
}
/**
* Returns a List of the columns returned from a search.
*
* @return a List of the columns returned from a search.
*/
public List<Column> getColumns() {
return Collections.unmodifiableList(new ArrayList<Column>(columns));
}
/**
* Returns the report's title. It is similar to the title on a web page or an X
* window.
*
* @return title of the report.
*/
public String getTitle() {
return title;
}
/**
*
* Represents the columns definition of the reported data.
*
* @author Gaston Dombiak
*/
public static class Column {
private String label;
private String variable;
private String type;
/**
* Creates a new column with the specified definition.
*
* @param label the columns's label.
* @param variable the variable name of the column.
* @param type the format for the returned data.
*/
public Column(String label, String variable, String type) {
this.label = label;
this.variable = variable;
this.type = type;
}
/**
* Returns the column's label.
*
* @return label of the column.
*/
public String getLabel() {
return label;
}
/**
* Returns the column's data format. Valid formats are:
*
* <ul>
* <li>text-single -> single line or word of text
* <li>text-private -> instead of showing the user what they typed, you show ***** to
* protect it
* <li>text-multi -> multiple lines of text entry
* <li>list-single -> given a list of choices, pick one
* <li>list-multi -> given a list of choices, pick one or more
* <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0
* <li>fixed -> fixed for putting in text to show sections, or just advertise your web
* site in the middle of the form
* <li>hidden -> is not given to the user at all, but returned with the questionnaire
* <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based
* on the rules for a JID.
* <li>jid-multi -> multiple entries for JIDs
* </ul>
*
* @return format for the returned data.
*/
public String getType() {
return type;
}
/**
* Returns the variable name that the column is showing.
*
* @return the variable name of the column.
*/
public String getVariable() {
return variable;
}
}
public static class Row {
private List<Field> fields = new ArrayList<Field>();
public Row(List<Field> fields) {
this.fields = fields;
}
/**
* Returns the values of the field whose variable matches the requested variable.
*
* @param variable the variable to match.
* @return the values of the field whose variable matches the requested variable.
*/
public List<String> getValues(String variable) {
for(Field field : getFields()) {
if (variable.equalsIgnoreCase(field.getVariable())) {
return field.getValues();
}
}
return null;
}
/**
* Returns the fields that define the data that goes with the item.
*
* @return the fields that define the data that goes with the item.
*/
private List<Field> getFields() {
return Collections.unmodifiableList(new ArrayList<Field>(fields));
}
}
public static class Field {
private String variable;
private List<String> values;
public Field(String variable, List<String> values) {
this.variable = variable;
this.values = values;
}
/**
* Returns the variable name that the field represents.
*
* @return the variable name of the field.
*/
public String getVariable() {
return variable;
}
/**
* Returns a List of the values reported as part of the search.
*
* @return the returned values of the search.
*/
public List<String> getValues() {
return Collections.unmodifiableList(values);
}
}
}

View file

@ -0,0 +1,147 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.search;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smackx.xdata.FormField;
import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayList;
import java.util.List;
/**
* SimpleUserSearch is used to support the non-dataform type of JEP 55. This provides
* the mechanism for allowing always type ReportedData to be returned by any search result,
* regardless of the form of the data returned from the server.
*
* @author Derek DeMoro
*/
class SimpleUserSearch extends IQ {
private Form form;
private ReportedData data;
public void setForm(Form form) {
this.form = form;
}
public ReportedData getReportedData() {
return data;
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"jabber:iq:search\">");
buf.append(getItemsToSearch());
buf.append("</query>");
return buf.toString();
}
private String getItemsToSearch() {
StringBuilder buf = new StringBuilder();
if (form == null) {
form = Form.getFormFrom(this);
}
if (form == null) {
return "";
}
for (FormField field : form.getFields()) {
String name = field.getVariable();
String value = getSingleValue(field);
if (value.trim().length() > 0) {
buf.append("<").append(name).append(">").append(value).append("</").append(name).append(">");
}
}
return buf.toString();
}
private static String getSingleValue(FormField formField) {
List<String> values = formField.getValues();
if (values.isEmpty()) {
return "";
} else {
return values.get(0);
}
}
protected void parseItems(XmlPullParser parser) throws Exception {
ReportedData data = new ReportedData();
data.addColumn(new ReportedData.Column("JID", "jid", "text-single"));
boolean done = false;
List<ReportedData.Field> fields = new ArrayList<ReportedData.Field>();
while (!done) {
if (parser.getAttributeCount() > 0) {
String jid = parser.getAttributeValue("", "jid");
List<String> valueList = new ArrayList<String>();
valueList.add(jid);
ReportedData.Field field = new ReportedData.Field("jid", valueList);
fields.add(field);
}
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("item")) {
fields = new ArrayList<ReportedData.Field>();
}
else if (eventType == XmlPullParser.END_TAG && parser.getName().equals("item")) {
ReportedData.Row row = new ReportedData.Row(fields);
data.addRow(row);
}
else if (eventType == XmlPullParser.START_TAG) {
String name = parser.getName();
String value = parser.nextText();
List<String> valueList = new ArrayList<String>();
valueList.add(value);
ReportedData.Field field = new ReportedData.Field(name, valueList);
fields.add(field);
boolean exists = false;
for (ReportedData.Column column : data.getColumns()) {
if (column.getVariable().equals(name)) {
exists = true;
break;
}
}
// Column name should be the same
if (!exists) {
ReportedData.Column column = new ReportedData.Column(name, name, "text-single");
data.addColumn(column);
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("query")) {
done = true;
}
}
}
this.data = data;
}
}

View file

@ -0,0 +1,212 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.search;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.xmlpull.v1.XmlPullParser;
/**
* Implements the protocol currently used to search information repositories on the Jabber network. To date, the jabber:iq:search protocol
* has been used mainly to search for people who have registered with user directories (e.g., the "Jabber User Directory" hosted at users.jabber.org).
* However, the jabber:iq:search protocol is not limited to user directories, and could be used to search other Jabber information repositories
* (such as chatroom directories) or even to provide a Jabber interface to conventional search engines.
* <p/>
* The basic functionality is to query an information repository regarding the possible search fields, to send a search query, and to receive search results.
*
* @author Derek DeMoro
*/
public class UserSearch extends IQ {
/**
* Creates a new instance of UserSearch.
*/
public UserSearch() {
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"jabber:iq:search\">");
buf.append(getExtensionsXML());
buf.append("</query>");
return buf.toString();
}
/**
* Returns the form for all search fields supported by the search service.
*
* @param con the current XMPPConnection.
* @param searchService the search service to use. (ex. search.jivesoftware.com)
* @return the search form received by the server.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public Form getSearchForm(XMPPConnection con, String searchService) throws NoResponseException, XMPPErrorException, NotConnectedException {
UserSearch search = new UserSearch();
search.setType(IQ.Type.GET);
search.setTo(searchService);
IQ response = (IQ) con.createPacketCollectorAndSend(search).nextResultOrThrow();
return Form.getFormFrom(response);
}
/**
* Sends the filled out answer form to be sent and queried by the search service.
*
* @param con the current XMPPConnection.
* @param searchForm the <code>Form</code> to send for querying.
* @param searchService the search service to use. (ex. search.jivesoftware.com)
* @return ReportedData the data found from the query.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public ReportedData sendSearchForm(XMPPConnection con, Form searchForm, String searchService) throws NoResponseException, XMPPErrorException, NotConnectedException {
UserSearch search = new UserSearch();
search.setType(IQ.Type.SET);
search.setTo(searchService);
search.addExtension(searchForm.getDataFormToSend());
IQ response = (IQ) con.createPacketCollectorAndSend(search).nextResultOrThrow();
return ReportedData.getReportedDataFrom(response);
}
/**
* Sends the filled out answer form to be sent and queried by the search service.
*
* @param con the current XMPPConnection.
* @param searchForm the <code>Form</code> to send for querying.
* @param searchService the search service to use. (ex. search.jivesoftware.com)
* @return ReportedData the data found from the query.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public ReportedData sendSimpleSearchForm(XMPPConnection con, Form searchForm, String searchService) throws NoResponseException, XMPPErrorException, NotConnectedException {
SimpleUserSearch search = new SimpleUserSearch();
search.setForm(searchForm);
search.setType(IQ.Type.SET);
search.setTo(searchService);
SimpleUserSearch response = (SimpleUserSearch) con.createPacketCollectorAndSend(search).nextResultOrThrow();
return response.getReportedData();
}
/**
* Internal Search service Provider.
*/
public static class Provider implements IQProvider {
/**
* Provider Constructor.
*/
public Provider() {
super();
}
public IQ parseIQ(XmlPullParser parser) throws Exception {
UserSearch search = null;
SimpleUserSearch simpleUserSearch = new SimpleUserSearch();
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("instructions")) {
buildDataForm(simpleUserSearch, parser.nextText(), parser);
return simpleUserSearch;
}
else if (eventType == XmlPullParser.START_TAG && parser.getName().equals("item")) {
simpleUserSearch.parseItems(parser);
return simpleUserSearch;
}
else if (eventType == XmlPullParser.START_TAG && parser.getNamespace().equals("jabber:x:data")) {
// Otherwise, it must be a packet extension.
search = new UserSearch();
search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(),
parser.getNamespace(), parser));
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("query")) {
done = true;
}
}
}
if (search != null) {
return search;
}
return simpleUserSearch;
}
}
private static void buildDataForm(SimpleUserSearch search, String instructions, XmlPullParser parser) throws Exception {
DataForm dataForm = new DataForm(Form.TYPE_FORM);
boolean done = false;
dataForm.setTitle("User Search");
dataForm.addInstruction(instructions);
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG && !parser.getNamespace().equals("jabber:x:data")) {
String name = parser.getName();
FormField field = new FormField(name);
// Handle hard coded values.
if(name.equals("first")){
field.setLabel("First Name");
}
else if(name.equals("last")){
field.setLabel("Last Name");
}
else if(name.equals("email")){
field.setLabel("Email Address");
}
else if(name.equals("nick")){
field.setLabel("Nickname");
}
field.setType(FormField.TYPE_TEXT_SINGLE);
dataForm.addField(field);
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("query")) {
done = true;
}
}
else if (eventType == XmlPullParser.START_TAG && parser.getNamespace().equals("jabber:x:data")) {
search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(),
parser.getNamespace(), parser));
done = true;
}
}
if (search.getExtension("x", "jabber:x:data") == null) {
search.addExtension(dataForm);
}
}
}

View file

@ -0,0 +1,129 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.search;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.xdata.Form;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* The UserSearchManager is a facade built upon Jabber Search Services (JEP-055) to allow for searching
* repositories on a Jabber Server. This implementation allows for transparency of implementation of
* searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both
* types of support.
* <pre>
* XMPPConnection con = new XMPPTCPConnection("jabber.org");
* con.login("john", "doe");
* UserSearchManager search = new UserSearchManager(con, "users.jabber.org");
* Form searchForm = search.getSearchForm();
* Form answerForm = searchForm.createAnswerForm();
* answerForm.setAnswer("last", "DeMoro");
* ReportedData data = search.getSearchResults(answerForm);
* // Use Returned Data
* </pre>
*
* @author Derek DeMoro
*/
public class UserSearchManager {
private XMPPConnection con;
private UserSearch userSearch;
/**
* Creates a new UserSearchManager.
*
* @param con the XMPPConnection to use.
*/
public UserSearchManager(XMPPConnection con) {
this.con = con;
userSearch = new UserSearch();
}
/**
* Returns the form to fill out to perform a search.
*
* @param searchService the search service to query.
* @return the form to fill out to perform a search.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public Form getSearchForm(String searchService) throws NoResponseException, XMPPErrorException, NotConnectedException {
return userSearch.getSearchForm(con, searchService);
}
/**
* Submits a search form to the server and returns the resulting information
* in the form of <code>ReportedData</code>
*
* @param searchForm the <code>Form</code> to submit for searching.
* @param searchService the name of the search service to use.
* @return the ReportedData returned by the server.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public ReportedData getSearchResults(Form searchForm, String searchService) throws NoResponseException, XMPPErrorException, NotConnectedException {
return userSearch.sendSearchForm(con, searchForm, searchService);
}
/**
* Returns a collection of search services found on the server.
*
* @return a Collection of search services found on the server.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public Collection<String> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException {
final List<String> searchServices = new ArrayList<String>();
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
DiscoverItems items = discoManager.discoverItems(con.getServiceName());
for (DiscoverItems.Item item : items.getItems()) {
try {
DiscoverInfo info;
try {
info = discoManager.discoverInfo(item.getEntityID());
}
catch (XMPPException e) {
// Ignore Case
continue;
}
if (info.containsFeature("jabber:iq:search")) {
searchServices.add(item.getEntityID());
}
}
catch (Exception e) {
// No info found.
break;
}
}
return searchServices;
}
}