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.datatype.RegistryObject;
21  import org.apache.juddi.datatype.response.DispositionReport;
22  import org.apache.juddi.error.RegistryException;
23  import org.apache.juddi.util.xml.XMLUtils;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  
27  /***
28   * Knows about the creation and populating of RegistryException
29   * objects.
30   *
31   * @author Steve Viens (sviens@apache.org)
32   */
33  public class RegistryExceptionHandler extends AbstractHandler
34  {
35    public static final String TAG_NAME = "fault";
36  
37    private HandlerMaker maker = null;
38  
39    protected RegistryExceptionHandler(HandlerMaker maker)
40    {
41      this.maker = maker;
42    }
43  
44    public RegistryObject unmarshal(Element element)
45    {
46      Vector nodeList = null;
47      AbstractHandler handler = null;
48  
49      // Attributes
50      // {none}
51  
52      // Text Node Value
53      // {none}
54  
55      // Child Elements
56      String fCode = null;
57      nodeList = XMLUtils.getChildElementsByTagName(element,"faultcode");
58      if (nodeList.size() > 0)
59        fCode = XMLUtils.getText((Element)nodeList.elementAt(0));
60  
61      String fString = null;
62      nodeList = XMLUtils.getChildElementsByTagName(element,"faultstring");
63      if (nodeList.size() > 0)
64        fString = XMLUtils.getText((Element)nodeList.elementAt(0));
65  
66      String fActor = null;
67      nodeList = XMLUtils.getChildElementsByTagName(element,"faultactor");
68      if (nodeList.size() > 0)
69        fActor = XMLUtils.getText((Element)nodeList.elementAt(0));
70  
71      DispositionReport dispRpt = null;
72      nodeList = XMLUtils.getChildElementsByTagName(element,"detail");
73      if (nodeList.size() > 0)
74      {
75        nodeList = XMLUtils.getChildElementsByTagName((Element)nodeList.elementAt(0),DispositionReportHandler.TAG_NAME);
76        if (nodeList.size() > 0)
77        {
78          handler = maker.lookup(DispositionReportHandler.TAG_NAME);
79          dispRpt = ((DispositionReport)handler.unmarshal((Element)nodeList.elementAt(0)));
80        }
81      }
82  
83      // Create RegistryException instance and return
84      RegistryException obj = new RegistryException(fCode,fString,fActor,dispRpt); 
85      
86      return obj;
87    }
88  
89    public void marshal(RegistryObject object,Element parent)
90    {
91      RegistryException regEx = (RegistryException)object;
92      Document document = parent.getOwnerDocument();
93      String generic = getGeneric(null);
94      String namespace = getUDDINamespace(generic);
95      Element fault = document.createElementNS(namespace,TAG_NAME);
96  
97      String fCode = regEx.getFaultCode();
98      if (fCode != null)
99      {
100       Element fCodeElement = document.createElement("faultcode");
101       fCodeElement.appendChild(document.createTextNode(fCode));
102       fault.appendChild(fCodeElement);
103     }
104 
105     String fString = regEx.getFaultString();
106     if (fString == null)
107       fString = "";
108 
109     Element fStringElement = document.createElement("faultstring");
110     fStringElement.appendChild(document.createTextNode(fString));
111     fault.appendChild(fStringElement);
112 
113     String fActor = regEx.getFaultActor();
114     if (fActor != null)
115     {
116       Element fActorElement = document.createElement("faultactor");
117       fActorElement.appendChild(document.createTextNode(fActor));
118       fault.appendChild(fActorElement);
119     }
120 
121     // check for a DispositionReport in the exception and if one exists,
122     // grab it, marshal it into xml and stuff it into a SOAP fault
123     // detail element.
124 
125     DispositionReport dispRpt = regEx.getDispositionReport();
126     if (dispRpt != null)
127     {
128       Element fDetailElement = document.createElement("detail");
129       IHandler handler = maker.lookup(DispositionReportHandler.TAG_NAME);
130       handler.marshal(dispRpt,fDetailElement);
131       fault.appendChild(fDetailElement);
132     }
133 
134     parent.appendChild(fault);
135   }
136 
137 
138   /****************************************************************************/
139   /****************************** TEST DRIVER *********************************/
140   /****************************************************************************/
141 
142 
143   public static void main(String args[])
144     throws Exception
145   {
146     HandlerMaker maker = HandlerMaker.getInstance();
147     AbstractHandler handler = maker.lookup(RegistryExceptionHandler.TAG_NAME);
148     Element parent = XMLUtils.newRootElement();
149     Element child = null;
150 
151     RegistryException regex = 
152         new org.apache.juddi.error.AuthTokenRequiredException("Test Exception");
153 
154     System.out.println();
155 
156     RegistryObject regObject = regex;
157     handler.marshal(regObject,parent);
158     child = (Element)parent.getFirstChild();
159     parent.removeChild(child);
160     XMLUtils.writeXML(child,System.out);
161 
162     System.out.println();
163 
164     regObject = handler.unmarshal(child);
165     handler.marshal(regObject,parent);
166     child = (Element)parent.getFirstChild();
167     parent.removeChild(child);
168     XMLUtils.writeXML(child,System.out);
169 
170     System.out.println();
171   }
172 }