XmlRpcWriter.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.serializer;
00020 
00021 import java.io.ByteArrayOutputStream;
00022 import java.io.ObjectOutputStream;
00023 import java.util.HashMap;
00024 import java.util.Map;
00025 
00026 import org.apache.xmlrpc.XmlRpcRequest;
00027 import org.apache.xmlrpc.XmlRpcRequestConfig;
00028 import org.apache.xmlrpc.common.TypeFactory;
00029 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
00030 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
00031 import org.xml.sax.Attributes;
00032 import org.xml.sax.ContentHandler;
00033 import org.xml.sax.SAXException;
00034 import org.xml.sax.helpers.AttributesImpl;
00035 
00036 
00040 public class XmlRpcWriter {
00043         public static final String EXTENSIONS_URI = "http://ws.apache.org/xmlrpc/namespaces/extensions";
00044         private static final Attributes ZERO_ATTRIBUTES = new AttributesImpl();
00045         private final XmlRpcStreamConfig config;
00046         private final TypeFactory typeFactory;
00047         private final ContentHandler handler;
00048 
00054         public XmlRpcWriter(XmlRpcStreamConfig pConfig, ContentHandler pHandler,
00055                                             TypeFactory pTypeFactory) {
00056                 config = pConfig;
00057                 handler = pHandler;
00058                 typeFactory = pTypeFactory;
00059         }
00060 
00065         public void write(XmlRpcRequest pRequest) throws SAXException {
00066                 handler.startDocument();
00067                 boolean extensions = pRequest.getConfig().isEnabledForExtensions();
00068                 if (extensions) {
00069                         handler.startPrefixMapping("ex", XmlRpcWriter.EXTENSIONS_URI);
00070                 }
00071                 handler.startElement("", "methodCall", "methodCall", ZERO_ATTRIBUTES);
00072                 handler.startElement("", "methodName", "methodName", ZERO_ATTRIBUTES);
00073                 String s = pRequest.getMethodName();
00074                 handler.characters(s.toCharArray(), 0, s.length());
00075                 handler.endElement("", "methodName", "methodName");
00076                 handler.startElement("", "params", "params", ZERO_ATTRIBUTES);
00077                 int num = pRequest.getParameterCount();
00078                 for (int i = 0;  i < num;  i++) {
00079                         handler.startElement("", "param", "param", ZERO_ATTRIBUTES);
00080                         writeValue(pRequest.getParameter(i));
00081                         handler.endElement("", "param", "param");
00082                 }
00083                 handler.endElement("", "params", "params");
00084         handler.endElement("", "methodCall", "methodCall");
00085                 if (extensions) {
00086                         handler.endPrefixMapping("ex");
00087                 }
00088                 handler.endDocument();
00089         }
00090 
00096         public void write(XmlRpcRequestConfig pConfig, Object pResult) throws SAXException {
00097                 handler.startDocument();
00098                 boolean extensions = pConfig.isEnabledForExtensions();
00099                 if (extensions) {
00100                         handler.startPrefixMapping("ex", XmlRpcWriter.EXTENSIONS_URI);
00101                 }
00102                 handler.startElement("", "methodResponse", "methodResponse", ZERO_ATTRIBUTES);
00103                 handler.startElement("", "params", "params", ZERO_ATTRIBUTES);
00104                 handler.startElement("", "param", "param", ZERO_ATTRIBUTES);
00105                 writeValue(pResult);
00106                 handler.endElement("", "param", "param");
00107                 handler.endElement("", "params", "params");
00108                 handler.endElement("", "methodResponse", "methodResponse");
00109                 if (extensions) {
00110                         handler.endPrefixMapping("ex");
00111                 }
00112                 handler.endDocument();
00113         }
00114 
00121     public void write(XmlRpcRequestConfig pConfig, int pCode, String pMessage) throws SAXException {
00122         write(pConfig, pCode, pMessage, null);
00123     }
00124 
00132         public void write(XmlRpcRequestConfig pConfig, int pCode, String pMessage,
00133             Throwable pThrowable) throws SAXException {
00134                 handler.startDocument();
00135                 boolean extensions = pConfig.isEnabledForExtensions();
00136                 if (extensions) {
00137                         handler.startPrefixMapping("ex", XmlRpcWriter.EXTENSIONS_URI);
00138                 }
00139                 handler.startElement("", "methodResponse", "methodResponse", ZERO_ATTRIBUTES);
00140                 handler.startElement("", "fault", "fault", ZERO_ATTRIBUTES);
00141                 Map map = new HashMap();
00142         map.put("faultCode", new Integer(pCode));
00143         map.put("faultString", pMessage == null ? "" : pMessage);
00144         if (pThrowable != null  &&  extensions  &&  (pConfig instanceof XmlRpcStreamRequestConfig)  &&
00145                 ((XmlRpcStreamRequestConfig) pConfig).isEnabledForExceptions()) {
00146             try {
00147                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
00148                 ObjectOutputStream oos = new ObjectOutputStream(baos);
00149                 oos.writeObject(pThrowable);
00150                 oos.close();
00151                 baos.close();
00152                 map.put("faultCause", baos.toByteArray());
00153             } catch (Throwable t) {
00154                 // Ignore me
00155             }
00156         }
00157         writeValue(map);
00158                 handler.endElement("", "fault", "fault");
00159                 handler.endElement("", "methodResponse", "methodResponse");
00160                 if (extensions) {
00161                         handler.endPrefixMapping("ex");
00162                 }
00163                 handler.endDocument();
00164         }
00165 
00170         protected void writeValue(Object pObject) throws SAXException {
00171                 TypeSerializer serializer = typeFactory.getSerializer(config, pObject);
00172                 if (serializer == null) {
00173                         throw new SAXException("Unsupported Java type: " + pObject.getClass().getName());
00174                 }
00175                 serializer.write(handler, pObject);
00176         }
00177 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49