KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > header > ClientHandler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.header;
23
24 import org.jboss.logging.Logger;
25
26 import javax.xml.namespace.QName JavaDoc;
27 import javax.xml.rpc.JAXRPCException JavaDoc;
28 import javax.xml.rpc.handler.GenericHandler JavaDoc;
29 import javax.xml.rpc.handler.MessageContext JavaDoc;
30 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
31 import javax.xml.soap.Name JavaDoc;
32 import javax.xml.soap.SOAPElement JavaDoc;
33 import javax.xml.soap.SOAPException JavaDoc;
34 import javax.xml.soap.SOAPFactory JavaDoc;
35 import javax.xml.soap.SOAPHeader JavaDoc;
36 import javax.xml.soap.SOAPHeaderElement JavaDoc;
37 import javax.xml.soap.SOAPMessage JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 public class ClientHandler extends GenericHandler JavaDoc
41 {
42    // provide logging
43
private final Logger log = Logger.getLogger(ClientHandler.class);
44
45    private static final String JavaDoc NAMESPACE_URI = ImplicitHeaderEndpoint.NAMESPACE_URI;
46    private static final String JavaDoc LOCAL_PART = ImplicitHeaderEndpoint.LOCAL_PART;
47    private static final String JavaDoc PREFIX = ImplicitHeaderEndpoint.PREFIX;
48
49    private String JavaDoc username = "kermit";
50    private String JavaDoc sessionID;
51
52    public QName JavaDoc[] getHeaders()
53    {
54       QName JavaDoc qname = new QName JavaDoc(NAMESPACE_URI, LOCAL_PART);
55       return new QName JavaDoc[]{qname};
56    }
57
58    public boolean handleRequest(MessageContext JavaDoc msgContext)
59    {
60       try
61       {
62          SOAPMessage JavaDoc soapMessage = ((SOAPMessageContext JavaDoc)msgContext).getMessage();
63          SOAPFactory JavaDoc factory = SOAPFactory.newInstance();
64
65          SOAPHeader JavaDoc soapHeader = soapMessage.getSOAPHeader();
66          Name JavaDoc name = factory.createName(LOCAL_PART, PREFIX, NAMESPACE_URI);
67          SOAPHeaderElement JavaDoc headerElement = soapHeader.addHeaderElement(name);
68
69          SOAPElement JavaDoc usrElement = headerElement.addChildElement("username", PREFIX, NAMESPACE_URI);
70          usrElement.setValue(username);
71
72          SOAPElement JavaDoc seElement = headerElement.addChildElement("sessionID", PREFIX, NAMESPACE_URI);
73          seElement.setValue(sessionID);
74
75          log.info("username: " + username);
76          log.info("sessionID: " + sessionID);
77          return true;
78       }
79       catch (SOAPException JavaDoc e)
80       {
81          throw new JAXRPCException JavaDoc(e.toString(), e);
82       }
83    }
84
85    public boolean handleResponse(MessageContext JavaDoc msgContext)
86    {
87       try
88       {
89          SOAPFactory JavaDoc factory = SOAPFactory.newInstance();
90
91          SOAPMessage JavaDoc soapMessage = ((SOAPMessageContext JavaDoc)msgContext).getMessage();
92          SOAPHeader JavaDoc soapHeader = soapMessage.getSOAPHeader();
93          Iterator JavaDoc it = soapHeader.extractAllHeaderElements();
94
95          if (it.hasNext() == false)
96             throw new SOAPException JavaDoc("No header element");
97
98          SOAPHeaderElement JavaDoc headerElement = (SOAPHeaderElement JavaDoc)it.next();
99
100          Name JavaDoc usernameName = factory.createName("username", PREFIX, NAMESPACE_URI);
101          SOAPElement JavaDoc usrElement = (SOAPElement JavaDoc)headerElement.getChildElements(usernameName).next();
102          String JavaDoc elementValue = usrElement.getValue();
103          if (username.equals(elementValue) == false)
104             throw new SOAPException JavaDoc("Invalid username: " + elementValue);
105
106          Name JavaDoc sessionIDName = factory.createName("sessionID", PREFIX, NAMESPACE_URI);
107          SOAPElement JavaDoc seElement = (SOAPElement JavaDoc)headerElement.getChildElements(sessionIDName).next();
108          sessionID = seElement.getValue();
109          if (sessionID == null || sessionID.length() == 0)
110             throw new SOAPException JavaDoc("Invalid sesionID: " + sessionID);
111
112          log.info("username: " + username);
113          log.info("sessionID: " + sessionID);
114          return true;
115       }
116       catch (SOAPException JavaDoc e)
117       {
118          throw new JAXRPCException JavaDoc(e.toString(), e);
119       }
120    }
121 }
122
Popular Tags