| 1 |
/** |
| 2 |
* ExtensibilityQueryBindingImpl.java |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
package test.wsdl.extensibility; |
| 7 |
|
| 8 |
import org.apache.axis.components.logger.LogFactory; |
| 9 |
import org.apache.axis.message.MessageElement; |
| 10 |
import org.apache.axis.message.Text; |
| 11 |
import org.apache.commons.logging.Log; |
| 12 |
|
| 13 |
import javax.xml.namespace.QName; |
| 14 |
import java.io.PrintWriter; |
| 15 |
import java.io.StringWriter; |
| 16 |
import java.rmi.RemoteException; |
| 17 |
import java.util.Calendar; |
| 18 |
|
| 19 |
public class ExtensibilityQueryBindingImpl implements ExtensibilityQueryPortType { |
| 20 |
private final static String[] books = new String[] { "The Grid", "The Oxford Dictionary" }; |
| 21 |
private final static String[] subjects = new String[] { "Computer Science", "English" }; |
| 22 |
protected static Log log = |
| 23 |
LogFactory.getLog(ExtensibilityQueryBindingImpl.class.getName()); |
| 24 |
|
| 25 |
public ExtensibilityType query(ExtensibilityType query) throws RemoteException { |
| 26 |
ExtensibilityType result = new ExtensibilityType(); |
| 27 |
Object obj = null; |
| 28 |
try { |
| 29 |
obj = query.get_any()[0].getObjectValue(BookType.class); |
| 30 |
} catch (Exception e) { |
| 31 |
StringWriter writer = new StringWriter(); |
| 32 |
PrintWriter out = new PrintWriter(writer); |
| 33 |
log.error("Error converting query: " + writer.toString()); |
| 34 |
throw new RemoteException(e.toString()); |
| 35 |
} |
| 36 |
log.error("Incoming MessageContext " + obj + " : " + query.get_any()[0].toString()); |
| 37 |
if (obj instanceof BookType) { |
| 38 |
BookType bookQuery = (BookType)obj; |
| 39 |
String subject = bookQuery.getSubject(); |
| 40 |
if (!"all".equals(subject)) { |
| 41 |
throw new RemoteException("ExtensibilityQueryBindingImpl: Book subject query should be all, instead was " + subject); |
| 42 |
} |
| 43 |
ResultListType resultList = new ResultListType(); |
| 44 |
QueryResultType[] queryResult = new QueryResultType[books.length]; |
| 45 |
for (int i = 0; i < books.length; i++) { |
| 46 |
queryResult[i] = new QueryResultType(); |
| 47 |
queryResult[i].setName(subjects[i]); |
| 48 |
queryResult[i].setStatus(StatusType.MORE); |
| 49 |
queryResult[i].setValue(books[i]); |
| 50 |
queryResult[i].setTime(Calendar.getInstance()); |
| 51 |
queryResult[i].setQueryType(new QName("urn:QueryType","BookQuery")); |
| 52 |
} |
| 53 |
resultList.setResult(queryResult); |
| 54 |
QName elementName = QueryResultElement.getTypeDesc().getFields()[0].getXmlName(); |
| 55 |
MessageElement me = new MessageElement(elementName.getNamespaceURI(), elementName.getLocalPart(), resultList); |
| 56 |
log.debug("Outgoing message: " + me.toString()); |
| 57 |
result.set_any(new MessageElement [] { me }); |
| 58 |
} else { |
| 59 |
throw new RemoteException("Failed to get FindBooksQueryExpressionElement. Got: " + obj); |
| 60 |
} |
| 61 |
return result; |
| 62 |
} |
| 63 |
|
| 64 |
public ExtensibilityType mixedQuery(ExtensibilityType query) |
| 65 |
throws RemoteException { |
| 66 |
MessageElement [] elements = query.get_any(); |
| 67 |
if (elements == null) { |
| 68 |
throw new RemoteException("No any"); |
| 69 |
} |
| 70 |
if (elements.length != 3) { |
| 71 |
throw new RemoteException("Expected: 3 got: " + elements.length + |
| 72 |
" element"); |
| 73 |
} |
| 74 |
|
| 75 |
String expected = "123 456"; |
| 76 |
String received = elements[0].toString(); |
| 77 |
|
| 78 |
if (!expected.equals(received)) { |
| 79 |
throw new RemoteException("Expected: " + expected + |
| 80 |
" received: " + received); |
| 81 |
} |
| 82 |
|
| 83 |
Object obj = null; |
| 84 |
try { |
| 85 |
obj = elements[1].getObjectValue(BookType.class); |
| 86 |
} catch (Exception e) { |
| 87 |
throw new RemoteException("Failed to deserialize", e); |
| 88 |
} |
| 89 |
BookType bookQuery = (BookType)obj; |
| 90 |
String subject = bookQuery.getSubject(); |
| 91 |
if (!"all".equals(subject)) { |
| 92 |
throw new RemoteException("ExtensibilityQueryBindingImpl: Book subject query should be all, instead was " + subject); |
| 93 |
} |
| 94 |
|
| 95 |
expected = "789"; |
| 96 |
received = elements[2].toString(); |
| 97 |
|
| 98 |
if (!expected.equals(received)) { |
| 99 |
throw new RemoteException("Expected: " + expected + |
| 100 |
" received: " + received); |
| 101 |
} |
| 102 |
|
| 103 |
ExtensibilityType reply = new ExtensibilityType(); |
| 104 |
|
| 105 |
MessageElement [] replyElements = new MessageElement[2]; |
| 106 |
|
| 107 |
BookType book = new BookType(); |
| 108 |
book.setSubject("gotAll"); |
| 109 |
QName elementName = FindBooksQueryExpressionElement.getTypeDesc().getFields()[0].getXmlName(); |
| 110 |
replyElements[0] = new MessageElement(elementName.getNamespaceURI(), elementName.getLocalPart(), book); |
| 111 |
replyElements[1] = new MessageElement(new Text("ABCD")); |
| 112 |
|
| 113 |
reply.set_any(replyElements); |
| 114 |
|
| 115 |
return reply; |
| 116 |
} |
| 117 |
} |