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 org.apache.juddi.datatype.RegistryObject;
19  import org.apache.juddi.datatype.request.GetAuthToken;
20  import org.apache.juddi.util.xml.XMLUtils;
21  import org.w3c.dom.Element;
22  
23  /***
24   * @author Steve Viens (sviens@apache.org)
25   */
26  public class GetAuthTokenHandler extends AbstractHandler
27  {
28    public static final String TAG_NAME = "get_authToken";
29  
30    protected GetAuthTokenHandler(HandlerMaker maker)
31    {
32    }
33  
34    public RegistryObject unmarshal(Element element)
35    {
36      GetAuthToken obj = new GetAuthToken();
37  
38      // Attributes
39      String userID = element.getAttribute("userID");
40      if ((userID != null && (userID.trim().length() > 0)))
41        obj.setUserID(userID);
42  
43      String credential = element.getAttribute("cred");
44      if ((credential != null && (credential.trim().length() > 0)))
45        obj.setCredential(credential);
46  
47      String generic = element.getAttribute("generic");
48      if ((generic != null && (generic.trim().length() > 0)))
49        obj.setGeneric(generic);
50  
51      // Text Node Value
52      // {none}
53  
54      // Child Elements
55      // {none}
56  
57      return obj;
58    }
59  
60    public void marshal(RegistryObject object,Element parent)
61    {
62      GetAuthToken request = (GetAuthToken)object;
63      String generic = request.getGeneric();
64      generic = getGeneric(generic);
65      String namespace = getUDDINamespace(generic);
66      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
67  
68      String cred = request.getCredential();
69      if ((cred != null && (cred.length() > 0)))
70        element.setAttribute("cred",cred);
71  
72      String userID = request.getUserID();
73      if ((userID != null && (userID.length() > 0)))
74        element.setAttribute("userID",userID);
75  
76      element.setAttribute("generic",generic);
77  
78      parent.appendChild(element);
79    }
80  
81  
82    /****************************************************************************/
83    /****************************** TEST DRIVER *********************************/
84    /****************************************************************************/
85  
86  
87    public static void main(String args[])
88      throws Exception
89    {
90      HandlerMaker maker = HandlerMaker.getInstance();
91      AbstractHandler handler = maker.lookup(GetAuthTokenHandler.TAG_NAME);
92  
93      Element parent = XMLUtils.newRootElement();
94      Element child = null;
95  
96      GetAuthToken request = new GetAuthToken();
97      request.setUserID("sviens");
98      request.setCredential("password");
99  
100     System.out.println();
101 
102     RegistryObject regObject = request;
103     handler.marshal(regObject,parent);
104     child = (Element)parent.getFirstChild();
105     parent.removeChild(child);
106     XMLUtils.writeXML(child,System.out);
107 
108     System.out.println();
109 
110     regObject = handler.unmarshal(child);
111     handler.marshal(regObject,parent);
112     child = (Element)parent.getFirstChild();
113     parent.removeChild(child);
114     XMLUtils.writeXML(child,System.out);
115 
116     System.out.println();
117   }
118 }