KickJava   Java API By Example, From Geeks To Geeks.

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


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