001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: Basic.java,v 1.13 2007/03/29 11:45:41 schnelle Exp $
021: *
022: * $Log: Basic.java,v $
023: * Revision 1.13 2007/03/29 11:45:41 schnelle
024: * Code cleanup.
025: *
026: * Revision 1.12 2007/03/27 21:59:42 mlipp
027: * Fixed lots of checkstyle warnings.
028: *
029: * Revision 1.11 2007/03/01 12:29:30 schnelle
030: * Enhanced Instance.SetProperties to process ContextData.
031: *
032: * Revision 1.10 2007/02/02 08:56:24 drmlipp
033: * Fixed URL.
034: *
035: * Revision 1.9 2007/02/02 08:18:29 schnelle
036: * Prepared tests with an Umlaut.
037: *
038: * Revision 1.8 2007/02/01 13:48:52 schnelle
039: * Extracted servlet URL into a constant.
040: *
041: * Revision 1.7 2007/01/31 15:13:20 schnelle
042: * Unified method to create the base url.
043: *
044: * Revision 1.6 2007/01/31 10:57:36 schnelle
045: * Adapted to change in receiver key.
046: *
047: * Revision 1.5 2007/01/30 11:56:15 drmlipp
048: * Merged Wf-XML branch.
049: *
050: * Revision 1.4.6.10 2007/01/10 14:17:21 schnelle
051: * Implemented unsubscribe.
052: *
053: * Revision 1.4.6.9 2006/12/12 09:35:19 schnelle
054: * Implemented ChangeState for Instance.
055: *
056: * Revision 1.4.6.8 2006/12/01 15:31:28 schnelle
057: * Separation of test cases dependend on the role.
058: *
059: * Revision 1.4.6.7 2006/12/01 14:18:00 schnelle
060: * Added support for schema type for data in create process
061: *
062: * Revision 1.4.6.6 2006/12/01 12:50:11 schnelle
063: * Basic import of context data for process creation.
064: *
065: * Revision 1.4.6.5 2006/11/30 12:45:20 schnelle
066: * Basic implementation of Factory CreateInstance.
067: *
068: * Revision 1.4.6.4 2006/11/30 10:38:21 schnelle
069: * Implementation of Factory ListInstance.
070: *
071: * Revision 1.4.6.3 2006/11/29 14:13:03 schnelle
072: * Take respect to namespaces of asap requests and responses.
073: *
074: * Revision 1.4.6.2 2006/11/29 11:05:40 schnelle
075: * Full implementation of the request and response headers.
076: *
077: * Revision 1.4.6.1 2006/11/28 15:32:35 schnelle
078: * Proper selection of the response generator.
079: *
080: * Revision 1.4 2006/09/29 12:32:11 drmlipp
081: * Consistently using WfMOpen as projct name now.
082: *
083: * Revision 1.3 2005/01/24 21:11:39 mlipp
084: * Extended.
085: *
086: * Revision 1.2 2005/01/24 20:26:01 mlipp
087: * Reverted saaj back to 1.1 to fit Axis version.
088: *
089: * Revision 1.1 2005/01/23 21:44:23 mlipp
090: * Initial version.
091: *
092: */
093: package wfxml;
094:
095: import java.net.MalformedURLException;
096: import java.net.URL;
097: import java.util.Iterator;
098:
099: import javax.xml.soap.MessageFactory;
100: import javax.xml.soap.Node;
101: import javax.xml.soap.SOAPBody;
102: import javax.xml.soap.SOAPBodyElement;
103: import javax.xml.soap.SOAPConnection;
104: import javax.xml.soap.SOAPConnectionFactory;
105: import javax.xml.soap.SOAPElement;
106: import javax.xml.soap.SOAPEnvelope;
107: import javax.xml.soap.SOAPException;
108: import javax.xml.soap.SOAPFault;
109: import javax.xml.soap.SOAPHeader;
110: import javax.xml.soap.SOAPHeaderElement;
111: import javax.xml.soap.SOAPMessage;
112:
113: import org.w3c.dom.NodeList;
114:
115: import junit.framework.AssertionFailedError;
116: import de.danet.an.util.XMLUtil;
117:
118: /**
119: * Utility methods for WfXML tests.
120: * @author Dirk Schnelle
121: */
122: public class Basic {
123: /** URL of the WfXML servlet. */
124: public static final String WFXML_SERVLET = "http://localhost:8080/wfxml";
125:
126: /** The URI of the ASAP version used. */
127: public static final String ASAP_NS = "http://www.oasis-open.org/asap/0.9/asap.xsd";
128:
129: /** The URI of the WfXML version used. */
130: public static final String WFXML_NS = "http://www.wfmc.org/wfxml/2.0/wfxml20.xsd";
131:
132: /**
133: * Utility method to create a base URL for the given resource.
134: * @param resource the target resource.
135: * @return base url.
136: */
137: static String getBaseUrl(String resource) {
138: return WFXML_SERVLET + "?Resource=" + resource;
139: }
140:
141: /**
142: * Utility method to create an empty SOAP message.
143: * @return Empty SOAP message.
144: * @throws SOAPException
145: */
146: static SOAPMessage createMessage() throws SOAPException {
147: MessageFactory messageFactory = MessageFactory.newInstance();
148: return messageFactory.createMessage();
149: }
150:
151: /**
152: * Fills all detail data in the request header.
153: * @param message the message to manipulate.
154: * @param receiver receiver key
155: * @param sender sender key
156: * @param id user defined id
157: * @throws SOAPException
158: * error manipulating the SOAP message.
159: */
160: static void fillRequestHeadet(SOAPMessage message, String receiver,
161: String sender, String id) throws SOAPException {
162: SOAPEnvelope env = message.getSOAPPart().getEnvelope();
163: SOAPHeader header = env.getHeader();
164: SOAPHeaderElement req = header.addHeaderElement(env.createName(
165: "Request", "as", Basic.ASAP_NS));
166: SOAPElement receiverkey = req.addChildElement("ReceiverKey",
167: "as");
168: receiverkey.addTextNode(receiver);
169: SOAPElement senderkey = req.addChildElement("SenderKey", "as");
170: senderkey.addTextNode(sender);
171: if (id != null) {
172: SOAPElement requestid = req.addChildElement("RequestID",
173: "as");
174: requestid.addTextNode(id);
175: }
176: }
177:
178: /**
179: * Creates an action body element with the given name.
180: * @param message
181: * @param request
182: * @return
183: * @throws SOAPException
184: */
185: static SOAPBodyElement createActionElement(SOAPMessage message,
186: String request) throws SOAPException {
187: SOAPEnvelope env = message.getSOAPPart().getEnvelope();
188: SOAPBody body = env.getBody();
189: return body.addBodyElement(env.createName(request, "wfx",
190: Basic.WFXML_NS));
191: }
192:
193: /**
194: * Calls the default endpoint with the given message.
195: * @param request the SOAP request.
196: * @return SOAP response.
197: * @throws SOAPException
198: * Error in the SOAP message.
199: */
200: static SOAPMessage call(SOAPMessage request) throws SOAPException {
201: SOAPConnectionFactory factory = SOAPConnectionFactory
202: .newInstance();
203: SOAPConnection connection = factory.createConnection();
204:
205: URL endpoint;
206: try {
207: endpoint = new URL(WFXML_SERVLET);
208: } catch (MalformedURLException e) {
209: throw new SOAPException(e.getMessage(), e);
210: }
211:
212: return connection.call(request, endpoint);
213: }
214:
215: /**
216: * Retrieves the given key from the header of the given message.
217: * @param message the message to inspect.
218: * @param key the key to retrieve.
219: * @return value of the <code>key</code>, <code>null</code> if there is no
220: * value.
221: * @throws SOAPException
222: * error in the SOAP message.
223: */
224: static String getHeaderValue(SOAPMessage message, String key)
225: throws SOAPException {
226: SOAPHeaderElement headerElement = null;
227:
228: SOAPHeader header = message.getSOAPHeader();
229: for (Iterator i = header.getChildElements(); i.hasNext();) {
230: Node node = (Node) i.next();
231: if (node instanceof SOAPHeaderElement) {
232: headerElement = (SOAPHeaderElement) node;
233: break;
234: }
235: }
236:
237: for (Iterator i = headerElement.getChildElements(); i.hasNext();) {
238: Node node = (Node) i.next();
239: if (node instanceof SOAPElement) {
240: SOAPElement element = (SOAPElement) node;
241: String name = element.getElementName().getLocalName();
242: if (name.equals(key)) {
243: return XMLUtil.getFirstLevelTextContent(element);
244: }
245: }
246: }
247:
248: return null;
249: }
250:
251: /**
252: * Checks, if the given message does not contain a fault message.
253: * @param message the message to inspect.
254: * @throws SOAPException
255: * Error in the SOAP message.
256: */
257: static void checkNoFault(SOAPMessage message) throws SOAPException {
258: SOAPBody body = message.getSOAPBody();
259: SOAPFault fault = body.getFault();
260: if (fault != null) {
261: throw new AssertionFailedError("request failed with: "
262: + fault.getFaultString());
263: }
264: }
265:
266: /**
267: * Retrieves the ASAP specific error code.
268: * @param message the response message.
269: * @return ASAP specific error code.
270: * @throws SOAPException
271: * Error in the SOAP message.
272: */
273: static String getAsapErrorCode(SOAPMessage message)
274: throws SOAPException {
275: SOAPBody body = message.getSOAPBody();
276: SOAPFault fault = body.getFault();
277: NodeList details = fault.getElementsByTagName("detail");
278: SOAPElement detail = (SOAPElement) details.item(0);
279:
280: return getChildTextContent(detail, "ErrorCode");
281: }
282:
283: /**
284: * Retrieves the first element of the given request body .
285: * @param message the current SOAP message.
286: * @return first element
287: * @throws SOAPException
288: * error evaluating the message.
289: */
290: static SOAPBodyElement getFirstBodyElement(SOAPMessage message)
291: throws SOAPException {
292: SOAPBody reqBody = message.getSOAPBody();
293: for (Iterator i = reqBody.getChildElements(); i.hasNext();) {
294: Node node = (Node) i.next();
295: if (node instanceof SOAPBodyElement) {
296: // TODO check for the correct namespace.
297: return (SOAPBodyElement) node;
298: }
299: }
300:
301: throw new AssertionFailedError("missing body");
302: }
303:
304: /**
305: * Retrieves the text content of the child node with the specified name.
306: * @param element the parent node.
307: * @param key name of the searched child node.
308: * @return text content, or <code>null</code> if there is no such child
309: * node.
310: * @throws SOAPException
311: * error inspecting the SOAP message.
312: */
313: static String getChildTextContent(SOAPElement element, String key)
314: throws SOAPException {
315: for (Iterator i = element.getChildElements(); i.hasNext();) {
316: Node node = (Node) i.next();
317: if (node instanceof SOAPElement) {
318: SOAPElement child = (SOAPElement) node;
319: String name = child.getElementName().getLocalName();
320: if (name.equals(key)) {
321: return XMLUtil.getFirstLevelTextContent(child);
322: }
323: }
324: }
325:
326: return "";
327: }
328: }
|