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.soap.jaxws;
031:
032: import java.io.*;
033:
034: import java.util.ArrayList;
035: import java.util.List;
036: import java.util.StringTokenizer;
037: import java.util.logging.Level;
038: import java.util.logging.Logger;
039:
040: import javax.jws.HandlerChain;
041: import javax.jws.WebService;
042:
043: import javax.xml.bind.JAXBContext;
044: import javax.xml.bind.Unmarshaller;
045: import javax.xml.stream.XMLStreamException;
046: import javax.xml.ws.WebServiceException;
047: import javax.xml.ws.handler.Handler;
048: import javax.xml.ws.handler.HandlerResolver;
049: import javax.xml.ws.handler.LogicalHandler;
050:
051: import com.caucho.util.L10N;
052:
053: import com.caucho.soap.jaxws.handlerchain.HandlerChains;
054:
055: import com.caucho.xml.stream.StaxUtil;
056: import com.caucho.xml.stream.XMLStreamReaderImpl;
057: import com.caucho.xml.stream.XMLStreamWriterImpl;
058:
059: public class JAXWSUtil {
060: private static final Logger log = Logger.getLogger(JAXWSUtil.class
061: .getName());
062: private final static L10N L = new L10N(JAXWSUtil.class);
063: private static Unmarshaller _handlerChainUnmarshaller = null;
064:
065: public static void writeStartSOAPEnvelope(Writer out,
066: String namespace) throws IOException {
067: out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
068: out.write("<soapenv:Envelope xmlns:soapenv=\"" + namespace
069: + "\">");
070: out.write("<soapenv:Body>");
071:
072: out.flush();
073: }
074:
075: public static void writeEndSOAPEnvelope(Writer out)
076: throws IOException {
077: out.write("</soapenv:Body>");
078: out.write("</soapenv:Envelope>");
079:
080: out.flush();
081: }
082:
083: public static void extractSOAPBody(InputStream in, OutputStream out)
084: throws WebServiceException {
085: boolean foundBody = false;
086:
087: try {
088: XMLStreamReaderImpl reader = new XMLStreamReaderImpl(in);
089:
090: // skip the Envelope
091: reader.nextTag();
092:
093: if (reader.getEventType() != reader.START_ELEMENT
094: || !"Envelope".equals(reader.getLocalName())) {
095: throw new WebServiceException(
096: L
097: .l("Invalid response from server: No Envelope found"));
098: }
099:
100: // find the body
101: while (reader.hasNext()) {
102: reader.next();
103:
104: if (reader.getEventType() == reader.START_ELEMENT
105: && "Body".equals(reader.getLocalName())) {
106:
107: // Copy the body contents to a StreamDataHandler
108: reader.nextTag();
109:
110: XMLStreamWriterImpl xmlWriter = new XMLStreamWriterImpl(
111: out, false);
112:
113: StaxUtil.copyReaderToWriter(reader, xmlWriter);
114:
115: xmlWriter.flush();
116:
117: foundBody = true;
118:
119: break;
120: }
121: }
122: } catch (XMLStreamException e) {
123: throw new WebServiceException(e);
124: }
125:
126: if (!foundBody)
127: throw new WebServiceException(L
128: .l("Invalid response from server"));
129: }
130:
131: public static HandlerResolver createHandlerResolver(Class cl,
132: HandlerChain handlerChain) throws WebServiceException {
133: try {
134: if (_handlerChainUnmarshaller == null) {
135: JAXBContext context = JAXBContext
136: .newInstance("com.caucho.soap.jaxws.handlerchain");
137: _handlerChainUnmarshaller = context
138: .createUnmarshaller();
139: }
140:
141: if (log.isLoggable(Level.FINER)) {
142: log.finer("Creating handler chain for " + cl
143: + " from file " + handlerChain.file());
144: }
145:
146: InputStream is = cl
147: .getResourceAsStream(handlerChain.file());
148:
149: HandlerChains handlerChains = (HandlerChains) _handlerChainUnmarshaller
150: .unmarshal(is);
151:
152: return handlerChains;
153: } catch (Exception e) {
154: throw new WebServiceException(e);
155: }
156: }
157:
158: public static List<Handler> sortHandlerChain(
159: List<Handler> handlerChain) {
160: // According to the JAX-WS documentation, handler chains must be sorted
161: // so that all LogicalHandlers appear before protocol Handlers
162: // (protocol Handlers are just Handlers that are not LogicalHandlers)
163: //
164: // XXX do this by bubbling up instead of creating a new list
165: List list = new ArrayList<Handler>();
166:
167: for (int i = 0; i < handlerChain.size(); i++) {
168: Handler handler = handlerChain.get(i);
169:
170: if (handler instanceof LogicalHandler)
171: list.add(handler);
172: }
173:
174: for (int i = 0; i < handlerChain.size(); i++) {
175: Handler handler = handlerChain.get(i);
176:
177: if (!(handler instanceof LogicalHandler))
178: list.add(handler);
179: }
180:
181: return list;
182: }
183:
184: public static Class getEndpointInterface(Class type) {
185: WebService webService = (WebService) type
186: .getAnnotation(WebService.class);
187:
188: if (webService != null
189: && !"".equals(webService.endpointInterface())) {
190: try {
191: ClassLoader loader = type.getClassLoader();
192: return loader.loadClass(webService.endpointInterface());
193: } catch (ClassNotFoundException e) {
194: throw new WebServiceException(e);
195: }
196: }
197:
198: return type;
199: }
200:
201: public static String getTargetNamespace(Class type, Class api) {
202: WebService webService = (WebService) type
203: .getAnnotation(WebService.class);
204:
205: // try to get the namespace from the annotation first...
206: if (webService != null) {
207: if (!"".equals(webService.targetNamespace()))
208: return webService.targetNamespace();
209:
210: else if (!api.equals(type)) {
211: webService = (WebService) api
212: .getAnnotation(WebService.class);
213:
214: if (!"".equals(webService.targetNamespace()))
215: return webService.targetNamespace();
216: }
217: }
218:
219: // get the namespace from the package name
220: String namespace = null;
221: String packageName = type.getPackage().getName();
222: StringTokenizer st = new StringTokenizer(packageName, ".");
223:
224: while (st.hasMoreTokens()) {
225: if (namespace == null)
226: namespace = st.nextToken();
227: else
228: namespace = st.nextToken() + "." + namespace;
229: }
230:
231: return "http://" + namespace + "/";
232: }
233: }
|