XmlRpcStreamServer.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.server;
00020 
00021 import java.io.ByteArrayOutputStream;
00022 import java.io.IOException;
00023 import java.io.InputStream;
00024 import java.io.OutputStream;
00025 import java.util.List;
00026 import java.util.zip.GZIPInputStream;
00027 import java.util.zip.GZIPOutputStream;
00028 
00029 import org.apache.commons.logging.Log;
00030 import org.apache.commons.logging.LogFactory;
00031 import org.apache.xmlrpc.XmlRpcException;
00032 import org.apache.xmlrpc.XmlRpcRequest;
00033 import org.apache.xmlrpc.XmlRpcRequestConfig;
00034 import org.apache.xmlrpc.common.ServerStreamConnection;
00035 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
00036 import org.apache.xmlrpc.common.XmlRpcStreamRequestProcessor;
00037 import org.apache.xmlrpc.parser.XmlRpcRequestParser;
00038 import org.apache.xmlrpc.serializer.DefaultXMLWriterFactory;
00039 import org.apache.xmlrpc.serializer.XmlRpcWriter;
00040 import org.apache.xmlrpc.serializer.XmlWriterFactory;
00041 import org.apache.xmlrpc.util.SAXParsers;
00042 import org.xml.sax.ContentHandler;
00043 import org.xml.sax.InputSource;
00044 import org.xml.sax.SAXException;
00045 import org.xml.sax.XMLReader;
00046 
00047 
00052 public abstract class XmlRpcStreamServer extends XmlRpcServer
00053                 implements XmlRpcStreamRequestProcessor {
00054         private static final Log log = LogFactory.getLog(XmlRpcStreamServer.class);
00055         private XmlWriterFactory writerFactory = new DefaultXMLWriterFactory();
00056         private static final XmlRpcErrorLogger theErrorLogger = new XmlRpcErrorLogger();
00057         private XmlRpcErrorLogger errorLogger = theErrorLogger;
00058         
00059         protected XmlRpcRequest getRequest(final XmlRpcStreamRequestConfig pConfig,
00060                                                                            InputStream pStream) throws XmlRpcException {
00061                 final XmlRpcRequestParser parser = new XmlRpcRequestParser(pConfig, getTypeFactory());
00062                 final XMLReader xr = SAXParsers.newXMLReader();
00063                 xr.setContentHandler(parser);
00064                 try {
00065                         xr.parse(new InputSource(pStream));
00066                 } catch (SAXException e) {
00067                         Exception ex = e.getException();
00068                         if (ex != null  &&  ex instanceof XmlRpcException) {
00069                                 throw (XmlRpcException) ex;
00070                         }
00071                         throw new XmlRpcException("Failed to parse XML-RPC request: " + e.getMessage(), e);
00072                 } catch (IOException e) {
00073                         throw new XmlRpcException("Failed to read XML-RPC request: " + e.getMessage(), e);
00074                 }
00075                 final List params = parser.getParams();
00076                 return new XmlRpcRequest(){
00077                         public XmlRpcRequestConfig getConfig() { return pConfig; }
00078                         public String getMethodName() { return parser.getMethodName(); }
00079                         public int getParameterCount() { return params == null ? 0 : params.size(); }
00080                         public Object getParameter(int pIndex) { return params.get(pIndex); }
00081                 };
00082         }
00083 
00084         protected XmlRpcWriter getXmlRpcWriter(XmlRpcStreamRequestConfig pConfig,
00085                                                                                    OutputStream pStream)
00086                         throws XmlRpcException {
00087                 ContentHandler w = getXMLWriterFactory().getXmlWriter(pConfig, pStream);
00088                 return new XmlRpcWriter(pConfig, w, getTypeFactory());
00089         }
00090 
00091         protected void writeResponse(XmlRpcStreamRequestConfig pConfig, OutputStream pStream,
00092                                                                  Object pResult) throws XmlRpcException {
00093                 try {
00094                         getXmlRpcWriter(pConfig, pStream).write(pConfig, pResult);
00095                 } catch (SAXException e) {
00096                         throw new XmlRpcException("Failed to write XML-RPC response: " + e.getMessage(), e);
00097                 }
00098         }
00099 
00104     protected Throwable convertThrowable(Throwable pError) {
00105         return pError;
00106     }
00107 
00108     protected void writeError(XmlRpcStreamRequestConfig pConfig, OutputStream pStream,
00109                                                           Throwable pError)
00110                         throws XmlRpcException {
00111         final Throwable error = convertThrowable(pError);
00112         final int code;
00113                 final String message;
00114                 if (error instanceof XmlRpcException) {
00115                         XmlRpcException ex = (XmlRpcException) error;
00116                         code = ex.code;
00117                 } else {
00118                         code = 0;
00119                 }
00120                 message = error.getMessage();
00121                 try {
00122                         getXmlRpcWriter(pConfig, pStream).write(pConfig, code, message, error);
00123                 } catch (SAXException e) {
00124                         throw new XmlRpcException("Failed to write XML-RPC response: " + e.getMessage(), e);
00125                 }
00126         }
00127 
00131         public void setXMLWriterFactory(XmlWriterFactory pFactory) {
00132                 writerFactory = pFactory;
00133         }
00134 
00138         public XmlWriterFactory getXMLWriterFactory() {
00139                 return writerFactory;
00140         }
00141 
00142         protected InputStream getInputStream(XmlRpcStreamRequestConfig pConfig,
00143                                                                                  ServerStreamConnection pConnection) throws IOException {
00144                 InputStream istream = pConnection.newInputStream();
00145                 if (pConfig.isEnabledForExtensions()  &&  pConfig.isGzipCompressing()) {
00146                         istream = new GZIPInputStream(istream);
00147                 }
00148                 return istream;
00149         }
00150 
00155         protected OutputStream getOutputStream(ServerStreamConnection pConnection,
00156                                                                                    XmlRpcStreamRequestConfig pConfig, OutputStream pStream) throws IOException {
00157                 if (pConfig.isEnabledForExtensions()  &&  pConfig.isGzipRequesting()) {
00158                         return new GZIPOutputStream(pStream);
00159                 } else {
00160                         return pStream;
00161                 }
00162         }
00163 
00169         protected OutputStream getOutputStream(XmlRpcStreamRequestConfig pConfig,
00170                                                                                    ServerStreamConnection pConnection,
00171                                                                                    int pSize) throws IOException {
00172             return pConnection.newOutputStream();
00173         }
00174 
00178         protected boolean isContentLengthRequired(XmlRpcStreamRequestConfig pConfig) {
00179                 return false;
00180         }
00181 
00189 
00263 
00270 


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