KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > MSGDispatch > TestService


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package test.MSGDispatch;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.message.SOAPBodyElement;
21 import org.apache.axis.message.SOAPEnvelope;
22 import org.apache.axis.message.SOAPHeaderElement;
23 import org.apache.axis.utils.XMLUtils;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29
30 /**
31  * This class is a message-based service with three methods. It tests:
32  *
33  * 1) Our ability to dispatch to multiple methods for a message service
34  * 2) That each of the valid signatures works as expected
35  *
36  * @author Glen Daniels (gdaniels@apache.org)
37  */

38 public class TestService {
39     // Adding these dummy methods to make sure that when we deploy this
40
// service using "allowedMethods="*" that we don't barf on them.
41
// This will ensure that people can take classes that have public
42
// methods (some available thru Axis and some not) and still be able
43
// to deploy them. (We used to throw exceptions about it)
44
public void testBody(int t) {}
45     public void testElement(int t) {}
46     public void testEnvelope(int t) {}
47
48     public SOAPBodyElement [] testBody(SOAPBodyElement [] bodies)
49             throws Exception JavaDoc {
50
51         String JavaDoc xml = "<m:bodyResult xmlns:m=\"http://db.com\"/>" ;
52         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(xml.getBytes());
53         SOAPBodyElement result = new SOAPBodyElement(is);
54         return new SOAPBodyElement [] { result };
55     }
56
57     public Element [] testElement(Element [] bodyElems)
58             throws Exception JavaDoc {
59         if (bodyElems == null || bodyElems.length != 1) {
60             throw new AxisFault("Wrong number of Elements in array!");
61         }
62         Element el = bodyElems[0];
63         if (el == null) {
64             throw new AxisFault("Null Element in array!");
65         }
66         if (!"http://db.com".equals(el.getNamespaceURI())) {
67             throw new AxisFault("Wrong namespace for Element (was \"" +
68                                 el.getNamespaceURI() + "\" should be " +
69                                 "\"http://db.com\"!");
70         }
71         String JavaDoc xml = "<m:elementResult xmlns:m=\"http://db.com\"/>" ;
72         Document JavaDoc doc = XMLUtils.newDocument(
73                 new ByteArrayInputStream JavaDoc(xml.getBytes()));
74         Element result = doc.getDocumentElement();
75         return new Element [] { result };
76     }
77
78     public Element [] testElementEcho(Element [] bodyElems)
79             throws Exception JavaDoc {
80         return bodyElems;
81     }
82     
83     public void testEnvelope(SOAPEnvelope req, SOAPEnvelope resp)
84             throws Exception JavaDoc {
85         // Throw a header in and echo back.
86
SOAPBodyElement body = req.getFirstBody();
87         resp.addBodyElement(body);
88         resp.addHeader(new SOAPHeaderElement("http://db.com", "local", "value"));
89     }
90 }
91
Popular Tags