KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > handler > AuthTokenHandler


1 /*
2  * Copyright 2001-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 package org.apache.juddi.handler;
17
18 import java.util.Vector JavaDoc;
19
20 import org.apache.juddi.IRegistry;
21 import org.apache.juddi.datatype.RegistryObject;
22 import org.apache.juddi.datatype.request.AuthInfo;
23 import org.apache.juddi.datatype.response.AuthToken;
24 import org.apache.juddi.util.xml.XMLUtils;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * @author Steve Viens (sviens@apache.org)
29  */

30 public class AuthTokenHandler extends AbstractHandler
31 {
32   public static final String JavaDoc TAG_NAME = "authToken";
33
34   private HandlerMaker maker = null;
35
36   public AuthTokenHandler(HandlerMaker maker)
37   {
38     this.maker = maker;
39   }
40
41   public RegistryObject unmarshal(Element JavaDoc element)
42   {
43     AuthToken obj = new AuthToken();
44     Vector JavaDoc nodeList = null;
45     AbstractHandler handler = null;
46
47     // We could use the generic attribute value to
48
// determine which version of UDDI was used to
49
// format the request XML. - Steve
50

51     // Attributes
52
obj.setGeneric(element.getAttribute("generic"));
53     obj.setOperator(element.getAttribute("operator"));
54
55     // We can ignore the xmlns attribute since we
56
// can always determine it's value using the
57
// "generic" attribute. - Steve
58

59     // Text Node Value
60
// {none}
61

62     // Child Elements
63
nodeList = XMLUtils.getChildElementsByTagName(element,AuthInfoHandler.TAG_NAME);
64     if (nodeList.size()> 0)
65     {
66       handler = maker.lookup(AuthInfoHandler.TAG_NAME);
67       obj.setAuthInfo((AuthInfo)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
68     }
69
70     return obj;
71   }
72
73   public void marshal(RegistryObject object,Element JavaDoc parent)
74   {
75     AuthToken authToken = (AuthToken)object;
76     Element JavaDoc element = parent.getOwnerDocument().createElement(AuthTokenHandler.TAG_NAME);
77     AbstractHandler handler = null;
78
79     String JavaDoc generic = authToken.getGeneric();
80     if ((generic != null) && (generic.trim().length() > 0))
81     {
82       element.setAttribute("generic",generic);
83
84       if (generic.equals(IRegistry.UDDI_V1_GENERIC))
85         element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
86       else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
87         element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
88       else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
89         element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
90     }
91     else // Default to UDDI v2 values
92
{
93       element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
94       element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
95     }
96
97     String JavaDoc operator = authToken.getOperator();
98     if (operator != null)
99       element.setAttribute("operator",operator);
100     else
101       element.setAttribute("operator","");
102
103     AuthInfo authInfo = authToken.getAuthInfo();
104     if (authInfo != null)
105     {
106       handler = maker.lookup(AuthInfoHandler.TAG_NAME);
107       handler.marshal(authInfo,element);
108     }
109
110     parent.appendChild(element);
111   }
112
113
114   /***************************************************************************/
115   /***************************** TEST DRIVER *********************************/
116   /***************************************************************************/
117
118
119   public static void main(String JavaDoc args[])
120     throws Exception JavaDoc
121   {
122     HandlerMaker maker = HandlerMaker.getInstance();
123     AbstractHandler handler = maker.lookup(AuthTokenHandler.TAG_NAME);
124     Element JavaDoc parent = XMLUtils.newRootElement();
125     Element JavaDoc child = null;
126     
127     AuthToken object = new AuthToken();
128     object.setAuthInfo(new AuthInfo("authToken:c9613c3c-fe55-4f34-a3da-b3167afbca4a"));
129     object.setGeneric(IRegistry.UDDI_V2_GENERIC);
130     object.setOperator("jUDDI.org");
131     
132     System.out.println();
133     
134     RegistryObject regObject = object;
135     handler.marshal(regObject,parent);
136     child = (Element JavaDoc)parent.getFirstChild();
137     parent.removeChild(child);
138     XMLUtils.writeXML(child,System.out);
139     
140     System.out.println();
141     
142     regObject = handler.unmarshal(child);
143     handler.marshal(regObject,parent);
144     child = (Element JavaDoc)parent.getFirstChild();
145     parent.removeChild(child);
146     XMLUtils.writeXML(child,System.out);
147     
148     System.out.println();
149   }
150 }
Popular Tags