View Javadoc

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;
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;
26  
27  /***
28   * @author Steve Viens (sviens@apache.org)
29   */
30  public class AuthTokenHandler extends AbstractHandler
31  {
32    public static final String 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 element)
42    {
43      AuthToken obj = new AuthToken();
44      Vector 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)nodeList.elementAt(0)));
68      }
69  
70      return obj;
71    }
72  
73    public void marshal(RegistryObject object,Element parent)
74    {
75      AuthToken authToken = (AuthToken)object;
76      String generic = authToken.getGeneric();
77      generic = getGeneric(generic);
78      String namespace = getUDDINamespace(generic);
79      Element element = parent.getOwnerDocument().createElementNS(namespace,AuthTokenHandler.TAG_NAME);
80      AbstractHandler handler = null;
81  
82      element.setAttribute("generic",generic);
83  
84      String operator = authToken.getOperator();
85      if (operator != null)
86        element.setAttribute("operator",operator);
87      else
88        element.setAttribute("operator","");
89  
90      AuthInfo authInfo = authToken.getAuthInfo();
91      if (authInfo != null)
92      {
93        handler = maker.lookup(AuthInfoHandler.TAG_NAME);
94        handler.marshal(authInfo,element);
95      }
96  
97      parent.appendChild(element);
98    }
99  
100 
101   /****************************************************************************/
102   /****************************** TEST DRIVER *********************************/
103   /****************************************************************************/
104 
105 
106   public static void main(String args[])
107     throws Exception
108   {
109     HandlerMaker maker = HandlerMaker.getInstance();
110     AbstractHandler handler = maker.lookup(AuthTokenHandler.TAG_NAME);
111     Element parent = XMLUtils.newRootElement();
112     Element child = null;
113     
114     AuthToken object = new AuthToken();
115     object.setAuthInfo(new AuthInfo("authToken:c9613c3c-fe55-4f34-a3da-b3167afbca4a"));
116     object.setGeneric(IRegistry.UDDI_V2_GENERIC);
117     object.setOperator("jUDDI.org");
118     
119     System.out.println();
120     
121     RegistryObject regObject = object;
122     handler.marshal(regObject,parent);
123     child = (Element)parent.getFirstChild();
124     parent.removeChild(child);
125     XMLUtils.writeXML(child,System.out);
126     
127     System.out.println();
128     
129     regObject = handler.unmarshal(child);
130     handler.marshal(regObject,parent);
131     child = (Element)parent.getFirstChild();
132     parent.removeChild(child);
133     XMLUtils.writeXML(child,System.out);
134     
135     System.out.println();
136   }
137 }