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.HandlerChainInvoker;
034: import com.caucho.soap.jaxws.JAXWSUtil;
035: import com.caucho.soap.jaxws.PortInfoImpl;
036: import com.caucho.util.L10N;
037:
038: import javax.annotation.PostConstruct;
039: import javax.jws.HandlerChain;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import javax.xml.namespace.QName;
043: import javax.xml.soap.SOAPMessage;
044: import javax.xml.transform.OutputKeys;
045: import javax.xml.transform.Source;
046: import javax.xml.transform.Transformer;
047: import javax.xml.transform.TransformerConfigurationException;
048: import javax.xml.transform.TransformerFactory;
049: import javax.xml.transform.dom.DOMResult;
050: import javax.xml.transform.dom.DOMSource;
051: import javax.xml.transform.stream.StreamSource;
052: import javax.xml.ws.BindingType;
053: import javax.xml.ws.Provider;
054: import javax.xml.ws.Service;
055: import javax.xml.ws.ServiceMode;
056: import javax.xml.ws.WebServiceProvider;
057: import javax.xml.ws.handler.Handler;
058: import javax.xml.ws.handler.HandlerResolver;
059: import javax.xml.ws.handler.PortInfo;
060: import java.io.ByteArrayInputStream;
061: import java.io.ByteArrayOutputStream;
062: import java.io.IOException;
063: import java.io.InputStream;
064: import java.io.OutputStream;
065: import java.lang.reflect.ParameterizedType;
066: import java.lang.reflect.Type;
067: import java.util.List;
068: import java.util.logging.Logger;
069:
070: /**
071: * Invokes a service Provider.
072: */
073: public abstract class ProviderEncoding implements ServiceEncoding {
074: private static final L10N L = new L10N(ProviderEncoding.class);
075: private static final Logger log = Logger
076: .getLogger(ProviderEncoding.class.getName());
077: private static final TransformerFactory factory = TransformerFactory
078: .newInstance();
079:
080: protected final Class _class;
081: protected final Provider _provider;
082: protected final Transformer _transformer;
083: protected final Service.Mode _mode;
084: protected HandlerChainInvoker _handlerChain;
085:
086: protected ProviderEncoding(Object service)
087: throws ConfigurationException {
088: _provider = (Provider) service;
089: _class = service.getClass();
090:
091: ServiceMode serviceMode = (ServiceMode) _class
092: .getAnnotation(ServiceMode.class);
093:
094: if (serviceMode != null)
095: _mode = serviceMode.value();
096: else
097: _mode = Service.Mode.PAYLOAD;
098:
099: try {
100: _transformer = factory.newTransformer();
101:
102: if (_mode == Service.Mode.PAYLOAD) {
103: _transformer.setOutputProperty(
104: OutputKeys.OMIT_XML_DECLARATION, "yes");
105: _transformer.setOutputProperty(OutputKeys.INDENT, "no");
106: }
107: } catch (TransformerConfigurationException e) {
108: throw new ConfigurationException(e);
109: }
110:
111: PortInfo portInfo = null;
112:
113: BindingType bindingType = (BindingType) _class
114: .getAnnotation(BindingType.class);
115:
116: WebServiceProvider provider = (WebServiceProvider) _class
117: .getAnnotation(WebServiceProvider.class);
118:
119: if (provider != null && bindingType != null) {
120: QName portName = new QName(provider.targetNamespace(),
121: provider.portName());
122: QName serviceName = new QName(provider.targetNamespace(),
123: provider.serviceName());
124:
125: portInfo = new PortInfoImpl(bindingType.value(), portName,
126: serviceName);
127: }
128:
129: HandlerChain handlerChain = (HandlerChain) _class
130: .getAnnotation(HandlerChain.class);
131:
132: if (handlerChain != null) {
133: if (portInfo != null) {
134: HandlerResolver handlerResolver = JAXWSUtil
135: .createHandlerResolver(_class, handlerChain);
136:
137: List<Handler> chain = handlerResolver
138: .getHandlerChain(portInfo);
139:
140: if (chain != null)
141: _handlerChain = new HandlerChainInvoker(chain);
142: } else {
143: log
144: .fine(L
145: .l(
146: "@HandlerChain given on Provider {0}, but @WebServiceProvider and/or @BindingType were not fully specified",
147: _class.getName()));
148: }
149: }
150: }
151:
152: public static ProviderEncoding createProviderEncoding(Object service)
153: throws ConfigurationException {
154: Class cl = service.getClass();
155: Type[] interfaces = cl.getGenericInterfaces();
156:
157: for (int i = 0; i < interfaces.length; i++) {
158: if (interfaces[i] instanceof ParameterizedType) {
159: ParameterizedType pType = (ParameterizedType) interfaces[i];
160:
161: if (Provider.class.equals(pType.getRawType())) {
162: Type[] args = pType.getActualTypeArguments();
163:
164: if (args.length == 1) {
165: if (Source.class.equals(args[0]))
166: return new SourceProviderEncoding(service);
167: else if (SOAPMessage.class.equals(args[0]))
168: return new SOAPMessageProviderEncoding(
169: service);
170: }
171: }
172: }
173: }
174:
175: throw new ConfigurationException(L.l(
176: "Class {0} does not implement valid Provider", cl
177: .getName()));
178: }
179:
180: public void setService(Object service) {
181: }
182:
183: @PostConstruct
184: public void init() {
185: }
186:
187: public void invoke(HttpServletRequest request,
188: HttpServletResponse response) throws Throwable {
189: InputStream in = request.getInputStream();
190: OutputStream out = response.getOutputStream();
191:
192: if (_handlerChain != null) {
193: in = _handlerChain.invokeServerInbound(request, out);
194: out = new ByteArrayOutputStream();
195:
196: if (in == null)
197: return;
198: }
199:
200: invoke(in, out);
201:
202: if (_handlerChain != null) {
203: byte[] output = ((ByteArrayOutputStream) out).toByteArray();
204: Source source = new StreamSource(new ByteArrayInputStream(
205: output));
206: DOMResult result = new DOMResult();
207:
208: _transformer.transform(source, result);
209:
210: out = response.getOutputStream();
211:
212: _handlerChain.invokeServerOutbound(new DOMSource(result
213: .getNode()), out);
214:
215: out.flush();
216: }
217: }
218: }
|