001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.soa.encoding;
031:
032: import com.caucho.config.ConfigurationException;
033: import com.caucho.soap.jaxws.JAXWSUtil;
034: import com.caucho.util.L10N;
035:
036: import javax.annotation.PostConstruct;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039: import static javax.xml.soap.SOAPConstants.*;
040: import javax.xml.transform.OutputKeys;
041: import javax.xml.transform.Source;
042: import javax.xml.transform.Transformer;
043: import javax.xml.transform.TransformerConfigurationException;
044: import javax.xml.transform.TransformerFactory;
045: import javax.xml.transform.stream.StreamResult;
046: import javax.xml.transform.stream.StreamSource;
047: import javax.xml.ws.BindingType;
048: import javax.xml.ws.Service;
049: import javax.xml.ws.ServiceMode;
050: import javax.xml.ws.soap.SOAPBinding;
051: import java.io.IOException;
052: import java.io.ByteArrayInputStream;
053: import java.io.ByteArrayOutputStream;
054: import java.io.InputStream;
055: import java.io.OutputStream;
056: import java.io.OutputStreamWriter;
057: import java.util.logging.Logger;
058:
059: /**
060: * Invokes a service Provider.
061: */
062: public class SourceProviderEncoding extends ProviderEncoding {
063: public static final L10N L = new L10N(SourceProviderEncoding.class);
064: private static final Logger log = Logger
065: .getLogger(SourceProviderEncoding.class.getName());
066:
067: private String _soapNamespace;
068:
069: protected SourceProviderEncoding(Object service)
070: throws ConfigurationException {
071: super (service);
072:
073: BindingType bindingType = (BindingType) _class
074: .getAnnotation(BindingType.class);
075:
076: if (bindingType != null) {
077: if (bindingType.value().equals(
078: SOAPBinding.SOAP11HTTP_BINDING))
079: _soapNamespace = URI_NS_SOAP_1_1_ENVELOPE;
080: else if (bindingType.value().equals(
081: SOAPBinding.SOAP12HTTP_BINDING))
082: _soapNamespace = URI_NS_SOAP_1_2_ENVELOPE;
083: }
084: }
085:
086: public void invoke(InputStream is, OutputStream os)
087: throws Throwable {
088: if (_mode == Service.Mode.PAYLOAD) {
089: // Put the payload into a StreamSource
090: ByteArrayOutputStream baos = new ByteArrayOutputStream();
091: JAXWSUtil.extractSOAPBody(is, baos);
092: ByteArrayInputStream bais = new ByteArrayInputStream(baos
093: .toByteArray());
094:
095: // invoke the provider
096: Source ret = (Source) _provider.invoke(new StreamSource(
097: bais));
098:
099: // Wrap and send the response
100: OutputStreamWriter writer = new OutputStreamWriter(os);
101: JAXWSUtil.writeStartSOAPEnvelope(writer, _soapNamespace);
102:
103: _transformer.transform(ret, new StreamResult(os));
104: os.flush();
105:
106: JAXWSUtil.writeEndSOAPEnvelope(writer);
107: } else {
108: Source ret = (Source) _provider
109: .invoke(new StreamSource(is));
110:
111: _transformer.transform(ret, new StreamResult(os));
112:
113: os.flush();
114: }
115: }
116: }
|