abstractaction.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,076 行 · 第 1/3 页
JAVA
1,076 行
/* * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Emil Ong, Scott Ferguson */package com.caucho.soap.skeleton;import com.caucho.jaxb.JAXBContextImpl;import com.caucho.jaxb.JAXBUtil;import com.caucho.jaxb.property.Property;import com.caucho.jaxb.property.AttachmentProperty;import com.caucho.soap.jaxws.HandlerChainInvoker;import static com.caucho.soap.wsdl.WSDLConstants.*;import com.caucho.soap.wsdl.WSDLBinding;import com.caucho.soap.wsdl.WSDLBindingOperation;import com.caucho.soap.wsdl.WSDLDefinitions;import com.caucho.soap.wsdl.WSDLParser;import com.caucho.soap.wsdl.WSDLPortType;import com.caucho.util.Attachment;import com.caucho.util.AttachmentReader;import com.caucho.util.L10N;import javax.activation.DataHandler;import javax.jws.Oneway;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import static javax.xml.XMLConstants.*;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import javax.xml.namespace.QName;import javax.xml.soap.MessageFactory;import javax.xml.soap.SOAPException;import javax.xml.soap.SOAPFault;import javax.xml.soap.SOAPMessage;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLOutputFactory;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.XMLStreamWriter;import javax.xml.transform.Source;import javax.xml.transform.dom.DOMResult;import javax.xml.transform.dom.DOMSource;import javax.xml.ws.Holder;import javax.xml.ws.WebServiceException;import static javax.xml.ws.handler.MessageContext.*;import javax.xml.ws.soap.SOAPFaultException;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.lang.annotation.Annotation;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.UUID;import java.util.logging.Level;import java.util.logging.Logger;/** * Invokes a SOAP request on a Java POJO method */public abstract class AbstractAction { private final static Logger log = Logger.getLogger(AbstractAction.class.getName()); private static final L10N L = new L10N(AbstractAction.class); private static final HashMap<Method,String> _methodNames = new HashMap<Method,String>(); protected static final String XML_SCHEMA_PREFIX = "xsd"; protected static final String TARGET_NAMESPACE_PREFIX = "m"; protected static final String SOAP_ENCODING_STYLE = "http://schemas.xmlsoap.org/soap/encoding/"; public final static String SOAP_ENVELOPE_PREFIX = "soapenv"; public final static String SOAP_ENVELOPE = "http://schemas.xmlsoap.org/soap/envelope/"; protected static XMLOutputFactory _xmlOutputFactory; protected static XMLInputFactory _xmlInputFactory = XMLInputFactory.newInstance(); protected static MessageFactory _messageFactory; protected static SOAPMessage _soapMessage; protected final Method _method; protected final int _arity; protected boolean _isOneway; protected String _responseName; protected String _operationName; protected String _portName; protected String _inputName; protected QName _requestName; protected QName _resultName; protected final HashMap<String,ParameterMarshal> _bodyArguments = new HashMap<String,ParameterMarshal>(); protected final ParameterMarshal[] _bodyArgs; protected final HashMap<String,ParameterMarshal> _headerArguments = new HashMap<String,ParameterMarshal>(); protected final ParameterMarshal[] _headerArgs; protected final HashMap<String,ParameterMarshal> _attachmentArguments = new HashMap<String,ParameterMarshal>(); protected final ParameterMarshal[] _attachmentArgs; protected final ParameterMarshal _returnMarshal; protected final boolean _headerReturn; protected final HashMap<Class,ParameterMarshal> _faults = new HashMap<Class,ParameterMarshal>(); protected final HashMap<QName,ParameterMarshal> _faultNames = new HashMap<QName,ParameterMarshal>(); protected int _attachmentInputs; protected int _headerInputs; protected int _bodyInputs; protected int _attachmentOutputs; protected int _headerOutputs; protected int _bodyOutputs; protected final JAXBContextImpl _jaxbContext; protected final String _targetNamespace; protected final String _soapAction; protected WSDLDefinitions _wsdl; protected WSDLBindingOperation _bindingOperation; protected static XMLOutputFactory getXMLOutputFactory() { if (_xmlOutputFactory == null) { _xmlOutputFactory = XMLOutputFactory.newInstance(); _xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); } return _xmlOutputFactory; } protected static SOAPFault createSOAPFault() throws SOAPException { if (_messageFactory == null) _messageFactory = MessageFactory.newInstance(); // XXX protocol if (_soapMessage == null) _soapMessage = _messageFactory.createMessage(); _soapMessage.getSOAPBody().removeContents(); return _soapMessage.getSOAPBody().addFault(); } protected AbstractAction(Method method, Method eiMethod, JAXBContextImpl jaxbContext, String targetNamespace, WSDLDefinitions wsdl, Marshaller marshaller, Unmarshaller unmarshaller) throws JAXBException, WebServiceException { _method = method; _arity = _method.getParameterTypes().length; _jaxbContext = jaxbContext; _targetNamespace = targetNamespace; // XXX introspect this from the method _isOneway = (method.getAnnotation(Oneway.class) != null); // set the names for the input/output messages, portType/operation, and // binding/operation. _operationName = getWebMethodName(method, eiMethod); _portName = getPortName(method, eiMethod); _inputName = _operationName; _responseName = _operationName + "Response"; _soapAction = getSOAPAction(method, eiMethod); // // Arguments // _wsdl = wsdl; if (_wsdl != null) { for (WSDLBinding binding : _wsdl.getBindings()) { WSDLPortType portType = binding.getPortType(); if (portType != null && portType.getName().equals(_portName)) { for (WSDLBindingOperation operation : binding.getOperations()) { if (operation.getName().equals(_operationName)) { _bindingOperation = operation; break; } } } } } Class[] params = method.getParameterTypes(); Type[] genericParams = method.getGenericParameterTypes(); Annotation[][] paramAnn = method.getParameterAnnotations(); Annotation[][] eiParamAnn = null; if (eiMethod != null) eiParamAnn = eiMethod.getParameterAnnotations(); ArrayList<ParameterMarshal> attachmentList = new ArrayList<ParameterMarshal>(); ArrayList<ParameterMarshal> headerList = new ArrayList<ParameterMarshal>(); ArrayList<ParameterMarshal> bodyList = new ArrayList<ParameterMarshal>(); for (int i = 0; i < params.length; i++) { boolean isHeader = false; boolean isAttachment = false; String localName = "arg" + i; // As per JAX-WS spec QName name = null; WebParam.Mode mode = WebParam.Mode.IN; WebParam webParam = null; for (Annotation ann : paramAnn[i]) { if (ann instanceof WebParam) { webParam = (WebParam) ann; break; } } if (webParam == null && eiParamAnn != null) { for (Annotation ann : eiParamAnn[i]) { if (ann instanceof WebParam) { webParam = (WebParam) ann; break; } } } if (webParam != null) { if (! "".equals(webParam.name())) localName = webParam.name(); if ("".equals(webParam.targetNamespace())) name = new QName(localName); else name = new QName(webParam.targetNamespace(), localName); if (params[i].equals(Holder.class)) { mode = webParam.mode(); if (_isOneway) { throw new WebServiceException(L.l("Method {0} annotated with @Oneway, but contains output argument", method.getName())); } } isHeader = webParam.header(); if (! isHeader) isAttachment = isAttachment(webParam); } else if (params[i].equals(Holder.class)) { mode = WebParam.Mode.INOUT; } if (name == null) name = new QName(localName); Type type = JAXBUtil.getActualParameterType(genericParams[i]); Property property = _jaxbContext.createProperty(type, true); ParameterMarshal pMarshal = ParameterMarshal.create(i, property, name, mode, marshaller, unmarshaller); if (isHeader) { if (pMarshal instanceof InParameterMarshal) _headerInputs++; else if (pMarshal instanceof OutParameterMarshal) _headerOutputs++; else { _headerInputs++; _headerOutputs++; } headerList.add(pMarshal); _headerArguments.put(localName, pMarshal); } else if (isAttachment) { if (! (property instanceof AttachmentProperty)) throw new WebServiceException(L.l("Argument {0} of method {1} is of type {2}: Attachment argument types must map to base64Binary", i, method.getName(), params[i])); if (pMarshal instanceof InParameterMarshal) _attachmentInputs++; else if (pMarshal instanceof OutParameterMarshal) _attachmentOutputs++; else { _attachmentInputs++; _attachmentOutputs++; } attachmentList.add(pMarshal); _attachmentArguments.put(localName, pMarshal); } else { if (pMarshal instanceof InParameterMarshal) _bodyInputs++; else if (pMarshal instanceof OutParameterMarshal) _bodyOutputs++; else {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?